From bfcb67dbcd4bd709d8cc4be310e3d6e5d92e9bc2 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 20 Jun 2017 12:19:12 -0700 Subject: [PATCH] Add (disabled) events filter unit test --- bin/exec.js | 7 ++++++- test/cli.js | 28 ++++++++++++++++++++++++++-- test/cli/events.js | 24 ++++++++++++++++++++++++ test/cli/testrpc-options.js | 4 +--- test/sources/cli/Events.sol | 17 +++++++++++++++++ 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 test/cli/events.js create mode 100644 test/sources/cli/Events.sol diff --git a/bin/exec.js b/bin/exec.js index 372c060..3df2650 100644 --- a/bin/exec.js +++ b/bin/exec.js @@ -25,10 +25,11 @@ const coverageDir = './coverageEnv'; // Env that instrumented .sols are t // Options let coverageOption = '--network coverage'; // Default truffle network execution flag let silence = ''; // Default log level: configurable by --silence -let log = console.log; // Default log level: configurable by --silence +let log = console.log; let testrpcProcess; // ref to testrpc server we need to close on exit let events; // ref to string loaded from 'allFiredEvents' +let testsErrored = null; // flag set to non-null if truffle tests error // --------------------------------------- Utilities ----------------------------------------------- /** @@ -47,6 +48,9 @@ function cleanUp(err) { if (err) { log(`${err}\nExiting without generating coverage...`); process.exit(1); + } else if (testsErrored) { + log('Some truffle tests failed while running coverage'); + process.exit(1); } else { process.exit(0); } @@ -161,6 +165,7 @@ try { const command = config.testCommand || defaultCommand; shell.cd('./coverageEnv'); shell.exec(command); + testsErrored = shell.error(); shell.cd('./..'); } catch (err) { cleanUp(err); diff --git a/test/cli.js b/test/cli.js index 5d4652e..50feeca 100644 --- a/test/cli.js +++ b/test/cli.js @@ -170,6 +170,30 @@ describe('cli', () => { collectGarbage(); }); + it('contract tests events: tests should pass without errors', () => { + assert(pathExists('./coverage') === false, 'should start without: coverage'); + assert(pathExists('./coverage.json') === false, 'should start without: coverage.json'); + + mock.install('Events.sol', 'events.js', config); + shell.exec(script); + + // **** DISABLED PENDING TESTRPC FILTER WORK ***** + // 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 uses inheritance: should generate coverage, cleanup & exit(0)', () => { // Run against a contract that 'is' another contract assert(pathExists('./coverage') === false, 'should start without: coverage'); @@ -191,14 +215,14 @@ describe('cli', () => { collectGarbage(); }); - it('truffle tests failing: should generate coverage, cleanup & exit(0)', () => { + it('truffle tests failing: should generate coverage, cleanup & exit(1)', () => { assert(pathExists('./coverage') === false, 'should start without: coverage'); assert(pathExists('./coverage.json') === false, 'should start without: coverage.json'); // Run with Simple.sol and a failing assertion in a truffle test mock.install('Simple.sol', 'truffle-test-fail.js', config); shell.exec(script); - assert(shell.error() === null, 'script should not error'); + assert(shell.error() !== null, 'script should exit 1'); assert(pathExists('./coverage') === true, 'script should gen coverage folder'); assert(pathExists('./coverage.json') === true, 'script should gen coverage.json'); diff --git a/test/cli/events.js b/test/cli/events.js new file mode 100644 index 0000000..6eea6a1 --- /dev/null +++ b/test/cli/events.js @@ -0,0 +1,24 @@ +/* eslint-env node, mocha */ +/* global artifacts, contract, assert */ + +const Events = artifacts.require('./Events.sol'); + +contract('Events', () => { + it('logs events correctly', done => { + const loggedEvents = []; + Events.deployed().then(instance => { + const allEvents = instance.allEvents(); + allEvents.watch((error, event) => { loggedEvents.push(event); }); + + instance.test(5).then(() => { + if (loggedEvents.length > 2) { + assert(false, 'Did not filter events correctly'); + } else { + assert(true); + } + + done(); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/cli/testrpc-options.js b/test/cli/testrpc-options.js index 704096a..b4c455d 100644 --- a/test/cli/testrpc-options.js +++ b/test/cli/testrpc-options.js @@ -6,9 +6,7 @@ const Simple = artifacts.require('./Simple.sol'); contract('Simple', accounts => { // Crash truffle if the account loaded in the options string isn't found here. it('should load with expected account', () => { - if (accounts[0] !== '0xa4860cedd5143bd63f347cab453bf91425f8404f') { - process.exit(1); - } + assert(accounts[0] === '0xa4860cedd5143bd63f347cab453bf91425f8404f'); }); // Generate some coverage so the script doesn't exit(1) because there are no events diff --git a/test/sources/cli/Events.sol b/test/sources/cli/Events.sol new file mode 100644 index 0000000..f8cdbde --- /dev/null +++ b/test/sources/cli/Events.sol @@ -0,0 +1,17 @@ +pragma solidity ^0.4.3; + +contract Events { + uint x = 0; + event LogEventOne( uint x, address y); + event LogEventTwo( uint x, address y); + + function test(uint val) { + LogEventOne(100, msg.sender); + x = x + val; + LogEventTwo(200, msg.sender); + } + + function getX() returns (uint){ + return x; + } +} \ No newline at end of file