make use of truffle for tests and add solidity-coverage

t
This commit is contained in:
Max Alekseenko 2020-04-28 14:49:20 +03:00
parent 505cc8463f
commit 2c8a866a27
7 changed files with 3145 additions and 11 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ node_modules
build
.env
.openzeppelin/.session
coverage.json
coverage

21
.solcover.js Normal file
View File

@ -0,0 +1,21 @@
module.exports = {
norpc: true,
compileCommand: '../node_modules/.bin/truffle compile',
testCommand: 'node --max-old-space-size=4096 ../node_modules/.bin/truffle test --network coverage',
copyPackages: ['openzeppelin-solidity'],
providerOptions: {
accounts: [
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501201', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501202', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501203', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501204', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501205', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501206', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501207', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501208', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501209', balance: '0xD3C21BCECCEDA1000000' },
{ secretKey: '0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501210', balance: '0xD3C21BCECCEDA1000000' },
],
}
};

3014
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "npx oz compile && mocha --timeout 300000 --exit --recursive test"
"test": "bash scripts/test.sh",
"coverage": "SOLIDITY_COVERAGE=true bash scripts/test.sh"
},
"repository": {
"type": "git",
@ -28,6 +29,9 @@
"@openzeppelin/test-environment": "^0.1.3",
"@openzeppelin/test-helpers": "^0.5.5",
"chai": "^4.2.0",
"mocha": "^7.1.1"
"ganache-cli": "^6.9.1",
"mocha": "^7.1.1",
"solidity-coverage": "^0.7.4",
"truffle": "^5.1.23"
}
}

66
scripts/test.sh Normal file
View File

@ -0,0 +1,66 @@
#!/usr/bin/env bash
# Exit script as soon as a command fails.
set -o errexit
# Executes cleanup function at script exit.
trap cleanup EXIT
cleanup() {
# Kill the ganache-cli instance that we started (if we started one and if it's still running).
if [ -n "$ganache_pid" ] && ps -p $ganache_pid > /dev/null; then
kill -9 $ganache_pid
fi
}
if [ "$SOLIDITY_COVERAGE" = true ]; then
ganache_port=8555
else
ganache_port=8544
fi
ganache_running() {
nc -z localhost "$ganache_port"
}
start_ganache() {
# We define 10 accounts with balance 1M ether
local accounts=(
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501201,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501202,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501203,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501204,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501205,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501206,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501207,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501208,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501209,1000000000000000000000000"
--account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501210,1000000000000000000000000"
)
if [ "$SOLIDITY_COVERAGE" = true ]; then
node_modules/.bin/testrpc-sc --gasLimit 0xfffffffffff --allowUnlimitedContractSize --port "$ganache_port" > /dev/null &
else
node_modules/.bin/ganache-cli --gasLimit 0xfffffffffff --port "$ganache_port" "${accounts[@]}" > /dev/null &
fi
ganache_pid=$!
}
if ganache_running; then
echo "Using existing ganache-cli instance"
else
echo "Starting ganache-cli instance"
start_ganache
fi
if [ "$SOLIDITY_COVERAGE" = true ]; then
node_modules/.bin/truffle run coverage --network coverage
if [ "$CONTINUOUS_INTEGRATION" = true ]; then
cat coverage/lcov.info | COVERALLS_REPO_TOKEN=$COVERALLS_REPO_TOKEN node_modules/.bin/coveralls
fi
else
node_modules/.bin/truffle test "$@" --network test
fi

View File

@ -1,14 +1,13 @@
const { accounts, contract } = require('@openzeppelin/test-environment');
const { ether, BN, expectRevert, expectEvent, time, constants, send, balance } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const PoaMania = contract.fromArtifact('PoaMania');
const RandomMock = contract.fromArtifact('RandomMock');
const DrawManager = contract.fromArtifact('DrawManager');
const Random = contract.fromArtifact('Random');
const SortitionSumTreeFactory = contract.fromArtifact('SortitionSumTreeFactory');
const PoaMania = artifacts.require('PoaMania');
const RandomMock = artifacts.require('RandomMock');
const DrawManager = artifacts.require('DrawManager');
const Random = artifacts.require('Random');
const SortitionSumTreeFactory = artifacts.require('SortitionSumTreeFactory');
describe('PoaMania', () => {
contract('PoaMania', accounts => {
const [owner, firstParticipant, secondParticipant, thirdParticipant, fourthParticipant, fifthParticipant] = accounts;
const roundDuration = new BN(600); // in seconds
const blockTime = new BN(5); // in seconds

32
truffle.js Normal file
View File

@ -0,0 +1,32 @@
module.exports = {
networks: {
test: {
host: "localhost",
port: 8544,
gas: 8000000,
network_id: "*",
},
coverage: {
host: "localhost",
port: 8555,
gas: 0xfffffffffff,
gasPrice: 0x01,
network_id: "*",
},
},
plugins: ["solidity-coverage"],
mocha: {
enableTimeouts: false,
},
compilers: {
solc: {
version: "0.5.16",
settings: {
optimizer: {
enabled: false,
runs: 200,
},
},
},
},
};