From 587f6c09573d595708ca1a8d4ab3abd3b39070c8 Mon Sep 17 00:00:00 2001 From: Jamie Wilkinson Date: Wed, 3 May 2023 17:15:38 +1000 Subject: [PATCH 1/3] test: Fix the location of the source config file. Previously it mattered to call `pytest` from the top level. Now the test can be called from the cwd too. --- nss_cache/config_test.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nss_cache/config_test.py b/nss_cache/config_test.py index 279da21b..1fd24981 100644 --- a/nss_cache/config_test.py +++ b/nss_cache/config_test.py @@ -52,9 +52,13 @@ class TestClassMethods(unittest.TestCase): def setUp(self): # create a directory with a writeable copy of nsscache.conf in it self.workdir = tempfile.mkdtemp() + # nsscache.conf is in the parent dir of this test. + self.srcdir = os.path.normpath(os.path.join(os.path.dirname(__file__), + '..')) conf_filename = 'nsscache.conf' self.conf_filename = os.path.join(self.workdir, conf_filename) - shutil.copy(conf_filename, self.conf_filename) + shutil.copy(os.path.join(self.srcdir, conf_filename), + self.conf_filename) os.chmod(self.conf_filename, 0o640) # prepare a config object with this config From a6eb0ac515332796572154305a383e4e26757feb Mon Sep 17 00:00:00 2001 From: Jamie Wilkinson Date: Wed, 3 May 2023 17:25:43 +1000 Subject: [PATCH 2/3] refactor: Fix the fcntl lock test to stop complaining about type errors. --- nss_cache/config_test.py | 4 ++-- nss_cache/lock_test.py | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/nss_cache/config_test.py b/nss_cache/config_test.py index 1fd24981..7d4f2e02 100644 --- a/nss_cache/config_test.py +++ b/nss_cache/config_test.py @@ -53,8 +53,8 @@ def setUp(self): # create a directory with a writeable copy of nsscache.conf in it self.workdir = tempfile.mkdtemp() # nsscache.conf is in the parent dir of this test. - self.srcdir = os.path.normpath(os.path.join(os.path.dirname(__file__), - '..')) + self.srcdir = os.path.normpath( + os.path.join(os.path.dirname(__file__), '..')) conf_filename = 'nsscache.conf' self.conf_filename = os.path.join(self.workdir, conf_filename) shutil.copy(os.path.join(self.srcdir, conf_filename), diff --git a/nss_cache/lock_test.py b/nss_cache/lock_test.py index f8628ee9..c1585f90 100644 --- a/nss_cache/lock_test.py +++ b/nss_cache/lock_test.py @@ -118,16 +118,14 @@ def testLockCreatesPidfiles(self): # Note that testing when self._file is not None is covered below. - def testLockLocksWithFcntl(self): + @mock.patch('fcntl.lockf') + def testLockLocksWithFcntl(self, lockf): locker = lock.PidFile(pid='PID') - with mock.patch.object( - locker, '_file') as f, mock.patch('fcntl.lockf') as lockf: - + with mock.patch.object(locker, '_file') as f: locker.Lock() self.assertTrue(locker._locked) - lockf.assert_called_once_with(locker._file, - fcntl.LOCK_EX | fcntl.LOCK_NB) + lockf.assert_called_once_with(f, fcntl.LOCK_EX | fcntl.LOCK_NB) def testLockStoresPid(self): locker = lock.PidFile(filename=self.filename, pid='PID') From 5c2953be37fab24ed576baab7b14a35f7f012d6f Mon Sep 17 00:00:00 2001 From: Jamie Wilkinson Date: Tue, 16 May 2023 18:42:14 +1000 Subject: [PATCH 3/3] Unskip test for printing help with bad args. --- nss_cache/app_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nss_cache/app_test.py b/nss_cache/app_test.py index 89e3233f..bd2a5f74 100644 --- a/nss_cache/app_test.py +++ b/nss_cache/app_test.py @@ -33,6 +33,9 @@ def setUp(self): dev_null = io.StringIO() self.stdout = sys.stdout sys.stdout = dev_null + self.srcdir = os.path.normpath( + os.path.join(os.path.dirname(__file__), '..')) + self.conf_filename = os.path.join(self.srcdir, 'nsscache.conf') def tearDown(self): sys.stdout = self.stdout @@ -119,16 +122,16 @@ def testHelpCommandOutput(self): self.assertTrue( stdout_buffer.getvalue().find('nsscache synchronises') >= 0) - @unittest.skip('cant pass unless theres a valid config') def testRunBadArgsPrintsGlobalHelp(self): # trap stdout into a StringIO stdout_buffer = io.StringIO() old_stdout = sys.stdout sys.stdout = stdout_buffer # verify bad arguments calls help - return_code = app.NssCacheApp().Run(['blarg'], {}) + return_code = app.NssCacheApp().Run( + ['blarg'], {'NSSCACHE_CONFIG': self.conf_filename}) sys.stdout = old_stdout - assert return_code == 1 + assert return_code == 70 # EX_SOFTWARE assert stdout_buffer.getvalue().find('enable debugging') >= 0