2019-09-11 19:16:35 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
set -e
|
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
# 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
|
2020-09-01 00:04:38 +00:00
|
|
|
READLINK=readlink
|
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
|
READLINK=greadlink
|
|
|
|
fi
|
|
|
|
REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..)
|
2020-04-17 13:02:40 +00:00
|
|
|
SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
|
|
|
|
SOLC=${SOLIDITY_BUILD_DIR}/solc/solc
|
2019-09-11 19:16:35 +00:00
|
|
|
SPLITSOURCES=${REPO_ROOT}/scripts/splitSources.py
|
|
|
|
|
|
|
|
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
|
2020-03-05 11:56:14 +00:00
|
|
|
ASTJSONTESTS_DIR="${REPO_ROOT}/test/libsolidity/ASTJSON"
|
2020-12-11 17:19:53 +00:00
|
|
|
NSOURCES="$(find "$SYNTAXTESTS_DIR" -type f | wc -l)"
|
2019-09-11 19:16:35 +00:00
|
|
|
|
|
|
|
# DEV_DIR="${REPO_ROOT}/../tmp/contracts/"
|
|
|
|
# NSOURCES="$(find $DEV_DIR -type f | wc -l)" #TODO use find command
|
|
|
|
|
|
|
|
FAILED=0
|
|
|
|
UNCOMPILABLE=0
|
|
|
|
TESTED=0
|
|
|
|
|
2020-12-12 02:44:54 +00:00
|
|
|
if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
|
2020-04-17 12:32:38 +00:00
|
|
|
echo "Test directory not empty. Skipping!"
|
2020-12-12 00:26:11 +00:00
|
|
|
exit 1
|
2019-09-11 19:16:35 +00:00
|
|
|
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
|
|
|
|
# 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 {
|
2020-12-11 20:34:55 +00:00
|
|
|
local nth_input_file="$1"
|
|
|
|
IFS=" " read -r -a all_input_files <<< "$2"
|
|
|
|
|
|
|
|
if $SOLC "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2021-09-22 09:19:15 +00:00
|
|
|
! [[ -e stderr.txt ]] || { echo "stderr.txt already exists. Refusing to overwrite."; exit 1; }
|
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
# save exported json as expected result (silently)
|
2021-09-30 14:17:36 +00:00
|
|
|
$SOLC --combined-json ast --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
|
2019-09-11 19:16:35 +00:00
|
|
|
# import it, and export it again as obtained result (silently)
|
2021-09-30 14:17:36 +00:00
|
|
|
if ! $SOLC --import-ast --combined-json ast --pretty-json expected.json > obtained.json 2> stderr.txt
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2020-04-17 12:32:38 +00:00
|
|
|
# For investigating, use exit 1 here so the script stops at the
|
|
|
|
# first failing test
|
|
|
|
# exit 1
|
|
|
|
FAILED=$((FAILED + 1))
|
2021-09-22 09:19:15 +00:00
|
|
|
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
|
2020-04-17 12:32:38 +00:00
|
|
|
return 1
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|
|
|
|
DIFF="$(diff expected.json obtained.json)"
|
|
|
|
if [ "$DIFF" != "" ]
|
|
|
|
then
|
|
|
|
if [ "$DIFFVIEW" == "" ]
|
|
|
|
then
|
|
|
|
echo -e "ERROR: JSONS differ for $1: \n $DIFF \n"
|
|
|
|
echo "Expected:"
|
2020-12-12 00:28:00 +00:00
|
|
|
cat ./expected.json
|
2019-09-11 19:16:35 +00:00
|
|
|
echo "Obtained:"
|
2020-12-12 00:28:00 +00:00
|
|
|
cat ./obtained.json
|
2019-09-11 19:16:35 +00:00
|
|
|
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
|
2021-09-22 09:19:15 +00:00
|
|
|
rm -f stderr.txt
|
2019-09-11 19:16:35 +00:00
|
|
|
else
|
|
|
|
# echo "contract $solfile could not be compiled "
|
|
|
|
UNCOMPILABLE=$((UNCOMPILABLE + 1))
|
|
|
|
fi
|
|
|
|
# return 0
|
|
|
|
}
|
|
|
|
echo "Looking at $NSOURCES .sol files..."
|
|
|
|
|
|
|
|
WORKINGDIR=$PWD
|
|
|
|
|
|
|
|
# for solfile in $(find $DEV_DIR -name *.sol)
|
2021-02-03 14:54:45 +00:00
|
|
|
# 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
|
2020-12-12 02:44:54 +00:00
|
|
|
# shellcheck disable=SC2044
|
|
|
|
for solfile in $(find "$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
|
2019-09-11 19:16:35 +00:00
|
|
|
do
|
|
|
|
echo -n "."
|
|
|
|
# create a temporary sub-directory
|
2020-02-14 11:28:55 +00:00
|
|
|
FILETMP=$(mktemp -d)
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$FILETMP"
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
set +e
|
2020-12-11 17:19:53 +00:00
|
|
|
OUTPUT=$("$SPLITSOURCES" "$solfile")
|
2020-09-01 00:04:38 +00:00
|
|
|
SPLITSOURCES_RC=$?
|
|
|
|
set -e
|
|
|
|
if [ ${SPLITSOURCES_RC} == 0 ]
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
|
|
|
# echo $OUTPUT
|
|
|
|
NSOURCES=$((NSOURCES - 1))
|
|
|
|
for i in $OUTPUT;
|
|
|
|
do
|
2020-12-12 00:23:27 +00:00
|
|
|
testImportExportEquivalence "$i" "$OUTPUT"
|
2019-09-11 19:16:35 +00:00
|
|
|
NSOURCES=$((NSOURCES + 1))
|
|
|
|
done
|
2020-09-01 00:04:38 +00:00
|
|
|
elif [ ${SPLITSOURCES_RC} == 1 ]
|
|
|
|
then
|
2020-12-11 17:19:53 +00:00
|
|
|
testImportExportEquivalence "$solfile"
|
2020-09-01 00:04:38 +00:00
|
|
|
elif [ ${SPLITSOURCES_RC} == 2 ]
|
|
|
|
then
|
|
|
|
# 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"
|
2020-12-11 17:19:53 +00:00
|
|
|
testImportExportEquivalence "$solfile"
|
2020-09-01 00:04:38 +00:00
|
|
|
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"
|
|
|
|
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$WORKINGDIR"
|
2020-09-01 00:04:38 +00:00
|
|
|
# Delete temporary files
|
2020-12-11 17:19:53 +00:00
|
|
|
rm -rf "$FILETMP"
|
2020-09-01 00:04:38 +00:00
|
|
|
|
|
|
|
exit 1
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$WORKINGDIR"
|
2019-09-11 19:16:35 +00:00
|
|
|
# Delete temporary files
|
2020-12-11 17:19:53 +00:00
|
|
|
rm -rf "$FILETMP"
|
2019-09-11 19:16:35 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
if [ "$FAILED" = 0 ]
|
|
|
|
then
|
|
|
|
echo "SUCCESS: $TESTED syntaxTests 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
|
|
|
|
fi
|