mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Unittest - Template updated to include hook test examples
This commit is contained in:
parent
db78851b40
commit
d68fa7ea8a
3 changed files with 125 additions and 0 deletions
|
@ -95,6 +95,93 @@ class TestBugNumber(TestCase):
|
||||||
"""Executed once after all tests in the class"""
|
"""Executed once after all tests in the class"""
|
||||||
|
|
||||||
|
|
||||||
|
class TestHooksBugNumber(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
"""Executed before each test in the class"""
|
||||||
|
self.t = Task()
|
||||||
|
self.t.activate_hooks()
|
||||||
|
|
||||||
|
def test_onmodify_custom(self):
|
||||||
|
# Testing a custom made hook
|
||||||
|
hookname = "on-modify-example-raw"
|
||||||
|
|
||||||
|
content = """#!/usr/bin/env python
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
|
original_task = sys.stdin.readline()
|
||||||
|
modified_task = sys.stdin.readline()
|
||||||
|
|
||||||
|
task = json.loads(modified_task)
|
||||||
|
task["description"] = "The hook did its magic"
|
||||||
|
|
||||||
|
sys.stdout.write(json.dumps(task, separators=(',', ':')) + '\\n')
|
||||||
|
sys.exit(0)
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.t.hooks.add(hookname, content)
|
||||||
|
|
||||||
|
self.t(("add", "Hello hooks"))
|
||||||
|
self.t(("1", "mod", "/Hello/Greetings/"))
|
||||||
|
code, out, err = self.t()
|
||||||
|
self.assertIn("The hook did its magic", out)
|
||||||
|
|
||||||
|
self.t.hooks[hookname].disable()
|
||||||
|
self.assertFalse(self.t.hooks[hookname].is_active())
|
||||||
|
|
||||||
|
self.t(("1", "mod", "/magic/thing/"))
|
||||||
|
code, out, err = self.t()
|
||||||
|
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 in found in test/test_hooks
|
||||||
|
hookname = "on-modify-for-template.py"
|
||||||
|
self.t.hooks.add_default(hookname, log=True)
|
||||||
|
|
||||||
|
self.t(("add", "Hello hooks"))
|
||||||
|
self.t(("1", "mod", "/Hello/Greetings/"))
|
||||||
|
code, out, err = self.t()
|
||||||
|
self.assertIn("This is an example modify hook", out)
|
||||||
|
|
||||||
|
logs = self.t.hooks[hookname].get_logs()
|
||||||
|
|
||||||
|
# Hook was called once
|
||||||
|
self.assertEqual(len(logs["calls"]), 1)
|
||||||
|
|
||||||
|
# Some message output from the hook
|
||||||
|
self.assertEqual(logs["output"]["msgs"][0],
|
||||||
|
"Hello from the template hook")
|
||||||
|
|
||||||
|
# This is what taskwarrior received
|
||||||
|
self.assertEqual(logs["output"]["json"][0]["description"],
|
||||||
|
"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 in found in test/test_hooks
|
||||||
|
hookname = "on-modify-for-template-badexit.py"
|
||||||
|
self.t.hooks.add_default(hookname, log=True)
|
||||||
|
|
||||||
|
self.t(("add", "Hello hooks"))
|
||||||
|
self.t.runError(("1", "mod", "/Hello/Greetings/"))
|
||||||
|
code, out, err = self.t()
|
||||||
|
self.assertNotIn("This is an example modify hook", out)
|
||||||
|
|
||||||
|
logs = self.t.hooks[hookname].get_logs()
|
||||||
|
|
||||||
|
# Hook was called once
|
||||||
|
self.assertEqual(len(logs["calls"]), 1)
|
||||||
|
|
||||||
|
# Some message output from the hook
|
||||||
|
self.assertEqual(logs["output"]["msgs"][0],
|
||||||
|
"Hello from the template hook")
|
||||||
|
|
||||||
|
# This is what taskwarrior would have used if hook finished cleanly
|
||||||
|
self.assertEqual(logs["output"]["json"][0]["description"],
|
||||||
|
"This is an example modify hook")
|
||||||
|
|
||||||
|
|
||||||
class ServerTestBugNumber(ServerTestCase):
|
class ServerTestBugNumber(ServerTestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
|
|
19
test/test_hooks/on-modify-for-template-badexit.py
Normal file
19
test/test_hooks/on-modify-for-template-badexit.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
|
original_task = sys.stdin.readline()
|
||||||
|
modified_task = sys.stdin.readline()
|
||||||
|
|
||||||
|
task = json.loads(modified_task)
|
||||||
|
task["description"] = "This is an example modify hook"
|
||||||
|
|
||||||
|
# A random message
|
||||||
|
sys.stdout.write("Hello from the template hook\n")
|
||||||
|
|
||||||
|
sys.stdout.write(json.dumps(task, separators=(',', ':')) + '\n')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# vim: ai sts=4 et sw=4
|
19
test/test_hooks/on-modify-for-template.py
Normal file
19
test/test_hooks/on-modify-for-template.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
|
original_task = sys.stdin.readline()
|
||||||
|
modified_task = sys.stdin.readline()
|
||||||
|
|
||||||
|
task = json.loads(modified_task)
|
||||||
|
task["description"] = "This is an example modify hook"
|
||||||
|
|
||||||
|
# A random message
|
||||||
|
sys.stdout.write("Hello from the template hook\n")
|
||||||
|
|
||||||
|
sys.stdout.write(json.dumps(task, separators=(',', ':')) + '\n')
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
# vim: ai sts=4 et sw=4
|
Loading…
Add table
Add a link
Reference in a new issue