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,8 +111,8 @@ function JsonEvmAsm_ImportExportEquivalence
done done
assembly=$(cat expected.asm.json) assembly=$(cat expected.asm.json)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ] [[ $assembly != "" && $assembly != "null" ]] || continue
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 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
printError printError
@ -155,62 +155,50 @@ function JsonEvmAsm_ImportExportEquivalence
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