1
1
#! /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
+ # ------------------------------------------------------------------------------
2
21
3
22
set -euo pipefail
4
23
IMPORT_TEST_TYPE=" ${1} "
36
55
37
56
function Ast_ImportExportEquivalence
38
57
{
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 "
42
60
# 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
44
62
# import it, and export it again as obtained result (silently)
45
63
if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
46
64
then
47
65
# For investigating, use exit 1 here so the script stops at the
48
66
# first failing test
49
67
# exit 1
50
68
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 "
52
70
printError
53
71
printError " Compiler stderr:"
54
72
cat ./stderr.txt >&2
@@ -60,11 +78,10 @@ function Ast_ImportExportEquivalence
60
78
set +e
61
79
diff_files expected.json obtained.json
62
80
DIFF=$?
63
- set -euo pipefail
81
+ set -e
64
82
if [[ ${DIFF} != 0 ]]
65
83
then
66
84
FAILED=$(( FAILED + 1 ))
67
- return 2
68
85
fi
69
86
TESTED=$(( TESTED + 1 ))
70
87
rm expected.json obtained.json
@@ -73,15 +90,14 @@ function Ast_ImportExportEquivalence
73
90
74
91
function JsonEvmAsm_ImportExportEquivalence
75
92
{
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 "
79
95
local outputs=( " asm" " bin" " bin-runtime" " opcodes" " srcmap" " srcmap-runtime" )
80
96
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
82
98
then
83
99
printError
84
- printError " $nth_input_file "
100
+ printError " $sol_file "
85
101
cat expected.error >&2
86
102
UNCOMPILABLE=$(( UNCOMPILABLE + 1 ))
87
103
return 0
@@ -90,16 +106,16 @@ function JsonEvmAsm_ImportExportEquivalence
90
106
do
91
107
for output in " ${outputs[@]} "
92
108
do
93
- jq --raw-output " .contracts.${contract} .\" ${output} \" " expected.json > " expected.${output} "
109
+ jq --raw-output " .contracts.${contract} .\" ${output} \" " expected.json > " expected.${output} .json "
94
110
done
95
111
96
- assembly=$( cat expected.asm)
112
+ assembly=$( cat expected.asm.json )
97
113
if [ " $assembly " != " " ] && [ " $assembly " != " null" ]
98
114
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
100
116
then
101
117
printError
102
- printError " $nth_input_file "
118
+ printError " $sol_file "
103
119
cat obtained.error >&2
104
120
FAILED=$(( FAILED + 1 ))
105
121
return 0
@@ -108,12 +124,8 @@ function JsonEvmAsm_ImportExportEquivalence
108
124
do
109
125
for obtained_contract in $( jq ' .contracts | keys | .[]' obtained.json 2> /dev/null)
110
126
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"
117
129
then
118
130
_TESTED=
119
131
FAILED=$(( FAILED + 1 ))
@@ -123,29 +135,25 @@ function JsonEvmAsm_ImportExportEquivalence
123
135
done
124
136
125
137
# 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
127
139
then
128
140
printError
129
- printError " $nth_input_file "
141
+ printError " $sol_file "
130
142
cat obtained_direct_import_export.error >&2
131
143
FAILED=$(( FAILED + 1 ))
132
144
return 0
133
145
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
148
155
fi
156
+
149
157
fi
150
158
fi
151
159
done
@@ -163,26 +171,20 @@ function JsonEvmAsm_ImportExportEquivalence
163
171
# $1 name of the file to be exported and imported
164
172
# $2 any files needed to do so that might be in parent directories
165
173
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
175
177
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." ; }
177
179
178
180
if [[ $IMPORT_TEST_TYPE == " ast" ]]
179
181
then
180
- Ast_ImportExportEquivalence " $nth_input_file " " ${all_input_files[@] } "
182
+ Ast_ImportExportEquivalence " ${sol_file} " " ${input_files } "
181
183
elif [[ $IMPORT_TEST_TYPE == " evm-assembly" ]]
182
184
then
183
- JsonEvmAsm_ImportExportEquivalence " $nth_input_file " " ${all_input_files[@] } "
185
+ JsonEvmAsm_ImportExportEquivalence " ${sol_file} " " ${input_files } "
184
186
else
185
- fail " unknown import test type. aborting ."
187
+ fail " Unknown import test type. Aborting ."
186
188
fi
187
189
else
188
190
UNCOMPILABLE=$(( UNCOMPILABLE + 1 ))
@@ -192,16 +194,16 @@ function testImportExportEquivalence {
192
194
WORKINGDIR=$PWD
193
195
NSOURCES=0
194
196
195
- check_executable " $SOLC " --version
196
- check_executable jq --version
197
+ command_available " ${ SOLC} " --version
198
+ command_available jq --version
197
199
198
200
# for solfile in $(find $DEV_DIR -name *.sol)
199
201
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
200
202
# bug. Since the test involves a malformed path, there is no point in running
201
203
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176
202
204
if [[ $IMPORT_TEST_TYPE == " ast" ]]
203
205
then
204
- TEST_DIRS=(" $SYNTAXTESTS_DIR " " $ASTJSONTESTS_DIR " )
206
+ TEST_DIRS=(" ${ SYNTAXTESTS_DIR} " " ${ ASTJSONTESTS_DIR} " )
205
207
elif [[ $IMPORT_TEST_TYPE == " evm-assembly" ]]
206
208
then
207
209
TEST_DIRS=(" ${SYNTAXTESTS_DIR} " " ${SEMANTICTESTS_DIR} " )
224
226
set +e
225
227
OUTPUT=$( " $SPLITSOURCES " " $solfile " )
226
228
SPLITSOURCES_RC=$?
227
- set -euo pipefail
229
+ set -e
228
230
if [ ${SPLITSOURCES_RC} == 0 ]
229
231
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[@]} "
236
237
elif [ ${SPLITSOURCES_RC} == 1 ]
237
238
then
238
- testImportExportEquivalence " $solfile " " "
239
+ testImportExportEquivalence " $solfile " " $solfile "
239
240
elif [ ${SPLITSOURCES_RC} == 2 ]
240
241
then
241
242
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
242
243
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
243
244
# 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 "
246
249
else
247
250
# All other return codes will be treated as critical errors. The script will exit.
251
+ echo >&2
248
252
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
250
256
251
257
cd " $WORKINGDIR "
252
258
# Delete temporary files
260
266
rm -rf " $FILETMP "
261
267
done
262
268
263
- echo " "
269
+ echo
264
270
265
- if [ " $ FAILED" = 0 ]
271
+ if (( FAILED == 0 ))
266
272
then
267
273
echo " SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
268
274
else
0 commit comments