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
322set -euo pipefail
423IMPORT_TEST_TYPE=" ${1} "
3655
3756function 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
7491function 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
165178function 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
202209if [[ $IMPORT_TEST_TYPE == " ast" ]]
203210then
204- TEST_DIRS=(" $SYNTAXTESTS_DIR " " $ASTJSONTESTS_DIR " )
211+ TEST_DIRS=(" ${ SYNTAXTESTS_DIR} " " ${ ASTJSONTESTS_DIR} " )
205212elif [[ $IMPORT_TEST_TYPE == " evm-assembly" ]]
206213then
207214 TEST_DIRS=(" ${SYNTAXTESTS_DIR} " " ${SEMANTICTESTS_DIR} " )
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
260273 rm -rf " $FILETMP "
261274done
262275
263- echo " "
276+ echo
264277
265- if [ " $ FAILED" = 0 ]
278+ if (( FAILED == 0 ))
266279then
267280 echo " SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
268281else
0 commit comments