Skip to content

Commit 0c2ee2a

Browse files
committed
[tests] Refactor scripts/ASTImportTest.sh.
1 parent 2201526 commit 0c2ee2a

File tree

3 files changed

+205
-108
lines changed

3 files changed

+205
-108
lines changed

scripts/ASTImportTest.sh

Lines changed: 141 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,161 @@
11
#!/usr/bin/env bash
2+
# ------------------------------------------------------------------------------
3+
# vim:ts=4:et
4+
# This file is part of solidity.
5+
#
6+
# solidity is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# solidity is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with solidity. If not, see <http://www.gnu.org/licenses/>
18+
#
19+
# (c) solidity contributors.
20+
# ------------------------------------------------------------------------------
21+
# Bash script to test the import/exports.
22+
# ast import/export tests:
23+
# - first exporting a .sol file to JSON, then loading it into the compiler
24+
# and exporting it again. The second JSON should be identical to the first.
25+
26+
set -euo pipefail
227

3-
set -e
4-
5-
# Bash script to test the ast-import option of the compiler by
6-
# first exporting a .sol file to JSON, then loading it into the compiler
7-
# and exporting it again. The second JSON should be identical to the first
828
READLINK=readlink
929
if [[ "$OSTYPE" == "darwin"* ]]; then
1030
READLINK=greadlink
1131
fi
1232
REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..)
1333
SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
14-
SOLC=${SOLIDITY_BUILD_DIR}/solc/solc
15-
SPLITSOURCES=${REPO_ROOT}/scripts/splitSources.py
34+
SOLC=$SOLIDITY_BUILD_DIR/solc/solc
35+
SPLITSOURCES=$REPO_ROOT/scripts/splitSources.py
36+
37+
# shellcheck source=scripts/common.sh
38+
source "${REPO_ROOT}/scripts/common.sh"
39+
40+
function print_usage
41+
{
42+
fail "Usage: ${0} ast [--exit-on-error]."
43+
}
44+
45+
IMPORT_TEST_TYPE=
46+
EXIT_ON_ERROR=0
47+
for PARAM in "$@"
48+
do
49+
case "$PARAM" in
50+
ast) IMPORT_TEST_TYPE="ast" ;;
51+
--exit-on-error) EXIT_ON_ERROR=1 ;;
52+
*) print_usage ;;
53+
esac
54+
done
1655

1756
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
1857
ASTJSONTESTS_DIR="${REPO_ROOT}/test/libsolidity/ASTJSON"
19-
NSOURCES="$(find "$SYNTAXTESTS_DIR" -type f | wc -l)"
20-
21-
# DEV_DIR="${REPO_ROOT}/../tmp/contracts/"
22-
# NSOURCES="$(find $DEV_DIR -type f | wc -l)" #TODO use find command
2358

2459
FAILED=0
2560
UNCOMPILABLE=0
2661
TESTED=0
2762

28-
if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
29-
echo "Test directory not empty. Skipping!"
30-
exit 1
31-
fi
63+
function ast_import_export_equivalence
64+
{
65+
local sol_file="$1"
66+
local input_files=( "${@:2}" )
3267

33-
# function tests whether exporting and importing again leaves the JSON ast unchanged
68+
# save ast json as expected result (silently)
69+
if ! $SOLC --combined-json ast --pretty-json --json-indent 4 "${input_files[@]}" > expected.json 2> stderr_export.txt
70+
then
71+
printError "ERROR: AST export failed for input file $sol_file."
72+
printError ""
73+
printError "Compiler stderr:"
74+
cat ./stderr_export.txt >&2
75+
printError ""
76+
printError "Compiler stdout:"
77+
cat ./expected.json >&2
78+
return 1
79+
fi
80+
81+
# import it, and export it again as obtained result (silently)
82+
if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr_import.txt
83+
then
84+
printError "ERROR: AST import failed for input file $sol_file"
85+
printError ""
86+
printError "Compiler stderr:"
87+
cat ./stderr_import.txt >&2
88+
printError ""
89+
printError "Compiler stdout:"
90+
cat ./obtained.json >&2
91+
return 1
92+
fi
93+
94+
if diff_files expected.json obtained.json
95+
then
96+
echo -n ""
97+
else
98+
printError "❌ ERROR: AST reimport failed for ${sol_file}"
99+
if [[ $EXIT_ON_ERROR == 1 ]]
100+
then
101+
printError "You can find the files used for this test here: $(pwd)"
102+
printError "Used commands for test:"
103+
printError "# export"
104+
printError "$ solc --combined-json ast --pretty-json --json-indent 4 ${input_files[*]} > expected.json 2> stderr_export.txt"
105+
printError "# import"
106+
printError "$ solc --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr_import.txt"
107+
# to simplify potential problem investigations - copy used solidity sources to test directory.
108+
cp "${input_files[@]}" "$(pwd)"
109+
exit 1
110+
fi
111+
FAILED=$((FAILED + 1))
112+
fi
113+
TESTED=$((TESTED + 1))
114+
}
115+
116+
# function tests whether exporting and importing again is equivalent.
34117
# Results are recorded by adding to FAILED or UNCOMPILABLE.
35-
# Also, in case of a mismatch a diff and the respective ASTs are printed
118+
# Also, in case of a mismatch a diff is printed
36119
# Expected parameters:
37120
# $1 name of the file to be exported and imported
38121
# $2 any files needed to do so that might be in parent directories
39122
function testImportExportEquivalence {
40-
local nth_input_file="$1"
41-
IFS=" " read -r -a all_input_files <<< "$2"
123+
local sol_file="$1"
124+
local input_files=( "${@:2}" )
42125

43-
if $SOLC "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
126+
# if compilable
127+
if "$SOLC" --bin "${input_files[@]}" > /dev/null 2>&1
44128
then
45-
! [[ -e stderr.txt ]] || { echo "stderr.txt already exists. Refusing to overwrite."; exit 1; }
46-
47-
# save exported json as expected result (silently)
48-
$SOLC --combined-json ast --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
49-
# import it, and export it again as obtained result (silently)
50-
if ! $SOLC --import-ast --combined-json ast --pretty-json expected.json > obtained.json 2> stderr.txt
51-
then
52-
# For investigating, use exit 1 here so the script stops at the
53-
# first failing test
54-
# exit 1
55-
FAILED=$((FAILED + 1))
56-
echo -e "ERROR: AST reimport failed for input file $nth_input_file"
57-
echo
58-
echo "Compiler stderr:"
59-
cat ./stderr.txt
60-
echo
61-
echo "Compiler stdout:"
62-
cat ./obtained.json
63-
return 1
64-
fi
65-
DIFF="$(diff expected.json obtained.json)"
66-
if [ "$DIFF" != "" ]
67-
then
68-
if [ "$DIFFVIEW" == "" ]
69-
then
70-
echo -e "ERROR: JSONS differ for $1: \n $DIFF \n"
71-
echo "Expected:"
72-
cat ./expected.json
73-
echo "Obtained:"
74-
cat ./obtained.json
75-
else
76-
# Use user supplied diff view binary
77-
$DIFFVIEW expected.json obtained.json
78-
fi
79-
FAILED=$((FAILED + 1))
80-
return 2
81-
fi
82-
TESTED=$((TESTED + 1))
83-
rm expected.json obtained.json
84-
rm -f stderr.txt
129+
case "$IMPORT_TEST_TYPE" in
130+
ast) ast_import_export_equivalence "${sol_file}" "${input_files[@]}" ;;
131+
*) fail "Unknown import test type '${IMPORT_TEST_TYPE}'. Aborting." ;;
132+
esac
85133
else
86-
# echo "contract $solfile could not be compiled "
87134
UNCOMPILABLE=$((UNCOMPILABLE + 1))
88135
fi
89-
# return 0
90136
}
91-
echo "Looking at $NSOURCES .sol files..."
92137

93138
WORKINGDIR=$PWD
94139

95-
# for solfile in $(find $DEV_DIR -name *.sol)
140+
command_available "$SOLC" --version
141+
command_available jq --version
142+
143+
case "$IMPORT_TEST_TYPE" in
144+
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
145+
*) print_usage ;;
146+
esac
147+
96148
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
97149
# bug. Since the test involves a malformed path, there is no point in running
98-
# AST tests on it. See https://github.com/boostorg/filesystem/issues/176
99-
# shellcheck disable=SC2044
100-
for solfile in $(find "$SYNTAXTESTS_DIR" "$ASTJSONTESTS_DIR" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
150+
# tests on it. See https://github.com/boostorg/filesystem/issues/176
151+
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
152+
153+
NSOURCES="$(echo "${IMPORT_TEST_FILES}" | wc -l)"
154+
echo "Looking at ${NSOURCES} .sol files..."
155+
156+
for solfile in $IMPORT_TEST_FILES
101157
do
102-
echo -n "."
158+
echo -n "·"
103159
# create a temporary sub-directory
104160
FILETMP=$(mktemp -d)
105161
cd "$FILETMP"
@@ -108,48 +164,39 @@ do
108164
OUTPUT=$("$SPLITSOURCES" "$solfile")
109165
SPLITSOURCES_RC=$?
110166
set -e
111-
if [ ${SPLITSOURCES_RC} == 0 ]
167+
168+
if [[ $SPLITSOURCES_RC == 0 ]]
112169
then
113-
# echo $OUTPUT
114-
NSOURCES=$((NSOURCES - 1))
115-
for i in $OUTPUT;
116-
do
117-
testImportExportEquivalence "$i" "$OUTPUT"
118-
NSOURCES=$((NSOURCES + 1))
119-
done
120-
elif [ ${SPLITSOURCES_RC} == 1 ]
170+
IFS=' ' read -ra OUTPUT_ARRAY <<< "$OUTPUT"
171+
testImportExportEquivalence "$solfile" "${OUTPUT_ARRAY[@]}"
172+
elif [ $SPLITSOURCES_RC == 1 ]
121173
then
122-
testImportExportEquivalence "$solfile"
123-
elif [ ${SPLITSOURCES_RC} == 2 ]
174+
testImportExportEquivalence "$solfile" "$solfile"
175+
elif [ $SPLITSOURCES_RC == 2 ]
124176
then
125177
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
126178
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
127179
# these errors, but print the actual output of the script.
128-
echo -e "\n${OUTPUT}\n"
129-
testImportExportEquivalence "$solfile"
180+
printError "\n\n${OUTPUT}\n\n"
181+
testImportExportEquivalence "$solfile" "$solfile"
130182
else
131183
# All other return codes will be treated as critical errors. The script will exit.
132-
echo -e "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
133-
echo -e "\n${OUTPUT}\n"
134-
135-
cd "$WORKINGDIR"
136-
# Delete temporary files
137-
rm -rf "$FILETMP"
184+
printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
185+
printError "\n\n${OUTPUT}\n\n"
138186

139187
exit 1
140188
fi
141189

142-
cd "$WORKINGDIR"
190+
cd "${WORKINGDIR}"
143191
# Delete temporary files
144-
rm -rf "$FILETMP"
192+
rm -rf "${FILETMP}"
145193
done
146194

147-
echo ""
195+
echo
148196

149-
if [ "$FAILED" = 0 ]
197+
if (( FAILED == 0 ))
150198
then
151-
echo "SUCCESS: $TESTED syntaxTests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
199+
echo "SUCCESS: ${TESTED} tests passed, ${FAILED} failed, ${UNCOMPILABLE} could not be compiled (${NSOURCES} sources total)."
152200
else
153-
echo "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
154-
exit 1
201+
fail "FAILURE: Out of ${NSOURCES} sources, ${FAILED} failed, (${UNCOMPILABLE} could not be compiled)."
155202
fi

scripts/common.sh

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ set -e
2626
# changes directory. The paths returned by `caller` are relative to it.
2727
_initial_work_dir=$(pwd)
2828

29-
if [ "$CIRCLECI" ]
29+
if [[ ${CIRCLECI:-} != "" ]]
3030
then
3131
export TERM="${TERM:-xterm}"
32-
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
33-
function printError { >&2 echo "$(tput setaf 1)$1$(tput setaf 7)"; }
34-
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput setaf 7)"; }
35-
function printLog { echo "$(tput setaf 3)$1$(tput setaf 7)"; }
32+
function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; }
33+
function printError { >&2 echo -e "$(tput setaf 1)$1$(tput setaf 7)"; }
34+
function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput setaf 7)"; }
35+
function printLog { echo -e "$(tput setaf 3)$1$(tput setaf 7)"; }
3636
else
37-
function printTask { echo "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; }
38-
function printError { >&2 echo "$(tput setaf 1)$1$(tput sgr0)"; }
39-
function printWarning { >&2 echo "$(tput setaf 11)$1$(tput sgr0)"; }
40-
function printLog { echo "$(tput setaf 3)$1$(tput sgr0)"; }
37+
function printTask { echo -e "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; }
38+
function printError { >&2 echo -e "$(tput setaf 1)$1$(tput sgr0)"; }
39+
function printWarning { >&2 echo -e "$(tput setaf 11)$1$(tput sgr0)"; }
40+
function printLog { echo -e "$(tput setaf 3)$1$(tput sgr0)"; }
4141
fi
4242

4343
function checkDputEntries
@@ -193,16 +193,56 @@ function msg_on_error
193193
fi
194194
}
195195

196+
196197
function diff_values
197198
{
198199
(( $# >= 2 )) || fail "diff_values requires at least 2 arguments."
199200

200201
local value1="$1"
201202
local value2="$2"
202-
shift
203-
shift
203+
shift 2
204204

205-
diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
205+
if ! diff --unified=0 <(echo "$value1") <(echo "$value2") "$@"
206+
then
207+
if [ "${DIFFVIEW:-}" == "" ]
208+
then
209+
printError "ERROR: values differ:"
210+
printError "Expected:"
211+
printError "${value1}"
212+
printError "Obtained:"
213+
printError "${value2}"
214+
else
215+
# Use user supplied diff view binary
216+
printError "ERROR: values differ."
217+
"$DIFFVIEW" "${file1}" "${file2}"
218+
fi
219+
return 1
220+
fi
221+
}
222+
223+
function diff_files
224+
{
225+
(( $# >= 2 )) || fail "diff_files requires at least 2 arguments."
226+
227+
local file1="$1"
228+
local file2="$2"
229+
230+
if ! diff "${file1}" "${file2}"
231+
then
232+
if [ "${DIFFVIEW:-}" == "" ]
233+
then
234+
printError "ERROR: files differ: ${file1} vs. ${file2}"
235+
printError "Expected:"
236+
cat "${file1}" >&2
237+
printError "Obtained:"
238+
cat "${file2}" >&2
239+
else
240+
# Use user supplied diff view binary
241+
"$DIFFVIEW" "${file1}" "${file2}"
242+
fi
243+
return 1
244+
fi
245+
return 0
206246
}
207247

208248
function safe_kill
@@ -277,3 +317,13 @@ function split_on_empty_lines_into_numbered_files
277317

278318
awk -v RS= "{print > (\"${path_prefix}_\"NR \"${path_suffix}\")}"
279319
}
320+
321+
function command_available
322+
{
323+
local program="$1"
324+
local parameters=${*:2}
325+
if ! "${program}" "${parameters}" > /dev/null 2>&1
326+
then
327+
fail "'${program}' not found or not executed successfully with parameter(s) '${parameters}'. aborting."
328+
fi
329+
}

0 commit comments

Comments
 (0)