diff options
author | rillig <rillig@pkgsrc.org> | 2019-03-23 22:59:11 +0000 |
---|---|---|
committer | rillig <rillig@pkgsrc.org> | 2019-03-23 22:59:11 +0000 |
commit | b20444a044fadcf3e63be37e24ac9865b06f40fc (patch) | |
tree | 41851958bdb4aee686b7474f0c46edef6b5345c8 /regress | |
parent | 56d92689641f948629651726e3c49672afa747b3 (diff) | |
download | pkgsrc-b20444a044fadcf3e63be37e24ac9865b06f40fc.tar.gz |
regress/tools: show that TOOLS_SCRIPT is not always logged properly
Diffstat (limited to 'regress')
-rw-r--r-- | regress/tools/Makefile | 12 | ||||
-rwxr-xr-x | regress/tools/files/logging-test.sh | 144 | ||||
-rw-r--r-- | regress/tools/files/tests.subr | 4 |
3 files changed, 118 insertions, 42 deletions
diff --git a/regress/tools/Makefile b/regress/tools/Makefile index 31daf26065b..4a6e5dd3118 100644 --- a/regress/tools/Makefile +++ b/regress/tools/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.12 2019/03/22 22:41:06 rillig Exp $ +# $NetBSD: Makefile,v 1.13 2019/03/23 22:59:11 rillig Exp $ # DISTNAME= # not applicable @@ -33,6 +33,16 @@ TOOLS_CREATE+= world TOOLS_SCRIPT.world= \ echo oops +# The script for this example tool contains single quotes, double quotes +# and backslashes to demonstrate that these are properly logged. +TOOLS_CREATE+= for-loop +TOOLS_SCRIPT.for-loop= \ + printf '%s' "$$0"; \ + for arg in "$$@"; do \ + printf ' <%s>' "$$arg"; \ + done; \ + printf '\n' + do-build: .for t in ${REGRESS_TESTS} ${RUN} cd ${WRKSRC}; \ diff --git a/regress/tools/files/logging-test.sh b/regress/tools/files/logging-test.sh index 3c716314b75..df37a73bcbc 100755 --- a/regress/tools/files/logging-test.sh +++ b/regress/tools/files/logging-test.sh @@ -1,5 +1,5 @@ #! /bin/sh -# $NetBSD: logging-test.sh,v 1.3 2019/03/22 22:41:06 rillig Exp $ +# $NetBSD: logging-test.sh,v 1.4 2019/03/23 22:59:11 rillig Exp $ # Up to March 2019, the command logging for the wrapped tools didn't properly # quote the command line arguments. This meant the logging did not reflect @@ -20,41 +20,51 @@ nopath_log="./nopath.log" rm -f "$tools_log" "$nopath_log" -TOOLS_WRAPPER_LOG="$tools_log" -export TOOLS_WRAPPER_LOG - -# Forcibly call the tools from the tools directory, not the shell builtins. -# The echo tool is a wrapped tool without additional arguments. -# The mkdir tool is a wrapped tool that always gets the -p option. -(exec echo "begin" "*" "*" "*" "end") -(exec echo "dquot" "\"" "end") -(exec echo "squot" "'" "end") -(exec echo "five" '\\\\\' "end") -(exec mkdir "directory with spaces") -(exec script-dquot) -(exec script-backslash) - -unset TOOLS_WRAPPER_LOG - -# usage: assert_file_equals $filename <<EOF ... EOF -assert_file_equals() { - actual=`cat "$1"` +test_case() { + test_name="$1" +} + +# usage: run_tool $tool $args... +run_tool() { + TOOLS_WRAPPER_LOG="$tools_log" + export TOOLS_WRAPPER_LOG + + # The exec makes sure that the tool from the tools directory is + # called, even for shell builtins. + (exec "$@") + + unset TOOLS_WRAPPER_LOG + +} + +# usage: assert_log <<EOF ... EOF +assert_log() { + # Replace the variable parts from the output with placeholders. + sed < "$tools_log" > "$nopath_log" \ + -e 's,/.*/\.tools/,WRKDIR/.tools/,' \ + -e 's,^<.> /[^ ]*/,<.> BINDIR/,' + + actual=`cat "$nopath_log"` expected=`cat` - assert_equal "$1" "$expected" "$actual" + assert_equal "$test_name" "$expected" "$actual" || exit $? + + rm -f "$tools_log" "$nopath_log" + unset test_name } -# Replace the variable parts from the output with placeholders. -sed < "$tools_log" > "$nopath_log" \ - -e 's,/.*/\.tools/,WRKDIR/.tools/,' \ - -e 's,^<.> /[^ ]*/,<.> BINDIR/,' +test_case "TOOLS_PATH without TOOLS_ARGS" +{ + # The "*" ensure that there is no accidental file expansion. + run_tool echo "begin" "*" "*" "*" "end" + run_tool echo "dquot" "\"" "end" + run_tool echo "squot" "'" "end" + run_tool echo "five" '\\\\\' "end" -# The double space in the "echo begin" below is because the echo command -# doesn't get any additional arguments by the tool wrapper (TOOLS_ARGS.echo). -# -# The log doesn't show delimiters for the arguments, which makes the call to -# mkdir ambiguous. Doing proper shell quoting would require code similar to -# shquote from mk/scripts/shell-lib. This may make the tools wrapper slower. -assert_file_equals "$nopath_log" <<'EOF' + # In the <.> lines there are 2 spaces between echo and its first + # argument. This is because the echo command doesn't get any + # additional arguments by the tool wrapper (TOOLS_ARGS.echo). + + assert_log <<'EOF' [*] WRKDIR/.tools/bin/echo begin * * * end <.> echo begin * * * end [*] WRKDIR/.tools/bin/echo dquot " end @@ -63,20 +73,76 @@ assert_file_equals "$nopath_log" <<'EOF' <.> echo squot ' end [*] WRKDIR/.tools/bin/echo five \\\\\ end <.> echo five \\\\\ end +EOF +} + +test_case "TOOLS_PATH with TOOLS_ARGS" +{ + # The mkdir tool always gets the -p option. + + run_tool mkdir "directory with spaces" + + # The log doesn't show delimiters for the arguments, which makes + # the call to mkdir ambiguous. Doing proper shell quoting would + # require code similar to shquote from mk/scripts/shell-lib. + # This may make the tools wrapper slower. + assert_log <<'EOF' [*] WRKDIR/.tools/bin/mkdir directory with spaces <.> BINDIR/mkdir -p directory with spaces +EOF +} + +test_case "TOOLS_SCRIPT with dquot" +{ + run_tool script-dquot + + # The following log output contains a trailing whitespace. This + # is because the tool didn't get any actual arguments. + # + # FIXME: the "echo oops" occurs because the script is not + # properly quoted during logging. + assert_log <<'EOF' [*] WRKDIR/.tools/bin/script-dquot [*] WRKDIR/.tools/bin/world <.> echo oops oops +EOF +} + +test_case "TOOLS_SCRIPT with backslashes" +{ + run_tool script-backslash + + # The following log output contains a trailing whitespace. This + # is because the tool didn't get any actual arguments. + assert_log <<'EOF' [*] WRKDIR/.tools/bin/script-backslash <.> echo hello\;\ world EOF +} + +test_case "TOOLS_SCRIPT with complicated replacement" +{ + run_tool for-loop "one" "two" "three" + + # TODO: Add proper quoting for the printf argument inside the loop. + assert_log <<'EOF' +[*] WRKDIR/.tools/bin/for-loop one two three +<.> printf '%s' WRKDIR/.tools/bin/for-loop; for arg in one two three; do printf ' <%s>' ; done; printf '\n' +EOF +} + +test_case "TOOLS_SCRIPT with actual arguments containing quotes" +{ + run_tool for-loop \ + -DSD='"a b"' \ + -DSS=''\''a b'\''' \ + -DDD="\"a b\"" \ + -DB=\"a\ b\" -# FIXME: The tool wrapper log must contain [*] and <.> equally often. -# Explanation: -# In WRKDIR/.tools/bin/script-dquot, the shell quoting is obviously wrong. -# This results in "hello" being echoed to stdout instead of the log file. -# This also results in the "hello" tool to be run. -# The output of that tool is appended to the log file, as can be seen in -# the tool wrapper script. + # TODO: Add proper quoting for the arguments. + assert_log <<'EOF' +[*] WRKDIR/.tools/bin/for-loop -DSD="a b" -DSS='a b' -DDD="a b" -DB="a b" +<.> printf '%s' WRKDIR/.tools/bin/for-loop; for arg in -DSD="a b" -DSS='a b' -DDD="a b" -DB="a b"; do printf ' <%s>' ; done; printf '\n' +EOF +} diff --git a/regress/tools/files/tests.subr b/regress/tools/files/tests.subr index aefdd9ea6ca..116ada99593 100644 --- a/regress/tools/files/tests.subr +++ b/regress/tools/files/tests.subr @@ -1,4 +1,4 @@ -# $NetBSD: tests.subr,v 1.3 2019/03/22 20:56:16 rillig Exp $ +# $NetBSD: tests.subr,v 1.4 2019/03/23 22:59:11 rillig Exp $ # # usage: testcase_start <testname> @@ -9,6 +9,6 @@ testcase_start() { # usage: assert_equal <testname> <expected> <got> assert_equal() { [ "x$2" = "x$3" ] && return 0 - printf "error: assert_equal failed for %s:\nexpected: %s\nbut got: %s\n" "$1" "$2" "$3" 1>&2 + printf "error: assert_equal failed for \"%s\":\nexpected: %s\nbut got: %s\n" "$1" "$2" "$3" 1>&2 return 1 } |