Skip to content

Commit 79af83e

Browse files
authored
Merge pull request #12191 from ethereum/more-flexible-external-test-scripts
Make external test scripts more flexible
2 parents cde5533 + 0745279 commit 79af83e

File tree

7 files changed

+149
-105
lines changed

7 files changed

+149
-105
lines changed

test/externalTests/colony.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,36 @@ source test/externalTests/common.sh
2727
verify_input "$1"
2828
SOLJSON="$1"
2929

30-
function install_fn { yarn; git submodule update --init; }
3130
function compile_fn { yarn run provision:token:contracts; }
3231
function test_fn { yarn run test:contracts; }
3332

3433
function colony_test
3534
{
36-
OPTIMIZER_LEVEL=3
37-
CONFIG="truffle.js"
35+
local repo="https://github.com/solidity-external-tests/colonyNetwork.git"
36+
local branch=develop_080
37+
local config_file="truffle.js"
38+
local min_optimizer_level=3
39+
local max_optimizer_level=3
3840

39-
truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/colonyNetwork.git develop_080
40-
run_install "$SOLJSON" install_fn
41+
setup_solcjs "$DIR" "$SOLJSON"
42+
download_project "$repo" "$branch" "$DIR"
43+
44+
neutralize_package_json_hooks
45+
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
46+
yarn
47+
git submodule update --init
4148

4249
cd lib
4350
rm -Rf dappsys
4451
git clone https://github.com/solidity-external-tests/dappsys-monolithic.git -b master_080 dappsys
4552
cd ..
4653

47-
truffle_run_test "$SOLJSON" compile_fn test_fn
54+
replace_version_pragmas
55+
force_solc_modules "${DIR}/solc"
56+
57+
for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do
58+
truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn
59+
done
4860
}
4961

5062
external_test ColonyNetworks colony_test

test/externalTests/common.sh

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ set -e
2222

2323
# Requires "${REPO_ROOT}/scripts/common.sh" to be included before.
2424

25+
CURRENT_EVM_VERSION=london
26+
2527
function verify_input
2628
{
2729
if [ ! -f "$1" ]; then
@@ -51,8 +53,8 @@ function setup_solcjs
5153
{
5254
local dir="$1"
5355
local soljson="$2"
54-
local branch="$3"
55-
local path="$4"
56+
local branch="${3:-master}"
57+
local path="${4:-solc/}"
5658

5759
cd "$dir"
5860
printLog "Setting up solc-js..."
@@ -86,16 +88,6 @@ function force_truffle_version
8688
sed -i 's/"truffle":\s*".*"/"truffle": "'"$version"'"/g' package.json
8789
}
8890

89-
function truffle_setup
90-
{
91-
local soljson="$1"
92-
local repo="$2"
93-
local branch="$3"
94-
95-
setup_solcjs "$DIR" "$soljson" "master" "solc"
96-
download_project "$repo" "$branch" "$DIR"
97-
}
98-
9991
function replace_version_pragmas
10092
{
10193
# Replace fixed-version pragmas (part of Consensys best practice).
@@ -104,25 +96,38 @@ function replace_version_pragmas
10496
find . test -name '*.sol' -type f -print0 | xargs -0 sed -i -E -e 's/pragma solidity [^;]+;/pragma solidity >=0.0;/'
10597
}
10698

107-
function force_truffle_solc_modules
99+
function neutralize_package_lock
108100
{
109-
local soljson="$1"
101+
# Remove lock files (if they exist) to prevent them from overriding our changes in package.json
102+
printLog "Removing package lock files..."
103+
rm --force --verbose yarn.lock
104+
rm --force --verbose package-lock.json
105+
}
106+
107+
function neutralize_package_json_hooks
108+
{
109+
printLog "Disabling package.json hooks..."
110+
[[ -f package.json ]] || fail "package.json not found"
111+
sed -i 's|"prepublish": *".*"|"prepublish": ""|g' package.json
112+
sed -i 's|"prepare": *".*"|"prepare": ""|g' package.json
113+
}
110114

111-
# Replace solc package by v0.5.0 and then overwrite with current version.
112-
printLog "Forcing solc version for all Truffle modules..."
113-
for d in node_modules node_modules/truffle/node_modules
115+
function force_solc_modules
116+
{
117+
local custom_solcjs_path="${1:-solc/}"
118+
119+
[[ -d node_modules/ ]] || assertFail
120+
121+
printLog "Replacing all installed solc-js with a link to the latest version..."
122+
soljson_binaries=$(find node_modules -type f -path "*/solc/soljson.js")
123+
for soljson_binary in $soljson_binaries
114124
do
115-
(
116-
if [ -d "$d" ]; then
117-
cd $d
118-
rm -rf solc
119-
git clone --depth 1 -b master https://github.com/ethereum/solc-js.git solc
120-
cp "$soljson" solc/soljson.js
121-
122-
cd solc
123-
npm install
124-
fi
125-
)
125+
local solc_module_path
126+
solc_module_path=$(dirname "$soljson_binary")
127+
128+
printLog "Found and replaced solc-js in $solc_module_path"
129+
rm -r "$solc_module_path"
130+
ln -s "$custom_solcjs_path" "$solc_module_path"
126131
done
127132
}
128133

@@ -131,7 +136,7 @@ function force_truffle_compiler_settings
131136
local config_file="$1"
132137
local solc_path="$2"
133138
local level="$3"
134-
local evm_version="$4"
139+
local evm_version="${4:-"$CURRENT_EVM_VERSION"}"
135140

136141
printLog "Forcing Truffle compiler settings..."
137142
echo "-------------------------------------"
@@ -147,30 +152,18 @@ function force_truffle_compiler_settings
147152
echo "module.exports['compilers'] = $(truffle_compiler_settings "$solc_path" "$level" "$evm_version");" >> "$config_file"
148153
}
149154

150-
function verify_compiler_version
155+
function truffle_verify_compiler_version
151156
{
152157
local solc_version="$1"
158+
local full_solc_version="$2"
153159

154-
printLog "Verify that the correct version ($solc_version) of the compiler was used to compile the contracts..."
155-
grep -e "$solc_version" -r build/contracts > /dev/null
160+
printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..."
161+
grep "$full_solc_version" --with-filename --recursive build/contracts || fail "Wrong compiler version detected."
156162
}
157163

158-
function clean
164+
function truffle_clean
159165
{
160-
rm -rf build || true
161-
}
162-
163-
function run_install
164-
{
165-
local soljson="$1"
166-
local init_fn="$2"
167-
printLog "Running install function..."
168-
169-
replace_version_pragmas
170-
force_truffle_solc_modules "$soljson"
171-
force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$OPTIMIZER_LEVEL" london
172-
173-
$init_fn
166+
rm -rf build/
174167
}
175168

176169
function run_test
@@ -219,31 +212,35 @@ function truffle_compiler_settings
219212
echo "}"
220213
}
221214

222-
function truffle_run_test
215+
function compile_and_run_test
223216
{
224-
local soljson="$1"
225-
local compile_fn="$2"
226-
local test_fn="$3"
217+
local compile_fn="$1"
218+
local test_fn="$2"
219+
local verify_fn="$3"
227220

228-
replace_version_pragmas
229-
force_truffle_solc_modules "$soljson"
221+
printLog "Running compile function..."
222+
$compile_fn
223+
$verify_fn "$SOLCVERSION_SHORT" "$SOLCVERSION"
230224

231-
for level in $(seq "$OPTIMIZER_LEVEL" 3)
232-
do
233-
clean
234-
force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$level" london
235-
236-
printLog "Running compile function..."
237-
$compile_fn
238-
verify_compiler_version "$SOLCVERSION"
239-
240-
if [[ "$COMPILE_ONLY" == 1 ]]; then
241-
printLog "Skipping test function..."
242-
else
243-
printLog "Running test function..."
244-
$test_fn
245-
fi
246-
done
225+
if [[ "$COMPILE_ONLY" == 1 ]]; then
226+
printLog "Skipping test function..."
227+
else
228+
printLog "Running test function..."
229+
$test_fn
230+
fi
231+
}
232+
233+
function truffle_run_test
234+
{
235+
local config_file="$1"
236+
local solc_path="$2"
237+
local optimizer_level="$3"
238+
local compile_fn="$4"
239+
local test_fn="$5"
240+
241+
truffle_clean
242+
force_truffle_compiler_settings "$config_file" "$solc_path" "$optimizer_level"
243+
compile_and_run_test compile_fn test_fn truffle_verify_compiler_version
247244
}
248245

249246
function external_test

test/externalTests/ens.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,34 @@ source test/externalTests/common.sh
2727
verify_input "$1"
2828
export SOLJSON="$1"
2929

30-
function install_fn { npm install; }
3130
function compile_fn { npx truffle compile; }
3231
function test_fn { npm run test; }
3332

3433
function ens_test
3534
{
36-
export OPTIMIZER_LEVEL=1
37-
export CONFIG="truffle-config.js"
35+
local repo="https://github.com/ensdomains/ens.git"
36+
local branch=master
37+
local config_file="truffle-config.js"
38+
local min_optimizer_level=1
39+
local max_optimizer_level=3
3840

39-
truffle_setup "$SOLJSON" https://github.com/ensdomains/ens.git master
41+
setup_solcjs "$DIR" "$SOLJSON"
42+
download_project "$repo" "$branch" "$DIR"
4043

4144
# Use latest Truffle. Older versions crash on the output from 0.8.0.
4245
force_truffle_version ^5.1.55
4346

44-
# Remove the lock file (if it exists) to prevent it from overriding our changes in package.json
45-
rm -f package-lock.json
47+
neutralize_package_lock
48+
neutralize_package_json_hooks
49+
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
50+
npm install
4651

47-
run_install "$SOLJSON" install_fn
52+
replace_version_pragmas
53+
force_solc_modules "${DIR}/solc"
4854

49-
truffle_run_test "$SOLJSON" compile_fn test_fn
55+
for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do
56+
truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn
57+
done
5058
}
5159

5260
external_test Ens ens_test

test/externalTests/gnosis-v2.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,29 @@ function test_fn { npm test; }
3333

3434
function gnosis_safe_test
3535
{
36-
OPTIMIZER_LEVEL=2
37-
CONFIG="truffle-config.js"
36+
local repo="https://github.com/solidity-external-tests/safe-contracts.git"
37+
local branch=v2_080
38+
local config_file="truffle-config.js"
39+
local min_optimizer_level=2
40+
local max_optimizer_level=3
3841

39-
truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/safe-contracts.git v2_080
42+
setup_solcjs "$DIR" "$SOLJSON"
43+
download_project "$repo" "$branch" "$DIR"
4044

4145
sed -i 's|github:gnosis/mock-contract#sol_0_5_0|github:solidity-external-tests/mock-contract#master_080|g' package.json
4246
sed -i -E 's|"@gnosis.pm/util-contracts": "[^"]+"|"@gnosis.pm/util-contracts": "github:solidity-external-tests/util-contracts#solc-7_080"|g' package.json
4347

44-
# Remove the lock file (if it exists) to prevent it from overriding our changes in package.json
45-
rm -f package-lock.json
48+
neutralize_package_lock
49+
neutralize_package_json_hooks
50+
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
51+
npm install --package-lock
4652

47-
run_install "$SOLJSON" install_fn
53+
replace_version_pragmas
54+
force_solc_modules "${DIR}/solc"
4855

49-
truffle_run_test "$SOLJSON" compile_fn test_fn
56+
for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do
57+
truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn
58+
done
5059
}
5160

5261
external_test Gnosis-Safe gnosis_safe_test

test/externalTests/gnosis.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,33 @@ source test/externalTests/common.sh
2727
verify_input "$1"
2828
SOLJSON="$1"
2929

30-
function install_fn { npm install --package-lock; }
3130
function compile_fn { npx truffle compile; }
3231
function test_fn { npm test; }
3332

3433
function gnosis_safe_test
3534
{
36-
OPTIMIZER_LEVEL=2
37-
CONFIG="truffle-config.js"
35+
local repo="https://github.com/solidity-external-tests/safe-contracts.git"
36+
local branch=development_080
37+
local config_file="truffle-config.js"
38+
local min_optimizer_level=2
39+
local max_optimizer_level=3
3840

39-
truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/safe-contracts.git development_080
41+
setup_solcjs "$DIR" "$SOLJSON"
42+
download_project "$repo" "$branch" "$DIR"
4043

4144
sed -i 's|github:gnosis/mock-contract#sol_0_5_0|github:solidity-external-tests/mock-contract#master_080|g' package.json
4245

43-
# Remove the lock file (if it exists) to prevent it from overriding our changes in package.json
44-
rm -f package-lock.json
46+
neutralize_package_lock
47+
neutralize_package_json_hooks
48+
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
49+
npm install --package-lock
4550

46-
run_install "$SOLJSON" install_fn
51+
replace_version_pragmas
52+
force_solc_modules "${DIR}/solc"
4753

48-
truffle_run_test "$SOLJSON" compile_fn test_fn
54+
for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do
55+
truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn
56+
done
4957
}
5058

5159
external_test Gnosis-Safe gnosis_safe_test

test/externalTests/solc-js/solc-js.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ verify_version_input "$1" "$2"
2828
SOLJSON="$1"
2929
VERSION="$2"
3030

31-
function install_fn { echo "Nothing to install."; }
3231
function compile_fn { echo "Nothing to compile."; }
3332
function test_fn { npm test; }
3433

0 commit comments

Comments
 (0)