From ddaf9ade22769b22e5e11788aff3dea311d6a78b Mon Sep 17 00:00:00 2001 From: Paul Mansfield Date: Sat, 5 Sep 2009 20:36:38 +0100 Subject: [PATCH 1/2] Bug Fix: Issue 6 append and replace unexpected behavior if there's an & in task (even in quotes) Using the cleaninput function, to escape &'s on certain actions. Currently the actions needing escaped &'s are append, prepend and replace. Other actions including add need unescaped &'s. --- tests/t1000-addlist.sh | 14 +++++++++++++ tests/t1100-replace.sh | 6 ++++++ tests/t1400-prepend.sh | 5 +++++ tests/t1600-append.sh | 47 ++++++++++++++++++++++++++++++++++++++++++ todo.sh | 8 ++++++- 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100755 tests/t1600-append.sh diff --git a/tests/t1000-addlist.sh b/tests/t1000-addlist.sh index 123a1f8..9cabdf5 100755 --- a/tests/t1000-addlist.sh +++ b/tests/t1000-addlist.sh @@ -55,4 +55,18 @@ TODO: 'smell the uppercase Roses' added on line 3. TODO: 2 of 3 tasks shown from $HOME/todo.txt EOF +test_todo_session 'add with &' <>> todo.sh add "dig the garden & water the flowers" +TODO: 'dig the garden & water the flowers' added on line 4. + +>>> todo.sh list +4 dig the garden & water the flowers +1 notice the daisies +2 smell the roses +3 smell the uppercase Roses +-- +TODO: 4 of 4 tasks shown from $HOME/todo.txt + +EOF + test_done diff --git a/tests/t1100-replace.sh b/tests/t1100-replace.sh index ecec0db..e1cee69 100755 --- a/tests/t1100-replace.sh +++ b/tests/t1100-replace.sh @@ -72,6 +72,12 @@ TODO: 4 prioritized (A). replaced with 4: (A) collect the bread EOF +test_todo_session 'replace with &' << EOF +>>> todo.sh replace 3 "thrash the hay & thresh the wheat" +3: jump on hay +replaced with +3: thrash the hay & thresh the wheat +EOF test_todo_session 'replace error' << EOF >>> todo.sh replace 10 "hej!" diff --git a/tests/t1400-prepend.sh b/tests/t1400-prepend.sh index 50610df..eaac618 100755 --- a/tests/t1400-prepend.sh +++ b/tests/t1400-prepend.sh @@ -52,4 +52,9 @@ TODO: 3 of 3 tasks shown from $HOME/todo.txt EOF +test_todo_session 'prepend with &' <>> todo.sh prepend 3 "no running & jumping now" +3: no running & jumping now stop +EOF + test_done diff --git a/tests/t1600-append.sh b/tests/t1600-append.sh new file mode 100755 index 0000000..54feaee --- /dev/null +++ b/tests/t1600-append.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +test_description='basic append functionality + +Ensure we can append items successfully. +' +. ./test-lib.sh + +# +# Set up the basic todo.txt +# +todo.sh add notice the daisies > /dev/null + +test_todo_session 'append usage' <>> todo.sh append adf asdfa +=== 1 +usage: todo.sh append ITEM# "TEXT TO APPEND" +EOF + +test_todo_session 'basic append' <>> todo.sh append 1 "smell the roses" +1: notice the daisies smell the roses + +>>> todo.sh list +1 notice the daisies smell the roses +-- +TODO: 1 of 1 tasks shown from $HOME/todo.txt +EOF + +test_todo_session 'basic append with &' <>> todo.sh append 1 "see the wasps & bees" +1: notice the daisies smell the roses see the wasps & bees + +>>> todo.sh list +1 notice the daisies smell the roses see the wasps & bees +-- +TODO: 1 of 1 tasks shown from $HOME/todo.txt +EOF + + +test_todo_session 'append error' << EOF +>>> todo.sh append 10 "hej!" +=== 1 +10: No such todo. +EOF + +test_done diff --git a/todo.sh b/todo.sh index 22d5285..386134b 100755 --- a/todo.sh +++ b/todo.sh @@ -244,8 +244,14 @@ cleanup() cleaninput() { # Cleanup the input - # Replace newlines with spaces + # Replace newlines with spaces Always input=`echo $input | tr -d '\r|\n'` + + # Check which action we are being used in as this affects what cleaning we do + if [[ $action =~ (append|app|prepend|prep|replace) ]]; then + # These actions use sed and & as the matched string so escape it + input=`echo $input | sed 's/\&/\\\&/g'` + fi } archive() From 51dd50b41d362a240d70db9f1d91428ccb5cce9c Mon Sep 17 00:00:00 2001 From: Paul Mansfield Date: Sat, 5 Sep 2009 23:55:22 +0100 Subject: [PATCH 2/2] Tighten the regex in Issue 6 bugfix. Checks for complete action names before escaping &'s --- todo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index 386134b..d25173d 100755 --- a/todo.sh +++ b/todo.sh @@ -248,7 +248,7 @@ cleaninput() input=`echo $input | tr -d '\r|\n'` # Check which action we are being used in as this affects what cleaning we do - if [[ $action =~ (append|app|prepend|prep|replace) ]]; then + if [[ $action =~ ^(append|app|prepend|prep|replace)$ ]]; then # These actions use sed and & as the matched string so escape it input=`echo $input | sed 's/\&/\\\&/g'` fi