Skip to content

Commit ce2e6c3

Browse files
authored
Add tests for buidler cli options (#435)
1 parent 9d98f3f commit ce2e6c3

File tree

12 files changed

+223
-72
lines changed

12 files changed

+223
-72
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ truffle run coverage [command-options]
3636
+ Coverage launches its own in-process ganache server.
3737
+ You can set [ganache options][1] using the `providerOptions` key in your `.solcover.js` [config][15].
3838
+ Coverage [distorts gas consumption][13]. Tests that check exact gas consumption should be [skipped][24].
39-
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]
39+
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]
4040

4141
## Command Options
4242
| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> |
4343
|--------------|------------------------------------|--------------------------------|
44-
| file | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
44+
| file (Truffle) | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
45+
| testFiles (Buidler) | `--testFiles test/file.js` | JS test file(s) to run.|
4546
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
46-
| network | `--network development` | Use network settings defined in the Truffle config |
47+
| network | `--network development` | Use network settings defined in the Truffle or Buidler config |
4748
| temp[<sup>*</sup>][14] | `--temp build` | :warning: **Caution** :warning: Path to a *disposable* folder to store compilation artifacts in. Useful when your test setup scripts include hard-coded paths to a build directory. [More...][14] |
48-
| version | | Version info |
49-
| help | | Usage notes |
5049

5150
[<sup>*</sup> Advanced use][14]
5251

dist/buidler.plugin.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ const {
1717
TASK_COMPILE,
1818
} = require("@nomiclabs/buidler/builtin-tasks/task-names");
1919

20+
ensurePluginLoadedWithUsePlugin();
21+
2022
function plugin() {
2123

2224
// UI for the task flags...
2325
const ui = new PluginUI();
2426

2527
task("coverage", "Generates a code coverage report for tests")
2628

27-
.addOptionalParam("file", ui.flags.file, null, types.string)
29+
.addOptionalParam("testFiles", ui.flags.file, null, types.string)
2830
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, null, types.string)
2931
.addOptionalParam('temp', ui.flags.temp, null, types.string)
3032

31-
.setAction(async function(taskArguments, env){
33+
.setAction(async function(args, env){
3234
let error;
3335
let ui;
3436
let api;
@@ -37,15 +39,19 @@ function plugin() {
3739
try {
3840
death(buidlerUtils.finish.bind(null, config, api)); // Catch interrupt signals
3941

40-
config = buidlerUtils.normalizeConfig(env.config);
42+
config = buidlerUtils.normalizeConfig(env.config, args);
4143
ui = new PluginUI(config.logger.log);
4244
api = new API(utils.loadSolcoverJS(config));
4345

4446
// ==============
4547
// Server launch
4648
// ==============
47-
48-
const network = buidlerUtils.setupNetwork(env, api);
49+
const network = buidlerUtils.setupNetwork(
50+
env,
51+
api,
52+
args,
53+
ui
54+
);
4955

5056
const address = await api.ganache(ganache);
5157
const web3 = new Web3(address);
@@ -88,12 +94,14 @@ function plugin() {
8894
// ==============
8995
// Compilation
9096
// ==============
97+
config.temp = args.temp;
9198

9299
const {
93100
tempArtifactsDir,
94101
tempContractsDir
95102
} = utils.getTempLocations(config);
96103

104+
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
97105
utils.save(targets, config.paths.sources, tempContractsDir);
98106
utils.save(skipped, config.paths.sources, tempContractsDir);
99107

@@ -109,7 +117,7 @@ function plugin() {
109117
// ======
110118
// Tests
111119
// ======
112-
const testFiles = buidlerUtils.getTestFilePaths(config);
120+
const testFiles = args.testFiles ? [args.testFiles] : [];
113121

114122
try {
115123
await env.run(TASK_TEST, {testFiles: testFiles})
@@ -128,7 +136,7 @@ function plugin() {
128136
error = e;
129137
}
130138

131-
await utils.finish(config, api);
139+
await buidlerUtils.finish(config, api);
132140

133141
if (error !== undefined ) throw error;
134142
if (process.exitCode > 0) throw new Error(ui.generate('tests-fail', [process.exitCode]));

dist/plugin-assets/buidler.ui.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class PluginUI extends UI {
4545
`\n${c.bold('============')}\n` +
4646
`${ct} ${c.bold('port')}: ${args[1]}\n` +
4747
`${ct} ${c.bold('network')}: ${args[0]}\n`,
48+
49+
'port-clash': `${w} ${c.red("The 'port' values in your Buidler url ")}` +
50+
`${c.red("and .solcover.js are different. Using Buidler's: ")} ${c.bold(args[0])}.\n`,
51+
4852
}
4953

5054
this._write(kinds[kind]);
@@ -70,8 +74,6 @@ class PluginUI extends UI {
7074

7175
'tests-fail': `${x} ${c.bold(args[0])} ${c.red('test(s) failed under coverage.')}`,
7276

73-
'no-network': `${c.red('Network: ')} ${args[0]} ` +
74-
`${c.red(' is not defined in your truffle-config networks. ')}`,
7577

7678
}
7779

dist/plugin-assets/buidler.utils.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,44 @@ const { createProvider } = require("@nomiclabs/buidler/internal/core/providers/c
1010
// Buidler Specific Plugin Utils
1111
// =============================
1212

13-
/**
14-
* Returns a list of test files to pass to TASK_TEST.
15-
* @param {BuidlerConfig} config
16-
* @return {String[]} list of files to pass to mocha
17-
*/
18-
function getTestFilePaths(config){
19-
let target;
20-
21-
// Handle --file <path|glob> cli option (subset of tests)
22-
(typeof config.file === 'string')
23-
? target = globby.sync([config.file])
24-
: target = [];
25-
26-
// Return list of test files
27-
const testregex = /.*\.(js|ts|es|es6|jsx)$/;
28-
return target.filter(f => f.match(testregex) != null);
29-
}
30-
3113
/**
3214
* Normalizes buidler paths / logging for use by the plugin utilities and
3315
* attaches them to the config
3416
* @param {BuidlerConfig} config
3517
* @return {BuidlerConfig} updated config
3618
*/
37-
function normalizeConfig(config){
19+
function normalizeConfig(config, args){
3820
config.workingDir = config.paths.root;
3921
config.contractsDir = config.paths.sources;
4022
config.testDir = config.paths.tests;
4123
config.artifactsDir = config.paths.artifacts;
4224
config.logger = config.logger ? config.logger : {log: null};
25+
config.solcoverjs = args.solcoverjs
4326

4427
return config;
4528
}
4629

47-
function setupNetwork(env, api){
48-
const networkConfig = {
49-
url: `http://${api.host}:${api.port}`,
50-
gas: api.gasLimit,
51-
gasPrice: api.gasPrice
30+
function setupNetwork(env, api, taskArgs, ui){
31+
let networkConfig = {};
32+
33+
if (taskArgs.network){
34+
networkConfig = env.config.networks[taskArgs.network];
35+
36+
const configPort = networkConfig.url.split(':')[2];
37+
38+
// Warn: port conflicts
39+
if (api.port !== api.defaultPort && api.port !== configPort){
40+
ui.report('port-clash', [ configPort ])
41+
}
42+
43+
// Prefer network port
44+
api.port = parseInt(configPort);
5245
}
5346

47+
networkConfig.url = `http://${api.host}:${api.port}`;
48+
networkConfig.gas = api.gasLimit;
49+
networkConfig.gasPrice = api.gasPrice;
50+
5451
const provider = createProvider(api.defaultNetworkName, networkConfig);
5552

5653
env.config.networks[api.defaultNetworkName] = networkConfig;
@@ -102,7 +99,6 @@ module.exports = {
10299
normalizeConfig: normalizeConfig,
103100
finish: finish,
104101
tempCacheDir: tempCacheDir,
105-
getTestFilePaths: getTestFilePaths,
106102
setupNetwork: setupNetwork
107103
}
108104

dist/plugin-assets/plugin.utils.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ function loadSource(_path){
4848
return fs.readFileSync(_path).toString();
4949
}
5050

51+
/**
52+
* Sets up temporary folders for instrumented contracts and their compilation artifacts
53+
* @param {PlatformConfig} config
54+
* @param {String} tempContractsDir
55+
* @param {String} tempArtifactsDir
56+
*/
57+
function setupTempFolders(config, tempContractsDir, tempArtifactsDir){
58+
checkContext(config, tempContractsDir, tempArtifactsDir);
59+
60+
shell.mkdir(tempContractsDir);
61+
shell.mkdir(tempArtifactsDir);
62+
}
63+
5164
/**
5265
* Save a set of instrumented files to a temporary directory.
5366
* @param {Object[]} targets array of targets generated by `assembleTargets`
@@ -122,16 +135,6 @@ function assembleFiles(config, skipFiles=[]){
122135
let skipFolders;
123136
let skipped = [];
124137

125-
const {
126-
tempContractsDir,
127-
tempArtifactsDir
128-
} = getTempLocations(config);
129-
130-
checkContext(config, tempContractsDir, tempArtifactsDir);
131-
132-
shell.mkdir(tempContractsDir);
133-
shell.mkdir(tempArtifactsDir);
134-
135138
targets = shell.ls(`${config.contractsDir}/**/*.sol`);
136139

137140
skipFiles = assembleSkipped(config, targets, skipFiles);
@@ -268,5 +271,6 @@ module.exports = {
268271
reportSkipped: reportSkipped,
269272
save: save,
270273
checkContext: checkContext,
271-
toRelativePath: toRelativePath
274+
toRelativePath: toRelativePath,
275+
setupTempFolders: setupTempFolders
272276
}

dist/truffle.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async function plugin(config){
8282
tempContractsDir
8383
} = utils.getTempLocations(config);
8484

85+
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
8586
utils.save(targets, config.contracts_directory, tempContractsDir);
8687
utils.save(skipped, config.contracts_directory, tempContractsDir);
8788

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"nyc": "SILENT=true nyc --exclude '**/sc_temp/**' --exclude '**/test/**'",
1515
"test": "npm run nyc -- mocha test/units/* --timeout 100000 --no-warnings --exit",
16-
"test:ci": "SILENT=true nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
16+
"test:ci": "SILENT=true node --max-old-space-size=3072 ./node_modules/.bin/nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
1717
"test:debug": "mocha test/units/* --timeout 100000 --no-warnings --exit"
1818
},
1919
"homepage": "https://github.com/sc-forks/solidity-coverage",

test/units/buidler/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs');
33
const path = require('path')
44
const pify = require('pify')
55
const shell = require('shelljs');
6-
const ganache = require('ganache-core-sc');
6+
const ganache = require('ganache-cli')
77

88
const verify = require('../../util/verifiers')
99
const mock = require('../../util/integration');

0 commit comments

Comments
 (0)