Skip to content

Commit

Permalink
Unit test copying project into env (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Jun 26, 2017
1 parent 82cac37 commit 058b10e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
23 changes: 23 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ describe('cli', () => {
collectGarbage();
});

it('tests require assets outside of test folder: should generate coverage, cleanup & exit(0)', () => {
// Directory should be clean
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');

// Run script (exits 0);
mock.install('Simple.sol', 'requires-externally.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');

// Directory should have coverage report
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');

// Coverage should be real.
// This test is tightly bound to the function names in Simple.sol
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
collectGarbage();
});

it('contract only uses .call: should generate coverage, cleanup & exit(0)', () => {
// Run against contract that only uses method.call.
assert(pathExists('./coverage') === false, 'should start without: coverage');
Expand Down
11 changes: 8 additions & 3 deletions test/util/mockTruffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
deployer.deploy(contract);
};`;

// Mock external asset
const asset = 'module.exports = { value: true };';

// Mock truffle.js
const trufflejs = _trufflejs ||

Expand All @@ -46,6 +49,8 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/assets');
shell.mkdir('./mock/node_modules');
shell.mkdir('./mock/test');

if (Array.isArray(contract)) {
Expand All @@ -60,15 +65,15 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
fs.writeFileSync('./mock/truffle.js', trufflejs);
fs.writeFileSync('./mock/assets/asset.js', asset);
fs.writeFileSync('./.solcover.js', configjs);
shell.cp(`./test/cli/${test}`, `./mock/test/${test}`);
};

/**
* Installs mock truffle project at ./mock with a single contract
* Installs mock truffle project at ./mock with two contracts - one inherits from the other
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
* @param {config} .solcover.js configuration
*/
module.exports.installInheritanceTest = function installInheritanceTest(config) {
shell.mkdir('./mock');
Expand Down

0 comments on commit 058b10e

Please sign in to comment.