diff --git a/test/basetest/exceptions.py b/test/basetest/exceptions.py index 998a24325..f1e227975 100644 --- a/test/basetest/exceptions.py +++ b/test/basetest/exceptions.py @@ -36,10 +36,11 @@ class HookError(Exception): class TimeoutWaitingForStream(object): def __init__(self, name): - self.stream = name + self.name = name def __repr__(self): - return "*** Timeout reached while waiting for %s ***".format(self.name) + return "*** Timeout reached while waiting for {0} ***".format( + self.name) class StreamsAreMerged(object): diff --git a/test/basetest/hooks.py b/test/basetest/hooks.py index 5c827bcb0..c53b7b760 100644 --- a/test/basetest/hooks.py +++ b/test/basetest/hooks.py @@ -441,7 +441,7 @@ class LoggedHook(Hook): for k1 in log: # Timestamps if k1 == "calls": - timestamp = lambda x: datetime.fromtimestamp(int(x) / 1e9) + timestamp = lambda x: datetime.fromtimestamp(float(x)) newlog[k1] = map(timestamp, log[k1]) elif k1 in ("input", "output"): @@ -460,35 +460,36 @@ class LoggedHook(Hook): return newlog - def assert_triggered(self): + def assertTriggered(self): """Check if current hook file was triggered/used by taskwarrior """ log = self._parse_log() - if log["calls"]: - return True - else: - return False + assert log["calls"], "{0} was never called".format(self.hookname) - def assert_triggered_count(self, count): + def assertTriggeredCount(self, count): """Check if current hook file was triggered/used by taskwarrior and how many times. """ log = self._parse_log() - if len(log["calls"]) == count: - return True - else: - return False + assert len(log["calls"]) == count, ("{0} calls expected for {1} but " + "found {2}".format( + count, + self.hookname, + log["calls"] + )) - def assert_exitcode(self, exitcode): + def assertExitcode(self, exitcode): """Check if current hook finished with the expected exit code """ log = self._parse_log() - if log["exitcode"] == exitcode: - return True - else: - return False + assert log["exitcode"] == exitcode, ("Expected exit code {0} for {1} " + "but found {2}".format( + exitcode, + self.hookname, + log["exitcode"] + )) # vim: ai sts=4 et sw=4 diff --git a/test/template.t b/test/template.t index 80d76cea6..36466df23 100644 --- a/test/template.t +++ b/test/template.t @@ -102,7 +102,7 @@ class TestHooksBugNumber(TestCase): self.t.activate_hooks() def test_onmodify_custom(self): - # Testing a custom made hook + """Testing a custom made hook""" hookname = "on-modify-example-raw" content = """#!/usr/bin/env python @@ -134,8 +134,10 @@ sys.exit(0) self.assertIn("The hook did its thing", out) def test_onmodify_builtin_with_log(self): - # Testing a builtin hook and keeping track of its input/output - # The builtin hook is found in test/test_hooks + """Testing a builtin hook and keeping track of its input/output + + The builtin hook in found in test/test_hooks + """ hookname = "on-modify-for-template.py" self.t.hooks.add_default(hookname, log=True) @@ -144,10 +146,12 @@ sys.exit(0) code, out, err = self.t() self.assertIn("This is an example modify hook", out) - logs = self.t.hooks[hookname].get_logs() + hook = self.t.hooks[hookname] + logs = hook.get_logs() # Hook was called once - self.assertEqual(len(logs["calls"]), 1) + hook.assertTriggeredCount(1) + hook.assertExitcode(0) # Some message output from the hook self.assertEqual(logs["output"]["msgs"][0], @@ -158,8 +162,10 @@ sys.exit(0) "This is an example modify hook") def test_onmodify_bad_builtin_with_log(self): - # Testing a builtin hook and keeping track of its input/output - # The builtin hook is found in test/test_hooks + """Testing a builtin hook and keeping track of its input/output + + The builtin hook in found in test/test_hooks + """ hookname = "on-modify-for-template-badexit.py" self.t.hooks.add_default(hookname, log=True) @@ -168,10 +174,12 @@ sys.exit(0) code, out, err = self.t() self.assertNotIn("This is an example modify hook", out) - logs = self.t.hooks[hookname].get_logs() + hook = self.t.hooks[hookname] + logs = hook.get_logs() # Hook was called once - self.assertEqual(len(logs["calls"]), 1) + hook.assertTriggeredCount(1) + hook.assertExitcode(1) # Some message output from the hook self.assertEqual(logs["output"]["msgs"][0], diff --git a/test/test_hooks/wrapper.sh b/test/test_hooks/wrapper.sh index 86a8513d4..0dee55eb1 100644 --- a/test/test_hooks/wrapper.sh +++ b/test/test_hooks/wrapper.sh @@ -6,7 +6,7 @@ IN="${ORIGINALHOOK}.log.in" OUT="${ORIGINALHOOK}.log.out" # Let it know that we were executed -echo "% Called at $(date +%s%N)" >> ${IN} +echo "% Called at $(python -c 'import time; print(time.time())')" >> ${IN} # Log what arrives via stdin to ${IN} and what comes via stdout to ${OUT} $ORIGINALHOOK < <(tee -a ${IN}) > >(tee -a ${OUT})