scripts/ImportExportTest.sh: refactorings.

This commit is contained in:
Alexander Arlt 2022-08-19 16:51:14 +02:00
parent 0e9505ccb6
commit bf5565d8dd

View File

@ -53,7 +53,7 @@ if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
fail "Test directory not empty. Skipping!" fail "Test directory not empty. Skipping!"
fi fi
function Ast_ImportExportEquivalence function ast_import_export_equivalence
{ {
local sol_file="$1" local sol_file="$1"
local input_files="$2" local input_files="$2"
@ -84,7 +84,7 @@ function Ast_ImportExportEquivalence
rm -f stderr.txt rm -f stderr.txt
} }
function JsonEvmAsm_ImportExportEquivalence function json_evm_asm_import_export_equivalence
{ {
local sol_file="$1" local sol_file="$1"
local input_files="$2" local input_files="$2"
@ -111,106 +111,94 @@ function JsonEvmAsm_ImportExportEquivalence
done done
assembly=$(cat expected.asm.json) assembly=$(cat expected.asm.json)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ] [[ $assembly != "" && $assembly != "null" ]] || continue
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
then then
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 printError
then printError "$sol_file"
printError cat obtained.error >&2
printError "$sol_file" FAILED=$((FAILED + 1))
cat obtained.error >&2 return 0
FAILED=$((FAILED + 1)) fi
return 0
fi
for output in "${outputs[@]}" for output in "${outputs[@]}"
do
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
do do
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null) jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
do if ! diff_files "expected.${output}.json" "obtained.${output}.json"
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json" then
if ! diff_files "expected.${output}.json" "obtained.${output}.json" _TESTED=
then FAILED=$((FAILED + 1))
_TESTED= return 0
FAILED=$((FAILED + 1)) fi
return 0
fi
done
done done
done
# direct export via --asm-json, if imported with --import-asm-json. # direct export via --asm-json, if imported with --import-asm-json.
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 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
then then
printError printError
printError "$sol_file" printError "$sol_file"
cat obtained_direct_import_export.error >&2 cat obtained_direct_import_export.error >&2
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
return 0 return 0
fi fi
# reformat jsons using jq. # reformat jsons using jq.
jq . expected.asm.json > expected.asm.json.pretty jq . expected.asm.json > expected.asm.json.pretty
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
then then
_TESTED= _TESTED=
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
return 0 return 0
fi
fi fi
done done
if [ -n "${_TESTED}" ] if [[ $_TESTED == "" ]]
then then
TESTED=$((TESTED + 1)) TESTED=$((TESTED + 1))
fi fi
} }
# function tests whether exporting and importing again leaves the JSON ast unchanged # function tests whether exporting and importing again is equivalent.
# Results are recorded by adding to FAILED or UNCOMPILABLE. # Results are recorded by adding to FAILED or UNCOMPILABLE.
# Also, in case of a mismatch a diff and the respective ASTs are printed # Also, in case of a mismatch a diff is printed
# Expected parameters: # Expected parameters:
# $1 name of the file to be exported and imported # $1 name of the file to be exported and imported
# $2 any files needed to do so that might be in parent directories # $2 any files needed to do so that might be in parent directories
function testImportExportEquivalence { function testImportExportEquivalence {
local sol_file="$1" local sol_file="$1"
local input_files="$2" local input_files="$2"
if $SOLC --bin "${input_files}" > /dev/null 2>&1 if "$SOLC" --bin "${input_files}" > /dev/null 2>&1
then then
! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; } ! [[ -e stderr.txt ]] || fail "stderr.txt already exists. Refusing to overwrite."
case "$IMPORT_TEST_TYPE" in
if [[ $IMPORT_TEST_TYPE == "ast" ]] ast) ast_import_export_equivalence "${sol_file}" "${input_files}" ;;
then evm-assembly) json_evm_asm_import_export_equivalence "${sol_file}" "${input_files}" ;;
Ast_ImportExportEquivalence "${sol_file}" "${input_files}" *) fail "Unknown import test type. Aborting." ;;
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]] esac
then
JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
else
fail "Unknown import test type. Aborting."
fi
else else
UNCOMPILABLE=$((UNCOMPILABLE + 1)) UNCOMPILABLE=$((UNCOMPILABLE + 1))
fi fi
} }
WORKINGDIR=$PWD WORKINGDIR=$PWD
NSOURCES=0
command_available "${SOLC}" --version command_available "${SOLC}" --version
command_available jq --version command_available jq --version
# for solfile in $(find $DEV_DIR -name *.sol) case "$IMPORT_TEST_TYPE" in
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
evm-assembly) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}") ;;
*) fail "Unknown import test type. Aborting. Please specify ${0} [ast|evm-assembly]." ;;
esac
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem # boost_filesystem_bug specifically tests a local fix for a boost::filesystem
# bug. Since the test involves a malformed path, there is no point in running # bug. Since the test involves a malformed path, there is no point in running
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176 # tests on it. See https://github.com/boostorg/filesystem/issues/176
if [[ $IMPORT_TEST_TYPE == "ast" ]]
then
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}")
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
else
fail "Unknown import test type. Aborting. Please specify ${0} [ast|evm-assembly]."
fi
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol") IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)" NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)"
@ -227,13 +215,11 @@ do
OUTPUT=$("$SPLITSOURCES" "$solfile") OUTPUT=$("$SPLITSOURCES" "$solfile")
SPLITSOURCES_RC=$? SPLITSOURCES_RC=$?
set -e set -e
if [ ${SPLITSOURCES_RC} == 0 ] if [[ ${SPLITSOURCES_RC} == 0 ]]
then then
OIFS=${IFS}
IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}" IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
IFS=${OIFS}
NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]})) NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
testImportExportEquivalence "$solfile" "${OUTPUT[@]}" testImportExportEquivalence "$solfile" "${OUTPUT_ARRAY[@]}"
elif [ ${SPLITSOURCES_RC} == 1 ] elif [ ${SPLITSOURCES_RC} == 1 ]
then then
testImportExportEquivalence "$solfile" "$solfile" testImportExportEquivalence "$solfile" "$solfile"
@ -242,17 +228,12 @@ do
# The script will exit with return code 2, if an UnicodeDecodeError occurred. # The script will exit with return code 2, if an UnicodeDecodeError occurred.
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore # This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
# these errors, but print the actual output of the script. # these errors, but print the actual output of the script.
echo >&2 printError "\n\n${OUTPUT}\n\n"
printError "\n${OUTPUT[*]}\n"
echo >&2
testImportExportEquivalence "$solfile" "$solfile" testImportExportEquivalence "$solfile" "$solfile"
else else
# All other return codes will be treated as critical errors. The script will exit. # All other return codes will be treated as critical errors. The script will exit.
echo >&2 printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting." printError "\n\n${OUTPUT}\n\n"
echo >&2
printError "\n${OUTPUT[*]}\n"
echo >&2
cd "$WORKINGDIR" cd "$WORKINGDIR"
# Delete temporary files # Delete temporary files