forked from celo-org/celo-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runTests.js
96 lines (85 loc) · 2.61 KB
/
runTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const ganache = require('@celo/ganache-cli')
const glob = require('glob-fs')({
gitignore: false,
})
const { exec, waitForPortOpen } = require('./lib/test-utils')
const minimist = require('minimist')
const networkName = 'development'
const network = require('./truffle-config.js').networks[networkName]
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, 1000 * seconds))
// As documented https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
const isCI = process.env.CI === 'true'
async function startGanache() {
const server = ganache.server({
default_balance_ether: network.defaultBalance,
network_id: network.network_id,
mnemonic: network.mnemonic,
gasPrice: network.gasPrice,
gasLimit: 20000000,
allowUnlimitedContractSize: true,
})
await new Promise((resolve, reject) => {
server.listen(8545, (err, blockchain) => {
if (err) {
reject(err)
} else {
resolve(blockchain)
}
})
})
return () =>
new Promise((resolve, reject) => {
server.close((err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
async function test() {
const argv = minimist(process.argv.slice(2), {
boolean: ['gas', 'coverage', 'verbose-rpc'],
})
try {
const closeGanache = await startGanache()
if (isCI) {
// If we are running on circle ci we need to wait for ganache to be up.
await waitForPortOpen('localhost', 8545, 60)
}
// --reset is a hack to trick truffle into using 20M gas.
let testArgs = ['run', 'truffle', 'test', '--reset']
if (argv['verbose-rpc']) {
testArgs.push('--verbose-rpc')
}
if (argv.coverage) {
testArgs = testArgs.concat(['--network', 'coverage'])
} else {
testArgs = testArgs.concat(['--network', networkName])
}
if (argv.gas) {
testArgs = testArgs.concat(['--color', '--gas'])
}
const testGlob =
argv._.length > 0
? argv._.map((testName) =>
testName.endsWith('/') ? `test/${testName}\*\*/*.ts` : `test/\*\*/${testName}.ts`
).join(' ')
: `test/\*\*/*.ts`
const testFiles = glob.readdirSync(testGlob)
if (testFiles.length === 0) {
// tslint:disable-next-line: no-console
console.error(`No test files matched with ${testGlob}`)
process.exit(1)
}
testArgs = testArgs.concat(testFiles)
await exec('yarn', testArgs)
await closeGanache()
} catch (e) {
// tslint:disable-next-line: no-console
console.error(e.stdout ? e.stdout : e)
process.nextTick(() => process.exit(1))
}
}
test()