Skip to content

Commit 0280c8d

Browse files
committed
Use fail and assertFail where appropriate in command-line tests
1 parent 3b1b9a0 commit 0280c8d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

scripts/common.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# (c) 2016-2019 solidity contributors.
2020
# ------------------------------------------------------------------------------
2121

22+
# The fail() function defined below requires set -e to be enabled.
23+
set -e
24+
2225
# Save the initial working directory so that printStackTrace() can access it even if the sourcing
2326
# changes directory. The paths returned by `caller` are relative to it.
2427
_initial_work_dir=$(pwd)
@@ -78,6 +81,8 @@ function printStackTrace
7881
function fail()
7982
{
8083
printError "$@"
84+
85+
# Using return rather than exit lets the invoking code handle the failure by suppressing the exit code.
8186
return 1
8287
}
8388

@@ -120,7 +125,7 @@ function msg_on_error()
120125
shift
121126
;;
122127
*)
123-
fail "Invalid option for msg_on_error: $1"
128+
assertFail "Invalid option for msg_on_error: $1"
124129
;;
125130
esac
126131
done

test/cmdlineTests.sh

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ echo "Using solc binary at ${SOLC}"
9292
INTERACTIVE=true
9393
if ! tty -s || [ "$CI" ]
9494
then
95-
INTERACTIVE=""
95+
INTERACTIVE=false
9696
fi
9797

9898
# extend stack size in case we run via ASAN
@@ -123,7 +123,7 @@ function update_expectation {
123123

124124
function ask_expectation_update
125125
{
126-
if [[ $INTERACTIVE != "" ]]
126+
if [[ $INTERACTIVE == true ]]
127127
then
128128
local newExpectation="${1}"
129129
local expectationFile="${2}"
@@ -142,12 +142,13 @@ function ask_expectation_update
142142
e*) "$editor" "$expectationFile"; break;;
143143
u*) update_expectation "$newExpectation" "$expectationFile"; break;;
144144
s*) return;;
145-
q*) exit 1;;
145+
q*) fail;;
146146
esac
147147
done
148148
fi
149149
else
150-
exit 1
150+
[[ $INTERACTIVE == false ]] || assertFail
151+
fail
151152
fi
152153
}
153154

@@ -252,7 +253,7 @@ EOF
252253
printError "Incorrect exit code. Expected $exit_code_expected but got $exitCode."
253254

254255
[[ $exit_code_expectation_file != "" ]] && ask_expectation_update "$exitCode" "$exit_code_expectation_file"
255-
[[ $exit_code_expectation_file == "" ]] && exit 1
256+
[[ $exit_code_expectation_file == "" ]] && fail
256257
fi
257258

258259
if [[ "$(cat "$stdout_path")" != "${stdout_expected}" ]]
@@ -266,7 +267,7 @@ EOF
266267
printError "When running $solc_command"
267268

268269
[[ $stdout_expectation_file != "" ]] && ask_expectation_update "$(cat "$stdout_path")" "$stdout_expectation_file"
269-
[[ $stdout_expectation_file == "" ]] && exit 1
270+
[[ $stdout_expectation_file == "" ]] && fail
270271
fi
271272

272273
if [[ "$(cat "$stderr_path")" != "${stderr_expected}" ]]
@@ -280,7 +281,7 @@ EOF
280281
printError "When running $solc_command"
281282

282283
[[ $stderr_expectation_file != "" ]] && ask_expectation_update "$(cat "$stderr_path")" "$stderr_expectation_file"
283-
[[ $stderr_expectation_file == "" ]] && exit 1
284+
[[ $stderr_expectation_file == "" ]] && fail
284285
fi
285286

286287
rm "$stdout_path" "$stderr_path"
@@ -300,10 +301,10 @@ function test_solc_assembly_output()
300301
if [ -z "$empty" ]
301302
then
302303
printError "Incorrect assembly output. Expected: "
303-
echo -e "${expected}"
304+
>&2 echo -e "${expected}"
304305
printError "with arguments ${solc_args[*]}, but got:"
305-
echo "${output}"
306-
exit 1
306+
>&2 echo "${output}"
307+
fail
307308
fi
308309
}
309310

@@ -373,7 +374,7 @@ printTask "Running general commandline tests..."
373374
then
374375
printError "Ambiguous input. Found input files in multiple formats:"
375376
echo -e "${inputFiles}"
376-
exit 1
377+
fail
377378
fi
378379

379380
# Use printf to get rid of the trailing newline
@@ -475,7 +476,8 @@ echo "Done."
475476

476477
printTask "Testing library checksum..."
477478
echo '' | msg_on_error --no-stdout "$SOLC" - --link --libraries a=0x90f20564390eAe531E810af625A22f51385Cd222
478-
echo '' | "$SOLC" - --link --libraries a=0x80f20564390eAe531E810af625A22f51385Cd222 &>/dev/null && exit 1
479+
echo '' | "$SOLC" - --link --libraries a=0x80f20564390eAe531E810af625A22f51385Cd222 &>/dev/null && \
480+
fail "solc --link did not reject a library address with an invalid checksum."
479481

480482
printTask "Testing long library names..."
481483
echo '' | msg_on_error --no-stdout "$SOLC" - --link --libraries aveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylonglibraryname=0x90f20564390eAe531E810af625A22f51385Cd222
@@ -503,7 +505,8 @@ SOLTMPDIR=$(mktemp -d)
503505
# First time it works
504506
echo 'contract C {}' | msg_on_error --no-stderr "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create"
505507
# Second time it fails
506-
echo 'contract C {}' | "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null && exit 1
508+
echo 'contract C {}' | "$SOLC" - --bin -o "$SOLTMPDIR/non-existing-stuff-to-create" 2>/dev/null && \
509+
fail "solc did not refuse to overwrite $SOLTMPDIR/non-existing-stuff-to-create."
507510
# Unless we force
508511
echo 'contract C {}' | msg_on_error --no-stderr "$SOLC" - --overwrite --bin -o "$SOLTMPDIR/non-existing-stuff-to-create"
509512
)
@@ -517,8 +520,8 @@ printTask "Testing assemble, yul, strict-assembly and optimize..."
517520

518521
# Test options above in conjunction with --optimize.
519522
# Using both, --assemble and --optimize should fail.
520-
echo '{}' | "$SOLC" - --assemble --optimize &>/dev/null && exit 1
521-
echo '{}' | "$SOLC" - --yul --optimize &>/dev/null && exit 1
523+
echo '{}' | "$SOLC" - --assemble --optimize &>/dev/null && fail "solc --assemble --optimize did not fail as expected."
524+
echo '{}' | "$SOLC" - --yul --optimize &>/dev/null && fail "solc --yul --optimize did not fail as expected."
522525

523526
# Test yul and strict assembly output
524527
# Non-empty code results in non-empty binary representation with optimizations turned off,
@@ -563,8 +566,8 @@ SOLTMPDIR=$(mktemp -d)
563566
cd "$SOLTMPDIR"
564567
if ! "$REPO_ROOT/scripts/ASTImportTest.sh"
565568
then
566-
rm -rf "$SOLTMPDIR"
567-
exit 1
569+
rm -r "$SOLTMPDIR"
570+
fail
568571
fi
569572
)
570573
rm -r "$SOLTMPDIR"

0 commit comments

Comments
 (0)