Skip to content

Commit 3f59e8a

Browse files
authored
Add --file command option (for js test subsets) (#387)
1 parent d53a44f commit 3f59e8a

File tree

17 files changed

+442
-7
lines changed

17 files changed

+442
-7
lines changed

dist/truffle.plugin.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const dir = require('node-dir');
3333
const Web3 = require('web3');
3434
const util = require('util');
3535
const ganache = require('ganache-core-sc');
36+
const globby = require('globby');
3637

3738
async function plugin(truffleConfig){
3839
let app;
@@ -116,16 +117,21 @@ async function plugin(truffleConfig){
116117
// Finish
117118
await app.cleanUp();
118119

119-
if (error !== undefined) throw new Error(error)
120+
if (error !== undefined) throw error;
120121
if (failures > 0) throw new Error(`${failures} test(s) failed under coverage.`)
121122
}
122123

123124
// -------------------------------------- Helpers --------------------------------------------------
124125

125126
function tests(truffle){
127+
let target;
128+
129+
(typeof truffle.file === 'string')
130+
? target = globby.sync([truffle.file])
131+
: target = dir.files(truffle.test_directory, { sync: true }) || [];
132+
126133
const regex = /.*\.(js|ts|es|es6|jsx|sol)$/;
127-
const files = dir.files(truffle.test_directory, { sync: true }) || [];
128-
return files.filter(f => f.match(regex) != null);
134+
return target.filter(f => f.match(regex) != null);
129135
}
130136

131137

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"death": "^1.1.0",
2525
"ganache-core-sc": "2.7.0-sc.0",
26+
"globby": "^10.0.1",
2627
"istanbul": "^0.4.5",
2728
"node-dir": "^0.1.17",
2829
"req-cwd": "^1.0.1",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
silent: process.env.SILENT ? true : false,
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pragma solidity ^0.5.0;
2+
3+
4+
contract ContractA {
5+
uint x;
6+
constructor() public {
7+
}
8+
9+
function sendFn() public {
10+
x = 5;
11+
}
12+
13+
function callFn() public pure returns (uint){
14+
uint y = 5;
15+
return y;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pragma solidity ^0.5.0;
2+
3+
4+
contract ContractB {
5+
uint x;
6+
constructor() public {
7+
}
8+
9+
function sendFn() public {
10+
x = 5;
11+
}
12+
13+
function callFn() public pure returns (uint){
14+
uint y = 5;
15+
return y;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pragma solidity ^0.5.0;
2+
3+
4+
contract ContractC {
5+
uint x;
6+
constructor() public {
7+
}
8+
9+
function sendFn() public {
10+
x = 5;
11+
}
12+
13+
function callFn() public pure returns (uint){
14+
uint y = 5;
15+
return y;
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity >=0.4.21 <0.6.0;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
constructor() public {
8+
owner = msg.sender;
9+
}
10+
11+
modifier restricted() {
12+
if (msg.sender == owner) _;
13+
}
14+
15+
function setCompleted(uint completed) public restricted {
16+
last_completed_migration = completed;
17+
}
18+
19+
function upgrade(address new_address) public restricted {
20+
Migrations upgraded = Migrations(new_address);
21+
upgraded.setCompleted(last_completed_migration);
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Migrations = artifacts.require("Migrations");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const ContractA = artifacts.require("ContractA");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(ContractA);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const ContractB = artifacts.require("ContractB");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(ContractB);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const ContractC = artifacts.require("ContractC");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(ContractC);
5+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const ContractB = artifacts.require("ContractB");
2+
3+
contract("contractB", function(accounts) {
4+
let instance;
5+
6+
before(async () => instance = await ContractB.deployed())
7+
8+
it('sends', async function(){
9+
await instance.sendFn();
10+
});
11+
12+
it('calls', async function(){
13+
await instance.callFn();
14+
})
15+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const ContractC = artifacts.require("ContractC");
2+
3+
contract("contractc", function(accounts) {
4+
let instance;
5+
6+
before(async () => instance = await ContractC.deployed())
7+
8+
it('sends', async function(){
9+
await instance.sendFn();
10+
});
11+
12+
it('calls', async function(){
13+
await instance.callFn();
14+
})
15+
16+
it('sends', async function(){
17+
await instance.sendFn();
18+
});
19+
20+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const ContractA = artifacts.require("ContractA");
2+
3+
contract("contracta", function(accounts) {
4+
let instance;
5+
6+
before(async () => instance = await ContractA.deployed())
7+
8+
it('sends', async function(){
9+
await instance.sendFn();
10+
});
11+
12+
it('calls', async function(){
13+
await instance.callFn();
14+
})
15+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Use this file to configure your truffle project. It's seeded with some
3+
* common settings for different networks and features like migrations,
4+
* compilation and testing. Uncomment the ones you need or modify
5+
* them to suit your project as necessary.
6+
*
7+
* More information about configuration can be found at:
8+
*
9+
* truffleframework.com/docs/advanced/configuration
10+
*
11+
* To deploy via Infura you'll need a wallet provider (like truffle-hdwallet-provider)
12+
* to sign your transactions before they're sent to a remote public node. Infura accounts
13+
* are available for free at: infura.io/register.
14+
*
15+
* You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
16+
* public/private key pairs. If you're publishing your code to GitHub make sure you load this
17+
* phrase from a file you've .gitignored so it doesn't accidentally become public.
18+
*
19+
*/
20+
21+
// const HDWalletProvider = require('truffle-hdwallet-provider');
22+
// const infuraKey = "fj4jll3k.....";
23+
//
24+
// const fs = require('fs');
25+
// const mnemonic = fs.readFileSync(".secret").toString().trim();
26+
27+
module.exports = {
28+
/**
29+
* Networks define how you connect to your ethereum client and let you set the
30+
* defaults web3 uses to send transactions. If you don't specify one truffle
31+
* will spin up a development blockchain for you on port 9545 when you
32+
* run `develop` or `test`. You can ask a truffle command to use a specific
33+
* network from the command line, e.g
34+
*
35+
* $ truffle test --network <network-name>
36+
*/
37+
38+
networks: {
39+
// Useful for testing. The `development` name is special - truffle uses it by default
40+
// if it's defined here and no other network is specified at the command line.
41+
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
42+
// tab if you use this network and you must also set the `host`, `port` and `network_id`
43+
// options below to some value.
44+
//
45+
// development: {
46+
// host: "127.0.0.1", // Localhost (default: none)
47+
// port: 8545, // Standard Ethereum port (default: none)
48+
// network_id: "*", // Any network (default: none)
49+
// },
50+
51+
// Another network with more advanced options...
52+
// advanced: {
53+
// port: 8777, // Custom port
54+
// network_id: 1342, // Custom network
55+
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
56+
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
57+
// from: <address>, // Account to send txs from (default: accounts[0])
58+
// websockets: true // Enable EventEmitter interface for web3 (default: false)
59+
// },
60+
61+
// Useful for deploying to a public network.
62+
// NB: It's important to wrap the provider as a function.
63+
// ropsten: {
64+
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
65+
// network_id: 3, // Ropsten's id
66+
// gas: 5500000, // Ropsten has a lower block limit than mainnet
67+
// confirmations: 2, // # of confs to wait between deployments. (default: 0)
68+
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
69+
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
70+
// },
71+
72+
// Useful for private networks
73+
// private: {
74+
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
75+
// network_id: 2111, // This network is yours, in the cloud.
76+
// production: true // Treats this network as if it was a public net. (default: false)
77+
// }
78+
},
79+
80+
// Set default mocha options here, use special reporters etc.
81+
mocha: {
82+
// timeout: 100000
83+
},
84+
85+
// Configure your compilers
86+
compilers: {
87+
solc: {
88+
// version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)
89+
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
90+
// settings: { // See the solidity docs for advice about optimization and evmVersion
91+
// optimizer: {
92+
// enabled: false,
93+
// runs: 200
94+
// },
95+
// evmVersion: "byzantium"
96+
// }
97+
}
98+
}
99+
}

test/units/app.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,38 @@ describe('app', function() {
9696
await plugin(truffleConfig);
9797
});
9898

99-
it('project with node_modules packages and relative path solidity imports', async function() {
99+
it('project with relative path solidity imports', async function() {
100100
assertCleanInitialState();
101101
mock.installFullProject('import-paths');
102102
await plugin(truffleConfig);
103103
});
104104

105+
it('truffle run coverage --file test/<fileName>', async function() {
106+
assertCleanInitialState();
107+
108+
const testPath = path.join(truffleConfig.working_directory, 'test/specific_a.js');
109+
truffleConfig.file = testPath;
110+
mock.installFullProject('test-files');
111+
await plugin(truffleConfig);
112+
});
113+
114+
it('truffle run coverage --file test/<glob*>', async function() {
115+
assertCleanInitialState();
116+
117+
const testPath = path.join(truffleConfig.working_directory, 'test/globby*');
118+
truffleConfig.file = testPath;
119+
mock.installFullProject('test-files');
120+
await plugin(truffleConfig);
121+
});
122+
123+
it('truffle run coverage --file test/gl{o,b}*.js', async function() {
124+
assertCleanInitialState();
125+
126+
const testPath = path.join(truffleConfig.working_directory, 'test/gl{o,b}*.js');
127+
truffleConfig.file = testPath;
128+
mock.installFullProject('test-files');
129+
await plugin(truffleConfig);
130+
});
105131

106132
it('contract only uses ".call"', async function(){
107133
assertCleanInitialState();
@@ -197,7 +223,7 @@ describe('app', function() {
197223
await plugin(truffleConfig);
198224
assert.fail()
199225
} catch(err){
200-
assert(err.message.includes('SyntaxError'));
226+
assert(err.toString().includes('SyntaxError'));
201227
}
202228
});
203229

@@ -211,7 +237,7 @@ describe('app', function() {
211237
await plugin(truffleConfig);
212238
assert.fail()
213239
} catch(err){
214-
assert(err.message.includes('Compilation failed'));
240+
assert(err.toString().includes('Compilation failed'));
215241
}
216242

217243
assertCoverageNotGenerated(truffleConfig);

0 commit comments

Comments
 (0)