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
}
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()
{
local PID=${1}

View File

@ -295,7 +295,7 @@ function test_solc_assembly_output()
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")
if [ -z "$empty" ]
then
@ -473,24 +473,24 @@ rm -r "$SOLTMPDIR"
echo "Done."
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
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..."
SOLTMPDIR=$(mktemp -d)
(
cd "$SOLTMPDIR"
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
grep -q '//' C.bin && grep -q '__' C.bin
# But not in library file.
grep -q -v '[/_]' L.bin
# 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.
grep -q -v '[/_]' C.bin
)
@ -500,19 +500,19 @@ printTask "Testing overwriting files..."
SOLTMPDIR=$(mktemp -d)
(
# 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
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
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"
printTask "Testing assemble, yul, strict-assembly and optimize..."
(
echo '{}' | "$SOLC" - --assemble &>/dev/null
echo '{}' | "$SOLC" - --yul &>/dev/null
echo '{}' | "$SOLC" - --strict-assembly &>/dev/null
echo '{}' | msg_on_error --silent "$SOLC" - --assemble
echo '{}' | msg_on_error --silent "$SOLC" - --yul
echo '{}' | msg_on_error --silent "$SOLC" - --strict-assembly
# Test options above in conjunction with --optimize.
# Using both, --assemble and --optimize should fail.
@ -545,16 +545,14 @@ SOLTMPDIR=$(mktemp -d)
fi
# 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
fail "Failed to compile a simple contract from standard input"
fi
# This should not fail
if ! output=$(echo '' | "$SOLC" --ast-compact-json - 2>/dev/null)
then
fail "Incorrect response to --ast-compact-json option with empty stdin"
fi
echo '' | msg_on_error --silent --msg "Incorrect response to --ast-compact-json option with empty stdin" \
"$SOLC" --ast-compact-json -
)
rm -r "$SOLTMPDIR"