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!"
fi
function Ast_ImportExportEquivalence
function ast_import_export_equivalence
{
local sol_file="$1"
local input_files="$2"
@ -84,7 +84,7 @@ function Ast_ImportExportEquivalence
rm -f stderr.txt
}
function JsonEvmAsm_ImportExportEquivalence
function json_evm_asm_import_export_equivalence
{
local sol_file="$1"
local input_files="$2"
@ -111,8 +111,8 @@ function JsonEvmAsm_ImportExportEquivalence
done
assembly=$(cat expected.asm.json)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
then
[[ $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
printError
@ -155,62 +155,50 @@ function JsonEvmAsm_ImportExportEquivalence
FAILED=$((FAILED + 1))
return 0
fi
fi
done
if [ -n "${_TESTED}" ]
if [[ $_TESTED == "" ]]
then
TESTED=$((TESTED + 1))
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.
# 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:
# $1 name of the file to be exported and imported
# $2 any files needed to do so that might be in parent directories
function testImportExportEquivalence {
local sol_file="$1"
local input_files="$2"
if $SOLC --bin "${input_files}" > /dev/null 2>&1
if "$SOLC" --bin "${input_files}" > /dev/null 2>&1
then
! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; }
if [[ $IMPORT_TEST_TYPE == "ast" ]]
then
Ast_ImportExportEquivalence "${sol_file}" "${input_files}"
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then
JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
else
fail "Unknown import test type. Aborting."
fi
! [[ -e stderr.txt ]] || fail "stderr.txt already exists. Refusing to overwrite."
case "$IMPORT_TEST_TYPE" in
ast) ast_import_export_equivalence "${sol_file}" "${input_files}" ;;
evm-assembly) json_evm_asm_import_export_equivalence "${sol_file}" "${input_files}" ;;
*) fail "Unknown import test type. Aborting." ;;
esac
else
UNCOMPILABLE=$((UNCOMPILABLE + 1))
fi
}
WORKINGDIR=$PWD
NSOURCES=0
command_available "${SOLC}" --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
# 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
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
# tests on it. See https://github.com/boostorg/filesystem/issues/176
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)"
@ -227,13 +215,11 @@ do
OUTPUT=$("$SPLITSOURCES" "$solfile")
SPLITSOURCES_RC=$?
set -e
if [ ${SPLITSOURCES_RC} == 0 ]
if [[ ${SPLITSOURCES_RC} == 0 ]]
then
OIFS=${IFS}
IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
IFS=${OIFS}
NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
testImportExportEquivalence "$solfile" "${OUTPUT[@]}"
testImportExportEquivalence "$solfile" "${OUTPUT_ARRAY[@]}"
elif [ ${SPLITSOURCES_RC} == 1 ]
then
testImportExportEquivalence "$solfile" "$solfile"
@ -242,17 +228,12 @@ do
# 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
# these errors, but print the actual output of the script.
echo >&2
printError "\n${OUTPUT[*]}\n"
echo >&2
printError "\n\n${OUTPUT}\n\n"
testImportExportEquivalence "$solfile" "$solfile"
else
# All other return codes will be treated as critical errors. The script will exit.
echo >&2
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
echo >&2
printError "\n${OUTPUT[*]}\n"
echo >&2
printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
printError "\n\n${OUTPUT}\n\n"
cd "$WORKINGDIR"
# Delete temporary files