mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-18 15:33:08 +02:00
Tests - Invalid JSON replies in hooks no longer fail the test framework
This commit is contained in:
parent
e6031183dd
commit
1b218d68f5
2 changed files with 47 additions and 2 deletions
|
@ -16,6 +16,26 @@ from .utils import DEFAULT_HOOK_PATH
|
|||
from .exceptions import HookError
|
||||
|
||||
|
||||
class InvalidJSON(object):
|
||||
"""Object representing the original unparsed JSON string and the JSON error
|
||||
"""
|
||||
def __init__(self, original, error):
|
||||
self.original = original
|
||||
self.error = error
|
||||
|
||||
|
||||
def json_decoder(string):
|
||||
"""Attempt to decode a JSON string and in case of error return an
|
||||
InvalidJSON object
|
||||
"""
|
||||
decoder = json.JSONDecoder().decode
|
||||
|
||||
try:
|
||||
return decoder(string)
|
||||
except json.JSONDecodeError as e:
|
||||
return InvalidJSON(string, str(e))
|
||||
|
||||
|
||||
class Hooks(object):
|
||||
"""Abstraction to help interact with hooks (add, remove) during tests and
|
||||
keep track of which are active.
|
||||
|
@ -436,8 +456,6 @@ class LoggedHook(Hook):
|
|||
log = self._parse_log()
|
||||
newlog = {}
|
||||
|
||||
json_decoder = json.JSONDecoder().decode
|
||||
|
||||
for k1 in log:
|
||||
# Timestamps
|
||||
if k1 == "calls":
|
||||
|
@ -492,4 +510,30 @@ class LoggedHook(Hook):
|
|||
log["exitcode"]
|
||||
))
|
||||
|
||||
def assertValidJSONOutput(self):
|
||||
"""Check if current hook output is valid JSON in all expected replies
|
||||
"""
|
||||
log = self.get_logs()
|
||||
|
||||
for i, out in enumerate(log["output"]["json"]):
|
||||
assert not isinstance(out, InvalidJSON), ("Invalid JSON found at "
|
||||
"reply number {0} with "
|
||||
"content {1}".format(
|
||||
i + 1,
|
||||
out.original
|
||||
))
|
||||
|
||||
def assertInvalidJSONOutput(self):
|
||||
"""Check if current hook output is invalid JSON in any expected reply
|
||||
"""
|
||||
log = self.get_logs()
|
||||
|
||||
for i, out in enumerate(log["output"]["json"]):
|
||||
assert isinstance(out, InvalidJSON), ("Valid JSON found at reply "
|
||||
"number {0} with content "
|
||||
"{1}".format(
|
||||
i + 1,
|
||||
out.original
|
||||
))
|
||||
|
||||
# vim: ai sts=4 et sw=4
|
||||
|
|
|
@ -125,6 +125,7 @@ class TestHooksOnAdd(TestCase):
|
|||
self.t.hooks[hookname].assertTriggered()
|
||||
self.t.hooks[hookname].assertTriggeredCount(1)
|
||||
self.t.hooks[hookname].assertExitcode(0)
|
||||
self.t.hooks[hookname].assertInvalidJSONOutput()
|
||||
logs = self.t.hooks[hookname].get_logs()
|
||||
self.assertEqual(self.t.hooks[hookname].get_logs()["output"]["msgs"][0], "FEEDBACK")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue