cmdlineTests.sh: Add msg_on_error helper that automatically prints stderr and stdout of a failed command

This commit is contained in:
Kamil Śliwak 2021-10-01 17:27:09 +02:00
parent 26f9a554fb
commit b4073179d7
2 changed files with 82 additions and 16 deletions

View File

@ -39,6 +39,74 @@ function fail()
return 1 return 1
} }
function msg_on_error()
{
local error_message
local no_stdout=false
local no_stderr=false
while [[ $1 =~ ^-- ]]
do
case "$1" in
--msg)
error_message="$2"
shift
shift
;;
--no-stdout)
no_stdout=true
shift
;;
--no-stderr)
no_stderr=true
shift
;;
--silent)
no_stdout=true
no_stderr=true
shift
;;
*)
fail "Invalid option for msg_on_error: $1"
;;
esac
done
local command=("$@")
local stdout_file stderr_file
stdout_file="$(mktemp -t cmdline_test_command_stdout_XXXXXX.txt)"
stderr_file="$(mktemp -t cmdline_test_command_stderr_XXXXXX.txt)"
if "${command[@]}" > "$stdout_file" 2> "$stderr_file"
then
[[ $no_stdout == "true" ]] || cat "$stdout_file"
[[ $no_stderr == "true" ]] || >&2 cat "$stderr_file"
rm "$stdout_file" "$stderr_file"
return 0
else
printError "Command failed: $SOLC ${command[*]}"
if [[ -s "$stdout_file" ]]
then
printError "stdout:"
>&2 cat "$stdout_file"
else
printError "stdout: <EMPTY>"
fi
if [[ -s "$stderr_file" ]]
then
printError "stderr:"
>&2 cat "$stderr_file"
else
printError "stderr: <EMPTY>"
fi
printError "$error_message"
rm "$stdout_file" "$stderr_file"
return 1
fi
}
safe_kill() safe_kill()
{ {
local PID=${1} local PID=${1}

View File

@ -295,7 +295,7 @@ function test_solc_assembly_output()
local expected_object="object \"object\" { code ${expected} }" local expected_object="object \"object\" { code ${expected} }"
output=$(echo "${input}" | "$SOLC" - "${solc_args[@]}" 2>/dev/null) output=$(echo "${input}" | msg_on_error --no-stderr "$SOLC" - "${solc_args[@]}")
empty=$(echo "$output" | tr '\n' ' ' | tr -s ' ' | sed -ne "/${expected_object}/p") empty=$(echo "$output" | tr '\n' ' ' | tr -s ' ' | sed -ne "/${expected_object}/p")
if [ -z "$empty" ] if [ -z "$empty" ]
then then
@ -473,24 +473,24 @@ rm -r "$SOLTMPDIR"
echo "Done." echo "Done."
printTask "Testing library checksum..." printTask "Testing library checksum..."
echo '' | "$SOLC" - --link --libraries a=0x90f20564390eAe531E810af625A22f51385Cd222 >/dev/null echo '' | msg_on_error --no-stdout "$SOLC" - --link --libraries a=0x90f20564390eAe531E810af625A22f51385Cd222
echo '' | "$SOLC" - --link --libraries a=0x80f20564390eAe531E810af625A22f51385Cd222 &>/dev/null && exit 1 echo '' | "$SOLC" - --link --libraries a=0x80f20564390eAe531E810af625A22f51385Cd222 &>/dev/null && exit 1
printTask "Testing long library names..." printTask "Testing long library names..."
echo '' | "$SOLC" - --link --libraries aveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylonglibraryname=0x90f20564390eAe531E810af625A22f51385Cd222 >/dev/null echo '' | msg_on_error --no-stdout "$SOLC" - --link --libraries aveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylonglibraryname=0x90f20564390eAe531E810af625A22f51385Cd222
printTask "Testing linking itself..." printTask "Testing linking itself..."
SOLTMPDIR=$(mktemp -d) SOLTMPDIR=$(mktemp -d)
( (
cd "$SOLTMPDIR" cd "$SOLTMPDIR"
echo 'library L { function f() public pure {} } contract C { function f() public pure { L.f(); } }' > x.sol echo 'library L { function f() public pure {} } contract C { function f() public pure { L.f(); } }' > x.sol
"$SOLC" --bin -o . x.sol 2>/dev/null msg_on_error --no-stderr "$SOLC" --bin -o . x.sol
# Explanation and placeholder should be there # Explanation and placeholder should be there
grep -q '//' C.bin && grep -q '__' C.bin grep -q '//' C.bin && grep -q '__' C.bin
# But not in library file. # But not in library file.
grep -q -v '[/_]' L.bin grep -q -v '[/_]' L.bin
# Now link # Now link
"$SOLC" --link --libraries x.sol:L=0x90f20564390eAe531E810af625A22f51385Cd222 C.bin msg_on_error "$SOLC" --link --libraries x.sol:L=0x90f20564390eAe531E810af625A22f51385Cd222 C.bin
# Now the placeholder and explanation should be gone. # Now the placeholder and explanation should be gone.
grep -q -v '[/_]' C.bin grep -q -v '[/_]' C.bin
) )
@ -500,19 +500,19 @@ printTask "Testing overwriting files..."
SOLTMPDIR=$(mktemp -d) SOLTMPDIR=$(mktemp -d)
( (
# First time it works # First time it works
echo 'contract C {} ' | "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null echo 'contract C {}' | msg_on_error --no-stderr "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create"
# Second time it fails # Second time it fails
echo 'contract C {} ' | "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null && exit 1 echo 'contract C {}' | "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null && exit 1
# Unless we force # Unless we force
echo 'contract C {} ' | "$SOLC" - --overwrite --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null echo 'contract C {}' | msg_on_error --no-stderr "$SOLC" - --overwrite --bin -o "$SOLTMPDIR/non-existing-stuff-to-create"
) )
rm -r "$SOLTMPDIR" rm -r "$SOLTMPDIR"
printTask "Testing assemble, yul, strict-assembly and optimize..." printTask "Testing assemble, yul, strict-assembly and optimize..."
( (
echo '{}' | "$SOLC" - --assemble &>/dev/null echo '{}' | msg_on_error --silent "$SOLC" - --assemble
echo '{}' | "$SOLC" - --yul &>/dev/null echo '{}' | msg_on_error --silent "$SOLC" - --yul
echo '{}' | "$SOLC" - --strict-assembly &>/dev/null echo '{}' | msg_on_error --silent "$SOLC" - --strict-assembly
# Test options above in conjunction with --optimize. # Test options above in conjunction with --optimize.
# Using both, --assemble and --optimize should fail. # Using both, --assemble and --optimize should fail.
@ -545,16 +545,14 @@ SOLTMPDIR=$(mktemp -d)
fi fi
# The contract should be compiled # The contract should be compiled
if ! output=$(echo 'contract C {} ' | "$SOLC" - --bin 2>/dev/null | grep -q "<stdin>:C") if ! echo 'contract C {}' | msg_on_error --no-stderr "$SOLC" - --bin | grep -q "<stdin>:C"
then then
fail "Failed to compile a simple contract from standard input" fail "Failed to compile a simple contract from standard input"
fi fi
# This should not fail # This should not fail
if ! output=$(echo '' | "$SOLC" --ast-compact-json - 2>/dev/null) echo '' | msg_on_error --silent --msg "Incorrect response to --ast-compact-json option with empty stdin" \
then "$SOLC" --ast-compact-json -
fail "Incorrect response to --ast-compact-json option with empty stdin"
fi
) )
rm -r "$SOLTMPDIR" rm -r "$SOLTMPDIR"