Merge pull request #46 from sc-forks/unit-test-39

Unit test copying project into env (#39)
pull/47/head
c-g-e-w-e-k-e- 8 years ago committed by GitHub
commit 8272ccc8f0
  1. 23
      test/cli.js
  2. 17
      test/cli/requires-externally.js
  3. 11
      test/util/mockTruffle.js

@ -180,6 +180,29 @@ describe('cli', () => {
collectGarbage(); 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)', () => { it('contract only uses .call: should generate coverage, cleanup & exit(0)', () => {
// Run against contract that only uses method.call. // Run against contract that only uses method.call.
assert(pathExists('./coverage') === false, 'should start without: coverage'); assert(pathExists('./coverage') === false, 'should start without: coverage');

@ -0,0 +1,17 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const asset = require('../assets/asset.js');
const Simple = artifacts.require('./Simple.sol');
contract('Simple', () => {
it('should be able to require an external asset', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
assert.equal(asset.value, true);
return simple.test(5); // Make sure we generate an event;
});
});
});

@ -29,6 +29,9 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
deployer.deploy(contract); deployer.deploy(contract);
};`; };`;
// Mock external asset
const asset = 'module.exports = { value: true };';
// Mock truffle.js // Mock truffle.js
const trufflejs = _trufflejs || const trufflejs = _trufflejs ||
@ -46,6 +49,8 @@ module.exports.install = function install(contract, test, config, _trufflejs) {
shell.mkdir('./mock'); shell.mkdir('./mock');
shell.mkdir('./mock/contracts'); shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations'); shell.mkdir('./mock/migrations');
shell.mkdir('./mock/assets');
shell.mkdir('./mock/node_modules');
shell.mkdir('./mock/test'); shell.mkdir('./mock/test');
if (Array.isArray(contract)) { if (Array.isArray(contract)) {
@ -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/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts); fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
fs.writeFileSync('./mock/truffle.js', trufflejs); fs.writeFileSync('./mock/truffle.js', trufflejs);
fs.writeFileSync('./mock/assets/asset.js', asset);
fs.writeFileSync('./.solcover.js', configjs); fs.writeFileSync('./.solcover.js', configjs);
shell.cp(`./test/cli/${test}`, `./mock/test/${test}`); 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. * and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/ * @param {config} .solcover.js configuration
* @param {[type]} test <testName.js> located in /test/cli/
*/ */
module.exports.installInheritanceTest = function installInheritanceTest(config) { module.exports.installInheritanceTest = function installInheritanceTest(config) {
shell.mkdir('./mock'); shell.mkdir('./mock');

Loading…
Cancel
Save