Skip to content

Commit

Permalink
Merge pull request ethereum#12191 from ethereum/more-flexible-externa…
Browse files Browse the repository at this point in the history
…l-test-scripts

Make external test scripts more flexible
  • Loading branch information
cameel authored Nov 9, 2021
2 parents cde5533 + 0745279 commit 79af83e
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 105 deletions.
24 changes: 18 additions & 6 deletions test/externalTests/colony.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,36 @@ 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"

neutralize_package_json_hooks
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_solc_modules "${DIR}/solc"

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
135 changes: 66 additions & 69 deletions test/externalTests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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..."
Expand Down Expand Up @@ -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).
Expand All @@ -104,25 +96,38 @@ function replace_version_pragmas
find . test -name '*.sol' -type f -print0 | xargs -0 sed -i -E -e 's/pragma solidity [^;]+;/pragma solidity >=0.0;/'
}

function force_truffle_solc_modules
function neutralize_package_lock
{
local soljson="$1"
# Remove lock files (if they exist) to prevent them from overriding our changes in package.json
printLog "Removing package lock files..."
rm --force --verbose yarn.lock
rm --force --verbose package-lock.json
}

function neutralize_package_json_hooks
{
printLog "Disabling package.json hooks..."
[[ -f package.json ]] || fail "package.json not found"
sed -i 's|"prepublish": *".*"|"prepublish": ""|g' package.json
sed -i 's|"prepare": *".*"|"prepare": ""|g' package.json
}

# Replace solc package by v0.5.0 and then overwrite with current version.
printLog "Forcing solc version for all Truffle modules..."
for d in node_modules node_modules/truffle/node_modules
function force_solc_modules
{
local custom_solcjs_path="${1:-solc/}"

[[ -d node_modules/ ]] || assertFail

printLog "Replacing all installed solc-js with a link to the latest version..."
soljson_binaries=$(find node_modules -type f -path "*/solc/soljson.js")
for soljson_binary in $soljson_binaries
do
(
if [ -d "$d" ]; then
cd $d
rm -rf solc
git clone --depth 1 -b master https://github.com/ethereum/solc-js.git solc
cp "$soljson" solc/soljson.js

cd solc
npm install
fi
)
local solc_module_path
solc_module_path=$(dirname "$soljson_binary")

printLog "Found and replaced solc-js in $solc_module_path"
rm -r "$solc_module_path"
ln -s "$custom_solcjs_path" "$solc_module_path"
done
}

Expand All @@ -131,7 +136,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 "-------------------------------------"
Expand All @@ -147,30 +152,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
Expand Down Expand Up @@ -219,31 +212,35 @@ function truffle_compiler_settings
echo "}"
}

function truffle_run_test
function compile_and_run_test
{
local soljson="$1"
local compile_fn="$2"
local test_fn="$3"
local compile_fn="$1"
local test_fn="$2"
local verify_fn="$3"

replace_version_pragmas
force_truffle_solc_modules "$soljson"
printLog "Running compile function..."
$compile_fn
$verify_fn "$SOLCVERSION_SHORT" "$SOLCVERSION"

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
if [[ "$COMPILE_ONLY" == 1 ]]; then
printLog "Skipping test function..."
else
printLog "Running test function..."
$test_fn
fi
}

function truffle_run_test
{
local config_file="$1"
local solc_path="$2"
local optimizer_level="$3"
local compile_fn="$4"
local test_fn="$5"

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
Expand Down
24 changes: 16 additions & 8 deletions test/externalTests/ens.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,34 @@ 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/ensdomains/ens.git"
local branch=master
local config_file="truffle-config.js"
local min_optimizer_level=1
local max_optimizer_level=3

truffle_setup "$SOLJSON" https://github.com/ensdomains/ens.git master
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

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

run_install "$SOLJSON" install_fn
replace_version_pragmas
force_solc_modules "${DIR}/solc"

truffle_run_test "$SOLJSON" compile_fn test_fn
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
23 changes: 16 additions & 7 deletions test/externalTests/gnosis-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,29 @@ 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

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

run_install "$SOLJSON" install_fn
replace_version_pragmas
force_solc_modules "${DIR}/solc"

truffle_run_test "$SOLJSON" compile_fn test_fn
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
24 changes: 16 additions & 8 deletions test/externalTests/gnosis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@ 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
neutralize_package_lock
neutralize_package_json_hooks
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
npm install --package-lock

run_install "$SOLJSON" install_fn
replace_version_pragmas
force_solc_modules "${DIR}/solc"

truffle_run_test "$SOLJSON" compile_fn test_fn
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
1 change: 0 additions & 1 deletion test/externalTests/solc-js/solc-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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; }

Expand Down
Loading

0 comments on commit 79af83e

Please sign in to comment.