From ddf6669f35ce0756da274d22521d59ae8d0f5208 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 5 Sep 2022 12:39:03 +0100 Subject: [PATCH] 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