From a9afea4506f7b0527a904131e7a5ebd6815455fe Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 11 Jan 2012 11:31:41 -0700 Subject: [PATCH] Quote vals in Bash libs. Add tests for log_warn_die.sh and alt_cmds.sh. Make --help test path independent. --- lib/bash/alt_cmds.sh | 2 +- lib/bash/daemon.sh | 12 +++++------ lib/bash/log_warn_die.sh | 8 +++---- lib/bash/parse_options.sh | 30 +++++++++++++------------- t/lib/bash/alt_cmds.sh | 15 +++++++++++++ t/lib/bash/log_warn_die.sh | 39 ++++++++++++++++++++++++++++++++++ t/lib/bash/parse_options.sh | 5 ++--- t/lib/samples/bash/help001.txt | 30 -------------------------- t/lib/samples/bash/seq1.txt | 5 +++++ 9 files changed, 87 insertions(+), 59 deletions(-) create mode 100644 t/lib/bash/alt_cmds.sh create mode 100644 t/lib/bash/log_warn_die.sh delete mode 100644 t/lib/samples/bash/help001.txt create mode 100644 t/lib/samples/bash/seq1.txt diff --git a/lib/bash/alt_cmds.sh b/lib/bash/alt_cmds.sh index 55a41e3a..c5a88f5e 100644 --- a/lib/bash/alt_cmds.sh +++ b/lib/bash/alt_cmds.sh @@ -25,7 +25,7 @@ set -u # seq N, return 1, ..., 5 _seq() { - local i=$1 + local i="$1" awk "BEGIN { for(i=1; i<=$i; i++) print i; }" } diff --git a/lib/bash/daemon.sh b/lib/bash/daemon.sh index 1fe823b8..9abe8b3e 100644 --- a/lib/bash/daemon.sh +++ b/lib/bash/daemon.sh @@ -30,8 +30,8 @@ set -u # file - File to write PID to. # pid - PID to write into file. make_pid_file() { - local file=$1 - local pid=$2 + local file="$1" + local pid="$2" # Yes there's a race condition here, between checking if the file exists # and creating it, but it's not important enough to handle. @@ -39,7 +39,7 @@ make_pid_file() { if [ -f "$file" ]; then # PID file already exists. See if the pid it contains is still running. # If yes, then die. Else, the pid file is stale and we can reclaim it. - local old_pid=$(cat $file) + local old_pid=$(cat "$file") if [ -z "$old_pid" ]; then # PID file is empty, so be safe and die since we can't check a # non-existent pid. @@ -56,13 +56,13 @@ make_pid_file() { fi # PID file doesn't exist, or it does but its pid is stale. - echo "$pid" > $file + echo "$pid" > "$file" } remove_pid_file() { - local file=$1 + local file="$1" if [ -f "$file" ]; then - rm $file + rm "$file" fi } diff --git a/lib/bash/log_warn_die.sh b/lib/bash/log_warn_die.sh index 6d5ca2e4..eaa7fed3 100644 --- a/lib/bash/log_warn_die.sh +++ b/lib/bash/log_warn_die.sh @@ -28,16 +28,16 @@ EXIT_STATUS=0 log() { TS=$(date +%F-%T | tr :- _); - echo "$TS $1" + echo "$TS $*" } warn() { - log "$1" >&2 - EXIT_STATUS=$((EXIT_STATUS | 1)) + log "$*" >&2 + EXIT_STATUS=1 } die() { - warn "$1" + warn "$*" exit 1 } diff --git a/lib/bash/parse_options.sh b/lib/bash/parse_options.sh index e4915247..4fde19c1 100644 --- a/lib/bash/parse_options.sh +++ b/lib/bash/parse_options.sh @@ -45,19 +45,19 @@ OPT_HELP="no" # If --help was specified # Optional Global Variables: # OPT_ERR - Command line option error message. usage() { - local file=$1 + local file="$1" - local usage=$(grep '^Usage: ' $file) + local usage=$(grep '^Usage: ' "$file") echo $usage >&2 echo >&2 echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2 } usage_or_errors() { - local file=$1 + local file="$1" if [ "$OPT_VERSION" = "yes" ]; then - local version=$(grep '^pt-[^ ]\+ [0-9]' $file) + local version=$(grep '^pt-[^ ]\+ [0-9]' "$file") echo "$version" return 1 fi @@ -78,7 +78,7 @@ usage_or_errors() { if [ $OPT_ERRS -gt 0 ]; then echo >&2 - usage $file + usage "$file" return 1 fi @@ -100,7 +100,7 @@ usage_or_errors() { # option, removing the option's leading --, changing all - to _, and # prefixing with "OPT_". E.g. --foo-bar becomes OPT_FOO_BAR. parse_options() { - local file=$1 + local file="$1" shift # Parse the program options (po) from the POD. Each option has @@ -111,11 +111,11 @@ parse_options() { # default=foo # That's the spec for --string-opt2. Each line is a key:value pair # from the option's POD line like "type: string; default: foo". - mkdir $TMPDIR/po/ 2>/dev/null - rm -rf $TMPDIR/po/* + mkdir "$TMPDIR/po/" 2>/dev/null + rm -rf "$TMPDIR"/po/* ( export PO_DIR="$TMPDIR/po" - cat $file | perl -ne ' + cat "$file" | perl -ne ' BEGIN { $/ = ""; } next unless $_ =~ m/^=head1 OPTIONS/; while ( defined(my $para = <>) ) { @@ -149,13 +149,13 @@ parse_options() { # Evaluate the program options into existence as global variables # transformed like --my-op == $OPT_MY_OP. If an option has a default # value, it's assigned that value. Else, it's value is an empty string. - for opt_spec in $(ls $TMPDIR/po/); do + for opt_spec in $(ls "$TMPDIR/po/"); do local opt="" local default_val="" local neg=0 while read line; do - local key=`echo $line | cut -d ':' -f 1` - local val=`echo $line | cut -d ':' -f 2` + local key=$(echo $line | cut -d ':' -f 1) + local val=$(echo $line | cut -d ':' -f 2) case "$key" in long) opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:]) @@ -178,7 +178,7 @@ parse_options() { echo "Invalid attribute in $TMPDIR/po/$opt_spec: $line" >&2 exit 1 esac - done < $TMPDIR/po/$opt_spec + done < "$TMPDIR/po/$opt_spec" if [ -z "$opt" ]; then echo "No long attribute in option spec $TMPDIR/po/$opt_spec" >&2 @@ -243,7 +243,7 @@ parse_options() { if [ -f "$TMPDIR/po/$opt" ]; then spec="$TMPDIR/po/$opt" else - spec=$(grep "^short form:-$opt\$" $TMPDIR/po/* | cut -d ':' -f 1) + spec=$(grep "^short form:-$opt\$" "$TMPDIR"/po/* | cut -d ':' -f 1) if [ -z "$spec" ]; then OPT_ERRS=$(($OPT_ERRS + 1)) echo "Unknown option: $real_opt" >&2 @@ -255,7 +255,7 @@ parse_options() { # says it has a type, then it requires a value and that value should # be the next item ($1). Else, typeless options (like --version) are # either "yes" if specified, else "no" if negatable and --no-opt. - required_arg=$(cat $spec | grep '^type:' | cut -d':' -f2) + local required_arg=$(cat $spec | awk -F: '/^type:/{print $2}') if [ -n "$required_arg" ]; then if [ $# -eq 0 ]; then OPT_ERRS=$(($OPT_ERRS + 1)) diff --git a/t/lib/bash/alt_cmds.sh b/t/lib/bash/alt_cmds.sh new file mode 100644 index 00000000..5d674bc0 --- /dev/null +++ b/t/lib/bash/alt_cmds.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +TESTS=1 + +source "$LIB_DIR/alt_cmds.sh" + +_seq 5 > $TEST_TMPDIR/out +no_diff \ + $TEST_TMPDIR/out \ + $T_LIB_DIR/samples/bash/seq1.txt \ + "_seq 5" + +# ########################################################################### +# Done +# ########################################################################### diff --git a/t/lib/bash/log_warn_die.sh b/t/lib/bash/log_warn_die.sh new file mode 100644 index 00000000..c3f4ed74 --- /dev/null +++ b/t/lib/bash/log_warn_die.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +TESTS=6 + +source "$LIB_DIR/log_warn_die.sh" + +log "Hello world!" > $TEST_TMPDIR/log +cmd_ok \ + "grep -q 'Hello world!' $TEST_TMPDIR/log" \ + "log msg" + +log "Hello" "world!" > $TEST_TMPDIR/log +cmd_ok \ + "grep -q 'Hello world!' $TEST_TMPDIR/log" \ + "log msg msg" + +is \ + "$EXIT_STATUS" \ + "0" \ + "Exit status 0" + +warn "Hello world!" 2> $TEST_TMPDIR/log +cmd_ok \ + "grep -q 'Hello world!' $TEST_TMPDIR/log" \ + "warn msg" + +warn "Hello" "world!" 2> $TEST_TMPDIR/log +cmd_ok \ + "grep -q 'Hello world!' $TEST_TMPDIR/log" \ + "warn msg msg" + +is \ + "$EXIT_STATUS" \ + "1" \ + "Exit status 1" + +# ########################################################################### +# Done +# ########################################################################### diff --git a/t/lib/bash/parse_options.sh b/t/lib/bash/parse_options.sh index 176c824c..482bc46d 100644 --- a/t/lib/bash/parse_options.sh +++ b/t/lib/bash/parse_options.sh @@ -77,9 +77,8 @@ is "$err" "1" "Non-zero exit on unknown option" # ########################################################################### parse_options "$T_LIB_DIR/samples/bash/po001.sh" --help usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1 -no_diff \ - "$TMPFILE" \ - "$T_LIB_DIR/samples/bash/help001.txt" \ +cmd_ok \ + "grep -q \"For more information, 'man pt-stalk' or 'perldoc\" $TMPFILE" \ "--help" # ############################################################################ diff --git a/t/lib/samples/bash/help001.txt b/t/lib/samples/bash/help001.txt deleted file mode 100644 index 65dc71b2..00000000 --- a/t/lib/samples/bash/help001.txt +++ /dev/null @@ -1,30 +0,0 @@ -Usage: pt-stalk [OPTIONS] [-- MYSQL_OPTIONS] - -For more information, 'man pt-stalk' or 'perldoc /Users/daniel/p/bash-tool-libs/t/lib/samples/bash/po001.sh'. - -Command line options: - ---help - Print help and exit. - ---int-opt - Int option without a default. - ---int-opt2 - Int option with a default. - ---noption - Negatable option. - ---string-opt - String option without a default. - ---string-opt2 - String option with a default. - ---typeless-option - Just an option. - ---version - Print tool's version and exit. - diff --git a/t/lib/samples/bash/seq1.txt b/t/lib/samples/bash/seq1.txt new file mode 100644 index 00000000..8a1218a1 --- /dev/null +++ b/t/lib/samples/bash/seq1.txt @@ -0,0 +1,5 @@ +1 +2 +3 +4 +5