Skip to content

Commit e7f6556

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

File tree

3 files changed

+174
-93
lines changed

3 files changed

+174
-93
lines changed

scripts/ASTImportTest.sh

Lines changed: 109 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
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+
22+
REDIRECT_TO="/dev/null"
23+
24+
set -euo pipefail
25+
26+
IMPORT_TEST_TYPE=
27+
for PARAM in "$@"
28+
do
29+
case "${PARAM}" in
30+
ast) IMPORT_TEST_TYPE="ast" ;;
31+
--show-errors) REDIRECT_TO="/dev/stderr"
32+
esac
33+
done
234

3-
set -e
35+
# Bash script to test the import/exports.
36+
# ast import/export tests:
37+
# - first exporting a .sol file to JSON, then loading it into the compiler
38+
# and exporting it again. The second JSON should be identical to the first.
439

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
840
READLINK=readlink
941
if [[ "$OSTYPE" == "darwin"* ]]; then
1042
READLINK=greadlink
@@ -14,90 +46,91 @@ SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
1446
SOLC=${SOLIDITY_BUILD_DIR}/solc/solc
1547
SPLITSOURCES=${REPO_ROOT}/scripts/splitSources.py
1648

49+
# shellcheck source=scripts/common.sh
50+
source "${REPO_ROOT}/scripts/common.sh"
51+
1752
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
1853
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
2354

2455
FAILED=0
2556
UNCOMPILABLE=0
2657
TESTED=0
2758

2859
if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
29-
echo "Test directory not empty. Skipping!"
30-
exit 1
60+
fail "Test directory not empty. Skipping!"
3161
fi
3262

33-
# function tests whether exporting and importing again leaves the JSON ast unchanged
63+
function ast_import_export_equivalence
64+
{
65+
local sol_file="$1"
66+
local input_files="$2"
67+
# save exported json as expected result (silently)
68+
$SOLC --combined-json ast --pretty-json --json-indent 4 "${input_files}" > expected.json 2> ${REDIRECT_TO}
69+
# import it, and export it again as obtained result (silently)
70+
if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
71+
then
72+
# For investigating, use exit 1 here so the script stops at the
73+
# first failing test
74+
# exit 1
75+
FAILED=$((FAILED + 1))
76+
printError "ERROR: AST reimport failed for input file $sol_file"
77+
printError
78+
printError "Compiler stderr:"
79+
cat ./stderr.txt >&2
80+
printError
81+
printError "Compiler stdout:"
82+
cat ./obtained.json >&2
83+
return 1
84+
fi
85+
if ! diff_files expected.json obtained.json
86+
then
87+
FAILED=$((FAILED + 1))
88+
fi
89+
TESTED=$((TESTED + 1))
90+
rm expected.json obtained.json
91+
rm -f stderr.txt
92+
}
93+
94+
# function tests whether exporting and importing again is equivalent.
3495
# Results are recorded by adding to FAILED or UNCOMPILABLE.
35-
# Also, in case of a mismatch a diff and the respective ASTs are printed
96+
# Also, in case of a mismatch a diff is printed
3697
# Expected parameters:
3798
# $1 name of the file to be exported and imported
3899
# $2 any files needed to do so that might be in parent directories
39100
function testImportExportEquivalence {
40-
local nth_input_file="$1"
41-
IFS=" " read -r -a all_input_files <<< "$2"
42-
43-
if $SOLC "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
101+
local sol_file="$1"
102+
local input_files="$2"
103+
if "$SOLC" --bin "${input_files}" > ${REDIRECT_TO} 2>&1
44104
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
105+
! [[ -e stderr.txt ]] || fail "stderr.txt already exists. Refusing to overwrite."
106+
case "$IMPORT_TEST_TYPE" in
107+
ast) ast_import_export_equivalence "${sol_file}" "${input_files}" ;;
108+
*) fail "Unknown import test type. Aborting." ;;
109+
esac
85110
else
86-
# echo "contract $solfile could not be compiled "
87111
UNCOMPILABLE=$((UNCOMPILABLE + 1))
88112
fi
89-
# return 0
90113
}
91-
echo "Looking at $NSOURCES .sol files..."
92114

93115
WORKINGDIR=$PWD
94116

95-
# for solfile in $(find $DEV_DIR -name *.sol)
117+
command_available "${SOLC}" --version
118+
command_available jq --version
119+
120+
case "$IMPORT_TEST_TYPE" in
121+
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
122+
*) fail "Unknown import test type. Aborting. Please specify ${0} ast [--show-errors]." ;;
123+
esac
124+
96125
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
97126
# 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")
127+
# tests on it. See https://github.com/boostorg/filesystem/issues/176
128+
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and -not -name "boost_filesystem_bug.sol")
129+
130+
NSOURCES="$(echo "$IMPORT_TEST_FILES" | wc -l)"
131+
echo "Looking at $NSOURCES .sol files..."
132+
133+
for solfile in ${IMPORT_TEST_FILES}
101134
do
102135
echo -n "."
103136
# create a temporary sub-directory
@@ -108,29 +141,25 @@ do
108141
OUTPUT=$("$SPLITSOURCES" "$solfile")
109142
SPLITSOURCES_RC=$?
110143
set -e
111-
if [ ${SPLITSOURCES_RC} == 0 ]
144+
if [[ ${SPLITSOURCES_RC} == 0 ]]
112145
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
146+
IFS=' ' read -ra OUTPUT_ARRAY <<< "${OUTPUT}"
147+
NSOURCES=$((NSOURCES - 1 + ${#OUTPUT_ARRAY[@]}))
148+
testImportExportEquivalence "$solfile" "${OUTPUT_ARRAY[*]}"
120149
elif [ ${SPLITSOURCES_RC} == 1 ]
121150
then
122-
testImportExportEquivalence "$solfile"
151+
testImportExportEquivalence "$solfile" "$solfile"
123152
elif [ ${SPLITSOURCES_RC} == 2 ]
124153
then
125154
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
126155
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
127156
# these errors, but print the actual output of the script.
128-
echo -e "\n${OUTPUT}\n"
129-
testImportExportEquivalence "$solfile"
157+
printError "\n\n${OUTPUT}\n\n"
158+
testImportExportEquivalence "$solfile" "$solfile"
130159
else
131160
# 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"
161+
printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
162+
printError "\n\n${OUTPUT}\n\n"
134163

135164
cd "$WORKINGDIR"
136165
# Delete temporary files
@@ -144,12 +173,11 @@ do
144173
rm -rf "$FILETMP"
145174
done
146175

147-
echo ""
176+
echo
148177

149-
if [ "$FAILED" = 0 ]
178+
if (( FAILED == 0 ))
150179
then
151-
echo "SUCCESS: $TESTED syntaxTests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
180+
echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
152181
else
153-
echo "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
154-
exit 1
182+
fail "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
155183
fi

scripts/common.sh

Lines changed: 63 additions & 10 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,6 +193,7 @@ function msg_on_error
193193
fi
194194
}
195195

196+
196197
function diff_values
197198
{
198199
(( $# >= 2 )) || fail "diff_values requires at least 2 arguments."
@@ -202,7 +203,49 @@ function diff_values
202203
shift
203204
shift
204205

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

208251
function safe_kill
@@ -277,3 +320,13 @@ function split_on_empty_lines_into_numbered_files
277320

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

test/cmdlineTests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,11 @@ SOLTMPDIR=$(mktemp -d)
662662
)
663663
rm -r "$SOLTMPDIR"
664664

665-
printTask "Testing AST import..."
665+
printTask "Testing AST import/export..."
666666
SOLTMPDIR=$(mktemp -d)
667667
(
668668
cd "$SOLTMPDIR"
669-
if ! "$REPO_ROOT/scripts/ASTImportTest.sh"
669+
if ! "$REPO_ROOT/scripts/ASTImportTest.sh" ast
670670
then
671671
rm -r "$SOLTMPDIR"
672672
fail

0 commit comments

Comments
 (0)