From cc37b23db4fd56a3c864cf9979ccad0d32d81ba5 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 09:22:55 +0100 Subject: [PATCH 01/12] tests: journal_lock: Fix up pytest tmpdir types --- tests/journal_lock.py/test_journal_lock.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 75a6be1f..bf6e9d3e 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -108,25 +108,28 @@ class TestJournalLock: """JournalLock test class.""" @pytest.fixture - def mock_journaldir(self, monkeypatch: _pytest_monkeypatch, tmpdir: _pytest_tmpdir) -> py_path_local_LocalPath: + def mock_journaldir( + self, monkeypatch: _pytest_monkeypatch, + tmpdir_factory: _pytest_tmpdir.TempPathFactory + ) -> py_path_local_LocalPath: """Fixture for mocking config.get_str('journaldir').""" def get_str(key: str, *, default: str = None) -> str: """Mock config.*Config get_str to provide fake journaldir.""" if key == 'journaldir': - return tmpdir + return tmpdir_factory print('Other key, calling up ...') return config.get_str(key) # Call the non-mocked with monkeypatch.context() as m: m.setattr(config, "get_str", get_str) - yield tmpdir + yield tmpdir_factory @pytest.fixture def mock_journaldir_changing( self, monkeypatch: _pytest_monkeypatch, - tmpdir_factory: _pytest_tmpdir.TempdirFactory + tmpdir_factory: _pytest_tmpdir.TempPathFactory ) -> py_path_local_LocalPath: """Fixture for mocking config.get_str('journaldir').""" def get_str(key: str, *, default: str = None) -> str: From b47f278655b0a55393414eb051a7ceedfae2813b Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 11:49:05 +0100 Subject: [PATCH 02/12] tests: journal_lock: Fix test_journal_lock_init test * It's `tmp_path_factory` now, not `tmpdir_factory`. * Need to `str(...)` if a string is what we want. --- tests/journal_lock.py/test_journal_lock.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index bf6e9d3e..330d40f9 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -110,45 +110,45 @@ class TestJournalLock: @pytest.fixture def mock_journaldir( self, monkeypatch: _pytest_monkeypatch, - tmpdir_factory: _pytest_tmpdir.TempPathFactory + tmp_path_factory: _pytest_tmpdir.TempPathFactory ) -> py_path_local_LocalPath: """Fixture for mocking config.get_str('journaldir').""" def get_str(key: str, *, default: str = None) -> str: """Mock config.*Config get_str to provide fake journaldir.""" if key == 'journaldir': - return tmpdir_factory + return str(tmp_path_factory) print('Other key, calling up ...') return config.get_str(key) # Call the non-mocked with monkeypatch.context() as m: m.setattr(config, "get_str", get_str) - yield tmpdir_factory + yield tmp_path_factory @pytest.fixture def mock_journaldir_changing( self, monkeypatch: _pytest_monkeypatch, - tmpdir_factory: _pytest_tmpdir.TempPathFactory + tmp_path_factory: _pytest_tmpdir.TempPathFactory ) -> py_path_local_LocalPath: """Fixture for mocking config.get_str('journaldir').""" def get_str(key: str, *, default: str = None) -> str: """Mock config.*Config get_str to provide fake journaldir.""" if key == 'journaldir': - return tmpdir_factory.mktemp("changing") + return tmp_path_factory.mktemp("changing") print('Other key, calling up ...') return config.get_str(key) # Call the non-mocked with monkeypatch.context() as m: m.setattr(config, "get_str", get_str) - yield tmpdir_factory + yield tmp_path_factory ########################################################################### # Tests against JournalLock.__init__() def test_journal_lock_init(self, mock_journaldir: py_path_local_LocalPath): """Test JournalLock instantiation.""" - tmpdir = mock_journaldir + tmpdir = str(mock_journaldir) jlock = JournalLock() # Check members are properly initialised. From 9915c5435dfa52d37cd08500580d91e6f1a4aa7c Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 11:51:12 +0100 Subject: [PATCH 03/12] tests: journal_lock: test_update_lock_same: Make `.obtain.lock()` an assert --- tests/journal_lock.py/test_journal_lock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 330d40f9..3a8c11cb 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -353,7 +353,7 @@ class TestJournalLock: """ # First actually obtain the lock, and check it worked jlock = JournalLock() - jlock.obtain_lock() + assert jlock.obtain_lock() == JournalLockResult.LOCKED assert jlock.locked # Now store the 'current' journaldir for reference and attempt From 27d93b5f31643336a98ecf2e5e7a781c6b8b7735 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:17:54 +0100 Subject: [PATCH 04/12] tests: journal_lock: Need .getbasetemp() and str() as necessary --- tests/journal_lock.py/test_journal_lock.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 3a8c11cb..22708edf 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -116,7 +116,7 @@ class TestJournalLock: def get_str(key: str, *, default: str = None) -> str: """Mock config.*Config get_str to provide fake journaldir.""" if key == 'journaldir': - return str(tmp_path_factory) + return str(tmp_path_factory.getbasetemp()) print('Other key, calling up ...') return config.get_str(key) # Call the non-mocked @@ -148,7 +148,8 @@ class TestJournalLock: # Tests against JournalLock.__init__() def test_journal_lock_init(self, mock_journaldir: py_path_local_LocalPath): """Test JournalLock instantiation.""" - tmpdir = str(mock_journaldir) + print(f'{type(mock_journaldir)=}') + tmpdir = str(mock_journaldir.getbasetemp()) jlock = JournalLock() # Check members are properly initialised. @@ -344,6 +345,8 @@ class TestJournalLock: assert jlock.journal_dir != old_journaldir assert jlock.locked + assert jlock.release_lock() + def test_update_lock_same(self, mock_journaldir: py_path_local_LocalPath): """ Test JournalLock.update_lock(). From ddf6669f35ce0756da274d22521d59ae8d0f5208 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:39:03 +0100 Subject: [PATCH 05/12] tests: journal_lock: fix `test_obtain_lock_with_tmpdir_ro()` * Set `tmpdir` properly for this test. * Do **NOT** unset `JournalLock.journal_dir_lockfile_name` on release, as this is needed to remove the file * Ensure prior test(s) release their locks, *and* remove the file. --- journal_lock.py | 4 +++- tests/journal_lock.py/test_journal_lock.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/journal_lock.py b/journal_lock.py index 1c984f90..91a7895f 100644 --- a/journal_lock.py +++ b/journal_lock.py @@ -182,7 +182,9 @@ class JournalLock: if hasattr(self, 'journal_dir_lockfile'): self.journal_dir_lockfile.close() - self.journal_dir_lockfile_name = None + # Doing this makes it impossible for tests to ensure the file + # is removed as a part of cleanup. So don't. + # self.journal_dir_lockfile_name = None # Avoids type hint issues, see 'declaration' in JournalLock.__init__() # self.journal_dir_lockfile = None diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 22708edf..514edac9 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -200,10 +200,15 @@ class TestJournalLock: locked = jlock.obtain_lock() assert locked == JournalLockResult.LOCKED assert jlock.locked + assert jlock.release_lock() + + # Cleanup, to avoid side-effect on other tests + os.unlink(str(jlock.journal_dir_lockfile_name)) def test_obtain_lock_with_tmpdir_ro(self, mock_journaldir: py_path_local_LocalPath): """Test JournalLock.obtain_lock() with read-only tmpdir.""" - tmpdir = mock_journaldir + tmpdir = str(mock_journaldir.getbasetemp()) + print(f'{tmpdir=}') # Make tmpdir read-only ? if sys.platform == 'win32': @@ -214,7 +219,7 @@ class TestJournalLock: # Fetch user details winuser, domain, type = win32security.LookupAccountName("", os.environ.get('USERNAME')) # Fetch the current security of tmpdir for that user. - sd = win32security.GetFileSecurity(str(tmpdir), win32security.DACL_SECURITY_INFORMATION) + sd = win32security.GetFileSecurity(tmpdir, win32security.DACL_SECURITY_INFORMATION) dacl = sd.GetSecurityDescriptorDacl() # instead of dacl = win32security.ACL() # Add Write to Denied list @@ -225,7 +230,7 @@ class TestJournalLock: dacl.AddAccessDeniedAce(win32security.ACL_REVISION, con.FILE_WRITE_DATA, winuser) # Apply that change. sd.SetSecurityDescriptorDacl(1, dacl, 0) # may not be necessary - win32security.SetFileSecurity(str(tmpdir), win32security.DACL_SECURITY_INFORMATION, sd) + win32security.SetFileSecurity(tmpdir, win32security.DACL_SECURITY_INFORMATION, sd) else: import stat @@ -251,7 +256,7 @@ class TestJournalLock: dacl.DeleteAce(i) # Apply that change. sd.SetSecurityDescriptorDacl(1, dacl, 0) # may not be necessary - win32security.SetFileSecurity(str(tmpdir), win32security.DACL_SECURITY_INFORMATION, sd) + win32security.SetFileSecurity(tmpdir, win32security.DACL_SECURITY_INFORMATION, sd) break i += 1 From fb0c139865dab28b684d77a76c5187bc0230247e Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:44:13 +0100 Subject: [PATCH 06/12] tests: journal_lock: Ensure all tests, that should, clean up --- tests/journal_lock.py/test_journal_lock.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 514edac9..b271543c 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -200,9 +200,9 @@ class TestJournalLock: locked = jlock.obtain_lock() assert locked == JournalLockResult.LOCKED assert jlock.locked - assert jlock.release_lock() # Cleanup, to avoid side-effect on other tests + assert jlock.release_lock() os.unlink(str(jlock.journal_dir_lockfile_name)) def test_obtain_lock_with_tmpdir_ro(self, mock_journaldir: py_path_local_LocalPath): @@ -314,6 +314,9 @@ class TestJournalLock: with open(mock_journaldir / 'edmc-journal-lock.txt', mode='w+') as lf: assert _obtain_lock('release-lock', lf) + # Cleanup, to avoid side-effect on other tests + os.unlink(str(jlock.journal_dir_lockfile_name)) + def test_release_lock_not_locked(self, mock_journaldir: py_path_local_LocalPath): """Test JournalLock.release_lock() when not locked.""" jlock = JournalLock() @@ -350,7 +353,9 @@ class TestJournalLock: assert jlock.journal_dir != old_journaldir assert jlock.locked + # Cleanup, to avoid side-effect on other tests assert jlock.release_lock() + os.unlink(str(jlock.journal_dir_lockfile_name)) def test_update_lock_same(self, mock_journaldir: py_path_local_LocalPath): """ @@ -370,3 +375,7 @@ class TestJournalLock: jlock.update_lock(None) # type: ignore assert jlock.journal_dir == old_journaldir assert jlock.locked + + # Cleanup, to avoid side-effect on other tests + assert jlock.release_lock() + os.unlink(str(jlock.journal_dir_lockfile_name)) From 34f1989b0b95c0a85bfc31e3d4e437c03bf0a078 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:49:11 +0100 Subject: [PATCH 07/12] tests: journal_lock: Use .getbasetemp() in `test_obtain_lock_already_locked` --- tests/journal_lock.py/test_journal_lock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index b271543c..f90bad31 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -277,7 +277,7 @@ class TestJournalLock: continue_q: mp.Queue = mp.Queue() exit_q: mp.Queue = mp.Queue() locker = mp.Process(target=other_process_lock, - args=(continue_q, exit_q, mock_journaldir) + args=(continue_q, exit_q, mock_journaldir.getbasetemp()) ) print('Starting sub-process other_process_lock()...') locker.start() From 725473e2dba608b3f9c80ed0323c0338ccec5c4c Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:51:52 +0100 Subject: [PATCH 08/12] tests: journal_lock: Finale factory change fixups & cleanup * `test_release_lock()` needed `.getbasetemp()` on `mock_journaldir` usage. * Add removal of lock file to `other_process_lock()` --- tests/journal_lock.py/test_journal_lock.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index f90bad31..8582f9df 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -69,6 +69,9 @@ def other_process_lock(continue_q: mp.Queue, exit_q: mp.Queue, lockfile: pathlib print('sub-process: Waiting for exit signal...') exit_q.get(block=True, timeout=None) + # And clean up + os.unlink(lockfile / 'edmc-journal-lock.txt') + def _obtain_lock(prefix: str, filehandle) -> bool: """ @@ -311,7 +314,7 @@ class TestJournalLock: assert jlock.release_lock() # And finally check it actually IS unlocked. - with open(mock_journaldir / 'edmc-journal-lock.txt', mode='w+') as lf: + with open(mock_journaldir.getbasetemp() / 'edmc-journal-lock.txt', mode='w+') as lf: assert _obtain_lock('release-lock', lf) # Cleanup, to avoid side-effect on other tests From 130bd619cf4db958e2c73a2af078c9637a4394a0 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:55:39 +0100 Subject: [PATCH 09/12] tests: journal_lock: Cleanup properly in `test_update_lock()` * As this involved two directories we also need to clean up the 'old' one. --- tests/journal_lock.py/test_journal_lock.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 8582f9df..eb100269 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -352,6 +352,7 @@ class TestJournalLock: # Now store the 'current' journaldir for reference and attempt # to update to a new one. old_journaldir = jlock.journal_dir + old_journaldir_lockfile_name = jlock.journal_dir_lockfile_name jlock.update_lock(None) # type: ignore assert jlock.journal_dir != old_journaldir assert jlock.locked @@ -359,6 +360,8 @@ class TestJournalLock: # Cleanup, to avoid side-effect on other tests assert jlock.release_lock() os.unlink(str(jlock.journal_dir_lockfile_name)) + # And the old_journaldir's lockfile too + os.unlink(str(old_journaldir_lockfile_name)) def test_update_lock_same(self, mock_journaldir: py_path_local_LocalPath): """ From ca6a0355d2205de27f1ebea32ba2554ad9dcee8b Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 13:07:28 +0100 Subject: [PATCH 10/12] tests: Run `pytest` as part of GitHub PR checks --- .github/workflows/pr-checks.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index 55ee97ff..918f703d 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -91,3 +91,11 @@ jobs: # explicitly ignore docstring errors (start with D) git diff "refs/remotes/${BASE_REPO_OWNER}/${BASE_REF}" -- | flake8 . --count --statistics --diff --extend-ignore D #################################################################### + + #################################################################### + # Ensure all tests pass + #################################################################### + - name: PyTest tests + run: | + pytest + #################################################################### From e2813c0ff9bb6193ce2d1bfbc90906364b686376 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 13:10:03 +0100 Subject: [PATCH 11/12] .github/workflows: Bump us up to Ubuntu 22.04, as 18.04 is deprecated --- .github/workflows/pr-checks.yml | 2 +- .github/workflows/push-checks.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index 918f703d..f61fff72 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -13,7 +13,7 @@ on: jobs: flake8: - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 steps: diff --git a/.github/workflows/push-checks.yml b/.github/workflows/push-checks.yml index 298bd4ff..b6994b02 100644 --- a/.github/workflows/push-checks.yml +++ b/.github/workflows/push-checks.yml @@ -14,7 +14,7 @@ on: jobs: build: - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 From 86810a97e59c1c93e5c9230591a86780c6fff11b Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 13:12:21 +0100 Subject: [PATCH 12/12] github pr-checks: Rename the overall job to `code-checks` It includes all of the setup, the actual flake8 run, and now a pytest run. --- .github/workflows/pr-checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index f61fff72..e90929bb 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -12,7 +12,7 @@ on: branches: [ develop ] jobs: - flake8: + code-checks: runs-on: ubuntu-22.04 steps: