Fix and test handling of backslashes in hooks (#3909)

* Update hooks to use `read -r` and `printf`

This portably avoids any interpretation of backslash escapes by the
shell.

* Support running make_tc_task elsewhere

* Add a test for backslashes in task descriptions
This commit is contained in:
Dustin J. Mitchell 2025-07-08 02:40:34 -04:00 committed by GitHub
parent 7fdcdebd19
commit c639cc030d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 115 additions and 75 deletions

View file

@ -10,7 +10,7 @@ read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo "$new_task" printf "%s\n" "$new_task"
echo 'on-add' echo 'on-add'
# Status: # Status:

View file

@ -2,11 +2,11 @@
read -r new_task read -r new_task
if (echo "$new_task" | grep -qE '[tT]eh'); if (printf "%s" "$new_task" | grep -qE '[tT]eh');
then then
new_task=$(echo "$new_task" | sed -r 's/([tT])eh/\1he/g') new_task=$(printf "%s" "$new_task" | sed -r 's/([tT])eh/\1he/g')
echo "Auto-corrected 'teh' --> 'the'" echo "Auto-corrected 'teh' --> 'the'"
fi fi
echo "$new_task" printf "%s" "$new_task"
exit 0 exit 0

View file

@ -12,8 +12,8 @@ read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo "$modified_task" printf "%s" "$modified_task"
echo 'on-modify' printf "%s" 'on-modify'
# Status: # Status:
# - 0: JSON accepted, non-JSON is feedback. # - 0: JSON accepted, non-JSON is feedback.

View file

@ -8,7 +8,13 @@ import tempfile
import unittest import unittest
from .exceptions import CommandError from .exceptions import CommandError
from .hooks import Hooks from .hooks import Hooks
from .utils import run_cmd_wait, run_cmd_wait_nofail, which, task_binary_location from .utils import (
run_cmd_wait,
run_cmd_wait_nofail,
which,
task_binary_location,
CMAKE_BINARY_DIR,
)
from .compat import STRING_TYPE from .compat import STRING_TYPE
@ -300,5 +306,21 @@ class Task(object):
# Use advanced time format # Use advanced time format
self._command = [cmd, "-f", faketime] + self._command self._command = [cmd, "-f", faketime] + self._command
def make_tc_task(self, **props):
"""Create a task directly in TaskChampion, bypassing TaskWarrior
entirely, and returning the UUID. The properties are not interpreted by
the shell.
"""
# Generate the path to the `make_tc_task` binary, which is a dependency of the
# test runner.
make_tc_task = os.path.abspath(
os.path.join(CMAKE_BINARY_DIR, "test", "make_tc_task")
)
cmd = [make_tc_task, self.datadir]
for p, v in props.items():
cmd.append(f"{p}={v}")
_, out, _ = run_cmd_wait(cmd)
return out.strip()
# vim: ai sts=4 et sw=4 # vim: ai sts=4 et sw=4

View file

@ -176,6 +176,34 @@ class TestHooksOnModify(TestCase):
hook.assertTriggeredCount(1) hook.assertTriggeredCount(1)
hook.assertExitcode(0) hook.assertExitcode(0)
def test_onmodify_escaped_backslash(self):
"""on-modify-accept - a well-behaved, successful, on-modify hook."""
# Create a task with a slash-escaped tab in it, avoiding TaskWarrior to ensure
# the slash exists in the DB.
uuid = self.t.make_tc_task(description=r"tab\ttab", status="pending")
hookname = "on-modify-accept"
self.t.hooks.add_default(hookname, log=True)
# `task _get` shows the backslash-escape.
code, out, err = self.t(f"_get 1.description")
self.assertEqual(out.strip(), r"tab\ttab")
code, out, err = self.t(f"{uuid} append foo")
hook = self.t.hooks[hookname]
hook.assertTriggeredCount(1)
hook.assertExitcode(0)
logs = hook.get_logs()
for msg in logs["output"]["msgs"]:
print(msg)
self.assertEqual(logs["output"]["msgs"][0], "FEEDBACK")
# `task _get` still shows the backslash-escape.
code, out, err = self.t(f"_get 1.description")
self.assertEqual(out.strip(), r"tab\ttab foo")
if __name__ == "__main__": if __name__ == "__main__":
from simpletap import TAPTestRunner from simpletap import TAPTestRunner

View file

@ -5,12 +5,12 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $new_task printf "%s\n" "$new_task"
echo 'FEEDBACK' echo 'FEEDBACK'
# Status: # Status:

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -5,12 +5,12 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $new_task printf "%s\n" "$new_task"
echo '{"description":"extra","status":"pending"}' echo '{"description":"extra","status":"pending"}'
echo 'FEEDBACK' echo 'FEEDBACK'

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -2,17 +2,17 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
if (echo $new_task | grep -qE '[tT]eh'); if (printf "%s\n" "$new_task" | grep -qE '[tT]eh');
then then
new_task=$(echo $new_task | sed -r 's/([tT])eh/\1he/g') new_task=$(printf "%s\n" "$new_task" | sed -r 's/([tT])eh/\1he/g')
fi fi
# Output: # Output:
# - JSON, modified # - JSON, modified
# - Optional feedback/error. # - Optional feedback/error.
echo $new_task printf "%s\n" "$new_task"
echo 'FEEDBACK' echo 'FEEDBACK'
# Status: # Status:

View file

@ -5,12 +5,12 @@
# Input: # Input:
# - Line of JSON for proposed new task. # - Line of JSON for proposed new task.
read new_task read -r new_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $new_task printf "%s\n" "$new_task"
echo 'FEEDBACK' echo 'FEEDBACK'
# Status: # Status:

View file

@ -2,9 +2,9 @@
echo "on-add executed" echo "on-add executed"
while read TASK; do while read -r TASK; do
echo "New task $TASK" echo "New task $TASK"
echo $TASK printf "%s\n" "$TASK"
done done
exit 0 exit 0

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Read-only line of JSON for each task added/modified # - Read-only line of JSON for each task added/modified
while read modified_task while read -r modified_task
do do
echo 'CHANGED TASK' echo 'CHANGED TASK'
done done

View file

@ -5,7 +5,7 @@
# Input: # Input:
# - Read-only line of JSON for each task added/modified # - Read-only line of JSON for each task added/modified
while read modified_task while read -r modified_task
do do
echo 'CHANGED TASK' echo 'CHANGED TASK'
done done

View file

@ -2,7 +2,7 @@
echo "on-exit executed" echo "on-exit executed"
while read TASK; do while read -r TASK; do
echo "New/modified task $TASK" echo "New/modified task $TASK"
done done

View file

@ -6,13 +6,13 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $modified_task printf "%s\n" "$modified_task"
echo 'FEEDBACK' echo 'FEEDBACK'
# Status: # Status:

View file

@ -6,8 +6,8 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -6,13 +6,13 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $modified_task printf "%s\n" "$modified_task"
echo '{"description":"extra","status":"pending"}' echo '{"description":"extra","status":"pending"}'
echo 'FEEDBACK' echo 'FEEDBACK'

View file

@ -6,8 +6,8 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -6,8 +6,8 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -6,8 +6,8 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.

View file

@ -6,13 +6,13 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $modified_task printf "%s\n" "$modified_task"
echo 'FEEDBACK' echo 'FEEDBACK'
# Status: # Status:

View file

@ -6,13 +6,13 @@
# Input: # Input:
# - line of JSON for the original task # - line of JSON for the original task
# - line of JSON for the modified task, the diff being the modification # - line of JSON for the modified task, the diff being the modification
read original_task read -r original_task
read modified_task read -r modified_task
# Output: # Output:
# - JSON, modified or unmodified. # - JSON, modified or unmodified.
# - Optional feedback/error. # - Optional feedback/error.
echo $original_task printf "%s\n" "$original_task"
# Status: # Status:
# - 0: JSON accepted, non-JSON is feedback. # - 0: JSON accepted, non-JSON is feedback.

View file

@ -2,9 +2,10 @@
echo "on-modify executed" echo "on-modify executed"
while read TASK MODTASK; do read -r TASK
echo "Existing task $TASK modified to $MODTASK" read -r MODTASK
echo $MODTASK
done echo "Existing task $TASK modified to $MODTASK"
printf "%s\n" "$MODTASK"
exit 0 exit 0

View file

@ -36,7 +36,6 @@ import unittest
sys.path.append(os.path.dirname(os.path.abspath(__file__))) sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from basetest import Task, TestCase from basetest import Task, TestCase
from basetest.utils import run_cmd_wait, CMAKE_BINARY_DIR
class TestUnusualTasks(TestCase): class TestUnusualTasks(TestCase):
@ -49,18 +48,8 @@ class TestUnusualTasks(TestCase):
) )
self.t.config("verbose", "nothing") self.t.config("verbose", "nothing")
def make_task(self, **props):
make_tc_task = os.path.abspath(
os.path.join(CMAKE_BINARY_DIR, "test", "make_tc_task")
)
cmd = [make_tc_task, self.t.datadir]
for p, v in props.items():
cmd.append(f"{p}={v}")
_, out, _ = run_cmd_wait(cmd)
return out.strip()
def test_empty_task_info(self): def test_empty_task_info(self):
uuid = self.make_task() uuid = self.t.make_tc_task()
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
self.assertNotIn("Entered", out) self.assertNotIn("Entered", out)
self.assertNotIn("Waiting", out) self.assertNotIn("Waiting", out)
@ -72,20 +61,20 @@ class TestUnusualTasks(TestCase):
self.assertRegex(out, r"Status\s+Pending") self.assertRegex(out, r"Status\s+Pending")
def test_modify_empty_task(self): def test_modify_empty_task(self):
uuid = self.make_task() uuid = self.t.make_tc_task()
self.t(f"{uuid} modify a description +taggy due:tomorrow") self.t(f"{uuid} modify a description +taggy due:tomorrow")
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Description\s+a description") self.assertRegex(out, r"Description\s+a description")
self.assertRegex(out, r"Tags\s+taggy") self.assertRegex(out, r"Tags\s+taggy")
def test_empty_task_recurring(self): def test_empty_task_recurring(self):
uuid = self.make_task(status="recurring") uuid = self.t.make_tc_task(status="recurring")
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Status\s+Recurring") self.assertRegex(out, r"Status\s+Recurring")
_, out, _ = self.t(f"{uuid} custom-report") _, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_invalid_rtype(self): def test_recurring_invalid_rtype(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
status="recurring", due=str(int(time.time())), rtype="occasional" status="recurring", due=str(int(time.time())), rtype="occasional"
) )
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
@ -94,7 +83,7 @@ class TestUnusualTasks(TestCase):
_, out, _ = self.t(f"{uuid} custom-report") _, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_invalid_recur(self): def test_recurring_invalid_recur(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
status="recurring", status="recurring",
due=str(int(time.time())), due=str(int(time.time())),
rtype="periodic", rtype="periodic",
@ -106,27 +95,27 @@ class TestUnusualTasks(TestCase):
_, out, _ = self.t(f"{uuid} custom-report") _, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_bad_quarters_rtype(self): def test_recurring_bad_quarters_rtype(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
status="recurring", due=str(int(time.time())), rtype="periodic", recur="9aq" status="recurring", due=str(int(time.time())), rtype="periodic", recur="9aq"
) )
_, out, _ = self.t(f"{uuid} custom-report") _, out, _ = self.t(f"{uuid} custom-report")
def test_invalid_entry_info(self): def test_invalid_entry_info(self):
uuid = self.make_task(entry="abcdef") uuid = self.t.make_tc_task(entry="abcdef")
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
self.assertNotIn("Entered", out) self.assertNotIn("Entered", out)
def test_invalid_modified_info(self): def test_invalid_modified_info(self):
uuid = self.make_task(modified="abcdef") uuid = self.t.make_tc_task(modified="abcdef")
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
self.assertNotIn(r"Last modified", out) self.assertNotIn(r"Last modified", out)
def test_invalid_start_info(self): def test_invalid_start_info(self):
uuid = self.make_task(start="abcdef") uuid = self.t.make_tc_task(start="abcdef")
_, out, _ = self.t(f"{uuid} info") _, out, _ = self.t(f"{uuid} info")
def test_invalid_dates_report(self): def test_invalid_dates_report(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
wait="wait", wait="wait",
scheduled="scheduled", scheduled="scheduled",
start="start", start="start",
@ -138,7 +127,7 @@ class TestUnusualTasks(TestCase):
_, out, _ = self.t(f"{uuid} custom-report") _, out, _ = self.t(f"{uuid} custom-report")
def test_invalid_dates_stop(self): def test_invalid_dates_stop(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
wait="wait", wait="wait",
scheduled="scheduled", scheduled="scheduled",
start="start", start="start",
@ -150,7 +139,7 @@ class TestUnusualTasks(TestCase):
_, out, _ = self.t(f"{uuid} stop") _, out, _ = self.t(f"{uuid} stop")
def test_invalid_dates_modify(self): def test_invalid_dates_modify(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
wait="wait", wait="wait",
scheduled="scheduled", scheduled="scheduled",
start="start", start="start",
@ -162,7 +151,7 @@ class TestUnusualTasks(TestCase):
_, out, _ = self.t(f"{uuid} mod a description +tag") _, out, _ = self.t(f"{uuid} mod a description +tag")
def test_invalid_dates_info(self): def test_invalid_dates_info(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
wait="wait", wait="wait",
scheduled="scheduled", scheduled="scheduled",
start="start", start="start",
@ -183,7 +172,7 @@ class TestUnusualTasks(TestCase):
# (note that 'modified' is not shown in the journal) # (note that 'modified' is not shown in the journal)
def test_invalid_dates_export(self): def test_invalid_dates_export(self):
uuid = self.make_task( uuid = self.t.make_tc_task(
wait="wait", wait="wait",
scheduled="scheduled", scheduled="scheduled",
start="start", start="start",