From e4cd22b7bd286c62c50b80479344aea1f3a7cd5b Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Fri, 1 May 2020 16:36:16 +0200 Subject: [PATCH] tests: Update suite for pytest --- pytest.ini | 2 ++ terminatorlib/borg.py | 1 - terminatorlib/freebsd.py | 27 ++++++++++--------- terminatorlib/plugin.py | 11 ++++---- tests/{testborg.py => test_borg.py} | 10 +++---- tests/test_doctests.py | 23 ---------------- tests/{testsignalman.py => test_signalman.py} | 2 +- 7 files changed, 27 insertions(+), 49 deletions(-) create mode 100644 pytest.ini rename tests/{testborg.py => test_borg.py} (83%) delete mode 100644 tests/test_doctests.py rename tests/{testsignalman.py => test_signalman.py} (96%) diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..68d55666 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +addopts = --doctest-modules --verbose diff --git a/terminatorlib/borg.py b/terminatorlib/borg.py index 6082c4d5..d52f1b60 100644 --- a/terminatorlib/borg.py +++ b/terminatorlib/borg.py @@ -16,7 +16,6 @@ class Borg: """Definition of a class that can never be duplicated. Correct usage is thus: - >>> from borg import Borg >>> class foo(Borg): ... # All attributes on a borg class *must* = None ... attribute = None diff --git a/terminatorlib/freebsd.py b/terminatorlib/freebsd.py index 67badf34..09c3d52b 100755 --- a/terminatorlib/freebsd.py +++ b/terminatorlib/freebsd.py @@ -12,6 +12,7 @@ Tested on FreeBSD 7-STABLE/amd64 from April 11 2008. """ +import platform from ctypes import * from ctypes.util import find_library @@ -44,18 +45,6 @@ class kinfo_file(Structure): ('kf_sa_peer', sockaddr_storage), ] -libc = CDLL(find_library('c')) - -uintlen = c_size_t(sizeof(c_uint)) -ver = c_uint(0) - -if (libc.sysctlbyname('kern.osreldate', byref(ver), byref(uintlen), None, 0) < 0): - raise OSError("sysctlbyname returned < 0") - -# kern.proc.filedesc added for procstat(1) after these __FreeBSD_versions -if ver.value < 700104 and ver.value < 800019: - raise NotImplementedError("cwd detection requires a recent 7.0-STABLE or 8-CURRENT") - def get_process_cwd(pid): """Return string containing the current working directory of the given pid, @@ -78,6 +67,20 @@ def get_process_cwd(pid): return kif.kf_path +if platform.system() in ['FreeBSD', 'OpenBSD']: + libc = CDLL(find_library('c')) + + uintlen = c_size_t(sizeof(c_uint)) + ver = c_uint(0) + + if (libc.sysctlbyname('kern.osreldate', byref(ver), byref(uintlen), None, 0) < 0): + raise OSError("sysctlbyname returned < 0") + + # kern.proc.filedesc added for procstat(1) after these __FreeBSD_versions + if ver.value < 700104 and ver.value < 800019: + raise NotImplementedError("cwd detection requires a recent 7.0-STABLE or 8-CURRENT") + + if __name__ == '__main__': import os, sys print(" => %d cwd = %s" % (os.getpid(), get_process_cwd(os.getpid()))) diff --git a/terminatorlib/plugin.py b/terminatorlib/plugin.py index fb4c386d..7e76d9c5 100644 --- a/terminatorlib/plugin.py +++ b/terminatorlib/plugin.py @@ -7,9 +7,10 @@ considered BSD licenced, per the authors wishes) >>> registry = PluginRegistry() ->>> registry.instances -{} ->>> registry.load_plugins(True) +>>> isinstance(registry.instances, dict) +True +>>> registry.enable('TestPlugin') +>>> registry.load_plugins() >>> plugins = registry.get_plugins_by_capability('test') >>> len(plugins) 1 @@ -69,7 +70,7 @@ class PluginRegistry(borg.Borg): if not self.available_plugins: self.available_plugins = {} - def load_plugins(self, testing=False): + def load_plugins(self): """Load all plugins present in the plugins/ directory in our module""" if self.done: dbg('PluginRegistry::load_plugins: Already loaded') @@ -98,7 +99,7 @@ class PluginRegistry(borg.Borg): func = getattr(module, item) self.available_plugins[item] = func - if not testing and item not in config['enabled_plugins']: + if item not in config['enabled_plugins']: dbg('plugin %s not enabled, skipping' % item) continue if item not in self.instances: diff --git a/tests/testborg.py b/tests/test_borg.py similarity index 83% rename from tests/testborg.py rename to tests/test_borg.py index 032872f8..d4ed6d3b 100755 --- a/tests/testborg.py +++ b/tests/test_borg.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # Terminator by Chris Jones # GPL v2 only -"""testborg.py - We are the borg. Resistance is futile. +"""test_borg.py - We are the borg. Resistance is futile. doctests for borg.py >>> obj1 = TestBorg() @@ -29,12 +29,9 @@ """ -import os -import sys, os.path -sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) - from terminatorlib.borg import Borg + class TestBorg(Borg): attribute = None @@ -46,6 +43,7 @@ class TestBorg(Borg): if not self.attribute: self.attribute = 0 + class TestBorg2(Borg): attribute = None @@ -56,5 +54,3 @@ class TestBorg2(Borg): def prepare_attributes(self): if not self.attribute: self.attribute = 1 - -# TODO: implement test? diff --git a/tests/test_doctests.py b/tests/test_doctests.py deleted file mode 100644 index 55f586d1..00000000 --- a/tests/test_doctests.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python -"""Load up the tests.""" - -import os -import sys, os.path -sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) - -from unittest import TestSuite -from doctest import DocTestSuite, ELLIPSIS - -def test_suite(): - suite = TestSuite() - for name in ( - 'config', - 'plugin', - 'cwd', - 'factory', - 'util', - 'tests.testborg', - 'tests.testsignalman', - ): - suite.addTest(DocTestSuite('terminatorlib.' + name)) - return suite diff --git a/tests/testsignalman.py b/tests/test_signalman.py similarity index 96% rename from tests/testsignalman.py rename to tests/test_signalman.py index 7db6aed3..0877dfb6 100755 --- a/tests/testsignalman.py +++ b/tests/test_signalman.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # Terminator by Chris Jones # GPL v2 only -"""testsignalman.py - Test the signalman class +"""test_signalman.py - Test the signalman class >>> widget = TestWidget() >>> signalman = Signalman()