From d68fa7ea8ab8097a573deebc050327ea228a049c Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Mon, 19 Jan 2015 14:57:33 +0000 Subject: [PATCH] Unittest - Template updated to include hook test examples --- test/template.t | 87 +++++++++++++++++++ .../on-modify-for-template-badexit.py | 19 ++++ test/test_hooks/on-modify-for-template.py | 19 ++++ 3 files changed, 125 insertions(+) create mode 100644 test/test_hooks/on-modify-for-template-badexit.py create mode 100644 test/test_hooks/on-modify-for-template.py diff --git a/test/template.t b/test/template.t index c8aeb0af7..5d6d9e042 100644 --- a/test/template.t +++ b/test/template.t @@ -95,6 +95,93 @@ class TestBugNumber(TestCase): """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): @classmethod def setUpClass(cls): diff --git a/test/test_hooks/on-modify-for-template-badexit.py b/test/test_hooks/on-modify-for-template-badexit.py new file mode 100644 index 000000000..883c9e621 --- /dev/null +++ b/test/test_hooks/on-modify-for-template-badexit.py @@ -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 diff --git a/test/test_hooks/on-modify-for-template.py b/test/test_hooks/on-modify-for-template.py new file mode 100644 index 000000000..6600b3a72 --- /dev/null +++ b/test/test_hooks/on-modify-for-template.py @@ -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