Skip to content

Commit 05e5538

Browse files
committed
Unit test copying project into env (#39)
1 parent 82cac37 commit 05e5538

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

test/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ describe('cli', () => {
180180
collectGarbage();
181181
});
182182

183+
it('tests require assets outside of test folder: should generate coverage, cleanup & exit(0)', () => {
184+
// Directory should be clean
185+
assert(pathExists('./coverage') === false, 'should start without: coverage');
186+
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
187+
188+
// Run script (exits 0);
189+
mock.install('Simple.sol', 'requires-externally.js', config);
190+
shell.exec(script);
191+
assert(shell.error() === null, 'script should not error');
192+
193+
// Directory should have coverage report
194+
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
195+
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
196+
197+
// Coverage should be real.
198+
// This test is tightly bound to the function names in Simple.sol
199+
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
200+
const path = Object.keys(produced)[0];
201+
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
202+
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
203+
collectGarbage();
204+
});
205+
183206
it('contract only uses .call: should generate coverage, cleanup & exit(0)', () => {
184207
// Run against contract that only uses method.call.
185208
assert(pathExists('./coverage') === false, 'should start without: coverage');

test/cli/requires-externally.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-env node, mocha */
2+
/* global artifacts, contract, assert */
3+
4+
const asset = require('../assets/asset.js');
5+
6+
const Simple = artifacts.require('./Simple.sol');
7+
8+
contract('Simple', () => {
9+
it('should be able to require an external asset', () => {
10+
let simple;
11+
return Simple.deployed().then(instance => {
12+
simple = instance;
13+
assert.equal(asset.value, true);
14+
return simple.test(5); // Make sure we generate an event;
15+
});
16+
});
17+
});

test/util/mockTruffle.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
2929
deployer.deploy(contract);
3030
};`;
3131

32+
// Mock external asset
33+
const asset = 'module.exports = { value: true };';
34+
3235
// Mock truffle.js
3336
const trufflejs = _trufflejs ||
3437

@@ -46,6 +49,8 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
4649
shell.mkdir('./mock');
4750
shell.mkdir('./mock/contracts');
4851
shell.mkdir('./mock/migrations');
52+
shell.mkdir('./mock/assets');
53+
shell.mkdir('./mock/node_modules');
4954
shell.mkdir('./mock/test');
5055

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

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

0 commit comments

Comments
 (0)