-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathtruffle.plugin.js
127 lines (98 loc) · 3.39 KB
/
truffle.plugin.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
const API = require('./../lib/api');
const utils = require('./resources/plugin.utils');
const truffleUtils = require('./resources/truffle.utils');
const PluginUI = require('./resources/truffle.ui');
const pkg = require('./../package.json');
const death = require('death');
const path = require('path');
/**
* Truffle Plugin: `truffle run coverage [options]`
* @param {Object} config @truffle/config config
* @return {Promise}
*/
async function plugin(config){
let ui;
let api;
let error;
let truffle;
let testsErrored = false;
try {
death(utils.finish.bind(null, config, api)); // Catch interrupt signals
config = truffleUtils.normalizeConfig(config);
ui = new PluginUI(config.logger.log);
if(config.help) return ui.report('help'); // Exit if --help
truffle = truffleUtils.loadLibrary(config);
api = new API(utils.loadSolcoverJS(config));
truffleUtils.setNetwork(config, api);
// Server launch
const client = api.client || truffle.ganache;
const address = await api.ganache(client);
const accountsRequest = await utils.getAccountsGanache(api.server.provider);
const nodeInfoRequest = await utils.getNodeInfoGanache(api.server.provider);
const ganacheVersion = nodeInfoRequest.result.split('/')[1];
truffleUtils.setNetworkFrom(config, accountsRequest.result);
// Version Info
ui.report('versions', [
truffle.version,
ganacheVersion,
pkg.version
]);
// Exit if --version
if (config.version) return await utils.finish(config, api);
ui.report('network', [
config.network,
config.networks[config.network].network_id,
config.networks[config.network].port
]);
// Run post-launch server hook;
await api.onServerReady(config);
// Instrument
const skipFiles = api.skipFiles || [];
skipFiles.push('Migrations.sol');
let {
targets,
skipped
} = utils.assembleFiles(config, skipFiles);
targets = api.instrument(targets);
utils.reportSkipped(config, skipped);
// Filesystem & Compiler Re-configuration
const {
tempArtifactsDir,
tempContractsDir
} = utils.getTempLocations(config);
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.contracts_directory, tempContractsDir);
utils.save(skipped, config.contracts_directory, tempContractsDir);
config.contracts_directory = tempContractsDir;
config.build_directory = tempArtifactsDir;
config.contracts_build_directory = path.join(
tempArtifactsDir,
path.basename(config.contracts_build_directory)
);
config.all = true;
config.compilers.solc.settings.optimizer.enabled = false;
// Run pre-compile hook;
await api.onPreCompile(config);
// Compile Instrumented Contracts
await truffle.contracts.compile(config);
await api.onCompileComplete(config);
config.test_files = await truffleUtils.getTestFilePaths(config);
// Run tests
try {
failures = await truffle.test.run(config)
} catch (e) {
error = e.stack;
}
await api.onTestsComplete(config);
// Run Istanbul
await api.report();
await api.onIstanbulComplete(config);
} catch(e){
error = e;
}
// Finish
await utils.finish(config, api);
if (error !== undefined) throw error;
if (failures > 0) throw new Error(ui.generate('tests-fail', [failures]));
}
module.exports = plugin;