Skip to content

Commit

Permalink
Run black on tests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
eloquence committed Feb 12, 2020
1 parent 5b58b11 commit 3a42dd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
58 changes: 31 additions & 27 deletions launcher/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
from tempfile import TemporaryDirectory

relpath_notify = "../sdw_notify/Notify.py"
path_to_notify = os.path.join(os.path.dirname(os.path.abspath(__file__)), relpath_notify)
path_to_notify = os.path.join(
os.path.dirname(os.path.abspath(__file__)), relpath_notify
)
notify = SourceFileLoader("Notify", path_to_notify).load_module()

relpath_updater = "../sdw_updater_gui/Updater.py"
path_to_updater = os.path.join(os.path.dirname(os.path.abspath(__file__)), relpath_updater)
path_to_updater = os.path.join(
os.path.dirname(os.path.abspath(__file__)), relpath_updater
)
updater = SourceFileLoader("Updater", path_to_updater).load_module()


Expand All @@ -21,7 +25,9 @@
NO_TIMESTAMP_REGEX = r"Timestamp file '.*' does not exist."

# Regex for warning log if we've updated too long ago, and grace period has elapsed
UPDATER_WARNING_REGEX = r"^Last successful update \(.* hours ago\) is above warning threshold "
UPDATER_WARNING_REGEX = (
r"^Last successful update \(.* hours ago\) is above warning threshold "
)
r"\(.* hours\). Uptime grace period of .* hours has elapsed (uptime: .* hours)."

# Regex for info log if we've updated too long ago, but grace period still ticking
Expand All @@ -30,7 +36,9 @@
r"yet \(uptime: .* hours\)."

# Regex for info log if we've updated recently enough
NO_WARNING_REGEX = r"Last successful update \(.* hours ago\) is below the warning threshold "
NO_WARNING_REGEX = (
r"Last successful update \(.* hours ago\) is below the warning threshold "
)
r"\(.* hours\)."

# Regex for bad contents in `sdw-last-updated` file
Expand All @@ -40,17 +48,15 @@
@mock.patch("Notify.sdlog.error")
@mock.patch("Notify.sdlog.warning")
@mock.patch("Notify.sdlog.info")
def test_warning_shown_if_updater_never_ran(
mocked_info, mocked_warning, mocked_error
):
def test_warning_shown_if_updater_never_ran(mocked_info, mocked_warning, mocked_error):
"""
Test whether we're correctly going to show a warning if the updater has
never run.
"""
# We're going to look for a nonexistent file in an existing tmpdir
with TemporaryDirectory() as tmpdir, \
mock.patch("Notify.LAST_UPDATED_FILE",
os.path.join(tmpdir, "not-a-file")):
with TemporaryDirectory() as tmpdir, mock.patch(
"Notify.LAST_UPDATED_FILE", os.path.join(tmpdir, "not-a-file")
):

warning_should_be_shown = notify.is_update_check_necessary()

Expand All @@ -68,25 +74,25 @@ def test_warning_shown_if_updater_never_ran(
assert re.search(NO_TIMESTAMP_REGEX, warning_string) is not None


@pytest.mark.parametrize("uptime,warning_expected", [
(notify.UPTIME_GRACE_PERIOD + 1, True),
(notify.UPTIME_GRACE_PERIOD - 1, False)
])
@pytest.mark.parametrize(
"uptime,warning_expected",
[(notify.UPTIME_GRACE_PERIOD + 1, True), (notify.UPTIME_GRACE_PERIOD - 1, False)],
)
@mock.patch("Notify.sdlog.error")
@mock.patch("Notify.sdlog.warning")
@mock.patch("Notify.sdlog.info")
def test_warning_shown_if_warning_threshold_exceeded(
mocked_info, mocked_warning, mocked_error, uptime, warning_expected
mocked_info, mocked_warning, mocked_error, uptime, warning_expected
):
"""
Primary use case for the notifier: are we showing the warning if the
system hasn't been (successfully) updated for longer than the warning
threshold? Expected result varies based on whether system uptime exceeds
a grace period (for the user to launch the app on their own).
"""
with TemporaryDirectory() as tmpdir, \
mock.patch("Notify.LAST_UPDATED_FILE",
os.path.join(tmpdir, "sdw-last-updated")):
with TemporaryDirectory() as tmpdir, mock.patch(
"Notify.LAST_UPDATED_FILE", os.path.join(tmpdir, "sdw-last-updated")
):
# Write a "last successfully updated" date well in the past for check
historic_date = datetime.date(2013, 6, 5).strftime(updater.DATE_FORMAT)
with open(notify.LAST_UPDATED_FILE, "w") as f:
Expand Down Expand Up @@ -120,9 +126,9 @@ def test_warning_not_shown_if_warning_threshold_not_exceeded(
Another high priority case: we don't want to warn the user if they've
recently run the updater successfully.
"""
with TemporaryDirectory() as tmpdir, \
mock.patch("Notify.LAST_UPDATED_FILE",
os.path.join(tmpdir, "sdw-last-updated")):
with TemporaryDirectory() as tmpdir, mock.patch(
"Notify.LAST_UPDATED_FILE", os.path.join(tmpdir, "sdw-last-updated")
):
# Write current timestamp into the file
just_now = datetime.datetime.now().strftime(updater.DATE_FORMAT)
with open(notify.LAST_UPDATED_FILE, "w") as f:
Expand All @@ -138,16 +144,14 @@ def test_warning_not_shown_if_warning_threshold_not_exceeded(
@mock.patch("Notify.sdlog.error")
@mock.patch("Notify.sdlog.warning")
@mock.patch("Notify.sdlog.info")
def test_corrupt_timestamp_file_handled(
mocked_info, mocked_warning, mocked_error
):
def test_corrupt_timestamp_file_handled(mocked_info, mocked_warning, mocked_error):
"""
The LAST_UPDATED_FILE must contain a timestamp in a specified format;
if it doesn't, we show the warning and log the error.
"""
with TemporaryDirectory() as tmpdir, \
mock.patch("Notify.LAST_UPDATED_FILE",
os.path.join(tmpdir, "sdw-last-updated")):
with TemporaryDirectory() as tmpdir, mock.patch(
"Notify.LAST_UPDATED_FILE", os.path.join(tmpdir, "sdw-last-updated")
):
with open(notify.LAST_UPDATED_FILE, "w") as f:
# With apologies to HAL 9000
f.write("daisy, daisy, give me your answer do")
Expand Down
30 changes: 11 additions & 19 deletions launcher/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
@mock.patch("Util.sdlog.error")
@mock.patch("Util.sdlog.warning")
@mock.patch("Util.sdlog.info")
def test_obtain_lock(
mocked_info, mocked_warning, mocked_error
):
def test_obtain_lock(mocked_info, mocked_warning, mocked_error):
"""
Test whether we can successfully obtain an exclusive lock
"""
Expand All @@ -36,8 +34,8 @@ def test_obtain_lock(
# We should be getting a lock handle back
assert lh is not None

cmd = ['lsof', '-w', os.path.join(util.LOCK_DIRECTORY, basename)]
output_lines = subprocess.check_output(cmd).decode("utf-8").strip().split('\n')
cmd = ["lsof", "-w", os.path.join(util.LOCK_DIRECTORY, basename)]
output_lines = subprocess.check_output(cmd).decode("utf-8").strip().split("\n")
# We expect exactly one process to be accessing this file, plus output header
assert len(output_lines) == 2
lsof_data = output_lines[1].split()
Expand All @@ -46,7 +44,7 @@ def test_obtain_lock(
# We expect the PID column to contain the ID of this process
assert lsof_data[1] == str(pid)
# We expect an exclusive write lock to be set for this process
assert lsof_data[3].find('W') != -1
assert lsof_data[3].find("W") != -1


@mock.patch("Util.sdlog.error")
Expand Down Expand Up @@ -80,9 +78,7 @@ def test_cannot_obtain_exclusive_lock_when_busy(
@mock.patch("Util.sdlog.error")
@mock.patch("Util.sdlog.warning")
@mock.patch("Util.sdlog.info")
def test_cannot_obtain_shared_lock_when_busy(
mocked_info, mocked_warning, mocked_error
):
def test_cannot_obtain_shared_lock_when_busy(mocked_info, mocked_warning, mocked_error):
"""
Test whether an exlusive lock on a lock file is successfully detected
by means of attempting to obtain a shared, nonexclusive lock on the same
Expand All @@ -109,9 +105,7 @@ def test_cannot_obtain_shared_lock_when_busy(
@mock.patch("Util.sdlog.error")
@mock.patch("Util.sdlog.warning")
@mock.patch("Util.sdlog.info")
def test_no_lockfile_no_problems(
mocked_info, mocked_warning, mocked_error
):
def test_no_lockfile_no_problems(mocked_info, mocked_warning, mocked_error):
"""
Test whether our shared lock test succeeds even when there's no lockfile
(which means the process has not run recently, or ever, and it's safe to
Expand All @@ -125,13 +119,13 @@ def test_no_lockfile_no_problems(
@mock.patch("Util.sdlog.error")
@mock.patch("Util.sdlog.warning")
@mock.patch("Util.sdlog.info")
def test_permission_error_is_handled(
mocked_info, mocked_warning, mocked_error
):
def test_permission_error_is_handled(mocked_info, mocked_warning, mocked_error):
"""
Test whether permission errors obtaining a lock are handled correctly
"""
with mock.patch("builtins.open", side_effect=PermissionError()) as mocked_open: # noqa: F821
with mock.patch(
"builtins.open", side_effect=PermissionError()
) as mocked_open: # noqa: F821
lock = util.obtain_lock("test-open-error.lock")
assert lock is None
mocked_open.assert_called_once()
Expand All @@ -143,9 +137,7 @@ def test_permission_error_is_handled(
@mock.patch("Util.sdlog.error")
@mock.patch("Util.sdlog.warning")
@mock.patch("Util.sdlog.info")
def test_stale_lockfile_has_no_effect(
mocked_info, mocked_warning, mocked_error
):
def test_stale_lockfile_has_no_effect(mocked_info, mocked_warning, mocked_error):
"""
Test whether we can get a shared lock when a lockfile exists, but nobody
is accessing it.
Expand Down

0 comments on commit 3a42dd3

Please sign in to comment.