scripts/ImportExportTest.sh: refactorings.

This commit is contained in:
Alexander Arlt 2022-03-28 17:55:24 -05:00
parent 3aa916e623
commit c5e6a5b4b8
2 changed files with 193 additions and 190 deletions

View File

@ -1,36 +1,161 @@
#!/usr/bin/env bash
set -e
IMPORT_TEST_TYPE="${1}"
# Bash script to test the import/exports.
# ast import/export tests:
# - first exporting a .sol file to JSON, then loading it into the compiler
# and exporting it again. The second JSON should be identical to the first.
# evm-assembly import/export tests:
# - <TODO>
# Bash script to test the ast-import option of the compiler by
# first exporting a .sol file to JSON, then loading it into the compiler
# and exporting it again. The second JSON should be identical to the first
READLINK=readlink
if [[ "$OSTYPE" == "darwin"* ]]; then
READLINK=greadlink
fi
IMPORT_TEST_TYPE=${1}
REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..)
SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
SOLC=${SOLIDITY_BUILD_DIR}/solc/solc
SPLITSOURCES=${REPO_ROOT}/scripts/splitSources.py
# shellcheck disable=SC1090,SC1091
source "${REPO_ROOT}/scripts/common.sh"
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
SEMANTICTESTS_DIR="${REPO_ROOT}/test/libsolidity/semanticTests"
ASTJSONTESTS_DIR="${REPO_ROOT}/test/libsolidity/ASTJSON"
# DEV_DIR="${REPO_ROOT}/../tmp/contracts/"
# NSOURCES="$(find $DEV_DIR -type f | wc -l)" #TODO use find command
FAILED=0
UNCOMPILABLE=0
TESTED=0
if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
echo "Test directory not empty. Skipping!"
exit 1
fail "Test directory not empty. Skipping!"
fi
function Ast_ImportExportEquivalence
{
local nth_input_file="$1"
IFS=" " read -r -a all_input_files <<< "$2"
# save exported json as expected result (silently)
$SOLC --combined-json ast --pretty-json --json-indent 4 "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
# import it, and export it again as obtained result (silently)
if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
then
# For investigating, use exit 1 here so the script stops at the
# first failing test
# exit 1
FAILED=$((FAILED + 1))
printError -e "ERROR: AST reimport failed for input file $nth_input_file"
printError
printError "Compiler stderr:"
cat ./stderr.txt >&2
printError
printError "Compiler stdout:"
cat ./obtained.json >&2
return 1
fi
set +e
diff_files expected.json obtained.json
DIFF=$?
set -e
if [[ ${DIFF} != 0 ]]
then
FAILED=$((FAILED + 1))
return 2
fi
TESTED=$((TESTED + 1))
rm expected.json obtained.json
rm -f stderr.txt
}
function JsonEvmAsm_ImportExportEquivalence
{
local nth_input_file="$1"
IFS=" " read -r -a all_input_files <<< "$2"
local outputs=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" )
local _TESTED=1
if ! $SOLC --combined-json "$(IFS=, ; echo "${outputs[*]}")" --pretty-json --json-indent 4 "$nth_input_file" "${all_input_files[@]}" > expected.json 2> expected.error
then
printError
printError "$nth_input_file"
cat expected.error >&2
UNCOMPILABLE=$((UNCOMPILABLE + 1))
return 0
else
for contract in $(jq '.contracts | keys | .[]' expected.json 2> /dev/null)
do
for output in "${outputs[@]}"
do
jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}"
done
assembly=$(cat expected.asm)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
then
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
then
printError
printError "$nth_input_file"
cat obtained.error >&2
FAILED=$((FAILED + 1))
return 0
else
for output in "${outputs[@]}"
do
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
do
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}"
set +e
diff_files "expected.${output}" "obtained.${output}"
DIFF=$?
set -e
if [[ ${DIFF} != 0 ]]
then
_TESTED=
FAILED=$((FAILED + 1))
return 0
fi
done
done
# direct export via --asm-json, if imported with --import-asm-json.
if ! $SOLC --asm-json --import-asm-json expected.asm | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
then
printError
printError "$nth_input_file"
cat obtained_direct_import_export.error >&2
FAILED=$((FAILED + 1))
return 0
else
for obtained_contract in $(jq '.contracts | keys | .[]' obtained_direct_import_export.json 2> /dev/null)
do
jq --raw-output ".contracts.${obtained_contract}.\"asm\"" obtained_direct_import_export.json > obtained_direct_import_export.asm
set +e
diff_files "expected.asm" "obtained_direct_import_export.asm"
DIFF=$?
set -e
if [[ ${DIFF} != 0 ]]
then
_TESTED=
FAILED=$((FAILED + 1))
return 0
fi
done
fi
fi
fi
done
fi
if [ -n "${_TESTED}" ]
then
TESTED=$((TESTED + 1))
fi
}
# function tests whether exporting and importing again leaves the JSON ast unchanged
# Results are recorded by adding to FAILED or UNCOMPILABLE.
# Also, in case of a mismatch a diff and the respective ASTs are printed
@ -43,166 +168,16 @@ function testImportExportEquivalence {
if $SOLC --bin "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
then
! [[ -e stderr.txt ]] || { echo "stderr.txt already exists. Refusing to overwrite."; exit 1; }
! [[ -e stderr.txt ]] || { printError "stderr.txt already exists. Refusing to overwrite."; exit 1; }
if [ "${IMPORT_TEST_TYPE}" == "ast" ]
if [[ $IMPORT_TEST_TYPE == "ast" ]]
then
# save exported json as expected result (silently)
$SOLC --combined-json ast --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
# import it, and export it again as obtained result (silently)
if ! $SOLC --import-ast --combined-json ast --pretty-json expected.json > obtained.json 2> stderr.txt
then
# For investigating, use exit 1 here so the script stops at the
# first failing test
# exit 1
FAILED=$((FAILED + 1))
echo -e "ERROR: AST reimport failed for input file $nth_input_file"
echo
echo "Compiler stderr:"
cat ./stderr.txt
echo
echo "Compiler stdout:"
cat ./obtained.json
return 1
fi
set +e
DIFF="$(diff expected.json obtained.json)"
set +e
if [ "$DIFF" != "" ]
then
if [ "$DIFFVIEW" == "" ]
then
echo -e "ERROR: JSONS differ for $1: \n $DIFF \n"
echo "Expected:"
cat ./expected.json
echo "Obtained:"
cat ./obtained.json
else
# Use user supplied diff view binary
$DIFFVIEW expected.json obtained.json
fi
FAILED=$((FAILED + 1))
return 2
fi
TESTED=$((TESTED + 1))
rm expected.json obtained.json
rm -f stderr.txt
elif [ "${IMPORT_TEST_TYPE}" == "evm-assembly" ]
Ast_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then
local types=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" )
local _TESTED=1
if ! $SOLC --combined-json bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> expected.error
then
printf "\n"
echo "$nth_input_file"
cat expected.error
UNCOMPILABLE=$((UNCOMPILABLE + 1))
return 0
else
for contract in $(jq '.contracts | keys | .[]' expected.json 2> /dev/null)
do
for type in "${types[@]}"
do
jq --raw-output ".contracts.${contract}.\"${type}\"" expected.json > "expected.${type}"
done
assembly=$(cat expected.asm)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
then
if ! $SOLC --combined-json bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime --pretty-json --import-asm-json expected.asm > obtained.json 2> obtained.error
then
printf "\n"
echo "$nth_input_file"
cat obtained.error
FAILED=$((FAILED + 1))
return 0
else
for type in "${types[@]}"
do
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
do
jq --raw-output ".contracts.${obtained_contract}.\"${type}\"" obtained.json > "obtained.${type}"
set +e
DIFF="$(diff "expected.${type}" "obtained.${type}")"
set -e
if [ "$DIFF" != "" ]
then
if [ "$DIFFVIEW" == "" ]
then
echo -e "ERROR: JSONS differ for $1: \n $DIFF \n"
echo "Expected:"
cat "expected.${type}"
echo "Obtained:"
cat "obtained.${type}"
else
# Use user supplied diff view binary
$DIFFVIEW expected.json obtained.json
fi
_TESTED=
FAILED=$((FAILED + 1))
return 0
fi
done
done
# direct export via --asm-json, if imported with --import-asm-json.
if ! $SOLC --asm-json --import-asm-json expected.asm | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
then
printf "\n"
echo "$nth_input_file"
cat obtained_direct_import_export.error
FAILED=$((FAILED + 1))
return 0
else
for obtained_contract in $(jq '.contracts | keys | .[]' obtained_direct_import_export.json 2> /dev/null)
do
jq --raw-output ".contracts.${obtained_contract}.\"asm\"" obtained_direct_import_export.json > obtained_direct_import_export.asm
set +e
DIFF="$(diff expected.asm obtained_direct_import_export.asm)"
set -e
if [ "$DIFF" != "" ]
then
if [ "$DIFFVIEW" == "" ]
then
echo -e "ERROR: JSONS differ for $1: \n $DIFF \n"
echo "Expected:"
cat expected.asm
echo "Obtained:"
cat obtained_direct_import_export.asm
else
# Use user supplied diff view binary
$DIFFVIEW expected.asm obtained_direct_import_export.asm
fi
_TESTED=
FAILED=$((FAILED + 1))
return 0
fi
done
fi
rm obtained.json
rm -f obtained.error
for type in "${types[@]}"
do
rm "obtained.${type}"
done
fi
for type in "${types[@]}"
do
rm "expected.${type}"
done
fi
done
rm expected.json
fi
if [ -n "${_TESTED}" ]
then
TESTED=$((TESTED + 1))
fi
JsonEvmAsm_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}"
else
echo "unknown import test type. aborting."
exit 1
fail "unknown import test type. aborting."
fi
else
UNCOMPILABLE=$((UNCOMPILABLE + 1))
@ -212,35 +187,25 @@ function testImportExportEquivalence {
WORKINGDIR=$PWD
NSOURCES=0
# check whether SOLC works.
if ! $SOLC --version > /dev/null 2>&1
then
echo "$SOLC not found. aborting."
exit 1
fi
# check whether jq can be found.
if ! jq --version > /dev/null 2>&1
then
echo "jq needed. please install. aborting."
exit 1
fi
check_executable "$SOLC" --version
check_executable jq --version
# for solfile in $(find $DEV_DIR -name *.sol)
# 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" ]
if [[ $IMPORT_TEST_TYPE == "ast" ]]
then
IMPORT_TEST_FILES=$(find "${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
elif [ "${IMPORT_TEST_TYPE}" == "evm-assembly" ]
TEST_DIRS=("$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR")
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then
IMPORT_TEST_FILES=$(find "${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
else
echo "unknown import test type. aborting. please specify $0 [ast|evm-assembly]."
exit 1
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")
NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)"
echo "Looking at $NSOURCES .sol files..."
@ -257,7 +222,6 @@ do
set -e
if [ ${SPLITSOURCES_RC} == 0 ]
then
# echo $OUTPUT
NSOURCES=$((NSOURCES - 1))
for i in $OUTPUT;
do
@ -272,12 +236,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 -e "\n${OUTPUT}\n"
printError "\n${OUTPUT}\n"
testImportExportEquivalence "$solfile"
else
# All other return codes will be treated as critical errors. The script will exit.
echo -e "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
echo -e "\n${OUTPUT}\n"
printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
printError "\n${OUTPUT}\n"
cd "$WORKINGDIR"
# Delete temporary files
@ -297,6 +261,5 @@ if [ "$FAILED" = 0 ]
then
echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
else
echo "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
exit 1
fail "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
fi

View File

@ -205,6 +205,35 @@ function diff_values
diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
}
function diff_files
{
(( $# >= 2 )) || fail "diff_files requires at least 2 arguments."
local file1="$1"
local file2="$2"
shift
shift
diff "${file1}" "${file2}"
local res=$?
if [[ $res != 0 ]]
then
if [ "$DIFFVIEW" == "" ]
then
printError "ERROR: files differ: ${file1} vs. ${file2}"
printError "Expected:"
cat "${file1}" >&2
printError "Obtained:"
cat "${file2}" >&2
else
# Use user supplied diff view binary
"$DIFFVIEW" "${file1}" "${file2}"
fi
return 1
fi
return 0
}
function safe_kill
{
local PID=${1}
@ -277,3 +306,14 @@ function split_on_empty_lines_into_numbered_files
awk -v RS= "{print > (\"${path_prefix}_\"NR \"${path_suffix}\")}"
}
function check_executable
{
local program="$1"
local parameters=${*:2}
if ! "${program}" "${parameters}" > /dev/null 2>&1
then
fail "'${program}' not found or not executed successfully with parameter(s) '${parameters}'. aborting."
fi
}