Refactorings.

This commit is contained in:
Alexander Arlt 2022-03-31 16:56:21 -05:00
parent 3e2b62ab89
commit d4c8c9947d
2 changed files with 102 additions and 87 deletions

View File

@ -1,4 +1,23 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ------------------------------------------------------------------------------
# vim:ts=4:et
# This file is part of solidity.
#
# solidity is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# solidity is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with solidity. If not, see <http://www.gnu.org/licenses/>
#
# (c) solidity contributors.
# ------------------------------------------------------------------------------
set -euo pipefail set -euo pipefail
IMPORT_TEST_TYPE="${1}" IMPORT_TEST_TYPE="${1}"
@ -36,11 +55,10 @@ fi
function Ast_ImportExportEquivalence function Ast_ImportExportEquivalence
{ {
local nth_input_file="$1" local sol_file="$1"
IFS=" " read -r -a all_input_files <<< "$2" local input_files="$2"
# save exported json as expected result (silently) # 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 $SOLC --combined-json ast --pretty-json --json-indent 4 "${input_files}" > expected.json 2> /dev/null
# import it, and export it again as obtained result (silently) # 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 if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
then then
@ -48,7 +66,7 @@ function Ast_ImportExportEquivalence
# first failing test # first failing test
# exit 1 # exit 1
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
printError -e "ERROR: AST reimport failed for input file $nth_input_file" printError -e "ERROR: AST reimport failed for input file $sol_file"
printError printError
printError "Compiler stderr:" printError "Compiler stderr:"
cat ./stderr.txt >&2 cat ./stderr.txt >&2
@ -60,11 +78,10 @@ function Ast_ImportExportEquivalence
set +e set +e
diff_files expected.json obtained.json diff_files expected.json obtained.json
DIFF=$? DIFF=$?
set -euo pipefail set -e
if [[ ${DIFF} != 0 ]] if [[ ${DIFF} != 0 ]]
then then
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
return 2
fi fi
TESTED=$((TESTED + 1)) TESTED=$((TESTED + 1))
rm expected.json obtained.json rm expected.json obtained.json
@ -73,15 +90,14 @@ function Ast_ImportExportEquivalence
function JsonEvmAsm_ImportExportEquivalence function JsonEvmAsm_ImportExportEquivalence
{ {
local nth_input_file="$1" local sol_file="$1"
IFS=" " read -r -a all_input_files <<< "$2" local input_files="$2"
local outputs=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" ) local outputs=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" )
local _TESTED=1 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 if ! "${SOLC}" --combined-json "$(IFS=, ; echo "${outputs[*]}")" --pretty-json --json-indent 4 "${input_files}" > expected.json 2> expected.error
then then
printError printError
printError "$nth_input_file" printError "$sol_file"
cat expected.error >&2 cat expected.error >&2
UNCOMPILABLE=$((UNCOMPILABLE + 1)) UNCOMPILABLE=$((UNCOMPILABLE + 1))
return 0 return 0
@ -90,16 +106,16 @@ function JsonEvmAsm_ImportExportEquivalence
do do
for output in "${outputs[@]}" for output in "${outputs[@]}"
do do
jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}" jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}.json"
done done
assembly=$(cat expected.asm) assembly=$(cat expected.asm.json)
if [ "$assembly" != "" ] && [ "$assembly" != "null" ] if [ "$assembly" != "" ] && [ "$assembly" != "null" ]
then 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 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
printError "$nth_input_file" printError "$sol_file"
cat obtained.error >&2 cat obtained.error >&2
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
return 0 return 0
@ -108,12 +124,8 @@ function JsonEvmAsm_ImportExportEquivalence
do do
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null) for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
do do
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}" jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
set +e if ! diff_files "expected.${output}.json" "obtained.${output}.json"
diff_files "expected.${output}" "obtained.${output}"
DIFF=$?
set -euo pipefail
if [[ ${DIFF} != 0 ]]
then then
_TESTED= _TESTED=
FAILED=$((FAILED + 1)) FAILED=$((FAILED + 1))
@ -123,29 +135,25 @@ function JsonEvmAsm_ImportExportEquivalence
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 | 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 "$nth_input_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
else else
for obtained_contract in $(jq '.contracts | keys | .[]' obtained_direct_import_export.json 2> /dev/null) # reformat jsons using jq.
do jq . expected.asm.json > expected.asm.json.pretty
jq --raw-output ".contracts.${obtained_contract}.\"asm\"" obtained_direct_import_export.json > obtained_direct_import_export.asm jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
set +e if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
diff_files "expected.asm" "obtained_direct_import_export.asm" then
DIFF=$? _TESTED=
set -euo pipefail FAILED=$((FAILED + 1))
if [[ ${DIFF} != 0 ]] return 0
then fi
_TESTED=
FAILED=$((FAILED + 1))
return 0
fi
done
fi fi
fi fi
fi fi
done done
@ -163,26 +171,20 @@ function JsonEvmAsm_ImportExportEquivalence
# $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 nth_input_file="$1" local sol_file="$1"
IFS=" " read -r -a all_input_files <<< "$2" local input_files="$2"
if $SOLC --bin "${input_files}" > /dev/null 2>&1
if [ -z ${all_input_files+x} ]
then then
all_input_files=( "" ) ! [[ -e stderr.txt ]] || { fail "stderr.txt already exists. Refusing to overwrite."; }
fi
if $SOLC --bin "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
then
! [[ -e stderr.txt ]] || { printError "stderr.txt already exists. Refusing to overwrite."; exit 1; }
if [[ $IMPORT_TEST_TYPE == "ast" ]] if [[ $IMPORT_TEST_TYPE == "ast" ]]
then then
Ast_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}" Ast_ImportExportEquivalence "${sol_file}" "${input_files}"
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]] elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then then
JsonEvmAsm_ImportExportEquivalence "$nth_input_file" "${all_input_files[@]}" JsonEvmAsm_ImportExportEquivalence "${sol_file}" "${input_files}"
else else
fail "unknown import test type. aborting." fail "Unknown import test type. Aborting."
fi fi
else else
UNCOMPILABLE=$((UNCOMPILABLE + 1)) UNCOMPILABLE=$((UNCOMPILABLE + 1))
@ -192,8 +194,8 @@ function testImportExportEquivalence {
WORKINGDIR=$PWD WORKINGDIR=$PWD
NSOURCES=0 NSOURCES=0
check_executable "$SOLC" --version command_available "${SOLC}" --version
check_executable jq --version command_available jq --version
# for solfile in $(find $DEV_DIR -name *.sol) # for solfile in $(find $DEV_DIR -name *.sol)
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem # boost_filesystem_bug specifically tests a local fix for a boost::filesystem
@ -201,7 +203,7 @@ check_executable jq --version
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176 # AST tests on it. See https://github.com/boostorg/filesystem/issues/176
if [[ $IMPORT_TEST_TYPE == "ast" ]] if [[ $IMPORT_TEST_TYPE == "ast" ]]
then then
TEST_DIRS=("$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR") TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}")
elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]] elif [[ $IMPORT_TEST_TYPE == "evm-assembly" ]]
then then
TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}") TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}")
@ -224,29 +226,33 @@ do
set +e set +e
OUTPUT=$("$SPLITSOURCES" "$solfile") OUTPUT=$("$SPLITSOURCES" "$solfile")
SPLITSOURCES_RC=$? SPLITSOURCES_RC=$?
set -euo pipefail set -e
if [ ${SPLITSOURCES_RC} == 0 ] if [ ${SPLITSOURCES_RC} == 0 ]
then then
NSOURCES=$((NSOURCES - 1)) OIFS=${IFS}
for i in $OUTPUT; IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
do IFS=${OIFS}
testImportExportEquivalence "$i" "$OUTPUT" NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
NSOURCES=$((NSOURCES + 1)) testImportExportEquivalence "$solfile" "${OUTPUT[@]}"
done
elif [ ${SPLITSOURCES_RC} == 1 ] elif [ ${SPLITSOURCES_RC} == 1 ]
then then
testImportExportEquivalence "$solfile" "" testImportExportEquivalence "$solfile" "$solfile"
elif [ ${SPLITSOURCES_RC} == 2 ] elif [ ${SPLITSOURCES_RC} == 2 ]
then then
# 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.
printError "\n${OUTPUT}\n" echo >&2
testImportExportEquivalence "$solfile" "" printError "\n${OUTPUT[*]}\n"
echo >&2
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 "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting." printError "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
printError "\n${OUTPUT}\n" echo >&2
printError "\n${OUTPUT[*]}\n"
echo >&2
cd "$WORKINGDIR" cd "$WORKINGDIR"
# Delete temporary files # Delete temporary files
@ -260,9 +266,9 @@ do
rm -rf "$FILETMP" rm -rf "$FILETMP"
done done
echo "" echo
if [ "$FAILED" = 0 ] if (( FAILED == 0 ))
then then
echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)." echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
else else

View File

@ -26,23 +26,18 @@ set -e
# changes directory. The paths returned by `caller` are relative to it. # changes directory. The paths returned by `caller` are relative to it.
_initial_work_dir=$(pwd) _initial_work_dir=$(pwd)
if [ -z ${CIRCLECI+x} ] if [[ ${CIRCLECI:-} != "" ]]
then
CIRCLECI=0
fi
if [ "$CIRCLECI" ]
then then
export TERM="${TERM:-xterm}" export TERM="${TERM:-xterm}"
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; } function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
function printError { >&2 echo "$(tput setaf 1)$1$(tput setaf 7)"; } function printError { >&2 echo -e "$(tput setaf 1)$1$(tput setaf 7)"; }
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput setaf 7)"; } function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput setaf 7)"; }
function printLog { echo "$(tput setaf 3)$1$(tput setaf 7)"; } function printLog { echo -e "$(tput setaf 3)$1$(tput setaf 7)"; }
else else
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; } function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; }
function printError { >&2 echo "$(tput setaf 1)$1$(tput sgr0)"; } function printError { >&2 echo -e "$(tput setaf 1)$1$(tput sgr0)"; }
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput sgr0)"; } function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput sgr0)"; }
function printLog { echo "$(tput setaf 3)$1$(tput sgr0)"; } function printLog { echo -e "$(tput setaf 3)$1$(tput sgr0)"; }
fi fi
function checkDputEntries function checkDputEntries
@ -198,6 +193,7 @@ function msg_on_error
fi fi
} }
function diff_values function diff_values
{ {
(( $# >= 2 )) || fail "diff_values requires at least 2 arguments." (( $# >= 2 )) || fail "diff_values requires at least 2 arguments."
@ -207,7 +203,22 @@ function diff_values
shift shift
shift shift
diff --unified=0 <(echo "$value1") <(echo "$value2") "$@" if ! diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
then
if [ "${DIFFVIEW:-}" == "" ]
then
printError "ERROR: values differ:"
printError "Expected:"
printError "${value1}"
printError "Obtained:"
printError "${value2}"
else
# Use user supplied diff view binary
printError "ERROR: values differ."
"$DIFFVIEW" "${file1}" "${file2}"
fi
return 1
fi
} }
function diff_files function diff_files
@ -219,11 +230,9 @@ function diff_files
shift shift
shift shift
diff "${file1}" "${file2}" if ! diff "${file1}" "${file2}"
local res=$?
if [[ $res != 0 ]]
then then
if [ "$DIFFVIEW" == "" ] if [ "${DIFFVIEW:-}" == "" ]
then then
printError "ERROR: files differ: ${file1} vs. ${file2}" printError "ERROR: files differ: ${file1} vs. ${file2}"
printError "Expected:" printError "Expected:"
@ -312,7 +321,7 @@ function split_on_empty_lines_into_numbered_files
awk -v RS= "{print > (\"${path_prefix}_\"NR \"${path_suffix}\")}" awk -v RS= "{print > (\"${path_prefix}_\"NR \"${path_suffix}\")}"
} }
function check_executable function command_available
{ {
local program="$1" local program="$1"
local parameters=${*:2} local parameters=${*:2}