From 5b87c962e7ff830728977a64b61f36f8b6e90d86 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Mon, 17 Apr 2023 08:20:36 +0200 Subject: [PATCH] Polish up bash scripts - Use '-n' instead of '! -z' - Quote all the things... - Enclose variable references in '{}' Signed-off-by: Thomas Lauf --- test/AtomicFile.t | 8 ++++---- test/bash_tap.sh | 10 +++++----- test/bash_tap_ti.sh | 4 ++-- test/performance-test.sh | 22 +++++++++++----------- test/timemachine | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/test/AtomicFile.t b/test/AtomicFile.t index b1f6f8ff..1acfb1fb 100755 --- a/test/AtomicFile.t +++ b/test/AtomicFile.t @@ -1,5 +1,5 @@ #!/bin/sh -BASEDIR=$(dirname "$0") +BASEDIR="$( dirname "$0" )" if [ "$(uname -s)" = "Darwin" ] ; then DLL_TOOL="otool -L" @@ -7,8 +7,8 @@ else DLL_TOOL="ldd" fi -if ${DLL_TOOL} ${BASEDIR}/AtomicFileTest | grep -q 'libfiu' ; then - exec fiu-run -x ${BASEDIR}/AtomicFileTest +if "${DLL_TOOL}" "${BASEDIR}/AtomicFileTest" | grep -q 'libfiu' ; then + exec fiu-run -x "${BASEDIR}/AtomicFileTest" else - exec ${BASEDIR}/AtomicFileTest + exec "${BASEDIR}/AtomicFileTest" fi diff --git a/test/bash_tap.sh b/test/bash_tap.sh index aa3850dc..a45db510 100644 --- a/test/bash_tap.sh +++ b/test/bash_tap.sh @@ -33,7 +33,7 @@ function bashtap_run_testcase { fi # Run file line by line. - if [ ! -z "$bashtap_line" ] && [ "${bashtap_line:0:2}" != "#!" ]; then + if [ -n "$bashtap_line" ] && [ "${bashtap_line:0:2}" != "#!" ]; then bashtap_output+="# $ $bashtap_line" bashtap_output+=$'\n' local cmd_output @@ -43,7 +43,7 @@ function bashtap_run_testcase { cmd_ret=$? cmd_output="$(sed 's/^/# >>> /' < bashtap_out_tmp)" - if [ ! -z "$cmd_output" ]; then + if [ -n "$cmd_output" ]; then bashtap_output+="$cmd_output" bashtap_output+=$'\n' fi @@ -55,7 +55,7 @@ function bashtap_run_testcase { } function bashtap_clean_tmpdir { - if [ ! -z "$bashtap_tmpdir" ] && [ -d "$bashtap_tmpdir" ]; then + if [ -n "$bashtap_tmpdir" ] && [ -d "$bashtap_tmpdir" ]; then cd "$bashtap_org_pwd" rm -rf "$bashtap_tmpdir" fi @@ -67,7 +67,7 @@ function bashtap_clean_tmpdir { function bashtap_get_absolute_path { # NOTE: No actual thought put into this. Might break. Horribly. # Using this instead of readlink/realpath for OSX compatibility. - echo $(cd "$(dirname "$1")" && pwd)/$(basename "$1") + echo "$( cd "$( dirname "$1" )" && pwd )/$( basename "$1" )" } @@ -87,7 +87,7 @@ if [ "${0:(-2)}" == ".t" ] || [ "$1" == "-t" ]; then # The different calls to mktemp are necessary for OSX compatibility. bashtap_tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'bash_tap') - if [ ! -z "$bashtap_tmpdir" ]; then + if [ -n "$bashtap_tmpdir" ]; then cd "$bashtap_tmpdir" else bashtap_line="Unable to create temporary directory." diff --git a/test/bash_tap_ti.sh b/test/bash_tap_ti.sh index 1b228696..14b31292 100644 --- a/test/bash_tap_ti.sh +++ b/test/bash_tap_ti.sh @@ -14,8 +14,8 @@ function setup_cfg { # Configuration for i in data/*.data timewarrior.cfg; do - if [ -f "$i" ]; then - rm "$i" 2>&1 >/dev/null + if [ -f "${i}" ]; then + rm "${i}" >/dev/null 2>&1 fi done diff --git a/test/performance-test.sh b/test/performance-test.sh index 12585e9c..b90e3432 100755 --- a/test/performance-test.sh +++ b/test/performance-test.sh @@ -4,21 +4,21 @@ set -e # Due to system integrity protection, macOS requires us to use a copy of date # if we want it to work with faketime. -DATE=$(command -v date) -if [ $(uname -s) = "Darwin" ]; then - TEMP_DATE=$(mktemp) - cp $DATE $TEMP_DATE - chmod +x $TEMP_DATE - DATE=$TEMP_DATE +DATE="$( command -v date )" +if [ "$( uname -s )" = "Darwin" ]; then + TEMP_DATE=$( mktemp ) + cp "${DATE}" "${TEMP_DATE}" + chmod +x "${TEMP_DATE}" + DATE="${TEMP_DATE}" function cleanup { - rm -fr $DATE + rm -fr "${DATE}" } trap cleanup EXIT fi -THREE_HOURS_BEFORE=$( faketime '3 hours ago' ${DATE} "+%Y%m%dT%H0000" ) -TWO_HOURS_BEFORE=$( faketime '2 hours ago' ${DATE} "+%Y%m%dT%H0000" ) -ONE_HOUR_BEFORE=$( faketime '1 hours ago' ${DATE} "+%Y%m%dT%H0000" ) +THREE_HOURS_BEFORE=$( faketime '3 hours ago' "${DATE}" "+%Y%m%dT%H0000" ) +TWO_HOURS_BEFORE=$( faketime '2 hours ago' "${DATE}" "+%Y%m%dT%H0000" ) +ONE_HOUR_BEFORE=$( faketime '1 hours ago' "${DATE}" "+%Y%m%dT%H0000" ) TIMEW_BIN="${BASH_SOURCE[0]%/*}/../src/timew" @@ -328,7 +328,7 @@ for timew_cmd in ${TIMEW_COMMANDS} ; do done for step in $( seq 0 20 ) ; do - YEAR_MONTH=$( faketime "${step} months ago" ${DATE} "+%Y-%m" ) + YEAR_MONTH="$( faketime "${step} months ago" "${DATE}" "+%Y-%m" )" if [[ "${step}" -gt 0 ]] ; then year=${YEAR_MONTH%-*} diff --git a/test/timemachine b/test/timemachine index dfd0ee6f..cb13b16c 100755 --- a/test/timemachine +++ b/test/timemachine @@ -130,7 +130,7 @@ for date in "${dates[@]}" ; do runtime=$( get_runtime_for "${single_test}" ) echo "Running test ${single_test} at ${date_time}${runtime:+" with ${runtime}"}" - if ! faketime "${date_time}" ${runtime} "${single_test}" ; then + if ! faketime "${date_time}" "${runtime}" "${single_test}" ; then REPORT+="${REPORT+$'\n'}Test ${single_test} broke at ${date_time}!" [[ "${fail_at_end-"no"}" == "no" ]] && break 4 fi