From d0292fb17f496a91a2b80a093611d4fc316b587d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Mon, 25 Oct 2021 14:29:43 +0200 Subject: [PATCH] Refactor external tests to be more explicit and easier to adjust for special cases --- test/externalTests/colony.sh | 25 ++++++-- test/externalTests/common.sh | 90 +++++++++++---------------- test/externalTests/ens.sh | 23 +++++-- test/externalTests/gnosis-v2.sh | 22 +++++-- test/externalTests/gnosis.sh | 23 +++++-- test/externalTests/solc-js/solc-js.sh | 1 - test/externalTests/zeppelin.sh | 24 +++++-- 7 files changed, 125 insertions(+), 83 deletions(-) diff --git a/test/externalTests/colony.sh b/test/externalTests/colony.sh index c807f9022..e73a15d68 100755 --- a/test/externalTests/colony.sh +++ b/test/externalTests/colony.sh @@ -24,24 +24,37 @@ source test/externalTests/common.sh verify_input "$1" SOLJSON="$1" -function install_fn { yarn; git submodule update --init; } function compile_fn { yarn run provision:token:contracts; } function test_fn { yarn run test:contracts; } function colony_test { - OPTIMIZER_LEVEL=3 - CONFIG="truffle.js" + local repo="https://github.com/solidity-external-tests/colonyNetwork.git" + local branch=develop_080 + local config_file="truffle.js" + local min_optimizer_level=3 + local max_optimizer_level=3 - truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/colonyNetwork.git develop_080 - run_install "$SOLJSON" install_fn + setup_solcjs "$DIR" "$SOLJSON" + download_project "$repo" "$branch" "$DIR" + + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level" + yarn + git submodule update --init cd lib rm -Rf dappsys git clone https://github.com/solidity-external-tests/dappsys-monolithic.git -b master_080 dappsys cd .. - truffle_run_test "$SOLJSON" compile_fn test_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + + for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do + truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn + done } external_test ColonyNetworks colony_test diff --git a/test/externalTests/common.sh b/test/externalTests/common.sh index bc24cd1d1..96ef7c69e 100644 --- a/test/externalTests/common.sh +++ b/test/externalTests/common.sh @@ -22,6 +22,8 @@ set -e # Requires "${REPO_ROOT}/scripts/common.sh" to be included before. +CURRENT_EVM_VERSION=london + function verify_input { if [ ! -f "$1" ]; then @@ -51,8 +53,8 @@ function setup_solcjs { local dir="$1" local soljson="$2" - local branch="$3" - local path="$4" + local branch="${3:-master}" + local path="${4:-solc/}" cd "$dir" printLog "Setting up solc-js..." @@ -86,16 +88,6 @@ function force_truffle_version sed -i 's/"truffle":\s*".*"/"truffle": "'"$version"'"/g' package.json } -function truffle_setup -{ - local soljson="$1" - local repo="$2" - local branch="$3" - - setup_solcjs "$DIR" "$soljson" "master" "solc" - download_project "$repo" "$branch" "$DIR" -} - function replace_version_pragmas { # Replace fixed-version pragmas (part of Consensys best practice). @@ -131,7 +123,7 @@ function force_truffle_compiler_settings local config_file="$1" local solc_path="$2" local level="$3" - local evm_version="$4" + local evm_version="${4:-"$CURRENT_EVM_VERSION"}" printLog "Forcing Truffle compiler settings..." echo "-------------------------------------" @@ -147,30 +139,18 @@ function force_truffle_compiler_settings echo "module.exports['compilers'] = $(truffle_compiler_settings "$solc_path" "$level" "$evm_version");" >> "$config_file" } -function verify_compiler_version +function truffle_verify_compiler_version { local solc_version="$1" + local full_solc_version="$2" - printLog "Verify that the correct version ($solc_version) of the compiler was used to compile the contracts..." - grep -e "$solc_version" -r build/contracts > /dev/null + printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..." + grep "$full_solc_version" --with-filename --recursive build/contracts || fail "Wrong compiler version detected." } -function clean +function truffle_clean { - rm -rf build || true -} - -function run_install -{ - local soljson="$1" - local init_fn="$2" - printLog "Running install function..." - - replace_version_pragmas - force_truffle_solc_modules "$soljson" - force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$OPTIMIZER_LEVEL" london - - $init_fn + rm -rf build/ } function run_test @@ -217,31 +197,35 @@ function truffle_compiler_settings { echo "}" } +function compile_and_run_test +{ + local compile_fn="$1" + local test_fn="$2" + local verify_fn="$3" + + printLog "Running compile function..." + $compile_fn + $verify_fn "$SOLCVERSION_SHORT" "$SOLCVERSION" + + if [[ "$COMPILE_ONLY" == 1 ]]; then + printLog "Skipping test function..." + else + printLog "Running test function..." + $test_fn + fi +} + function truffle_run_test { - local soljson="$1" - local compile_fn="$2" - local test_fn="$3" + local config_file="$1" + local solc_path="$2" + local optimizer_level="$3" + local compile_fn="$4" + local test_fn="$5" - replace_version_pragmas - force_truffle_solc_modules "$soljson" - - for level in $(seq "$OPTIMIZER_LEVEL" 3) - do - clean - force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$level" london - - printLog "Running compile function..." - $compile_fn - verify_compiler_version "$SOLCVERSION" - - if [[ "$COMPILE_ONLY" == 1 ]]; then - printLog "Skipping test function..." - else - printLog "Running test function..." - $test_fn - fi - done + truffle_clean + force_truffle_compiler_settings "$config_file" "$solc_path" "$optimizer_level" + compile_and_run_test compile_fn test_fn truffle_verify_compiler_version } function external_test diff --git a/test/externalTests/ens.sh b/test/externalTests/ens.sh index a19ce859d..ea66ff8e8 100755 --- a/test/externalTests/ens.sh +++ b/test/externalTests/ens.sh @@ -24,16 +24,19 @@ source test/externalTests/common.sh verify_input "$1" export SOLJSON="$1" -function install_fn { npm install; } function compile_fn { npx truffle compile; } function test_fn { npm run test; } function ens_test { - export OPTIMIZER_LEVEL=1 - export CONFIG="truffle-config.js" + local repo="https://github.com/solidity-external-tests/ens.git" + local branch=master_080 + local config_file="truffle-config.js" + local min_optimizer_level=1 + local max_optimizer_level=3 - truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/ens.git master_080 + setup_solcjs "$DIR" "$SOLJSON" + download_project "$repo" "$branch" "$DIR" # Use latest Truffle. Older versions crash on the output from 0.8.0. force_truffle_version ^5.1.55 @@ -41,9 +44,17 @@ function ens_test # Remove the lock file (if it exists) to prevent it from overriding our changes in package.json rm -f package-lock.json - run_install "$SOLJSON" install_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level" + npm install - truffle_run_test "$SOLJSON" compile_fn test_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + + for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do + truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn + done } external_test Ens ens_test diff --git a/test/externalTests/gnosis-v2.sh b/test/externalTests/gnosis-v2.sh index 19ff3edcb..e4050b886 100755 --- a/test/externalTests/gnosis-v2.sh +++ b/test/externalTests/gnosis-v2.sh @@ -30,10 +30,14 @@ function test_fn { npm test; } function gnosis_safe_test { - OPTIMIZER_LEVEL=2 - CONFIG="truffle-config.js" + local repo="https://github.com/solidity-external-tests/safe-contracts.git" + local branch=v2_080 + local config_file="truffle-config.js" + local min_optimizer_level=2 + local max_optimizer_level=3 - truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/safe-contracts.git v2_080 + setup_solcjs "$DIR" "$SOLJSON" + download_project "$repo" "$branch" "$DIR" sed -i 's|github:gnosis/mock-contract#sol_0_5_0|github:solidity-external-tests/mock-contract#master_080|g' package.json sed -i -E 's|"@gnosis.pm/util-contracts": "[^"]+"|"@gnosis.pm/util-contracts": "github:solidity-external-tests/util-contracts#solc-7_080"|g' package.json @@ -41,9 +45,17 @@ function gnosis_safe_test # Remove the lock file (if it exists) to prevent it from overriding our changes in package.json rm -f package-lock.json - run_install "$SOLJSON" install_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level" + npm install --package-lock - truffle_run_test "$SOLJSON" compile_fn test_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + + for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do + truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn + done } external_test Gnosis-Safe gnosis_safe_test diff --git a/test/externalTests/gnosis.sh b/test/externalTests/gnosis.sh index f8ca999d6..1d8d092ea 100755 --- a/test/externalTests/gnosis.sh +++ b/test/externalTests/gnosis.sh @@ -24,25 +24,36 @@ source test/externalTests/common.sh verify_input "$1" SOLJSON="$1" -function install_fn { npm install --package-lock; } function compile_fn { npx truffle compile; } function test_fn { npm test; } function gnosis_safe_test { - OPTIMIZER_LEVEL=2 - CONFIG="truffle-config.js" + local repo="https://github.com/solidity-external-tests/safe-contracts.git" + local branch=development_080 + local config_file="truffle-config.js" + local min_optimizer_level=2 + local max_optimizer_level=3 - truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/safe-contracts.git development_080 + setup_solcjs "$DIR" "$SOLJSON" + download_project "$repo" "$branch" "$DIR" sed -i 's|github:gnosis/mock-contract#sol_0_5_0|github:solidity-external-tests/mock-contract#master_080|g' package.json # Remove the lock file (if it exists) to prevent it from overriding our changes in package.json rm -f package-lock.json - run_install "$SOLJSON" install_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level" + npm install --package-lock - truffle_run_test "$SOLJSON" compile_fn test_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + + for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do + truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn + done } external_test Gnosis-Safe gnosis_safe_test diff --git a/test/externalTests/solc-js/solc-js.sh b/test/externalTests/solc-js/solc-js.sh index 3762ba7a2..a6a45dac2 100755 --- a/test/externalTests/solc-js/solc-js.sh +++ b/test/externalTests/solc-js/solc-js.sh @@ -25,7 +25,6 @@ verify_version_input "$1" "$2" SOLJSON="$1" VERSION="$2" -function install_fn { echo "Nothing to install."; } function compile_fn { echo "Nothing to compile."; } function test_fn { npm test; } diff --git a/test/externalTests/zeppelin.sh b/test/externalTests/zeppelin.sh index 8d8cb4845..8132224f8 100755 --- a/test/externalTests/zeppelin.sh +++ b/test/externalTests/zeppelin.sh @@ -24,19 +24,31 @@ source test/externalTests/common.sh verify_input "$1" SOLJSON="$1" -function install_fn { npm install; } function compile_fn { npx truffle compile; } function test_fn { npm run test; } function zeppelin_test { - OPTIMIZER_LEVEL=1 - CONFIG="truffle-config.js" + local repo="https://github.com/solidity-external-tests/openzeppelin-contracts.git" + local branch=master_080 + local config_file="truffle-config.js" + local min_optimizer_level=1 + local max_optimizer_level=3 - truffle_setup "$SOLJSON" https://github.com/solidity-external-tests/openzeppelin-contracts.git master_080 - run_install "$SOLJSON" install_fn + setup_solcjs "$DIR" "$SOLJSON" + download_project "$repo" "$branch" "$DIR" - truffle_run_test "$SOLJSON" compile_fn test_fn + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level" + npm install + + replace_version_pragmas + force_truffle_solc_modules "$SOLJSON" + + for level in $(seq "$min_optimizer_level" "$max_optimizer_level"); do + truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn + done } external_test Zeppelin zeppelin_test