summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorAndrei Pavel <andrei@isc.org>2020-12-03 10:19:15 +0100
committerAndrei Pavel <andrei@isc.org>2020-12-14 11:08:11 +0100
commit9ca8b2fcc7af992e58668bcd5fe81b1edb6626bc (patch)
treeac05ba23f55667c1f092ebbfe543df986d9340f6 /tools
parent[#1574] googletest-like output for shell tests (diff)
downloadkea-9ca8b2fcc7af992e58668bcd5fe81b1edb6626bc.tar.xz
kea-9ca8b2fcc7af992e58668bcd5fe81b1edb6626bc.zip
[#1574] make all shell scripts POSIX compliant
* solve all shellcheck warnings * shebang in all scripts * set -eu everywhere * make scripts callable from everywhere * quote variables to prevent globbing and word splitting * add missing copyrights * remove unused variables * use $() instead of backticks `` ... and so on
Diffstat (limited to 'tools')
-rwxr-xr-xtools/add-config-h.sh6
-rwxr-xr-xtools/cql_config4
-rwxr-xr-xtools/mk_cfgrpt.sh25
-rw-r--r--tools/path_replacer.sh.in23
-rwxr-xr-xtools/sysrepo_config4
-rwxr-xr-xtools/tests_in_valgrind.sh34
6 files changed, 62 insertions, 34 deletions
diff --git a/tools/add-config-h.sh b/tools/add-config-h.sh
index 2276f2df2d..8c33f3d297 100755
--- a/tools/add-config-h.sh
+++ b/tools/add-config-h.sh
@@ -19,6 +19,10 @@
# Reason: blackslash+linefeed inside single quotes in sed are needed for
# POSIX-compliance. \n works only with GNU sed.
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
# Print usage.
print_usage() {
printf \
@@ -142,7 +146,6 @@ else
file=$(mktemp)
# Run once for files that have at least one include.
- # shellcheck disable=SC1004
for i in $(get_source_files); do
sed '1,/#include/s/#include/#include <config.h>\
\
@@ -152,7 +155,6 @@ else
# Run again for files that don't have any includes. The second command would
# work for the first case, but the first command is more elegant.
- # shellcheck disable=SC1004
for i in $(get_source_files); do
sed '1,/\(\/\/ file, You can obtain one.*\)/s/\(\/\/ file, You can obtain one.*\)/\1\
\
diff --git a/tools/cql_config b/tools/cql_config
index 4ab9c6a86a..cbf3690daf 100755
--- a/tools/cql_config
+++ b/tools/cql_config
@@ -10,6 +10,10 @@
# This script is used to run Kea from installation directory,
# as well as for running tests.
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
cql_lib="cassandra"
if test "$(uname -s)" = "Darwin"; then
diff --git a/tools/mk_cfgrpt.sh b/tools/mk_cfgrpt.sh
index c9f5f241f9..91242335c3 100755
--- a/tools/mk_cfgrpt.sh
+++ b/tools/mk_cfgrpt.sh
@@ -7,27 +7,32 @@
#
# This script embeds config.report into src/bin/cfgrpt/config_report.cc
# Called by configure
-#
+
+# shellcheck disable=SC2129
+# SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
report_file="$1"
dest="$2"
-if [ -z ${report_file} ]
+if [ -z "${report_file}" ]
then
echo "ERROR mk_cfgrpt.sh - input report: $report_file does not exist"
- exit -1
+ exit 1
fi
# Initializes
-cat /dev/null > $dest
-if [ $? -ne 0 ]
+if ! cat /dev/null > "${dest}"
then
- echo "ERROR mk_cfgrpt.sh - cannot create config output file: $dest"
- exit -1
+ echo "ERROR mk_cfgrpt.sh - cannot create config output file: ${dest}"
+ exit 2
fi
# Header
-cat >> $dest << END
+cat >> "${dest}" << END
// config_report.cc. Generated from config.report by tools/mk_cfgrpt.sh
namespace isc {
@@ -38,10 +43,10 @@ END
# Body: escape '\'s and '"'s, preprend ' ";;;; ' and append '",'
sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/ ";;;; /' -e 's/$/",/' \
- < $report_file >> $dest
+ < "${report_file}" >> "${dest}"
# Trailer
-cat >> $dest <<END
+cat >> "${dest}" <<END
""
};
diff --git a/tools/path_replacer.sh.in b/tools/path_replacer.sh.in
index 509f59d07a..f23bfd7fee 100644
--- a/tools/path_replacer.sh.in
+++ b/tools/path_replacer.sh.in
@@ -1,10 +1,11 @@
#!/bin/sh
+
# Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
-#
+
# This script replaces @prefix@, @localstatedir@ and other automake/autoconf
# variables with their actual content.
#
@@ -14,13 +15,19 @@
#
# This script is initially used to generate configuration files, but it is
# generic and can be used to generate any text files.
-#
-prefix=@prefix@
-sysconfdir=@sysconfdir@
-localstatedir=@localstatedir@
-exec_prefix=@exec_prefix@
-libdir=@libdir@
+# shellcheck disable=SC2034
+# SC2034: ... appears unused. Verify use (or export if used externally).
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+prefix="@prefix@"
+sysconfdir="@sysconfdir@"
+localstatedir="@localstatedir@"
+exec_prefix="@exec_prefix@"
+libdir="@libdir@"
echo "Replacing \@prefix\@ with ${prefix}"
echo "Replacing \@libdir\@ with ${libdir}"
@@ -30,4 +37,4 @@ echo "Replacing \@localstatedir\@ with ${localstatedir}"
echo "Input file: $1"
echo "Output file: $2"
-sed -e "s@SEP@\@libdir\@@SEP@${libdir}@SEP@g; s@SEP@\@localstatedir\@@SEP@${localstatedir}@SEP@g; s@SEP@\@prefix\@@SEP@${prefix}@SEP@g; s@SEP@\@sysconfdir\@@SEP@${sysconfdir}@SEP@g" $1 > $2
+sed -e "s@SEP@\@libdir\@@SEP@${libdir}@SEP@g; s@SEP@\@localstatedir\@@SEP@${localstatedir}@SEP@g; s@SEP@\@prefix\@@SEP@${prefix}@SEP@g; s@SEP@\@sysconfdir\@@SEP@${sysconfdir}@SEP@g" "${1}" > "${2}"
diff --git a/tools/sysrepo_config b/tools/sysrepo_config
index ba15afe5a8..3194366c99 100755
--- a/tools/sysrepo_config
+++ b/tools/sysrepo_config
@@ -10,6 +10,10 @@
# This script is used to run Kea from installation directory,
# as well as for running tests.
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
if [ "$(uname -s)" = "Darwin" ]; then
DIR=$(stat -f %N "$0" | xargs dirname)
else
diff --git a/tools/tests_in_valgrind.sh b/tools/tests_in_valgrind.sh
index 115fb8cf06..08e9bd62aa 100755
--- a/tools/tests_in_valgrind.sh
+++ b/tools/tests_in_valgrind.sh
@@ -1,6 +1,6 @@
#!/bin/sh
-###########################################
+################################################################################
# This script runs all tests in valgrind. Configure and compile kea the way
# you want it to be tested (you should use --with-gtest or --with-gtest-source,
# however, or you get no tests). Then run this script from the top build
@@ -16,21 +16,27 @@
# used unmodified inside a sed pattern with # as a modifier, which can
# easily break it. There was no motivation to fix this.
# * VALGRIND_FILE is the file to store the output into. Default is valgrind.log
-###########################################
+################################################################################
+
+# Change directory to Kea's top build directory.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+cd "${script_path}/.."
# First, make sure the tests are up to date
make
-if [ $? = 2 ] ; then
- echo "Did you run configure? Or maybe you're running the script from the tools directory? (you need to run it from the top kea build directory)"
+if test $? = 2; then
+ printf 'Did you run configure?\n'
exit 1
fi
-set -e
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
# Some configuration
# TODO Escape for sed, this might break
-LOGFILE="${VALGRIND_FILE:-`pwd`/valgrind.log}"
+LOGFILE="${VALGRIND_FILE:-$(pwd)/valgrind.log}"
FLAGS="${VALGRIND_FLAGS:---leak-check=full --track-fds=yes}"
FLAGS="$FLAGS --log-file=$LOGFILE.%p"
@@ -39,14 +45,14 @@ FAILED=
# Find all the tests (yes, doing it by a name is a nasty hack)
# Since the while runs in a subprocess, we need to get the assignments out, done by the eval
-eval $(find . -type f -name *_unittests -print | grep -v '\.libs/' | while read testname ; do
+find . -type f -name '*_unittests' -print | grep -Fv '.libs/' | while read -r testname ; do
sed -e 's#exec "#exec valgrind '"$FLAGS"' "#' "$testname" > "$testname.valgrind"
chmod +x "$testname.valgrind"
echo "$testname" >>"$LOGFILE"
echo "===============" >>"$LOGFILE"
- OLDDIR="`pwd`"
- cd $(dirname "$testname")
- ./$(basename $testname).valgrind >&2 &
+ OLDDIR=$(pwd)
+ cd "$(dirname "$testname")"
+ "./$(basename "$testname").valgrind" >&2 &
PID="$!"
set +e
wait "$PID"
@@ -54,18 +60,18 @@ eval $(find . -type f -name *_unittests -print | grep -v '\.libs/' | while read
set -e
cd "$OLDDIR"
if [ "$CODE" != 0 ] ; then
- echo 'FAILED="$FAILED
-'"$testname"'"'
+ printf 'FAILED="%s
+%s"' "${FAILED}" "${testname}"
fi
NAME="$LOGFILE.$PID"
rm "$testname.valgrind"
# Remove the ones from death tests
- if [ -e $NAME ]; then
+ if [ -e "${NAME}" ]; then
grep "==$PID==" "$NAME" >>"$LOGFILE"
rm "$NAME"
fi
echo 'FOUND_ANY=true'
-done)
+done
if test -n "$FAILED"; then
echo "These tests failed:" >&2