* Add test case to cover https://github.com/GothenburgBitFactory/taskwarrior/issues/3416 * Add (builtin) on-add-modify test hook and use it * TDB2::add() move hook invocation before save (#3416)
This commit is contained in:
@@ -81,6 +81,9 @@ void TDB2::add (Task& task)
|
|||||||
std::string uuid = task.get ("uuid");
|
std::string uuid = task.get ("uuid");
|
||||||
changes[uuid] = task;
|
changes[uuid] = task;
|
||||||
|
|
||||||
|
// run hooks for this new task
|
||||||
|
Context::getContext ().hooks.onAdd (task);
|
||||||
|
|
||||||
auto innertask = replica.import_task_with_uuid (uuid);
|
auto innertask = replica.import_task_with_uuid (uuid);
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -121,9 +124,6 @@ void TDB2::add (Task& task)
|
|||||||
// update the cached working set with the new information
|
// update the cached working set with the new information
|
||||||
_working_set = std::make_optional (std::move (ws));
|
_working_set = std::make_optional (std::move (ws));
|
||||||
|
|
||||||
// run hooks for this new task
|
|
||||||
Context::getContext ().hooks.onAdd (task);
|
|
||||||
|
|
||||||
if (id.has_value ()) {
|
if (id.has_value ()) {
|
||||||
task.id = id.value();
|
task.id = id.value();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,23 @@ class TestHooksOnAdd(TestCase):
|
|||||||
code, out, err = self.t("1 info")
|
code, out, err = self.t("1 info")
|
||||||
self.assertIn("Description foo", out)
|
self.assertIn("Description foo", out)
|
||||||
|
|
||||||
|
def test_onadd_builtin_accept_modify(self):
|
||||||
|
"""on-add-accept-modify - a well-behaved, successful, on-add hook, that modifies the added task."""
|
||||||
|
hookname = 'on-add-modify'
|
||||||
|
self.t.hooks.add_default(hookname, log=True)
|
||||||
|
|
||||||
|
code, out, err = self.t("add teh foo")
|
||||||
|
|
||||||
|
hook = self.t.hooks[hookname]
|
||||||
|
hook.assertTriggeredCount(1)
|
||||||
|
hook.assertExitcode(0)
|
||||||
|
|
||||||
|
logs = hook.get_logs()
|
||||||
|
self.assertEqual(logs["output"]["msgs"][0], "FEEDBACK")
|
||||||
|
|
||||||
|
code, out, err = self.t("1 info")
|
||||||
|
self.assertIn("Description the foo", out)
|
||||||
|
|
||||||
def test_onadd_builtin_reject(self):
|
def test_onadd_builtin_reject(self):
|
||||||
"""on-add-reject - a well-behaved, failing, on-add hook."""
|
"""on-add-reject - a well-behaved, failing, on-add hook."""
|
||||||
hookname = 'on-add-reject'
|
hookname = 'on-add-reject'
|
||||||
|
|||||||
21
test/test_hooks/on-add-modify
Normal file
21
test/test_hooks/on-add-modify
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Input:
|
||||||
|
# - Line of JSON for proposed new task.
|
||||||
|
read new_task
|
||||||
|
|
||||||
|
if (echo $new_task | grep -qE '[tT]eh');
|
||||||
|
then
|
||||||
|
new_task=$(echo $new_task | sed -r 's/([tT])eh/\1he/g')
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# - JSON, modified
|
||||||
|
# - Optional feedback/error.
|
||||||
|
echo $new_task
|
||||||
|
echo 'FEEDBACK'
|
||||||
|
|
||||||
|
# Status:
|
||||||
|
# - 0: JSON accepted, non-JSON is feedback.
|
||||||
|
# - non-0: JSON ignored, non-JSON is error.
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user