Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Shared: PoC testing contracts versions #782

Open
wants to merge 2 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+145 −36
Diff settings

Always

Just for now

@@ -55,6 +55,8 @@
"ethereumjs-abi": "^0.6.5",
"solidity-coverage": "0.5.11",
"solium": "^1.1.8",
"truffle": "4.1.14"
"truffle": "4.1.14",
"truffle-config": "1.0.6",
"truffle-contract": "3.0.6"
}
}
@@ -0,0 +1,16 @@
const TruffleConfig = require('truffle-config')
const TruffleContract = require('truffle-contract')
const VersionedContractsProvider = require('@aragon/test-helpers/lib/VersionedContractsProvider')(web3, TruffleContract, TruffleConfig)

contract('Payroll previous version', ([whatever]) => {
it.only('previous version should allow to call isAllowedToken when not initialized', async () => {
const commit = 'aff584ec78545cfb71415dcf2e56426e17a9f9cd'
const projectDir = 'future-apps/payroll'
const provider = VersionedContractsProvider.fromBranch({ commit, projectDir })
This conversation was marked as resolved by facuspagnuolo

This comment has been minimized.

Copy link
@sohkai

sohkai Apr 9, 2019

Member

This is super, super cool!


const PayrollOld = provider.getContract('Payroll')
const payroll = await PayrollOld.new()

assert.equal(await payroll.isTokenAllowed(whatever), false, 'should not fail calling to isTokenAllowed without initialization')
}).timeout(1800000)
})
@@ -1,50 +1,58 @@
#!/usr/bin/env bash

# Exit script as soon as a command fails.
set -o errexit
set -o errexit -o pipefail

if [ "$SOLIDITY_COVERAGE" = true ]; then
testrpc_port=8555
else
testrpc_port=8545
fi
# Executes cleanup function at script exit.
trap cleanup EXIT

testrpc_running() {
nc -z localhost "$testrpc_port"
cleanup() {
# Kill the RPC instance that we started (if we started one and if it's still running).
if [ -n "$rpc_pid" ] && ps -p $rpc_pid > /dev/null; then
kill -9 $rpc_pid
fi
}

start_testrpc() {
echo "Starting our own testrpc instance at port $testrpc_port"
if [ "$SOLIDITY_COVERAGE" = true ]; then
npx testrpc-sc -i 16 --gasLimit 0xfffffffffff --port "$testrpc_port" > /dev/null &
else
npx ganache-cli -i 15 --gasLimit 50000000 --port "$testrpc_port" > /dev/null &
fi
start_ganache() {
echo "Starting ganache-cli..."
npx ganache-cli -i ${NETWORK_ID} -l ${GAS_LIMIT} -e ${BALANCE} -p ${PORT} > /dev/null &
rpc_pid=$!
sleep 3
echo "Running ganache-cli with pid ${rpc_pid}"
}

testrpc_pid=$!
start_testrpc() {
echo "Starting testrpc-sc..."
npx testrpc-sc -i ${NETWORK_ID} -l ${GAS_LIMIT} -e ${BALANCE} -p ${PORT} > /dev/null &
rpc_pid=$!
sleep 3
echo "Running testrpc-sc with pid ${rpc_pid}"
}

if testrpc_running; then
echo "Killing testrpc instance at port $testrpc_port"
kill -9 $(lsof -i:$testrpc_port -t)
fi
measure_coverage() {
echo "Measuring coverage..."
npx solidity-coverage "$@"
}

start_testrpc
sleep 5
run_tests() {
echo "Running tests..."
npx truffle test --network rpc "$@"
}

# Exit error mode so the testrpc instance always gets killed
set +e
result=0
if [ "$SOLIDITY_COVERAGE" = true ]; then
npx solidity-coverage "$@"
result=$?
elif [ "$TRUFFLE_TEST" = true ]; then
truffle test --network rpc "$@"
result=$?
fi
PORT=8555
BALANCE=10000
GAS_LIMIT=0xfffffffffff
NETWORK_ID=16

if [ ! -z "$testrpc_pid" ]; then
kill -9 $testrpc_pid
fi
start_testrpc
measure_coverage
else
PORT=8545
BALANCE=100000
GAS_LIMIT=50000000
NETWORK_ID=15

exit $result
start_ganache
run_tests
fi
@@ -0,0 +1,83 @@
const fs = require('fs')
const path = require('path')
const { spawnSync } = require('child_process')

const APPS_GIT_URL = 'https://github.com/aragon/aragon-apps.git'

module.exports = (web3, TruffleContract, TruffleConfig) => {
return class VersionedContractsProvider {
static fromBranch({ commit, source = APPS_GIT_URL, projectDir = '.', outputDir = 'tmp' }) {

This comment has been minimized.

Copy link
@sohkai

sohkai Apr 9, 2019

Member

Wonder if we could be more generic than using "branch" terminology. I foresee us using tags most of the time, but I imagine there's a better abstraction for git references in general.

This comment has been minimized.

Copy link
@facuspagnuolo

facuspagnuolo Apr 9, 2019

Author Contributor

I started it with a tag/branch abstraction, but I thought that maybe commit was more flexible. We can extend it to whatever we want actually :)

const outputPath = path.resolve(outputDir)
const compiler = new RemoteCompiler(source, commit, projectDir, outputPath)
const buildDirPath = compiler.call()
return new VersionedContractsProvider(buildDirPath)
}

static fromPath(buildDirPath) {
return new VersionedContractsProvider(buildDirPath)
}

constructor(buildDirPath) {
this.buildDirPath = buildDirPath
}

getContract(contractName) {
const path = `${this.buildDirPath}/${contractName}.json`
const contract = TruffleContract(require(path))
return this._provideContract(contract)
}

_provideContract(contract) {
contract.setNetwork('test')
contract.defaults(this._getContractsDefaults())
contract.setProvider(web3.currentProvider)
return contract
}

_getContractsDefaults() {
const config = TruffleConfig.detect({ logger: console })
const defaults = this._pickKeys(config, ['from', 'gas', 'gasPrice'])
if (!defaults.from) defaults.from = web3.eth.accounts[0]
return defaults
}

_pickKeys(object, keys) {
return keys.reduce((result, key) => {
if (object[key] !== undefined) result[key] = object[key]
return result
}, {})
}
}
}

class RemoteCompiler {
constructor(source, commit, projectDir, outputPath) {
this.source = source
this.commit = commit
this.outputPath = outputPath
this.projectPath = path.resolve(this.outputPath, projectDir)
}

call() {
this.cloneGithubRepo()
return this.compile()
}

cloneGithubRepo() {
if (!fs.existsSync(this.outputPath)) fs.mkdirSync(this.outputPath)
this._run('git', ['clone', this.source, this.outputPath])
this._run('git', ['reset', '--hard', this.commit], this.outputPath)
}

compile() {
this._run('npm', ['install'], this.outputPath)
this._run('npx', ['truffle', 'compile'], this.projectPath)
return path.resolve(this.projectPath, 'build/contracts')
}

_run(cmd, args = [], cwd = '.') {
console.log(`Running ${cmd} with args ${args} in ${cwd}`)
const output = spawnSync(cmd, args, { cwd })
if (output.error) throw new Error(`Failed running command ${cmd} in ${cwd}: \n${output.error}`)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.