Skip to content

Commit f5333b3

Browse files
committed
Refactorings.
1 parent f2d47b8 commit f5333b3

File tree

2 files changed

+102
-87
lines changed

2 files changed

+102
-87
lines changed

scripts/ImportExportTest.sh

Lines changed: 73 additions & 67 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) 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,12 +124,8 @@ 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}"
112-
set +e
113-
diff_files "expected.${output}" "obtained.${output}"
114-
DIFF=$?
115-
set -euo pipefail
116-
if [[ ${DIFF} != 0 ]]
127+
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
128+
if ! diff_files "expected.${output}.json" "obtained.${output}.json"
117129
then
118130
_TESTED=
119131
FAILED=$((FAILED + 1))
@@ -123,29 +135,25 @@ function JsonEvmAsm_ImportExportEquivalence
123135
done
124136

125137
# 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
138+
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
127139
then
128140
printError
129-
printError "$nth_input_file"
141+
printError "$sol_file"
130142
cat obtained_direct_import_export.error >&2
131143
FAILED=$((FAILED + 1))
132144
return 0
133145
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
146+
# reformat jsons using jq.
147+
jq . expected.asm.json > expected.asm.json.pretty
148+
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
149+
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
150+
then
151+
_TESTED=
152+
FAILED=$((FAILED + 1))
153+
return 0
154+
fi
148155
fi
156+
149157
fi
150158
fi
151159
done
@@ -163,26 +171,20 @@ function JsonEvmAsm_ImportExportEquivalence
163171
# $1 name of the file to be exported and imported
164172
# $2 any files needed to do so that might be in parent directories
165173
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
174+
local sol_file="$1"
175+
local input_files="$2"
176+
if $SOLC --bin "${input_files}" > /dev/null 2>&1
175177
then
176-
! [[ -e stderr.txt ]] || { printError "stderr.txt already exists. Refusing to overwrite."; exit 1; }
178+
! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; }
177179

178180
if [[ $IMPORT_TEST_TYPE == "ast" ]]
179181
then
180-
Ast_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
182+
Ast_ImportExportEquivalence "${sol_file}" "${input_files}"
181183
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
182184
then
183-
JsonEvmAsm_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
185+
JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
184186
else
185-
fail "unknown import test type. aborting."
187+
fail "Unknown import test type. Aborting."
186188
fi
187189
else
188190
UNCOMPILABLE=$((UNCOMPILABLE + 1))
@@ -192,16 +194,16 @@ function testImportExportEquivalence {
192194
WORKINGDIR=$PWD
193195
NSOURCES=0
194196

195-
check_executable "$SOLC" --version
196-
check_executable jq --version
197+
command_available "${SOLC}" --version
198+
command_available jq --version
197199

198200
# for solfile in $(find $DEV_DIR -name *.sol)
199201
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
200202
# bug. Since the test involves a malformed path, there is no point in running
201203
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176
202204
if [[ $IMPORT_TEST_TYPE == "ast" ]]
203205
then
204-
TEST_DIRS=("$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR")
206+
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}")
205207
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
206208
then
207209
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
@@ -224,29 +226,33 @@ do
224226
set +e
225227
OUTPUT=$("$SPLITSOURCES" "$solfile")
226228
SPLITSOURCES_RC=$?
227-
set -euo pipefail
229+
set -e
228230
if [ ${SPLITSOURCES_RC} == 0 ]
229231
then
230-
NSOURCES=$((NSOURCES - 1))
231-
for i in $OUTPUT;
232-
do
233-
testImportExportEquivalence "$i" "$OUTPUT"
234-
NSOURCES=$((NSOURCES + 1))
235-
done
232+
OIFS=${IFS}
233+
IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
234+
IFS=${OIFS}
235+
NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
236+
testImportExportEquivalence "$solfile" "${OUTPUT[@]}"
236237
elif [ ${SPLITSOURCES_RC} == 1 ]
237238
then
238-
testImportExportEquivalence "$solfile" ""
239+
testImportExportEquivalence "$solfile" "$solfile"
239240
elif [ ${SPLITSOURCES_RC} == 2 ]
240241
then
241242
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
242243
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
243244
# these errors, but print the actual output of the script.
244-
printError "\n${OUTPUT}\n"
245-
testImportExportEquivalence "$solfile" ""
245+
echo >&2
246+
printError "\n${OUTPUT[*]}\n"
247+
echo >&2
248+
testImportExportEquivalence "$solfile" "$solfile"
246249
else
247250
# All other return codes will be treated as critical errors. The script will exit.
251+
echo >&2
248252
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
249-
printError "\n${OUTPUT}\n"
253+
echo >&2
254+
printError "\n${OUTPUT[*]}\n"
255+
echo >&2
250256

251257
cd "$WORKINGDIR"
252258
# Delete temporary files
@@ -260,9 +266,9 @@ do
260266
rm -rf "$FILETMP"
261267
done
262268

263-
echo ""
269+
echo
264270

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

scripts/common.sh

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,18 @@ 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}"
37-
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
38-
function printError { >&2 echo "$(tput setaf 1)$1$(tput setaf 7)"; }
39-
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput setaf 7)"; }
40-
function printLog { echo "$(tput setaf 3)$1$(tput setaf 7)"; }
32+
function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
33+
function printError { >&2 echo -e "$(tput setaf 1)$1$(tput setaf 7)"; }
34+
function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput setaf 7)"; }
35+
function printLog { echo -e "$(tput setaf 3)$1$(tput setaf 7)"; }
4136
else
42-
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; }
43-
function printError { >&2 echo "$(tput setaf 1)$1$(tput sgr0)"; }
44-
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput sgr0)"; }
45-
function printLog { echo "$(tput setaf 3)$1$(tput sgr0)"; }
37+
function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; }
38+
function printError { >&2 echo -e "$(tput setaf 1)$1$(tput sgr0)"; }
39+
function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput sgr0)"; }
40+
function printLog { echo -e "$(tput setaf 3)$1$(tput sgr0)"; }
4641
fi
4742

4843
function checkDputEntries
@@ -198,6 +193,7 @@ function msg_on_error
198193
fi
199194
}
200195

196+
201197
function diff_values
202198
{
203199
(( $# >= 2 )) || fail "diff_values requires at least 2 arguments."
@@ -207,7 +203,22 @@ function diff_values
207203
shift
208204
shift
209205

210-
diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
206+
if ! diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
207+
then
208+
if [ "${DIFFVIEW:-}" == "" ]
209+
then
210+
printError "ERROR: values differ:"
211+
printError "Expected:"
212+
printError "${value1}"
213+
printError "Obtained:"
214+
printError "${value2}"
215+
else
216+
# Use user supplied diff view binary
217+
printError "ERROR: values differ."
218+
"$DIFFVIEW" "${file1}" "${file2}"
219+
fi
220+
return 1
221+
fi
211222
}
212223

213224
function diff_files
@@ -219,11 +230,9 @@ function diff_files
219230
shift
220231
shift
221232

222-
diff "${file1}" "${file2}"
223-
local res=$?
224-
if [[ $res != 0 ]]
233+
if ! diff "${file1}" "${file2}"
225234
then
226-
if [ "$DIFFVIEW" == "" ]
235+
if [ "${DIFFVIEW:-}" == "" ]
227236
then
228237
printError "ERROR: files differ: ${file1} vs. ${file2}"
229238
printError "Expected:"
@@ -312,7 +321,7 @@ function split_on_empty_lines_into_numbered_files
312321
awk -v RS= "{print > (\"${path_prefix}_\"NR \"${path_suffix}\")}"
313322
}
314323

315-
function check_executable
324+
function command_available
316325
{
317326
local program="$1"
318327
local parameters=${*:2}

0 commit comments

Comments
 (0)