mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-15 00:30:33 +03:00
JournalLock: Cleanup pytest coverage
* Some `# pragma: no cover' added where it's more pain to get coverage than to put up with lack of testing. We are choosing to NOT test retry_lock() or the call to it from update_lock() as it's tkinter stuff. * Check `self.journal_dir_lockfile` exists before trying to call .close() on it. * Note where platform checks means only one if branch gets run, so of course coverage will complain about the other one. * Add test_release_lock_lie_locked() to test when not locked, but pretend we were so release_lock() returns False. * Add test_update_lock_same() to check if an attempt is made to 'update' to the same directory.
This commit is contained in:
parent
8bf5a43c5b
commit
f2fd16307c
@ -13,7 +13,7 @@ from EDMCLogging import get_main_logger
|
|||||||
|
|
||||||
logger = get_main_logger()
|
logger = get_main_logger()
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
def _(x: str) -> str:
|
def _(x: str) -> str:
|
||||||
return x
|
return x
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ class JournalLock:
|
|||||||
try:
|
try:
|
||||||
self.journal_dir_path = pathlib.Path(self.journal_dir)
|
self.journal_dir_path = pathlib.Path(self.journal_dir)
|
||||||
|
|
||||||
except Exception:
|
except Exception: # pragma: no cover
|
||||||
logger.exception("Couldn't make pathlib.Path from journal_dir")
|
logger.exception("Couldn't make pathlib.Path from journal_dir")
|
||||||
|
|
||||||
def open_journal_dir_lockfile(self) -> bool:
|
def open_journal_dir_lockfile(self) -> bool:
|
||||||
@ -107,7 +107,7 @@ class JournalLock:
|
|||||||
f", assuming another process running: {e!r}")
|
f", assuming another process running: {e!r}")
|
||||||
return JournalLockResult.ALREADY_LOCKED
|
return JournalLockResult.ALREADY_LOCKED
|
||||||
|
|
||||||
else:
|
else: # pytest coverage only sees this on !win32
|
||||||
logger.trace('NOT win32, using fcntl')
|
logger.trace('NOT win32, using fcntl')
|
||||||
try:
|
try:
|
||||||
import fcntl
|
import fcntl
|
||||||
@ -133,7 +133,7 @@ class JournalLock:
|
|||||||
|
|
||||||
return JournalLockResult.LOCKED
|
return JournalLockResult.LOCKED
|
||||||
|
|
||||||
def release_lock(self) -> bool:
|
def release_lock(self) -> bool: # noqa: CCR001
|
||||||
"""
|
"""
|
||||||
Release lock on journal directory.
|
Release lock on journal directory.
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ class JournalLock:
|
|||||||
else:
|
else:
|
||||||
unlocked = True
|
unlocked = True
|
||||||
|
|
||||||
else:
|
else: # pytest coverage only sees this on !win32
|
||||||
logger.trace('NOT win32, using fcntl')
|
logger.trace('NOT win32, using fcntl')
|
||||||
try:
|
try:
|
||||||
import fcntl
|
import fcntl
|
||||||
@ -179,7 +179,8 @@ class JournalLock:
|
|||||||
unlocked = True
|
unlocked = True
|
||||||
|
|
||||||
# Close the file whether or not the unlocking succeeded.
|
# Close the file whether or not the unlocking succeeded.
|
||||||
self.journal_dir_lockfile.close()
|
if hasattr(self, 'journal_dir_lockfile'):
|
||||||
|
self.journal_dir_lockfile.close()
|
||||||
|
|
||||||
self.journal_dir_lockfile_name = None
|
self.journal_dir_lockfile_name = None
|
||||||
# Avoids type hint issues, see 'declaration' in JournalLock.__init__()
|
# Avoids type hint issues, see 'declaration' in JournalLock.__init__()
|
||||||
@ -187,7 +188,7 @@ class JournalLock:
|
|||||||
|
|
||||||
return unlocked
|
return unlocked
|
||||||
|
|
||||||
class JournalAlreadyLocked(tk.Toplevel):
|
class JournalAlreadyLocked(tk.Toplevel): # pragma: no cover
|
||||||
"""Pop-up for when Journal directory already locked."""
|
"""Pop-up for when Journal directory already locked."""
|
||||||
|
|
||||||
def __init__(self, parent: tk.Tk, callback: Callable) -> None:
|
def __init__(self, parent: tk.Tk, callback: Callable) -> None:
|
||||||
@ -263,9 +264,9 @@ class JournalLock:
|
|||||||
|
|
||||||
if self.obtain_lock() == JournalLockResult.ALREADY_LOCKED:
|
if self.obtain_lock() == JournalLockResult.ALREADY_LOCKED:
|
||||||
# Pop-up message asking for Retry or Ignore
|
# Pop-up message asking for Retry or Ignore
|
||||||
self.retry_popup = self.JournalAlreadyLocked(parent, self.retry_lock)
|
self.retry_popup = self.JournalAlreadyLocked(parent, self.retry_lock) # pragma: no cover
|
||||||
|
|
||||||
def retry_lock(self, retry: bool, parent: tk.Tk) -> None:
|
def retry_lock(self, retry: bool, parent: tk.Tk) -> None: # pragma: no cover
|
||||||
"""
|
"""
|
||||||
Try again to obtain a lock on the Journal Directory.
|
Try again to obtain a lock on the Journal Directory.
|
||||||
|
|
||||||
|
@ -298,6 +298,12 @@ class TestJournalLock:
|
|||||||
jlock = JournalLock()
|
jlock = JournalLock()
|
||||||
assert jlock.release_lock() is True
|
assert jlock.release_lock() is True
|
||||||
|
|
||||||
|
def test_release_lock_lie_locked(self, mock_journaldir: py_path_local_LocalPath):
|
||||||
|
"""Test JournalLock.release_lock() when not locked, but lie we are."""
|
||||||
|
jlock = JournalLock()
|
||||||
|
jlock.locked = True
|
||||||
|
assert jlock.release_lock() is False
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# Tests against JournalLock.update_lock()
|
# Tests against JournalLock.update_lock()
|
||||||
def test_update_lock(
|
def test_update_lock(
|
||||||
@ -319,6 +325,25 @@ class TestJournalLock:
|
|||||||
# Now store the 'current' journaldir for reference and attempt
|
# Now store the 'current' journaldir for reference and attempt
|
||||||
# to update to a new one.
|
# to update to a new one.
|
||||||
old_journaldir = jlock.journal_dir
|
old_journaldir = jlock.journal_dir
|
||||||
jlock.update_lock(None)
|
jlock.update_lock(None) # type: ignore
|
||||||
assert jlock.journal_dir != old_journaldir
|
assert jlock.journal_dir != old_journaldir
|
||||||
assert jlock.locked is True
|
assert jlock.locked is True
|
||||||
|
|
||||||
|
def test_update_lock_same(self, mock_journaldir: py_path_local_LocalPath):
|
||||||
|
"""
|
||||||
|
Test JournalLock.update_lock().
|
||||||
|
|
||||||
|
Due to using 'static' mock_journaldir this should 'work', because the
|
||||||
|
directory is still the same.
|
||||||
|
"""
|
||||||
|
# First actually obtain the lock, and check it worked
|
||||||
|
jlock = JournalLock()
|
||||||
|
jlock.obtain_lock()
|
||||||
|
assert jlock.locked is True
|
||||||
|
|
||||||
|
# Now store the 'current' journaldir for reference and attempt
|
||||||
|
# to update to a new one.
|
||||||
|
old_journaldir = jlock.journal_dir
|
||||||
|
jlock.update_lock(None) # type: ignore
|
||||||
|
assert jlock.journal_dir == old_journaldir
|
||||||
|
assert jlock.locked is True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user