From 05e5538b5030ad1fd2f4c98ffcbd7573e23722b5 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Mon, 26 Jun 2017 13:22:43 -0700 Subject: [PATCH] Unit test copying project into env (#39) --- test/cli.js | 23 +++++++++++++++++++++++ test/cli/requires-externally.js | 17 +++++++++++++++++ test/util/mockTruffle.js | 11 ++++++++--- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 test/cli/requires-externally.js diff --git a/test/cli.js b/test/cli.js index d968194..186a02f 100644 --- a/test/cli.js +++ b/test/cli.js @@ -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'); diff --git a/test/cli/requires-externally.js b/test/cli/requires-externally.js new file mode 100644 index 0000000..911601f --- /dev/null +++ b/test/cli/requires-externally.js @@ -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; + }); + }); +}); \ No newline at end of file diff --git a/test/util/mockTruffle.js b/test/util/mockTruffle.js index c28c331..2d54aab 100644 --- a/test/util/mockTruffle.js +++ b/test/util/mockTruffle.js @@ -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 || @@ -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)) { @@ -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 located in /test/sources/cli/ - * @param {[type]} test located in /test/cli/ + * @param {config} .solcover.js configuration */ module.exports.installInheritanceTest = function installInheritanceTest(config) { shell.mkdir('./mock');