summaryrefslogtreecommitdiff
path: root/contrib/cirrus/lib.sh.t
diff options
context:
space:
mode:
authorEd Santiago <santiago@redhat.com>2019-05-02 05:49:50 -0600
committerEd Santiago <santiago@redhat.com>2019-05-02 08:14:59 -0600
commit295c531ecec8661408f01ac4e015d75fec865376 (patch)
tree207a92de67c76264dc4563588e84a261facb8570 /contrib/cirrus/lib.sh.t
parent7d05ff3fc772a7be4860ed4a3cd59a62f8bb893a (diff)
downloadpodman-295c531ecec8661408f01ac4e015d75fec865376.tar.gz
podman-295c531ecec8661408f01ac4e015d75fec865376.tar.bz2
podman-295c531ecec8661408f01ac4e015d75fec865376.zip
cirrus lib.sh: refactor req_env_var()
Existing code was not working due to a bash gotcha ('exit' from a pipeline). It also had unnecessary duplication. New version is safer; also includes unit tests run under localunit. Existing invocations of req_env_var replaced via: $ [ edit setup_environment.sh, move one closing quote to its own line ] $ perl -ni -e 's/(?<=req_env_var )"(\S+)\s+\$\1"/$1/; if (/req_env_var "$/ .. /^\s*"/) { chomp; s/(?<=\S)\s.*//; if (/^\s*"/) { print "\n" } else { unless (/req_env_var/) { s/^\s+//; print " ";} print;} } else { print }' $(ack -l req_env_var) $ [ hand-massage an incorrect instance of '@' in lib.sh:ircmsg() ] Signed-off-by: Ed Santiago <santiago@redhat.com>
Diffstat (limited to 'contrib/cirrus/lib.sh.t')
-rwxr-xr-xcontrib/cirrus/lib.sh.t81
1 files changed, 81 insertions, 0 deletions
diff --git a/contrib/cirrus/lib.sh.t b/contrib/cirrus/lib.sh.t
new file mode 100755
index 000000000..ce51f8ad2
--- /dev/null
+++ b/contrib/cirrus/lib.sh.t
@@ -0,0 +1,81 @@
+#!/bin/bash
+#
+# Unit tests for some functions in lib.sh
+#
+source $(dirname $0)/lib.sh
+
+# Iterator and return code; updated in test functions
+testnum=0
+rc=0
+
+function check_result {
+ testnum=$(expr $testnum + 1)
+ if [ "$1" = "$2" ]; then
+ echo "ok $testnum $3 = $1"
+ else
+ echo "not ok $testnum $3"
+ echo "# expected: $2"
+ echo "# actual: $1"
+ rc=1
+ fi
+}
+
+###############################################################################
+# tests for die()
+
+function test_die() {
+ local input_status=$1
+ local input_msg=$2
+ local expected_status=$3
+ local expected_msg=$4
+
+ local msg
+ msg=$(die $input_status "$input_msg")
+ local status=$?
+
+ check_result "$msg" "$expected_msg" "die $input_status $input_msg"
+}
+
+test_die 1 "a message" 1 "a message"
+test_die 2 "" 2 "FATAL ERROR (but no message given!) in test_die()"
+test_die '' '' 1 "FATAL ERROR (but no message given!) in test_die()"
+
+###############################################################################
+# tests for req_env_var()
+
+function test_rev() {
+ local input_args=$1
+ local expected_status=$2
+ local expected_msg=$3
+
+ # bash gotcha: doing 'local msg=...' on one line loses exit status
+ local msg
+ msg=$(req_env_var $input_args)
+ local status=$?
+
+ check_result "$msg" "$expected_msg" "req_env_var $input_args"
+ check_result "$status" "$expected_status" "req_env_var $input_args (rc)"
+}
+
+# error if called with no args
+test_rev '' 1 'FATAL: req_env_var: invoked without arguments'
+
+# error if desired envariable is unset
+unset FOO BAR
+test_rev FOO 9 'FATAL: test_rev() requires $FOO to be non-empty'
+test_rev BAR 9 'FATAL: test_rev() requires $BAR to be non-empty'
+
+# OK if desired envariable is unset
+FOO=1
+test_rev FOO 0 ''
+
+# ...but error if any single desired one is unset
+test_rev "FOO BAR" 9 'FATAL: test_rev() requires $BAR to be non-empty'
+
+# ...and OK if all args are set
+BAR=1
+test_rev "FOO BAR" 0 ''
+
+###############################################################################
+
+exit $rc