diff --git a/tests/README b/tests/README index 7e748fa..e09c874 100644 --- a/tests/README +++ b/tests/README @@ -105,7 +105,7 @@ First digit tells the family: 0 - the absolute basics and global stuff 1 - basic every-day usage - 2 - add ins + 2 - add ins Second digit tells the particular command we are testing. diff --git a/tests/t1020-addtolistfile.sh b/tests/t1020-addtolistfile.sh new file mode 100755 index 0000000..16e09de --- /dev/null +++ b/tests/t1020-addtolistfile.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +test_description='basic addto and list functionality + +This test just makes sure the basic addto and listfile +commands work, including support for filtering. +' +. ./test-lib.sh + +# +# Addto and listfile +# +test_todo_session 'nonexistant file' <>> todo.sh addto garden.txt notice the daisies +TODO: Destination file $HOME/garden.txt does not exist. +EOF + +touch "$HOME/garden.txt" + +test_todo_session 'basic addto/listfile' <>> todo.sh addto garden.txt notice the daisies +GARDEN: 'notice the daisies' added on line 1. + +>>> todo.sh listfile garden.txt +1 notice the daisies +-- +TODO: 1 of 1 tasks shown from garden.txt + +>>> todo.sh addto garden.txt smell the roses +GARDEN: 'smell the roses' added on line 2. + +>>> todo.sh listfile garden.txt +1 notice the daisies +2 smell the roses +-- +TODO: 2 of 2 tasks shown from garden.txt +EOF + +# +# Filter +# +test_todo_session 'basic listfile filtering' <>> todo.sh listfile garden.txt daisies +1 notice the daisies +-- +TODO: 1 of 2 tasks shown from garden.txt + +>>> todo.sh listfile garden.txt smell +2 smell the roses +-- +TODO: 1 of 2 tasks shown from garden.txt +EOF + +test_todo_session 'case-insensitive filtering' <>> todo.sh addto garden.txt smell the uppercase Roses +GARDEN: 'smell the uppercase Roses' added on line 3. + +>>> todo.sh listfile garden.txt roses +2 smell the roses +3 smell the uppercase Roses +-- +TODO: 2 of 3 tasks shown from garden.txt +EOF + +test_todo_session 'addto with &' <>> todo.sh addto garden.txt "dig the garden & water the flowers" +GARDEN: 'dig the garden & water the flowers' added on line 4. + +>>> todo.sh listfile garden.txt +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 garden.txt +EOF + +test_done diff --git a/tests/t1030-addto-date.sh b/tests/t1030-addto-date.sh new file mode 100755 index 0000000..f4d908c --- /dev/null +++ b/tests/t1030-addto-date.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +test_description='test the date on addto feature + +Tests paths by which we might automatically add +a date to each item. +' +. ./test-lib.sh + +touch "$HOME/garden.txt" + +# +# Add and list +# +test_todo_session 'cmd line first day' <>> todo.sh -t addto garden.txt notice the daisies +GARDEN: '2009-02-13 notice the daisies' added on line 1. + +>>> todo.sh listfile garden.txt +1 2009-02-13 notice the daisies +-- +TODO: 1 of 1 tasks shown from garden.txt +EOF + +test_tick + +test_todo_session 'cmd line second day' <>> todo.sh -t addto garden.txt smell the roses +GARDEN: '2009-02-14 smell the roses' added on line 2. + +>>> todo.sh listfile garden.txt +1 2009-02-13 notice the daisies +2 2009-02-14 smell the roses +-- +TODO: 2 of 2 tasks shown from garden.txt +EOF + +test_tick + +test_todo_session 'cmd line third day' <>> todo.sh -t addto garden.txt mow the lawn +GARDEN: '2009-02-15 mow the lawn' added on line 3. + +>>> todo.sh listfile garden.txt +1 2009-02-13 notice the daisies +2 2009-02-14 smell the roses +3 2009-02-15 mow the lawn +-- +TODO: 3 of 3 tasks shown from garden.txt +EOF + +# Switch to config file +echo "export TODOTXT_DATE_ON_ADD=1" >> todo.cfg + +# Bump the clock, for good measure. +test_tick 3600 + +test_todo_session 'config file third day' <>> todo.sh addto garden.txt take out the trash +GARDEN: '2009-02-15 take out the trash' added on line 4. + +>>> todo.sh listfile garden.txt +1 2009-02-13 notice the daisies +2 2009-02-14 smell the roses +3 2009-02-15 mow the lawn +4 2009-02-15 take out the trash +-- +TODO: 4 of 4 tasks shown from garden.txt +EOF + +test_done diff --git a/todo.sh b/todo.sh index 684e577..8a45b05 100755 --- a/todo.sh +++ b/todo.sh @@ -473,6 +473,24 @@ fi # === HEAVY LIFTING === shopt -s extglob +_addto() { + file="$1" + input="$2" + cleaninput $input + + if [[ $TODOTXT_DATE_ON_ADD = 1 ]]; then + now=`date '+%Y-%m-%d'` + input="$now $input" + fi + echo "$input" >> "$file" + [ $TODOTXT_VERBOSE -gt 0 ] && { + TASKNUM=$(sed -n '$ =' "$file") + BASE=$(basename "$file") + PREFIX=$(echo ${BASE%%.[^.]*} | tr [a-z] [A-Z]) + echo "${PREFIX}: '$input' added on line $TASKNUM." + } +} + _list() { local FILE="$1" ## If the file starts with a "/" use absolute path. Otherwise, @@ -616,17 +634,7 @@ case $action in shift input=$* fi - cleaninput $input - - if [[ $TODOTXT_DATE_ON_ADD = 1 ]]; then - now=`date '+%Y-%m-%d'` - input="$now $input" - fi - echo "$input" >> "$TODO_FILE" - [ $TODOTXT_VERBOSE -gt 0 ] && { - TASKNUM=$(sed -n '$ =' "$TODO_FILE") - echo "TODO: '$input' added on line $TASKNUM." - } + _addto "$TODO_FILE" "$input" cleanup;; "addm") @@ -646,15 +654,7 @@ case $action in # Treat each line seperately for line in $input ; do - if [[ $TODOTXT_DATE_ON_ADD = 1 ]]; then - now=`date '+%Y-%m-%d'` - line="$now $line" - fi - echo "$line" >> "$TODO_FILE" - [ $TODOTXT_VERBOSE -gt 0 ] && { - TASKNUM=$(sed -n '$ =' "$TODO_FILE") - echo "TODO: '$line' added on line $TASKNUM." - } + _addto "$TODO_FILE" "$line" done IFS=$SAVEIFS cleanup;; @@ -668,11 +668,7 @@ case $action in input=$* if [ -f "$dest" ]; then - echo "$input" >> "$dest" - [ $TODOTXT_VERBOSE -gt 0 ] && { - TASKNUM=$(sed -n '$ =' "$dest") - echo "TODO: '$input' added to $dest on line $TASKNUM." - } + _addto "$dest" "$input" else echo "TODO: Destination file $dest does not exist." fi