-
Notifications
You must be signed in to change notification settings - Fork 494
/
Copy pathindex.js
280 lines (240 loc) · 9.02 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import 'colors';
import {__} from 'embark-i18n';
const async = require('async');
const EmbarkJS = require('embarkjs');
const Mocha = require('mocha');
const Web3 = require('web3');
const Reporter = require('./reporter');
const GAS_LIMIT = 8000000;
const JAVASCRIPT_TEST_MATCH = /^.+\.js$/i;
const TEST_TIMEOUT = 15000; // 15 seconds in milliseconds
class MochaTestRunner {
constructor(embark, options) {
this.embark = embark;
this.events = embark.events;
this.plugins = options.plugins;
this.logger = embark.logger;
this.fs = embark.fs;
this.files = [];
this.options = {};
this.web3 = null;
this.events.request('tests:runner:register',
'JavaScript (Mocha)',
this.match.bind(this),
this.addFile.bind(this),
this.run.bind(this)
);
}
static originalRequire = require('module').prototype.require;
static requireArtifact(artifactName, compiledContracts) {
if (artifactName === 'EmbarkJS') return EmbarkJS;
const instance = compiledContracts[artifactName];
if (!instance) {
compiledContracts[artifactName] = {};
}
if (!compiledContracts[artifactName].abiDefinition) {
return compiledContracts[artifactName];
}
try {
return Object.setPrototypeOf(
instance,
EmbarkJS.Blockchain.Contract(instance)
);
} catch (e) {
return instance;
}
}
addFile(path) {
if (!this.match(path)) {
throw new Error(`invalid JavaScript test path: ${path}`);
}
this.files.push(path);
}
match(path) {
return JAVASCRIPT_TEST_MATCH.test(path);
}
async run(options, cb) {
const {events} = this.embark;
const {reporter} = options;
this.options = options;
const Module = require("module");
let accounts = [];
let compiledContracts = {};
const config = (cfg, acctCb) => {
global.before((done) => {
async.waterfall([
(next) => {
events.request("tests:deployment:check", cfg, this.options, (err, provider) => {
if (err) {
return next(err);
}
if (provider) {
this.web3.setProvider(provider);
}
next();
});
},
(next) => {
events.request('tests:config:check', cfg, (err) => {
if (err) {
return next(err);
}
next();
});
},
(next) => {
this.web3.eth.getAccounts((err, accts) => {
if (err) {
return next(err);
}
accounts = accts;
this.web3.eth.defaultAccount = accts[0];
global.web3.eth.defaultAccount = accts[0];
next();
});
},
(next) => {
// Remove contracts that are not in the configs
const realContracts = {};
const deployKeys = Object.keys(cfg.contracts.deploy);
Object.keys(compiledContracts).forEach((className) => {
if (deployKeys.includes(className)) {
realContracts[className] = compiledContracts[className];
}
});
events.request("contracts:build", cfg, realContracts, next);
},
(contractsList, contractDeps, next) => {
events.request("deployment:contracts:deploy", contractsList, contractDeps, next);
},
(_result, next) => {
events.request("contracts:list", next);
},
(contracts, next) => {
for(const contract of contracts) {
const instance = EmbarkJS.Blockchain.Contract(contract);
// Here we switch the prototype of the instance we had lying around to the more
// complete web3 contract instance (with some methods of our own.) Despite this
// looking hacky, it's necessary. As mocha tests look something like this:
//
// const SimpleStorage = require('Embark/contracts/SimpleStorage');
//
// config({
// contracts: {
// SimpleStorage: { args: [100] }
// }
// }, (err, accounts) => {
//
// });
//
// it means that we have to return something before the address is set. So,
// due to that constraint, the only sane way to modify the object the test
// file is hanging on to is to replace the prototype instead of switching
// it around and having the test losing the reference.
if (!compiledContracts[contract.className]) {
compiledContracts[contract.className] = {};
}
instance.options.from = accounts[0];
instance.options.gas = GAS_LIMIT;
Object.setPrototypeOf(compiledContracts[contract.className], instance);
}
next();
}
], (err) => {
// Reset the gas accumulator so that we don't show deployment gas on the
// first test.
reporter.resetGas();
if (acctCb) {
acctCb(err, accounts);
}
events.emit('tests:ready', accounts);
done(err);
});
});
};
const provider = await this.events.request2("tests:blockchain:start", this.options);
this.web3 = new Web3(provider);
accounts = await this.web3.eth.getAccounts();
await events.request2("contracts:reset");
let contractFiles = await events.request2("config:contractsFiles");
async.waterfall([
(next) => {
this.plugins.emitAndRunActionsForEvent('tests:contracts:compile:before', contractFiles, next);
},
(_contractFiles, next) => {
contractFiles = _contractFiles;
events.request("compiler:contracts:compile", _contractFiles, next);
},
(_compiledContracts, next) => {
this.plugins.emitAndRunActionsForEvent('tests:contracts:compile:after', _compiledContracts, next);
},
(_compiledContracts, next) => {
compiledContracts = _compiledContracts;
const fns = this.files.map((file) => {
return (seriesCb) => {
this.fs.readFile(file, (err, data) => {
if (err) {
self.logger.error(__('Error reading file %s', file));
self.logger.error(err);
seriesCb(null, 1);
}
if (data.toString().search(/contract\(|describe\(/) === -1) {
return seriesCb(null, 0);
}
let testRunner = this;
Module.prototype.require = function(req) {
if (["Embark/EmbarkJS", "EmbarkJS"].includes(req)) {
testRunner.logger.warn(
`${__('WARNING!')} ${__('Use')} ${`artifacts.require('EmbarkJS')`.cyan}, ${__('the syntax').yellow} ${`require('${req}')`.cyan} ${__('has been deprecated and will be removed in future versions').yellow}`
);
return MochaTestRunner.requireArtifact("EmbarkJS");
}
const prefix = "Embark/contracts/";
if (req.startsWith(prefix)) {
const artifactName = req.replace(prefix, "");
testRunner.logger.warn(
`${__('WARNING!')} ${__('Use')} ${`artifacts.require('${artifactName}')`.cyan}, ${__('the syntax').yellow} ${`require('${req}')`.cyan} ${__('has been deprecated and will be removed in future versions').yellow}`
);
return MochaTestRunner.requireArtifact(
artifactName,
compiledContracts
);
}
return MochaTestRunner.originalRequire.call(this, req);
};
const mocha = new Mocha();
mocha.reporter(Reporter, {reporter: options.reporter});
const describeWithAccounts = (scenario, cb) => {
Mocha.describe(scenario, cb.bind(mocha, accounts));
};
mocha.suite.on('pre-require', () => {
global.describe = describeWithAccounts;
global.contract = describeWithAccounts;
global.config = config;
});
global.artifacts = {
require: (artifactName) => MochaTestRunner.requireArtifact(
artifactName,
compiledContracts
)
};
mocha.suite.timeout(TEST_TIMEOUT);
mocha.addFile(file);
mocha.run((failures) => {
Module.prototype.require = MochaTestRunner.originalRequire;
seriesCb(null, failures);
});
});
};
});
async.series(fns, next);
}
], (err) => {
this.embark.config.plugins.runActionsForEvent('tests:finished', () => {
Module.prototype.require = MochaTestRunner.originalRequire;
cb(err);
});
});
}
}
module.exports = MochaTestRunner;