More parse_options tests. Make --opt yes have value "yes" instead of "1". Change ok() to cmd_ok() in test-bash-functions. Mimic Perl modulue headers in Bash libs so update-modules will work with the latter.

This commit is contained in:
Daniel Nichter
2011-10-28 11:08:59 -06:00
parent 1ec666de0e
commit f2b644ba72
7 changed files with 115 additions and 41 deletions

View File

@@ -15,14 +15,15 @@
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA. # Place, Suite 330, Boston, MA 02111-1307 USA.
# ########################################################################### # ###########################################################################
# Begin log_warn_die lib # log_warn_die package
# ########################################################################### # ###########################################################################
# Library: log_warn_die # Package: log_warn_die
# log_warn_die provides standard log(), warn(), and die() subs. # log_warn_die provides standard log(), warn(), and die() subs.
set -u set -u
# Global variables.
EXIT_STATUS=0 EXIT_STATUS=0
log() { log() {
@@ -41,5 +42,5 @@ die() {
} }
# ########################################################################### # ###########################################################################
# End log_warn_die lib # End log_warn_die package
# ########################################################################### # ###########################################################################

View File

@@ -15,10 +15,10 @@
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA. # Place, Suite 330, Boston, MA 02111-1307 USA.
# ########################################################################### # ###########################################################################
# Begin parse_options lib # parse_options package
# ########################################################################### # ###########################################################################
# Library: parse_options # Package: parse_options
# parse_options parses Perl POD options from Bash tools and creates # parse_options parses Perl POD options from Bash tools and creates
# global variables for each option. # global variables for each option.
@@ -36,7 +36,7 @@ declare EXT_ARGV # everything after -- (args for an external command)
# file - Program file with Perl POD which has usage and options. # file - Program file with Perl POD which has usage and options.
# #
# Required Global Variables: # Required Global Variables:
# TIMDIR - Temp directory. # TIMDIR - Temp directory set by <set_TMPDIR()>.
# TOOL - Tool's name. # TOOL - Tool's name.
# #
# Optional Global Variables: # Optional Global Variables:
@@ -66,9 +66,9 @@ usage() {
# file - Program file with Perl POD options. # file - Program file with Perl POD options.
# #
# Required Global Variables: # Required Global Variables:
# TIMDIR - Temp directory. # TIMDIR - Temp directory set by <set_TMPDIR()>.
# #
# Declared Global Variables: # Set Global Variables:
# This sub decalres a global var for each option by uppercasing the # This sub decalres a global var for each option by uppercasing the
# option, removing the option's leading --, changing all - to _, and # option, removing the option's leading --, changing all - to _, and
# prefixing with "OPT_". E.g. --foo-bar becomes OPT_FOO_BAR. # prefixing with "OPT_". E.g. --foo-bar becomes OPT_FOO_BAR.
@@ -121,7 +121,7 @@ parse_options() {
while read spec; do while read spec; do
opt=$(echo $spec | cut -d',' -f1 | sed 's/-/_/g' | tr [:lower:] [:upper:]) opt=$(echo $spec | cut -d',' -f1 | sed 's/-/_/g' | tr [:lower:] [:upper:])
default=$(echo $spec | cut -d',' -f4) default=$(echo $spec | cut -d',' -f4)
eval "$opt"="$default" eval "OPT_${opt}"="$default"
done < <(cat $TMPDIR/options) done < <(cat $TMPDIR/options)
for opt; do for opt; do
@@ -156,7 +156,7 @@ parse_options() {
fi fi
opt=$(echo $spec | cut -d',' -f1) opt=$(echo $spec | cut -d',' -f1)
required_arg=$(echo $spec | cut -d',' -f3) required_arg=$(echo $spec | cut -d',' -f3)
val=1 val="yes"
if [ -n "$required_arg" ]; then if [ -n "$required_arg" ]; then
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
die "--$opt requires a $required_arg argument" die "--$opt requires a $required_arg argument"
@@ -166,10 +166,10 @@ parse_options() {
fi fi
fi fi
opt=$(echo $opt | sed 's/-/_/g' | tr [:lower:] [:upper:]) opt=$(echo $opt | sed 's/-/_/g' | tr [:lower:] [:upper:])
eval "$opt"="$val" eval "OPT_${opt}"="$val"
done done
} }
# ########################################################################### # ###########################################################################
# End parse_options lib # End parse_options package
# ########################################################################### # ###########################################################################

View File

@@ -15,17 +15,26 @@
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA. # Place, Suite 330, Boston, MA 02111-1307 USA.
# ########################################################################### # ###########################################################################
# Begin tmpdir lib # tmpdir package
# ########################################################################### # ###########################################################################
# Library: tmpdir # Package: tmpdir
# tmpdir make a secure temporary directory using mktemp. # tmpdir make a secure temporary directory using mktemp.
set -u set -u
# Global variables.
TMPDIR="" TMPDIR=""
OPT_TMPDIR={OPT_TMPDIR:""} OPT_TMPDIR={OPT_TMPDIR:""}
# Sub: set_TMPDIR
# Create a secure tmpdir and set TMPDIR.
#
# Optional Global Variables:
# OPT_TMPDIR - User-specified --tmpdir (default none).
#
# Set Global Variables:
# TMPDIR - Absolute path of secure temp directory.
set_TMPDIR() { set_TMPDIR() {
if [ -n "$OPT_TMPDIR" ]; then if [ -n "$OPT_TMPDIR" ]; then
TMPDIR="$OPT_TMPDIR" TMPDIR="$OPT_TMPDIR"
@@ -38,6 +47,14 @@ set_TMPDIR() {
fi fi
} }
# Sub: rm_TMPDIR
# Remove the tmpdir and unset TMPDIR.
#
# Optional Global Variables:
# TMPDIR - TMPDIR set by <set_TMPDIR()>.
#
# Set Global Variables:
# TMPDIR - Set to "".
rm_TMPDIR() { rm_TMPDIR() {
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
rm -rf $TMPDIR rm -rf $TMPDIR
@@ -46,5 +63,5 @@ rm_TMPDIR() {
} }
# ########################################################################### # ###########################################################################
# End tmpdir lib # End tmpdir package
# ########################################################################### # ###########################################################################

0
t/lib/bash.t Executable file → Normal file
View File

View File

@@ -1,25 +1,81 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TESTS=17 TESTS=37
TMPFILE="$TEST_TMPDIR/parse-opts-output"
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/parse_options.sh" source "$LIB_DIR/parse_options.sh"
parse_options "$T_LIB_DIR/samples/bash/po001.sh" # ############################################################################
# Parse options from POD using all default values.
# ############################################################################
is "$THRESHOLD" "100" parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
is "$VARIABLE" "Threads_connected"
is "$CYCLES" "1" TEST_NAME="No warnings or errors"
is "$GDB" "no" is "`cat $TMPFILE`" ""
is "$OPROFILE" "yes"
is "$STRACE" "no" TEST_NAME="Default opts"
is "$TCPDUMP" "yes" is "$OPT_THRESHOLD" "100"
is "$EMAIL" "" is "$OPT_VARIABLE" "Threads_connected"
is "$INTERVAL" "30" is "$OPT_CYCLES" "1"
is "$MAYBE_EMPTY" "no" is "$OPT_GDB" "no"
is "$COLLECT" "${HOME}/bin/pt-collect" is "$OPT_OPROFILE" "yes"
is "$DEST" "${HOME}/collected/" is "$OPT_STRACE" "no"
is "$DURATION" "30" is "$OPT_TCPDUMP" "yes"
is "$SLEEP" "300" is "$OPT_EMAIL" ""
is "$PCT_THRESHOLD" "95" is "$OPT_INTERVAL" "30"
is "$MB_THRESHOLD" "100" is "$OPT_MAYBE_EMPTY" "no"
is "$PURGE" "30" is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
is "$OPT_DEST" "${HOME}/collected/"
is "$OPT_DURATION" "30"
is "$OPT_SLEEP" "300"
is "$OPT_PCT_THRESHOLD" "95"
is "$OPT_MB_THRESHOLD" "100"
is "$OPT_PURGE" "30"
# ############################################################################
# Specify some opts, but use default values for the rest.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --threshold 50 --gdb yes --email user@example.com
TEST_NAME="User-specified opts with defaults"
is "$OPT_THRESHOLD" "50" # specified
is "$OPT_VARIABLE" "Threads_connected"
is "$OPT_CYCLES" "1"
is "$OPT_GDB" "yes" # specified
is "$OPT_OPROFILE" "yes"
is "$OPT_STRACE" "no"
is "$OPT_TCPDUMP" "yes"
is "$OPT_EMAIL" "user@example.com" # specified
is "$OPT_INTERVAL" "30"
is "$OPT_MAYBE_EMPTY" "no"
is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
is "$OPT_DEST" "${HOME}/collected/"
is "$OPT_DURATION" "30"
is "$OPT_SLEEP" "300"
is "$OPT_PCT_THRESHOLD" "95"
is "$OPT_MB_THRESHOLD" "100"
is "$OPT_PURGE" "30"
# ############################################################################
# An unknown option should produce an error.
# ############################################################################
# Have to call this in a subshell because the error will cause an exit.
(
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
)
local err=$?
TEST_NAME="Non-zero exit on unknown option"
is "$err" "1"
TEST_NAME="Error on unknown option"
cmd_ok "grep -q 'Unknown option: foo' $TMPFILE"
# ############################################################################
# Done
# ############################################################################
exit

View File

@@ -10,13 +10,13 @@ is "$TMPDIR" ""
TEST_NAME="set_TMPDIR makes secure tmpdir" TEST_NAME="set_TMPDIR makes secure tmpdir"
set_TMPDIR set_TMPDIR
ok "test -d $TMPDIR" cmd_ok "test -d $TMPDIR"
tmpdir=$TMPDIR; tmpdir=$TMPDIR;
TEST_NAME="rm_TMPDIR" TEST_NAME="rm_TMPDIR"
rm_TMPDIR rm_TMPDIR
ok "test ! -d $tmpdir" cmd_ok "test ! -d $tmpdir"
TEST_NAME="rm_TMPDIR resets TMPDIR" TEST_NAME="rm_TMPDIR resets TMPDIR"
is "$TMPDIR" "" is "$TMPDIR" ""
@@ -28,17 +28,17 @@ TEST_NAME="TMPDIR not defined"
is "$TMPDIR" "" is "$TMPDIR" ""
TEST_NAME="--tmpdir does not exist yet" TEST_NAME="--tmpdir does not exist yet"
ok "test ! -d $OPT_TMPDIR" cmd_ok "test ! -d $OPT_TMPDIR"
set_TMPDIR set_TMPDIR
TEST_NAME="set_TMPDIR uses --tmpdir" TEST_NAME="set_TMPDIR uses --tmpdir"
is "$TMPDIR" "/tmp/use--tmpdir" is "$TMPDIR" "/tmp/use--tmpdir"
TEST_NAME="set_TMPDIR creates --tmpdir" TEST_NAME="set_TMPDIR creates --tmpdir"
ok "test -d $TMPDIR" cmd_ok "test -d $TMPDIR"
tmpdir=$TMPDIR; tmpdir=$TMPDIR;
TEST_NAME="rm_TMPDIR removes --tmpdir" TEST_NAME="rm_TMPDIR removes --tmpdir"
rm_TMPDIR rm_TMPDIR
ok "test ! -d $tmpdir" cmd_ok "test ! -d $tmpdir"

View File

@@ -128,9 +128,9 @@ is() {
result $? result $?
} }
ok() { cmd_ok() {
local test_command=$1 local test_command=$1
$test_command eval $test_command
result $? result $?
} }