1
1
#! /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
+ set -euo pipefail
23
+
24
+ REDIRECT_TO=" /dev/null"
25
+ IMPORT_TEST_TYPE=
26
+ for PARAM in " $@ "
27
+ do
28
+ case " ${PARAM} " in
29
+ ast) IMPORT_TEST_TYPE=" ast" ;;
30
+ --show-errors) REDIRECT_TO=" /dev/stderr"
31
+ esac
32
+ done
2
33
3
- set -e
34
+ # Bash script to test the import/exports.
35
+ # ast import/export tests:
36
+ # - first exporting a .sol file to JSON, then loading it into the compiler
37
+ # and exporting it again. The second JSON should be identical to the first.
4
38
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
8
39
READLINK=readlink
9
40
if [[ " $OSTYPE " == " darwin" * ]]; then
10
41
READLINK=greadlink
@@ -14,90 +45,91 @@ SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
14
45
SOLC=${SOLIDITY_BUILD_DIR} /solc/solc
15
46
SPLITSOURCES=${REPO_ROOT} /scripts/splitSources.py
16
47
48
+ # shellcheck source=scripts/common.sh
49
+ source " ${REPO_ROOT} /scripts/common.sh"
50
+
17
51
SYNTAXTESTS_DIR=" ${REPO_ROOT} /test/libsolidity/syntaxTests"
18
52
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
23
53
24
54
FAILED=0
25
55
UNCOMPILABLE=0
26
56
TESTED=0
27
57
28
58
if [[ " $( find . -maxdepth 0 -type d -empty) " == " " ]]; then
29
- echo " Test directory not empty. Skipping!"
30
- exit 1
59
+ fail " Test directory not empty. Skipping!"
31
60
fi
32
61
33
- # function tests whether exporting and importing again leaves the JSON ast unchanged
62
+ function ast_import_export_equivalence
63
+ {
64
+ local sol_file=" $1 "
65
+ local input_files=" $2 "
66
+ # save exported json as expected result (silently)
67
+ $SOLC --combined-json ast --pretty-json --json-indent 4 " ${input_files} " > expected.json 2> ${REDIRECT_TO}
68
+ # import it, and export it again as obtained result (silently)
69
+ if ! $SOLC --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json > obtained.json 2> stderr.txt
70
+ then
71
+ # For investigating, use exit 1 here so the script stops at the
72
+ # first failing test
73
+ # exit 1
74
+ FAILED=$(( FAILED + 1 ))
75
+ printError " ERROR: AST reimport failed for input file $sol_file "
76
+ printError
77
+ printError " Compiler stderr:"
78
+ cat ./stderr.txt >&2
79
+ printError
80
+ printError " Compiler stdout:"
81
+ cat ./obtained.json >&2
82
+ return 1
83
+ fi
84
+ if ! diff_files expected.json obtained.json
85
+ then
86
+ FAILED=$(( FAILED + 1 ))
87
+ fi
88
+ TESTED=$(( TESTED + 1 ))
89
+ rm expected.json obtained.json
90
+ rm -f stderr.txt
91
+ }
92
+
93
+ # function tests whether exporting and importing again is equivalent.
34
94
# Results are recorded by adding to FAILED or UNCOMPILABLE.
35
- # Also, in case of a mismatch a diff and the respective ASTs are printed
95
+ # Also, in case of a mismatch a diff is printed
36
96
# Expected parameters:
37
97
# $1 name of the file to be exported and imported
38
98
# $2 any files needed to do so that might be in parent directories
39
99
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
100
+ local sol_file=" $1 "
101
+ local input_files=" $2 "
102
+ if " $SOLC " --bin " ${input_files} " > ${REDIRECT_TO} 2>&1
44
103
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
104
+ ! [[ -e stderr.txt ]] || fail " stderr.txt already exists. Refusing to overwrite."
105
+ case " $IMPORT_TEST_TYPE " in
106
+ ast) ast_import_export_equivalence " ${sol_file} " " ${input_files} " ;;
107
+ * ) fail " Unknown import test type. Aborting." ;;
108
+ esac
85
109
else
86
- # echo "contract $solfile could not be compiled "
87
110
UNCOMPILABLE=$(( UNCOMPILABLE + 1 ))
88
111
fi
89
- # return 0
90
112
}
91
- echo " Looking at $NSOURCES .sol files..."
92
113
93
114
WORKINGDIR=$PWD
94
115
95
- # for solfile in $(find $DEV_DIR -name *.sol)
116
+ command_available " ${SOLC} " --version
117
+ command_available jq --version
118
+
119
+ case " $IMPORT_TEST_TYPE " in
120
+ ast) TEST_DIRS=(" ${SYNTAXTESTS_DIR} " " ${ASTJSONTESTS_DIR} " ) ;;
121
+ * ) fail " Unknown import test type. Aborting. Please specify ${0} ast [--show-errors]." ;;
122
+ esac
123
+
96
124
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
97
125
# 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" )
126
+ # tests on it. See https://github.com/boostorg/filesystem/issues/176
127
+ IMPORT_TEST_FILES=$( find " ${TEST_DIRS[@]} " -name " *.sol" -and -not -name " boost_filesystem_bug.sol" )
128
+
129
+ NSOURCES=" $( echo " $IMPORT_TEST_FILES " | wc -l) "
130
+ echo " Looking at $NSOURCES .sol files..."
131
+
132
+ for solfile in ${IMPORT_TEST_FILES}
101
133
do
102
134
echo -n " ."
103
135
# create a temporary sub-directory
108
140
OUTPUT=$( " $SPLITSOURCES " " $solfile " )
109
141
SPLITSOURCES_RC=$?
110
142
set -e
111
- if [ ${SPLITSOURCES_RC} == 0 ]
143
+ if [[ ${SPLITSOURCES_RC} == 0 ] ]
112
144
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
145
+ IFS=' ' read -ra OUTPUT_ARRAY <<< " ${OUTPUT}"
146
+ NSOURCES=$(( NSOURCES - 1 + ${# OUTPUT_ARRAY[@]} ))
147
+ testImportExportEquivalence " $solfile " " ${OUTPUT_ARRAY[*]} "
120
148
elif [ ${SPLITSOURCES_RC} == 1 ]
121
149
then
122
- testImportExportEquivalence " $solfile "
150
+ testImportExportEquivalence " $solfile " " $solfile "
123
151
elif [ ${SPLITSOURCES_RC} == 2 ]
124
152
then
125
153
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
126
154
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
127
155
# these errors, but print the actual output of the script.
128
- echo -e " \n${OUTPUT} \n"
129
- testImportExportEquivalence " $solfile "
156
+ printError " \n\n ${OUTPUT} \n \n"
157
+ testImportExportEquivalence " $solfile " " $solfile "
130
158
else
131
159
# 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"
160
+ printError " \n \nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES} . Aborting."
161
+ printError " \n\n ${OUTPUT} \n \n"
134
162
135
163
cd " $WORKINGDIR "
136
164
# Delete temporary files
144
172
rm -rf " $FILETMP "
145
173
done
146
174
147
- echo " "
175
+ echo
148
176
149
- if [ " $ FAILED" = 0 ]
177
+ if (( FAILED == 0 ))
150
178
then
151
- echo " SUCCESS: $TESTED syntaxTests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
179
+ echo " SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
152
180
else
153
- echo " FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
154
- exit 1
181
+ fail " FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
155
182
fi
0 commit comments