From 50aea1e4f001bf988d851f5f4e45b8e61981f204 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 30 Aug 2012 12:20:09 +0200 Subject: [PATCH 1/3] Tests: Add basic coverage of -h / shorthelp. For a start, this is basically an adapted version of tests/t2100-help.sh. --- tests/t2120-shorthelp.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 tests/t2120-shorthelp.sh diff --git a/tests/t2120-shorthelp.sh b/tests/t2120-shorthelp.sh new file mode 100755 index 0000000..8a08de0 --- /dev/null +++ b/tests/t2120-shorthelp.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# + +test_description='shorthelp functionality + +This test covers the output of the -h option and the shorthelp action. +' +. ./actions-test-lib.sh +. ./test-lib.sh + +# Note: To avoid having to adapt the test whenever the actions change, only +# check for the section headers. +test_todo_session '-h output' <>> todo.sh -h | sed '/^ [A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + See "help" for more details. +EOF + +test_todo_session 'shorthelp output' <>> todo.sh shorthelp | sed '/^ [A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + See "help" for more details. +EOF + +make_action "foo" +test_todo_session 'shorthelp output with custom action' <>> todo.sh -v shorthelp | sed '/^ [A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + Add-on Actions: + See "help" for more details. +EOF + +test_done From 8d8ef812a2082ff26777d061f6c0fe9c43037d6a Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 30 Aug 2012 12:38:54 +0200 Subject: [PATCH 2/3] FIX: Short help -h inaccessible when custom config is used. When short-circuiting the option parsing, the OPTIND must be adapted, too, so that the getopts processing loop is quit correctly. --- tests/t2120-shorthelp.sh | 28 ++++++++++++++++++++++++++++ todo.sh | 1 + 2 files changed, 29 insertions(+) diff --git a/tests/t2120-shorthelp.sh b/tests/t2120-shorthelp.sh index 8a08de0..58ce429 100755 --- a/tests/t2120-shorthelp.sh +++ b/tests/t2120-shorthelp.sh @@ -36,4 +36,32 @@ test_todo_session 'shorthelp output with custom action' <> custom.cfg + +test_todo_session '-h fatal error without config' <>> todo.sh -h +Fatal Error: Cannot read configuration file $HOME/.todo/config +=== 1 +EOF +test_todo_session '-h fatal error with trailing custom config' <>> todo.sh -h -d custom.cfg +Fatal Error: Cannot read configuration file $HOME/.todo/config +=== 1 +EOF + +test_todo_session '-h output with preceding custom config' <>> todo.sh -d custom.cfg -h | sed '/^ [A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + Add-on Actions: + See "help" for more details. +EOF + test_done diff --git a/todo.sh b/todo.sh index a6c6c54..ea56449 100755 --- a/todo.sh +++ b/todo.sh @@ -514,6 +514,7 @@ do # Cannot just invoke shorthelp() because we need the configuration # processed to locate the add-on actions directory. set -- '-h' 'shorthelp' + OPTIND=2 ;; n ) OVR_TODOTXT_PRESERVE_LINE_NUMBERS=0 From 8ff79102a5119736b4eb38ed202a31706ffabfa9 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 30 Aug 2012 13:16:02 +0200 Subject: [PATCH 3/3] ENH: Handle -h, shorthelp and help when a Fatal Error happens. The user may need the help to solve any fatal error that appears while todo.sh isn't properly set up. As the help actions do not depend on any setting that the fatal errors check, we can still invoke them. Factor out dieWithHelp() and use that for printing the fatal errors. --- tests/t2120-shorthelp.sh | 28 ++++++++++++++++++++-------- todo.sh | 17 +++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/tests/t2120-shorthelp.sh b/tests/t2120-shorthelp.sh index 58ce429..1724dee 100755 --- a/tests/t2120-shorthelp.sh +++ b/tests/t2120-shorthelp.sh @@ -40,23 +40,35 @@ EOF # Verify that custom configuration is actually processed (when the -d option # precedes the -h option) by specifying a different actions directory and moving # our custom action there. The help output should mention the "Add-On Actions". +set -o pipefail # So that the sed filter doesn't swallow todo.sh's exit code. mv todo.cfg custom.cfg mv .todo.actions.d custom.actions echo 'export TODO_ACTIONS_DIR=$HOME/custom.actions' >> custom.cfg -test_todo_session '-h fatal error without config' <>> todo.sh -h -Fatal Error: Cannot read configuration file $HOME/.todo/config -=== 1 -EOF -test_todo_session '-h fatal error with trailing custom config' <>> todo.sh -h -d custom.cfg +test_todo_session '-h and fatal error without config' <>> todo.sh -h | sed '/^ \\{0,2\\}[A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + See "help" for more details. Fatal Error: Cannot read configuration file $HOME/.todo/config === 1 EOF +# Config option comes too late; "Add-on Actions" is *not* mentioned here. +test_todo_session '-h and fatal error with trailing custom config' <>> todo.sh -h -d custom.cfg | sed '/^ \\{0,2\\}[A-Z]/!d' + Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] + Actions: + Actions can be added and overridden using scripts in the actions + See "help" for more details. +Fatal Error: Cannot read configuration file $HOME/.todo/config +=== 1 +EOF + +# Config option processed; "Add-on Actions" is mentioned here. test_todo_session '-h output with preceding custom config' <>> todo.sh -d custom.cfg -h | sed '/^ [A-Z]/!d' +>>> todo.sh -d custom.cfg -h | sed '/^ \\{0,2\\}[A-Z]/!d' Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] Actions: Actions can be added and overridden using scripts in the actions diff --git a/todo.sh b/todo.sh index ea56449..7e75ceb 100755 --- a/todo.sh +++ b/todo.sh @@ -81,7 +81,6 @@ shorthelp() See "help" for more details. EndHelpFooter - exit 0 } help() @@ -327,6 +326,16 @@ actionUsage() done } +dieWithHelp() +{ + case "$1" in + help) help;; + shorthelp) shorthelp;; + esac + shift + + die "$@" +} die() { echo "$*" @@ -655,7 +664,7 @@ fi } # === SANITY CHECKS (thanks Karl!) === -[ -r "$TODOTXT_CFG_FILE" ] || die "Fatal Error: Cannot read configuration file $TODOTXT_CFG_FILE" +[ -r "$TODOTXT_CFG_FILE" ] || dieWithHelp "$1" "Fatal Error: Cannot read configuration file $TODOTXT_CFG_FILE" . "$TODOTXT_CFG_FILE" @@ -694,8 +703,8 @@ fi ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -z "$ACTION" ] && usage -[ -d "$TODO_DIR" ] || die "Fatal Error: $TODO_DIR is not a directory" -( cd "$TODO_DIR" ) || die "Fatal Error: Unable to cd to $TODO_DIR" +[ -d "$TODO_DIR" ] || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory" +( cd "$TODO_DIR" ) || dieWithHelp "$1" "Fatal Error: Unable to cd to $TODO_DIR" [ -f "$TODO_FILE" ] || cp /dev/null "$TODO_FILE" [ -f "$DONE_FILE" ] || cp /dev/null "$DONE_FILE"