-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
109 lines (85 loc) · 2.48 KB
/
gulpfile.js
File metadata and controls
109 lines (85 loc) · 2.48 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* To use this, just do `gulp watch` or `CONTRACT=<contract> gulp watch`
* (e.g. `CONTRACT=MetaTx gulp watch`)
*/
const gulp = require('gulp');
const watch = require('gulp-watch');
const { exec, spawn } = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const contractToTest = (process.env.CONTRACT || '*').trim();
gulp.task('default', async () => {
console.log('Options: ', 'test');
});
gulp.task('watch', async cb => {
compileContractsAndRunTest();
watch([`./contracts/**/${contractToTest}.sol`], cb => {
compileContractsAndRunTest(cb);
});
watch([`./test/**/*.js`], cb => {
test(cb);
});
});
const compileContractsAndRunTest = cb => {
console.log('\n===============\nCompiling\n===============\n');
exec(`truffle compile`, function(err, stdout, stderr) {
console.log(stdout);
if (err) {
console.log('Error running UNIX commands');
console.error(err);
}
if (stderr) {
console.error(stderr);
}
if (!err & !stderr) {
test(cb);
}
});
};
const test = cb => {
console.log('\n===============\nTesting\n===============\n');
if (contractToTest !== '*') {
runOneTest(`${contractToTest}`);
} else if (cb && cb.history && cb.history[0]) {
const contracts = fs.readdirSync('./contracts');
for (let i = 0; i < contracts.length; i++) {
contracts[i] = contracts[i].replace('.sol', '');
}
let fileName = path
.basename(cb.history[0])
.replace('.sol', '')
.replace('.js', '');
if (contracts.includes(fileName)) {
runOneTest(`${fileName}`);
} else {
runAllTests();
}
} else {
runAllTests();
}
};
const runAllTests = () => {
const cmd = spawn('truffle', ['test']).on('error', error => {
console.log('Error running UNIX command');
console.error(error);
});
cmd.stdout.on('data', data => process.stdout.write(`${data}`));
cmd.stderr.on('data', data => console.error(`${data}`));
cmd.on('close', () =>
console.log('\n===============\nFinished\n===============\n')
);
};
const runOneTest = test => {
const cmd = spawn('truffle', ['test', `./test/${test}.js`]).on(
'error',
error => {
console.log('Error running UNIX command');
console.error(error);
}
);
cmd.stdout.on('data', data => process.stdout.write(`${data}`));
cmd.stderr.on('data', data => console.error(`${data}`));
cmd.on('close', () =>
console.log('\n===============\nFinished\n===============\n')
);
};