diff --git a/tests/t0002-actions.sh b/tests/t0002-actions.sh new file mode 100755 index 0000000..d3b31ed --- /dev/null +++ b/tests/t0002-actions.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +test_description='todo.sh actions.d + +This test just makes sure that todo.sh can locate custom actions. +' +. ./test-lib.sh + +# All the below tests will output the custom action message +cat > expect << EOF +TODO: foo +EOF + +cat > foo << EOF +echo "TODO: foo" +EOF +chmod +x foo + +test_expect_success 'custom action (default location 1)' ' + mkdir .todo.actions.d + cp foo .todo.actions.d/ + todo.sh foo > output; + test_cmp expect output && rm -rf .todo.actions.d +' + +test_expect_success 'custom action (default location 2)' ' + mkdir -p .todo/actions + cp foo .todo/actions/ + todo.sh foo > output; + test_cmp expect output && rm -rf .todo/actions +' + +test_expect_success 'custom action (env variable)' ' + mkdir myactions + cp foo myactions/ + TODO_ACTIONS_DIR=myactions todo.sh foo > output; + test_cmp expect output && rm -rf myactions +' + +test_done diff --git a/tests/t1330-ls-highlighting.sh b/tests/t1330-ls-highlighting.sh new file mode 100755 index 0000000..c3c3558 --- /dev/null +++ b/tests/t1330-ls-highlighting.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# + +test_description='list highlighting + +This test checks the highlighting (with colors) of prioritized tasks. +' +. ./test-lib.sh + +TEST_TODO_=todo.cfg + +# +# check the highlighting of prioritized tasks +# +cat > todo.txt <>> todo.sh ls +1 (A) @con01 +prj01 -- Some project 01 task, pri A +2 (B) @con02 +prj02 -- Some project 02 task, pri B +3 (C) @con01 +prj01 -- Some project 01 task, pri C +4 (D) @con02 +prj02 -- Some project 02 task, pri D +5 (E) @con01 +prj01 -- Some project 01 task, pri E +6 (Z) @con02 +prj02 -- Some project 02 task, pri Z +7 @con01 +prj01 -- Some project 01 task, no priority +8 @con02 +prj02 -- Some project 02 task, no priority +-- +TODO: 8 of 8 tasks shown +EOF + +# +# check changing the color definitions into something other than ANSI color +# escape sequences +# +TEST_TODO_CUSTOM=todo-custom.cfg +cat todo.cfg > "$TEST_TODO_CUSTOM" +cat >> "$TEST_TODO_CUSTOM" <<'EOF' +export YELLOW='${color yellow}' +export GREEN='${color green}' +export LIGHT_BLUE='${color LightBlue}' +export WHITE='${color white}' +export DEFAULT='${color}' +export PRI_A=$YELLOW +export PRI_B=$GREEN +export PRI_C=$LIGHT_BLUE +export PRI_X=$WHITE +EOF +test_todo_session 'customized highlighting' <<'EOF' +>>> todo.sh -d "$TEST_TODO_CUSTOM" ls +${color yellow}1 (A) @con01 +prj01 -- Some project 01 task, pri A${color} +${color green}2 (B) @con02 +prj02 -- Some project 02 task, pri B${color} +${color LightBlue}3 (C) @con01 +prj01 -- Some project 01 task, pri C${color} +${color white}4 (D) @con02 +prj02 -- Some project 02 task, pri D${color} +${color white}5 (E) @con01 +prj01 -- Some project 01 task, pri E${color} +${color white}6 (Z) @con02 +prj02 -- Some project 02 task, pri Z${color} +7 @con01 +prj01 -- Some project 01 task, no priority +8 @con02 +prj02 -- Some project 02 task, no priority +-- +TODO: 8 of 8 tasks shown +EOF + +# +# check defining highlightings for more priorities than the default A, B, C +# +TEST_TODO_ADDITIONAL=todo-additional.cfg +cat todo.cfg > "$TEST_TODO_ADDITIONAL" +cat >> "$TEST_TODO_ADDITIONAL" <<'EOF' +export PRI_E=$BROWN +export PRI_Z=$LIGHT_PURPLE +EOF +test_todo_session 'additional highlighting pri E+Z' <<'EOF' +>>> todo.sh -d "$TEST_TODO_ADDITIONAL" ls +1 (A) @con01 +prj01 -- Some project 01 task, pri A +2 (B) @con02 +prj02 -- Some project 02 task, pri B +3 (C) @con01 +prj01 -- Some project 01 task, pri C +4 (D) @con02 +prj02 -- Some project 02 task, pri D +5 (E) @con01 +prj01 -- Some project 01 task, pri E +6 (Z) @con02 +prj02 -- Some project 02 task, pri Z +7 @con01 +prj01 -- Some project 01 task, no priority +8 @con02 +prj02 -- Some project 02 task, no priority +-- +TODO: 8 of 8 tasks shown +EOF + +# check changing the fallback highlighting for undefined priorities +# +TEST_TODO_PRI_X=todo-pri-x.cfg +cat todo.cfg > "$TEST_TODO_PRI_X" +cat >> "$TEST_TODO_PRI_X" <<'EOF' +export PRI_X=$BROWN +EOF +test_todo_session 'different highlighting for pri X' <<'EOF' +>>> todo.sh -d "$TEST_TODO_PRI_X" ls +1 (A) @con01 +prj01 -- Some project 01 task, pri A +2 (B) @con02 +prj02 -- Some project 02 task, pri B +3 (C) @con01 +prj01 -- Some project 01 task, pri C +4 (D) @con02 +prj02 -- Some project 02 task, pri D +5 (E) @con01 +prj01 -- Some project 01 task, pri E +6 (Z) @con02 +prj02 -- Some project 02 task, pri Z +7 @con01 +prj01 -- Some project 01 task, no priority +8 @con02 +prj02 -- Some project 02 task, no priority +-- +TODO: 8 of 8 tasks shown +EOF + +test_done diff --git a/tests/t8000-actions.sh b/tests/t8000-actions.sh new file mode 100755 index 0000000..3d92248 --- /dev/null +++ b/tests/t8000-actions.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +test_description='custom actions functionality + +This test covers the contract between todo.sh and custom actions. +' +. ./test-lib.sh + +unset TODO_ACTIONS_DIR +mkdir .todo.actions.d +cat > .todo.actions.d/foo << EOF +echo "TODO: foo" +EOF + +test_todo_session 'nonexecutable action' <>> todo.sh foo +Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] +Try 'todo.sh -h' for more information. +=== 1 +EOF + +chmod +x .todo.actions.d/foo +test_todo_session 'executable action' <>> todo.sh foo +TODO: foo +EOF + +cat > .todo.actions.d/ls << EOF +echo "TODO: my ls" +EOF +chmod +x .todo.actions.d/ls +test_todo_session 'overriding built-in action' <>> todo.sh ls +TODO: my ls + +>>> todo.sh command ls +-- +TODO: 0 of 0 tasks shown +EOF + +cat > .todo.actions.d/bad << EOF +echo "TODO: bad" +exit 42 +EOF +chmod +x .todo.actions.d/bad +test_todo_session 'failing action' <>> todo.sh bad +TODO: bad +=== 42 +EOF + +test_done diff --git a/tests/test-lib.sh b/tests/test-lib.sh index 34cd9e4..efe8960 100644 --- a/tests/test-lib.sh +++ b/tests/test-lib.sh @@ -542,7 +542,7 @@ test_todo_session () { if [ $status = 0 ]; then test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output" else - test_expect_success "$1 $subnum" "$cmd > output || test $? = $status && test_cmp expect output" + test_expect_success "$1 $subnum" "$cmd > output ; test \$? = $status && test_cmp expect output" fi subnum=$(($subnum + 1)) @@ -560,7 +560,7 @@ test_todo_session () { if [ $status = 0 ]; then test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output" else - test_expect_success "$1 $subnum" "$cmd > output || test $? = $status && test_cmp expect output" + test_expect_success "$1 $subnum" "$cmd > output ; test \$? = $status && test_cmp expect output" fi fi } diff --git a/todo.sh b/todo.sh index cad4312..d2fc5de 100755 --- a/todo.sh +++ b/todo.sh @@ -250,7 +250,7 @@ die() cleanup() { [ -f "$TMP_FILE" ] && rm "$TMP_FILE" - exit 0 + return 0 } cleaninput() @@ -279,7 +279,6 @@ archive() sed -n 'G; s/\n/&&/; /^\([ ~-]*\n\).*\n\1/d; s/\n//; h; P' "$TMP_FILE" > "$TODO_FILE" #[[ $TODOTXT_VERBOSE -gt 0 ]] && echo "TODO: Duplicate tasks have been removed." [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: $TODO_FILE archived." - cleanup } @@ -460,9 +459,9 @@ ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -f "$REPORT_FILE" ] || cp /dev/null "$REPORT_FILE" if [ $TODOTXT_PLAIN = 1 ]; then - PRI_A=$NONE - PRI_B=$NONE - PRI_C=$NONE + for clr in ${!PRI_@}; do + export $clr=$NONE + done PRI_X=$NONE DEFAULT=$NONE fi @@ -570,14 +569,14 @@ _list() { s/^ /0/; ''' \ | eval ${TODOTXT_SORT_COMMAND} \ - | sed ''' - /^[0-9]\{'$PADDING'\} x /! { - /(A)/ s|^.*|'$PRI_A'&'$DEFAULT'| - /(B)/ s|^.*|'$PRI_B'&'$DEFAULT'| - /(C)/ s|^.*|'$PRI_C'&'$DEFAULT'| - /([D-Z])/ s|^.*|'$PRI_X'&'$DEFAULT'| - } - ''' \ + | awk '''{ + pos = match($0, /\([A-Z]\)/) + if( pos > 0 && match($0, /^[0-9]+ x /) != 1 ) { + clr=ENVIRON["PRI_" substr($0, pos+1, 1)] + str = ( clr ? clr : ENVIRON["PRI_X"] ) $0 ENVIRON["DEFAULT"] + gsub( /\\+033/, "\033", str) ; print str + } else { print } + }''' \ | sed ''' s/'${HIDE_PRIORITY_SUBSTITUTION:-^}'//g s/'${HIDE_PROJECTS_SUBSTITUTION:-^}'//g @@ -602,7 +601,7 @@ _list() { fi } -export -f _list +export -f _list die # == HANDLE ACTION == action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' ) @@ -620,7 +619,9 @@ then elif [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ] then "$TODO_ACTIONS_DIR/$action" "$@" + status=$? cleanup + exit $status fi ## Only run if $action isn't found in .todo.actions.d @@ -635,7 +636,7 @@ case $action in input=$* fi _addto "$TODO_FILE" "$input" - cleanup;; + ;; "addm") if [[ -z "$2" && $TODOTXT_FORCE = 0 ]]; then @@ -657,7 +658,7 @@ case $action in _addto "$TODO_FILE" "$line" done IFS=$SAVEIFS - cleanup;; + ;; "addto" ) [ -z "$2" ] && die "usage: $TODO_SH addto DEST \"TODO ITEM\"" @@ -672,7 +673,7 @@ case $action in else echo "TODO: Destination file $dest does not exist." fi - cleanup;; + ;; "append" | "app" ) errmsg="usage: $TODO_SH append ITEM# \"TEXT TO APPEND\"" @@ -698,7 +699,7 @@ case $action in else echo "TODO: Error appending task $item." fi - cleanup;; + ;; "archive" ) archive;; @@ -730,7 +731,6 @@ case $action in sed -i.bak -e $item"s/^.*//" "$TODO_FILE" fi [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: '$DELETEME' deleted." - cleanup else echo "TODO: No tasks were deleted." fi @@ -740,7 +740,8 @@ case $action in else sed -i.bak -e $item"s/$3/ /g" "$TODO_FILE" [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: $3 removed from $item." - fi ;; + fi + ;; "depri" | "dp" ) errmsg="usage: $TODO_SH depri ITEM#[, ITEM#, ITEM#, ...]" @@ -768,7 +769,7 @@ case $action in die "$errmsg" fi done - cleanup ;; + ;; "do" ) errmsg="usage: $TODO_SH do ITEM#[, ITEM#, ITEM#, ...]" @@ -804,7 +805,7 @@ case $action in if [ $TODOTXT_AUTO_ARCHIVE = 1 ]; then archive fi - cleanup ;; + ;; "help" ) if [ -t 1 ] ; then # STDOUT is a TTY @@ -819,8 +820,6 @@ case $action in "list" | "ls" ) shift ## Was ls; new $1 is first search term _list "$TODO_FILE" "$@" - - cleanup ;; "listall" | "lsa" ) @@ -828,8 +827,6 @@ case $action in cat "$TODO_FILE" "$DONE_FILE" > "$TMP_FILE" _list "$TMP_FILE" "$@" - - cleanup ;; "listfile" | "lf" ) @@ -838,18 +835,15 @@ case $action in shift ## Was filename; next $1 is first search term _list "$FILE" "$@" - - cleanup ;; "listcon" | "lsc" ) grep -o '[^ ]*@[^ ]\+' "$TODO_FILE" | grep '^@' | sort -u - cleanup ;; + ;; "listproj" | "lsprj" ) grep -o '[^ ]*+[^ ]\+' "$TODO_FILE" | grep '^+' | sort -u - cleanup ;; - + ;; "listpri" | "lsp" ) shift ## was "listpri", new $1 is priority to list @@ -904,7 +898,6 @@ case $action in echo "$MOVEME" >> "$dest" [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: '$MOVEME' moved from '$src' to '$dest'." - cleanup else echo "TODO: No tasks moved." fi @@ -917,7 +910,7 @@ case $action in else echo "TODO: Source file $src does not exist." fi - cleanup;; + ;; "prepend" | "prep" ) errmsg="usage: $TODO_SH prepend ITEM# \"TEXT TO PREPEND\"" @@ -963,7 +956,7 @@ case $action in echo "TODO: Error prepending task $item." fi fi - cleanup;; + ;; "pri" | "p" ) item=$2 @@ -986,10 +979,10 @@ note: PRIORITY must be anywhere from A to Z." echo "$item: $NEWTODO" echo "TODO: $item prioritized ($newpri)." } - cleanup else die "$errmsg" - fi;; + fi + ;; "replace" ) errmsg="usage: $TODO_SH replace ITEM# \"UPDATED ITEM\"" @@ -1026,7 +1019,7 @@ note: PRIORITY must be anywhere from A to Z." echo "replaced with" echo "$item: $NEWTODO" } - cleanup;; + ;; "report" ) #archive first @@ -1045,9 +1038,10 @@ note: PRIORITY must be anywhere from A to Z." echo $TECHO >> "$REPORT_FILE" [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: Report file updated." cat "$REPORT_FILE" - cleanup;; + ;; * ) - usage - ;; + usage;; esac + +cleanup