Skip to content

Commit

Permalink
Fix OpenZeppelin external tests to actually use Hardhat and the binar…
Browse files Browse the repository at this point in the history
…y built in CI
  • Loading branch information
cameel committed Oct 28, 2021
1 parent 02baf45 commit 789a12b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
95 changes: 95 additions & 0 deletions test/externalTests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function setup_solcjs
npm install
cp "$soljson" soljson.js
SOLCVERSION=$(./solcjs --version)
SOLCVERSION_SHORT=$(echo "$SOLCVERSION" | sed -En 's/^([0-9.]+).*\+commit\.[0-9a-f]+.*$/\1/p')
printLog "Using solcjs version $SOLCVERSION"
cd ..
}
Expand Down Expand Up @@ -150,6 +151,40 @@ function force_truffle_compiler_settings
echo "module.exports['compilers'] = $(truffle_compiler_settings "$solc_path" "$level" "$evm_version");" >> "$config_file"
}

function force_hardhat_compiler_binary
{
local config_file="$1"
local solc_path="$2"

printLog "Configuring Hardhat..."
echo "-------------------------------------"
echo "Config file: ${config_file}"
echo "Compiler path: ${solc_path}"
hardhat_solc_build_subtask "$SOLCVERSION_SHORT" "$SOLCVERSION" "$solc_path" >> "$config_file"
}

function force_hardhat_compiler_settings
{
local config_file="$1"
local level="$2"
local evm_version="${3:-"$CURRENT_EVM_VERSION"}"

printLog "Configuring Hardhat..."
echo "-------------------------------------"
echo "Config file: ${config_file}"
echo "Optimization level: ${level}"
echo "Optimizer settings: $(optimizer_settings_for_level "$level")"
echo "EVM version: ${evm_version}"
echo "Compiler version: ${SOLCVERSION_SHORT}"
echo "Compiler version (full): ${SOLCVERSION}"
echo "-------------------------------------"

{
echo -n 'module.exports["solidity"] = '
hardhat_compiler_settings "$SOLCVERSION_SHORT" "$level" "$evm_version"
} >> "$config_file"
}

function truffle_verify_compiler_version
{
local solc_version="$1"
Expand All @@ -159,11 +194,26 @@ function truffle_verify_compiler_version
grep "$full_solc_version" --with-filename --recursive build/contracts || fail "Wrong compiler version detected."
}

function hardhat_verify_compiler_version
{
local solc_version="$1"
local full_solc_version="$2"

printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..."
grep '"solcVersion": "'"${solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
grep '"solcLongVersion": "'"${full_solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
}

function truffle_clean
{
rm -rf build/
}

function hardhat_clean
{
rm -rf artifacts/ cache/
}

function run_test
{
local compile_fn="$1"
Expand Down Expand Up @@ -210,6 +260,39 @@ function truffle_compiler_settings
echo "}"
}

function hardhat_solc_build_subtask {
local solc_version="$1"
local full_solc_version="$2"
local solc_path="$3"

echo "const {TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD} = require('hardhat/builtin-tasks/task-names');"
echo "const assert = require('assert');"
echo
echo "subtask(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, async (args, hre, runSuper) => {"
echo " assert(args.solcVersion == '${solc_version}', 'Unexpected solc version: ' + args.solcVersion)"
echo " return {"
echo " compilerPath: '$(realpath "$solc_path")',"
echo " isSolcJs: true,"
echo " version: args.solcVersion,"
echo " longVersion: '${full_solc_version}'"
echo " }"
echo "})"
}

function hardhat_compiler_settings {
local solc_version="$1"
local level="$2"
local evm_version="$3"

echo "{"
echo " version: '${solc_version}',"
echo " settings: {"
echo " optimizer: $(optimizer_settings_for_level "$level"),"
echo " evmVersion: '${evm_version}'"
echo " }"
echo "}"
}

function compile_and_run_test
{
local compile_fn="$1"
Expand Down Expand Up @@ -241,6 +324,18 @@ function truffle_run_test
compile_and_run_test compile_fn test_fn truffle_verify_compiler_version
}

function hardhat_run_test
{
local config_file="$1"
local optimizer_level="$2"
local compile_fn="$3"
local test_fn="$4"

hardhat_clean
force_hardhat_compiler_settings "$config_file" "$optimizer_level"
compile_and_run_test compile_fn test_fn hardhat_verify_compiler_version
}

function external_test
{
local name="$1"
Expand Down
16 changes: 8 additions & 8 deletions test/externalTests/zeppelin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ source test/externalTests/common.sh
verify_input "$1"
SOLJSON="$1"

function compile_fn { npx truffle compile; }
function test_fn { npm run test; }
function compile_fn { npm run compile; }
function test_fn { npm test; }

function zeppelin_test
{
local repo="https://github.com/solidity-external-tests/openzeppelin-contracts.git"
local branch=master_080
local config_file="truffle-config.js"
local repo="https://github.com/OpenZeppelin/openzeppelin-contracts.git"
local branch=master
local config_file="hardhat.config.js"
local min_optimizer_level=1
local max_optimizer_level=3

setup_solcjs "$DIR" "$SOLJSON"
download_project "$repo" "$branch" "$DIR"

neutralize_package_json_hooks
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
force_hardhat_compiler_binary "$config_file" "$SOLJSON"
force_hardhat_compiler_settings "$config_file" "$min_optimizer_level"
npm install

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
hardhat_run_test "$config_file" "$level" compile_fn test_fn
done
}

Expand Down

0 comments on commit 789a12b

Please sign in to comment.