Skip to content

Commit

Permalink
Use recursive-readdir to resolve test files (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke authored Nov 30, 2019
1 parent 40c7ce3 commit f7603ac
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"istanbul": "^0.4.5",
"jsonschema": "^1.2.4",
"lodash": "^4.17.15",
"node-dir": "^0.1.17",
"node-emoji": "^1.10.0",
"pify": "^4.0.1",
"recursive-readdir": "^2.2.2",
"shelljs": "^0.8.3",
"solidity-parser-antlr": "0.4.7",
"web3": "1.2.1",
Expand Down
7 changes: 3 additions & 4 deletions plugins/resources/truffle.utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const PluginUI = require('./truffle.ui');

const globalModules = require('global-modules');
const TruffleProvider = require('@truffle/provider');
const dir = require('node-dir');
const recursive = require('recursive-readdir');
const globby = require('globby');
const path = require('path');

Expand All @@ -15,15 +14,15 @@ const path = require('path');
* @param {Object} config truffleConfig
* @return {String[]} list of files to pass to mocha
*/
function getTestFilePaths(config){
async function getTestFilePaths(config){
let target;
let ui = new PluginUI(config.logger.log);


// Handle --file <path|glob> cli option (subset of tests)
(typeof config.file === 'string')
? target = globby.sync([config.file])
: target = dir.files(config.testDir, { sync: true }) || [];
: target = await recursive(config.testDir);

// Filter native solidity tests and warn that they're skipped
const solregex = /.*\.(sol)$/;
Expand Down
2 changes: 1 addition & 1 deletion plugins/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function plugin(config){
);

config.all = true;
config.test_files = truffleUtils.getTestFilePaths(config);
config.test_files = await truffleUtils.getTestFilePaths(config);
config.compilers.solc.settings.optimizer.enabled = false;

// Compile Instrumented Contracts
Expand Down
4 changes: 4 additions & 0 deletions test/integration/projects/tests-folder/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"silent": false,
"istanbulReporter": [ "json-summary", "text"]
}
8 changes: 8 additions & 0 deletions test/integration/projects/tests-folder/buidler.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");

module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};
17 changes: 17 additions & 0 deletions test/integration/projects/tests-folder/contracts/ContractA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.5.0;


contract ContractA {
uint x;
constructor() public {
}

function sendFn() public {
x = 5;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}
17 changes: 17 additions & 0 deletions test/integration/projects/tests-folder/contracts/ContractB.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.5.0;


contract ContractB {
uint x;
constructor() public {
}

function sendFn() public {
x = 5;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}
17 changes: 17 additions & 0 deletions test/integration/projects/tests-folder/contracts/ContractC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.5.0;


contract ContractC {
uint x;
constructor() public {
}

function sendFn() public {
x = 5;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}
23 changes: 23 additions & 0 deletions test/integration/projects/tests-folder/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity >=0.4.21 <0.6.0;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
15 changes: 15 additions & 0 deletions test/integration/projects/tests-folder/test/contracta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ContractA = artifacts.require("ContractA");

contract("contracta", function(accounts) {
let instance;

before(async () => instance = await ContractA.new())

it('sends [ @skipForCoverage ]', async function(){
await instance.sendFn();
});

it('calls [ @skipForCoverage ]', async function(){
await instance.callFn();
})
});
15 changes: 15 additions & 0 deletions test/integration/projects/tests-folder/test/folder/contractb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ContractB = artifacts.require("ContractB");

contract("contractB [ @skipForCoverage ]", function(accounts) {
let instance;

before(async () => instance = await ContractB.new())

it('sends', async function(){
await instance.sendFn();
});

it('calls', async function(){
await instance.callFn();
})
});
20 changes: 20 additions & 0 deletions test/integration/projects/tests-folder/test/folder/contractc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ContractC = artifacts.require("ContractC");

contract("contractc", function(accounts) {
let instance;

before(async () => instance = await ContractC.new())

it('sends', async function(){
await instance.sendFn();
});

it('calls', async function(){
await instance.callFn();
})

it('sends', async function(){
await instance.sendFn();
});

});
7 changes: 7 additions & 0 deletions test/integration/projects/tests-folder/truffle-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
networks: {},
mocha: {},
compilers: {
solc: {}
}
}
23 changes: 23 additions & 0 deletions test/units/truffle/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ describe('Truffle Plugin: standard use cases', function() {
verify.lineCoverage(expected);
});


it('tests in first layer and in a sub-folder', async function() {
mock.installFullProject('tests-folder');
await plugin(truffleConfig);

const expected = [
{
file: mock.pathToContract(truffleConfig, 'ContractA.sol'),
pct: 100
},
{
file: mock.pathToContract(truffleConfig, 'ContractB.sol'),
pct: 100,
},
{
file: mock.pathToContract(truffleConfig, 'ContractC.sol'),
pct: 100,
},
];

verify.lineCoverage(expected);
});

it('with relative path solidity imports', async function() {
mock.installFullProject('import-paths');
await plugin(truffleConfig);
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5038,7 +5038,7 @@ minimalistic-crypto-utils@^1.0.0, minimalistic-crypto-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=

"minimatch@2 || 3", [email protected], minimatch@^3.0.2, minimatch@^3.0.4:
"minimatch@2 || 3", [email protected], minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
Expand Down Expand Up @@ -5229,13 +5229,6 @@ node-alias@^1.0.4:
chalk "^1.1.1"
lodash "^4.2.0"

node-dir@^0.1.17:
version "0.1.17"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5"
integrity sha1-X1Zl2TNRM1yqvvjxxVRRbPXx5OU=
dependencies:
minimatch "^3.0.2"

node-emoji@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.10.0.tgz#8886abd25d9c7bb61802a658523d1f8d2a89b2da"
Expand Down Expand Up @@ -6222,6 +6215,13 @@ rechoir@^0.6.2:
dependencies:
resolve "^1.1.6"

recursive-readdir@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
integrity sha512-nRCcW9Sj7NuZwa2XvH9co8NPeXUBhZP7CRKJtU+cS6PW9FpCIFoI5ib0NT1ZrbNuPoRy0ylyCaUL8Gih4LSyFg==
dependencies:
minimatch "3.0.4"

regenerate@^1.2.1:
version "1.4.0"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
Expand Down

0 comments on commit f7603ac

Please sign in to comment.