mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 21:19:59 +00:00
Quote vals in Bash libs. Add tests for log_warn_die.sh and alt_cmds.sh. Make --help test path independent.
This commit is contained in:
@@ -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; }"
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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))
|
||||
|
15
t/lib/bash/alt_cmds.sh
Normal file
15
t/lib/bash/alt_cmds.sh
Normal file
@@ -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
|
||||
# ###########################################################################
|
39
t/lib/bash/log_warn_die.sh
Normal file
39
t/lib/bash/log_warn_die.sh
Normal file
@@ -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
|
||||
# ###########################################################################
|
@@ -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"
|
||||
|
||||
# ############################################################################
|
||||
|
@@ -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.
|
||||
|
5
t/lib/samples/bash/seq1.txt
Normal file
5
t/lib/samples/bash/seq1.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
Reference in New Issue
Block a user