Skip to content

Commit

Permalink
Merge 7f6a4d9 into 9dd4b27
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Apr 9, 2019
2 parents 9dd4b27 + 7f6a4d9 commit ff3aca6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion future-apps/payroll/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
16 changes: 16 additions & 0 deletions future-apps/payroll/test/contracts/Payroll_upgrade.test.js
Original file line number Diff line number Diff line change
@@ -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 })

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)
})
83 changes: 83 additions & 0 deletions shared/test-helpers/lib/VersionedContractsProvider.js
Original file line number Diff line number Diff line change
@@ -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' }) {
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}`)
}
}

0 comments on commit ff3aca6

Please sign in to comment.