From b99293d1b31bbaedd41653a674e2f8c3e8d5e01d Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Thu, 23 Feb 2012 13:58:57 -0700 Subject: [PATCH] Simplify and individuate Bash tests so prove reports failures where they happen. --- t/lib/bash.t | 25 ---------- t/lib/bash/alt_cmds.sh | 2 +- t/lib/bash/alt_cmds.t | 1 + t/lib/bash/collect.sh | 8 ++-- t/lib/bash/collect.t | 1 + t/lib/bash/daemon.sh | 6 +-- t/lib/bash/daemon.t | 1 + t/lib/bash/log_warn_die.sh | 2 +- t/lib/bash/log_warn_die.t | 1 + t/lib/bash/parse_options.sh | 4 +- t/lib/bash/parse_options.t | 1 + t/lib/bash/safeguards.sh | 2 +- t/lib/bash/safeguards.t | 1 + t/lib/bash/tmpdir.sh | 4 +- t/lib/bash/tmpdir.t | 1 + t/lib/samples/bash/dummy.sh | 4 -- util/test-bash-functions | 94 ++++++++++++++++++++++--------------- 17 files changed, 77 insertions(+), 81 deletions(-) delete mode 100644 t/lib/bash.t create mode 120000 t/lib/bash/alt_cmds.t create mode 120000 t/lib/bash/collect.t create mode 120000 t/lib/bash/daemon.t create mode 120000 t/lib/bash/log_warn_die.t create mode 120000 t/lib/bash/parse_options.t create mode 120000 t/lib/bash/safeguards.t create mode 120000 t/lib/bash/tmpdir.t delete mode 100644 t/lib/samples/bash/dummy.sh diff --git a/t/lib/bash.t b/t/lib/bash.t deleted file mode 100644 index 68706d3c..00000000 --- a/t/lib/bash.t +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env perl - -BEGIN { - die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" - unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; - unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; -}; - -use strict; -use warnings FATAL => 'all'; -use English qw(-no_match_vars); - -use PerconaTest; - -my ($tool) = $PROGRAM_NAME =~ m/([\w-]+)\.t$/; -push @ARGV, "$trunk/t/lib/bash/*.sh" unless @ARGV; - -$ENV{BIN_DIR} = "$trunk/bin"; -$ENV{LIB_DIR} = "$trunk/lib/bash"; -$ENV{T_LIB_DIR} = "$trunk/t/lib"; -$ENV{SANDBOX_VERSION} = "$sandbox_version"; - -system("$trunk/util/test-bash-functions $trunk/t/lib/samples/bash/dummy.sh @ARGV"); - -exit; diff --git a/t/lib/bash/alt_cmds.sh b/t/lib/bash/alt_cmds.sh index 5d674bc0..b37e7321 100644 --- a/t/lib/bash/alt_cmds.sh +++ b/t/lib/bash/alt_cmds.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=1 +plan 1 source "$LIB_DIR/alt_cmds.sh" diff --git a/t/lib/bash/alt_cmds.t b/t/lib/bash/alt_cmds.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/alt_cmds.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/collect.sh b/t/lib/bash/collect.sh index a40a1dfb..02c8c027 100644 --- a/t/lib/bash/collect.sh +++ b/t/lib/bash/collect.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=20 +plan 20 TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPDIR="$TEST_TMPDIR" @@ -18,7 +18,7 @@ source "$LIB_DIR/collect.sh" parse_options "$BIN_DIR/pt-stalk" --run-time 1 -- --defaults-file=/tmp/12345/my.sandbox.cnf # Prefix (with path) for the collect files. -local p="$TMPDIR/collect/2011_12_05" +p="$TMPDIR/collect/2011_12_05" # Default collect, no extras like gdb, tcpdump, etc. collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1 @@ -113,7 +113,7 @@ cmd_ok \ "grep -qP '^wait_timeout\t\d' $p-variables" \ "variables" -local iters=$(cat $p-df | grep -c '^TS ') +iters=$(cat $p-df | grep -c '^TS ') is "$iters" "1" "1 iteration/1s run time" empty_files=0 @@ -140,7 +140,7 @@ rm $TMPDIR/collect/* collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1 -local iters=$(cat $p-df | grep -c '^TS ') +iters=$(cat $p-df | grep -c '^TS ') is "$iters" "2" "2 iteration/2s run time" # ############################################################################ diff --git a/t/lib/bash/collect.t b/t/lib/bash/collect.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/collect.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/daemon.sh b/t/lib/bash/daemon.sh index 0ffad457..c46d560f 100644 --- a/t/lib/bash/daemon.sh +++ b/t/lib/bash/daemon.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -TESTS=9 +plan 9 TMPDIR="$TEST_TMPDIR" -local file="$TMPDIR/pid-file" +file="$TMPDIR/pid-file" source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/daemon.sh" @@ -18,7 +18,7 @@ cmd_ok \ "test -f $file" \ "PID file created" -local pid=`cat $file` +pid=`cat $file` is \ "$pid" \ "$$" \ diff --git a/t/lib/bash/daemon.t b/t/lib/bash/daemon.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/daemon.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/log_warn_die.sh b/t/lib/bash/log_warn_die.sh index c3f4ed74..b31c1308 100644 --- a/t/lib/bash/log_warn_die.sh +++ b/t/lib/bash/log_warn_die.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=6 +plan 6 source "$LIB_DIR/log_warn_die.sh" diff --git a/t/lib/bash/log_warn_die.t b/t/lib/bash/log_warn_die.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/log_warn_die.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/parse_options.sh b/t/lib/bash/parse_options.sh index 8c462eff..f9536d65 100644 --- a/t/lib/bash/parse_options.sh +++ b/t/lib/bash/parse_options.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=78 +plan 78 TMPFILE="$TEST_TMPDIR/parse-opts-output" TOOL="pt-stalk" @@ -93,7 +93,7 @@ parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1 cmd_ok "grep -q 'Unknown option: --foo' $TMPFILE" "Error on unknown option" usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1 -local err=$? +err=$? is "$err" "1" "Non-zero exit on unknown option" # ########################################################################### diff --git a/t/lib/bash/parse_options.t b/t/lib/bash/parse_options.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/parse_options.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/safeguards.sh b/t/lib/bash/safeguards.sh index cf678d90..adcd3b62 100644 --- a/t/lib/bash/safeguards.sh +++ b/t/lib/bash/safeguards.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=11 +plan 11 source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/safeguards.sh" diff --git a/t/lib/bash/safeguards.t b/t/lib/bash/safeguards.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/safeguards.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/bash/tmpdir.sh b/t/lib/bash/tmpdir.sh index 55098f4d..78f15f92 100644 --- a/t/lib/bash/tmpdir.sh +++ b/t/lib/bash/tmpdir.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=9 +plan 9 source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/tmpdir.sh" @@ -21,7 +21,7 @@ is "$TMPDIR" "" "rm_tmpdir resets TMPDIR" # User-specified tmpdir. # ########################################################################### -local dir="/tmp/use--tmpdir" +dir="/tmp/use--tmpdir" is "$TMPDIR" "" "TMPDIR not defined" diff --git a/t/lib/bash/tmpdir.t b/t/lib/bash/tmpdir.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/t/lib/bash/tmpdir.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/t/lib/samples/bash/dummy.sh b/t/lib/samples/bash/dummy.sh deleted file mode 100644 index a7eea1ee..00000000 --- a/t/lib/samples/bash/dummy.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -# This is a dummy script for testing the Bash libs. t/lib/bashLibs.t -# calls "util/test-bash-functions dummy.sh ". diff --git a/util/test-bash-functions b/util/test-bash-functions index c6c0cd61..73795fcc 100755 --- a/util/test-bash-functions +++ b/util/test-bash-functions @@ -14,21 +14,26 @@ die() { exit 255 } -( - if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then - BRANCH=$PERCONA_TOOLKIT_BRANCH - cd $BRANCH - else - while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do - cd .. - done - if [ ! -f Makefile.PL ]; then - die "Cannot find the root directory of the Percona Toolkit branch" - exit 1 - fi - BRANCH=`pwd` +cwd="$PWD" +if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then + BRANCH=$PERCONA_TOOLKIT_BRANCH + cd $BRANCH +else + while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do + cd .. + done + if [ ! -f Makefile.PL ]; then + die "Cannot find the root directory of the Percona Toolkit branch" + exit 1 fi -) + BRANCH="$PWD" +fi +cd "$cwd" + +BIN_DIR="$BRANCH/bin"; +LIB_DIR="$BRANCH/lib/bash"; +T_LIB_DIR="$BRANCH/t/lib"; +SANDBOX_VERSION="$($BRANCH/sandbox/test-env version)" # ############################################################################ # Paths @@ -108,6 +113,13 @@ result() { return $result } +plan() { + local n_tests=${1:-""} + if [ "$n_tests" ]; then + echo "1..$n_tests" + fi +} + # # The following subs are for the test files to call. # @@ -141,32 +153,38 @@ cmd_ok() { # Script starts here # ############################################################################ -if [ $# -lt 2 ]; then - die "Usage: test-back-functions FILE TESTS" -fi - -# Check and source the bash file. This is the code being tested. -# All its global vars and subs will be imported. -bash_file=$1 -shift -if [ ! -f "$bash_file" ]; then - die "$bash_file does not exist" -fi -head -n1 $bash_file | grep -q -E 'bash|sh' || die "$bash_file is not a bash file" -source $bash_file - -# Load (count) the tests so that we can write a TAP test plan like 1..5 -# for expecting 5 tests. Perl prove needs this. -declare -a tests -load_tests "$@" - -# Run the test files. testno=1 failed_tests=0 -for t in "${tests[@]}"; do - run_test $t -done + +if [ $# -eq 0 ]; then + TEST_FILE=$(basename "$0") + TEST="${TEST_FILE%".t"}" + source "$BRANCH/t/lib/bash/$TEST.sh" +else + if [ $# -lt 2 ]; then + die "Usage: test-bash-functions FILE TESTS" + fi + + # Check and source the bash file. This is the code being tested. + # All its global vars and subs will be imported. + bash_file=$1 + shift + if [ ! -f "$bash_file" ]; then + die "$bash_file does not exist" + fi + head -n1 $bash_file | grep -q -E 'bash|sh' || die "$bash_file is not a bash file" + source $bash_file + + # Load (count) the tests so that we can write a TAP test plan like 1..5 + # for expecting 5 tests. Perl prove needs this. + declare -a tests + load_tests "$@" + + # Run the test files. + for t in "${tests[@]}"; do + run_test $t + done +fi rm -rf $TEST_TMPDIR - exit $failed_tests