Skip to content

Commit 9fb38f5

Browse files
committed
scripts/ImportExportTest.sh: refactorings.
1 parent 42a11f8 commit 9fb38f5

File tree

1 file changed

+61
-80
lines changed

1 file changed

+61
-80
lines changed

scripts/ImportExportTest.sh

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
5353
fail "Test directory not empty. Skipping!"
5454
fi
5555

56-
function Ast_ImportExportEquivalence
56+
function ast_import_export_equivalence
5757
{
5858
local sol_file="$1"
5959
local input_files="$2"
@@ -84,7 +84,7 @@ function Ast_ImportExportEquivalence
8484
rm -f stderr.txt
8585
}
8686

87-
function JsonEvmAsm_ImportExportEquivalence
87+
function json_evm_asm_import_export_equivalence
8888
{
8989
local sol_file="$1"
9090
local input_files="$2"
@@ -111,106 +111,94 @@ function JsonEvmAsm_ImportExportEquivalence
111111
done
112112

113113
assembly=$(cat expected.asm.json)
114-
if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
114+
[[ $assembly != "" && $assembly != "null" ]] || continue
115+
116+
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
115117
then
116-
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
117-
then
118-
printError
119-
printError "$sol_file"
120-
cat obtained.error >&2
121-
FAILED=$((FAILED + 1))
122-
return 0
123-
fi
118+
printError
119+
printError "$sol_file"
120+
cat obtained.error >&2
121+
FAILED=$((FAILED + 1))
122+
return 0
123+
fi
124124

125-
for output in "${outputs[@]}"
125+
for output in "${outputs[@]}"
126+
do
127+
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
126128
do
127-
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
128-
do
129-
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
130-
if ! diff_files "expected.${output}.json" "obtained.${output}.json"
131-
then
132-
_TESTED=
133-
FAILED=$((FAILED + 1))
134-
return 0
135-
fi
136-
done
129+
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
130+
if ! diff_files "expected.${output}.json" "obtained.${output}.json"
131+
then
132+
_TESTED=
133+
FAILED=$((FAILED + 1))
134+
return 0
135+
fi
137136
done
137+
done
138138

139-
# direct export via --asm-json, if imported with --import-asm-json.
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
141-
then
142-
printError
143-
printError "$sol_file"
144-
cat obtained_direct_import_export.error >&2
145-
FAILED=$((FAILED + 1))
146-
return 0
147-
fi
139+
# direct export via --asm-json, if imported with --import-asm-json.
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
141+
then
142+
printError
143+
printError "$sol_file"
144+
cat obtained_direct_import_export.error >&2
145+
FAILED=$((FAILED + 1))
146+
return 0
147+
fi
148148

149-
# reformat jsons using jq.
150-
jq . expected.asm.json > expected.asm.json.pretty
151-
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
152-
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
153-
then
154-
_TESTED=
155-
FAILED=$((FAILED + 1))
156-
return 0
157-
fi
149+
# reformat jsons using jq.
150+
jq . expected.asm.json > expected.asm.json.pretty
151+
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
152+
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
153+
then
154+
_TESTED=
155+
FAILED=$((FAILED + 1))
156+
return 0
158157
fi
159158
done
160159

161-
if [ -n "${_TESTED}" ]
160+
if [[ $_TESTED == "" ]]
162161
then
163162
TESTED=$((TESTED + 1))
164163
fi
165164
}
166165

167-
# function tests whether exporting and importing again leaves the JSON ast unchanged
166+
# function tests whether exporting and importing again is equivalent.
168167
# Results are recorded by adding to FAILED or UNCOMPILABLE.
169-
# Also, in case of a mismatch a diff and the respective ASTs are printed
168+
# Also, in case of a mismatch a diff is printed
170169
# Expected parameters:
171170
# $1 name of the file to be exported and imported
172171
# $2 any files needed to do so that might be in parent directories
173172
function testImportExportEquivalence {
174173
local sol_file="$1"
175174
local input_files="$2"
176-
if $SOLC --bin "${input_files}" > /dev/null 2>&1
175+
if "$SOLC" --bin "${input_files}" > /dev/null 2>&1
177176
then
178-
! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; }
179-
180-
if [[ $IMPORT_TEST_TYPE == "ast" ]]
181-
then
182-
Ast_ImportExportEquivalence "${sol_file}" "${input_files}"
183-
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
184-
then
185-
JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
186-
else
187-
fail "Unknown import test type. Aborting."
188-
fi
177+
! [[ -e stderr.txt ]] || fail "stderr.txt already exists. Refusing to overwrite."
178+
case "$IMPORT_TEST_TYPE" in
179+
ast) ast_import_export_equivalence "${sol_file}" "${input_files}" ;;
180+
evm-assembly) json_evm_asm_import_export_equivalence "${sol_file}" "${input_files}" ;;
181+
*) fail "Unknown import test type. Aborting." ;;
182+
esac
189183
else
190184
UNCOMPILABLE=$((UNCOMPILABLE + 1))
191185
fi
192186
}
193187

194188
WORKINGDIR=$PWD
195-
NSOURCES=0
196189

197190
command_available "${SOLC}" --version
198191
command_available jq --version
199192

200-
# for solfile in $(find $DEV_DIR -name *.sol)
193+
case "$IMPORT_TEST_TYPE" in
194+
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
195+
evm-assembly) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}") ;;
196+
*) fail "Unknown import test type. Aborting. Please specify ${0} [ast|evm-assembly]." ;;
197+
esac
198+
201199
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
202200
# bug. Since the test involves a malformed path, there is no point in running
203-
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176
204-
if [[ $IMPORT_TEST_TYPE == "ast" ]]
205-
then
206-
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}")
207-
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
208-
then
209-
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
210-
else
211-
fail "Unknown import test type. Aborting. Please specify ${0} [ast|evm-assembly]."
212-
fi
213-
201+
# tests on it. See https://github.com/boostorg/filesystem/issues/176
214202
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
215203

216204
NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)"
@@ -227,13 +215,11 @@ do
227215
OUTPUT=$("$SPLITSOURCES" "$solfile")
228216
SPLITSOURCES_RC=$?
229217
set -e
230-
if [ ${SPLITSOURCES_RC} == 0 ]
218+
if [[ ${SPLITSOURCES_RC} == 0 ]]
231219
then
232-
OIFS=${IFS}
233220
IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
234-
IFS=${OIFS}
235221
NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
236-
testImportExportEquivalence "$solfile" "${OUTPUT[@]}"
222+
testImportExportEquivalence "$solfile" "${OUTPUT_ARRAY[@]}"
237223
elif [ ${SPLITSOURCES_RC} == 1 ]
238224
then
239225
testImportExportEquivalence "$solfile" "$solfile"
@@ -242,17 +228,12 @@ do
242228
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
243229
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
244230
# these errors, but print the actual output of the script.
245-
echo >&2
246-
printError "\n${OUTPUT[*]}\n"
247-
echo >&2
231+
printError "\n\n${OUTPUT}\n\n"
248232
testImportExportEquivalence "$solfile" "$solfile"
249233
else
250234
# All other return codes will be treated as critical errors. The script will exit.
251-
echo >&2
252-
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
253-
echo >&2
254-
printError "\n${OUTPUT[*]}\n"
255-
echo >&2
235+
printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
236+
printError "\n\n${OUTPUT}\n\n"
256237

257238
cd "$WORKINGDIR"
258239
# Delete temporary files

0 commit comments

Comments
 (0)