Skip to content

Commit 24c56db

Browse files
committed
Refactorings.
1 parent a36a1a2 commit 24c56db

File tree

2 files changed

+93
-72
lines changed

2 files changed

+93
-72
lines changed

scripts/ImportExportTest.sh

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
#!/usr/bin/env bash
2+
# ------------------------------------------------------------------------------
3+
# vim:ts=4:et
4+
# This file is part of solidity.
5+
#
6+
# solidity is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# solidity is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with solidity. If not, see <http://www.gnu.org/licenses/>
18+
#
19+
# (c) 2016-2022 solidity contributors.
20+
# ------------------------------------------------------------------------------
221

322
set -euo pipefail
423
IMPORT_TEST_TYPE="${1}"
@@ -36,19 +55,18 @@ fi
3655

3756
function Ast_ImportExportEquivalence
3857
{
39-
local nth_input_file="$1"
40-
IFS=" " read -r -a all_input_files <<< "$2"
41-
58+
local sol_file="$1"
59+
local input_files="$2"
4260
# save exported json as expected result (silently)
43-
$SOLC --combined-json ast --pretty-json --json-indent 4 "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
61+
$SOLC --combined-json ast --pretty-json --json-indent 4 "${input_files}" > expected.json 2> /dev/null
4462
# import it, and export it again as obtained result (silently)
4563
if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
4664
then
4765
# For investigating, use exit 1 here so the script stops at the
4866
# first failing test
4967
# exit 1
5068
FAILED=$((FAILED + 1))
51-
printError -e "ERROR: AST reimport failed for input file $nth_input_file"
69+
printError -e "ERROR: AST reimport failed for input file $sol_file"
5270
printError
5371
printError "Compiler stderr:"
5472
cat ./stderr.txt >&2
@@ -60,11 +78,10 @@ function Ast_ImportExportEquivalence
6078
set +e
6179
diff_files expected.json obtained.json
6280
DIFF=$?
63-
set -euo pipefail
81+
set -e
6482
if [[ ${DIFF} != 0 ]]
6583
then
6684
FAILED=$((FAILED + 1))
67-
return 2
6885
fi
6986
TESTED=$((TESTED + 1))
7087
rm expected.json obtained.json
@@ -73,15 +90,14 @@ function Ast_ImportExportEquivalence
7390

7491
function JsonEvmAsm_ImportExportEquivalence
7592
{
76-
local nth_input_file="$1"
77-
IFS=" " read -r -a all_input_files <<< "$2"
78-
93+
local sol_file="$1"
94+
local input_files="$2"
7995
local outputs=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" )
8096
local _TESTED=1
81-
if ! $SOLC --combined-json "$(IFS=, ; echo "${outputs[*]}")" --pretty-json --json-indent 4 "$nth_input_file" "${all_input_files[@]}" > expected.json 2> expected.error
97+
if ! $SOLC --combined-json "$(IFS=, ; echo "${outputs[*]}")" --pretty-json --json-indent 4 "${input_files}" > expected.json 2> expected.error
8298
then
8399
printError
84-
printError "$nth_input_file"
100+
printError "$sol_file"
85101
cat expected.error >&2
86102
UNCOMPILABLE=$((UNCOMPILABLE + 1))
87103
return 0
@@ -90,16 +106,16 @@ function JsonEvmAsm_ImportExportEquivalence
90106
do
91107
for output in "${outputs[@]}"
92108
do
93-
jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}"
109+
jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}.json"
94110
done
95111

96-
assembly=$(cat expected.asm)
112+
assembly=$(cat expected.asm.json)
97113
if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
98114
then
99-
if ! $SOLC --combined-json bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime --pretty-json --json-indent 4 --import-asm-json expected.asm > obtained.json 2> obtained.error
115+
if ! $SOLC --combined-json bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime --pretty-json --json-indent 4 --import-asm-json expected.asm.json > obtained.json 2> obtained.error
100116
then
101117
printError
102-
printError "$nth_input_file"
118+
printError "$sol_file"
103119
cat obtained.error >&2
104120
FAILED=$((FAILED + 1))
105121
return 0
@@ -108,44 +124,41 @@ function JsonEvmAsm_ImportExportEquivalence
108124
do
109125
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
110126
do
111-
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}"
127+
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
112128
set +e
113-
diff_files "expected.${output}" "obtained.${output}"
114-
DIFF=$?
115-
set -euo pipefail
116-
if [[ ${DIFF} != 0 ]]
129+
if ! diff_files "expected.${output}.json" "obtained.${output}.json"
117130
then
118131
_TESTED=
119132
FAILED=$((FAILED + 1))
120133
return 0
121134
fi
135+
set -e
122136
done
123137
done
124138

125139
# direct export via --asm-json, if imported with --import-asm-json.
126-
if ! $SOLC --asm-json --import-asm-json expected.asm | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
140+
if ! $SOLC --asm-json --import-asm-json expected.asm.json --pretty-json --json-indent 4 | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
127141
then
128142
printError
129-
printError "$nth_input_file"
143+
printError "$sol_file"
130144
cat obtained_direct_import_export.error >&2
131145
FAILED=$((FAILED + 1))
132146
return 0
133147
else
134-
for obtained_contract in $(jq '.contracts | keys | .[]' obtained_direct_import_export.json 2> /dev/null)
135-
do
136-
jq --raw-output ".contracts.${obtained_contract}.\"asm\"" obtained_direct_import_export.json > obtained_direct_import_export.asm
137-
set +e
138-
diff_files "expected.asm" "obtained_direct_import_export.asm"
139-
DIFF=$?
140-
set -euo pipefail
141-
if [[ ${DIFF} != 0 ]]
142-
then
143-
_TESTED=
144-
FAILED=$((FAILED + 1))
145-
return 0
146-
fi
147-
done
148+
# reformat jsons - yeah, somehow its not generating the same format
149+
# todo: fix that
150+
jq . expected.asm.json > expected.asm.json.pretty
151+
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
152+
set +e
153+
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
154+
then
155+
_TESTED=
156+
FAILED=$((FAILED + 1))
157+
return 0
158+
fi
159+
set -e
148160
fi
161+
149162
fi
150163
fi
151164
done
@@ -163,26 +176,20 @@ function JsonEvmAsm_ImportExportEquivalence
163176
# $1 name of the file to be exported and imported
164177
# $2 any files needed to do so that might be in parent directories
165178
function testImportExportEquivalence {
166-
local nth_input_file="$1"
167-
IFS=" " read -r -a all_input_files <<< "$2"
168-
169-
if [ -z ${all_input_files+x} ]
170-
then
171-
all_input_files=( "" )
172-
fi
173-
174-
if $SOLC --bin "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
179+
local sol_file="$1"
180+
local input_files="$2"
181+
if $SOLC --bin "${input_files}" > /dev/null 2>&1
175182
then
176-
! [[ -e stderr.txt ]] || { printError "stderr.txt already exists. Refusing to overwrite."; exit 1; }
183+
! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; }
177184

178185
if [[ $IMPORT_TEST_TYPE == "ast" ]]
179186
then
180-
Ast_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
187+
Ast_ImportExportEquivalence "${sol_file}" "${input_files}"
181188
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
182189
then
183-
JsonEvmAsm_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
190+
JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
184191
else
185-
fail "unknown import test type. aborting."
192+
fail "Unknown import test type. Aborting."
186193
fi
187194
else
188195
UNCOMPILABLE=$((UNCOMPILABLE + 1))
@@ -201,7 +208,7 @@ check_executable jq --version
201208
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176
202209
if [[ $IMPORT_TEST_TYPE == "ast" ]]
203210
then
204-
TEST_DIRS=("$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR")
211+
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}")
205212
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
206213
then
207214
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
@@ -224,29 +231,35 @@ do
224231
set +e
225232
OUTPUT=$("$SPLITSOURCES" "$solfile")
226233
SPLITSOURCES_RC=$?
227-
set -euo pipefail
234+
set -e
228235
if [ ${SPLITSOURCES_RC} == 0 ]
229236
then
230237
NSOURCES=$((NSOURCES - 1))
231-
for i in $OUTPUT;
238+
# shellcheck disable=SC2034
239+
for i in $OUTPUT
232240
do
233-
testImportExportEquivalence "$i" "$OUTPUT"
234241
NSOURCES=$((NSOURCES + 1))
235242
done
243+
testImportExportEquivalence "$solfile" "${OUTPUT[@]}"
236244
elif [ ${SPLITSOURCES_RC} == 1 ]
237245
then
238-
testImportExportEquivalence "$solfile" ""
246+
testImportExportEquivalence "$solfile" "$solfile"
239247
elif [ ${SPLITSOURCES_RC} == 2 ]
240248
then
241249
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
242250
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
243251
# these errors, but print the actual output of the script.
244-
printError "\n${OUTPUT}\n"
245-
testImportExportEquivalence "$solfile" ""
252+
echo >&2
253+
printError "${OUTPUT}"
254+
echo >&2
255+
testImportExportEquivalence "$solfile" "$solfile"
246256
else
247257
# All other return codes will be treated as critical errors. The script will exit.
248-
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
249-
printError "\n${OUTPUT}\n"
258+
echo >&2
259+
printError "Got unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
260+
echo >&2
261+
printError "${OUTPUT}"
262+
echo >&2
250263

251264
cd "$WORKINGDIR"
252265
# Delete temporary files
@@ -260,9 +273,9 @@ do
260273
rm -rf "$FILETMP"
261274
done
262275

263-
echo ""
276+
echo
264277

265-
if [ "$FAILED" = 0 ]
278+
if (( FAILED == 0 ))
266279
then
267280
echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
268281
else

scripts/common.sh

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ set -e
2626
# changes directory. The paths returned by `caller` are relative to it.
2727
_initial_work_dir=$(pwd)
2828

29-
if [ -z ${CIRCLECI+x} ]
30-
then
31-
CIRCLECI=0
32-
fi
33-
34-
if [ "$CIRCLECI" ]
29+
if [[ ${CIRCLECI:-} != "" ]]
3530
then
3631
export TERM="${TERM:-xterm}"
3732
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
@@ -176,6 +171,7 @@ function msg_on_error
176171
fi
177172
}
178173

174+
179175
function diff_values
180176
{
181177
(( $# >= 2 )) || fail "diff_values requires at least 2 arguments."
@@ -185,7 +181,21 @@ function diff_values
185181
shift
186182
shift
187183

188-
diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
184+
if ! diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
185+
then
186+
if [ "${DIFFVIEW:-}" == "" ]
187+
then
188+
printError "ERROR: values differ: '${value1}' vs. '${value2}'"
189+
printError "Expected:"
190+
printError "${value1}"
191+
printError "Obtained:"
192+
printError "${value2}"
193+
else
194+
# Use user supplied diff view binary
195+
"$DIFFVIEW" "${file1}" "${file2}"
196+
fi
197+
return 1
198+
fi
189199
}
190200

191201
function diff_files
@@ -197,11 +207,9 @@ function diff_files
197207
shift
198208
shift
199209

200-
diff "${file1}" "${file2}"
201-
local res=$?
202-
if [[ $res != 0 ]]
210+
if ! diff "${file1}" "${file2}"
203211
then
204-
if [ "$DIFFVIEW" == "" ]
212+
if [ "${DIFFVIEW:-}" == "" ]
205213
then
206214
printError "ERROR: files differ: ${file1} vs. ${file2}"
207215
printError "Expected:"

0 commit comments

Comments
 (0)