Skip to content

Add onServerReady, onTestsComplete, onIstanbulComplete Hooks #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module.exports = {
| skipFiles | *Array* | `['Migrations.sol']` | Array of contracts or folders (with paths expressed relative to the `contracts` directory) that should be skipped when doing instrumentation. `Migrations.sol` is skipped by default, and does not need to be added to this configuration option if it is used. |
| istanbulReporter | *Array* | ['html', 'lcov', 'text'] | Coverage reporters for Istanbul. Optional reporter replaces the default reporters. |
| silent | *Boolean* | false | suppress logging output |
| onServerReady | *Function* | `async (config) => {}` | Function run when server is ready to receive calls, but before the tests execute. Useful if you need to launch the Oraclize/Provable bridge or have preparatory scripts which rely on the server's availability |
| onTestsComplete | *Function* | `async (config) => {}` | Function run immediately after the tests
complete, but before Istanbul reports are generated. |
| onIstanbulComplete | *Function* | `async (config) => {}` | Function run immediately after the Istanbul reports are generated, but before the ganache server is shut down. Useful if you need to clean resources up. |



### FAQ

Expand Down
7 changes: 6 additions & 1 deletion dist/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ async function plugin(config){
config.networks[config.network].port
]);

// Run post-launch server hook;
await api.onServerReady(config);

// Instrument
let {
targets,
Expand Down Expand Up @@ -97,8 +100,10 @@ async function plugin(config){
} catch (e) {
error = e.stack;
}
// Run Istanbul

await api.onTestsComplete(config);
await api.report();
await api.onIstanbulComplete(config);

} catch(e){
error = e;
Expand Down
6 changes: 6 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class API {
this.cwd = config.cwd || process.cwd();
this.originalContractsDir = config.originalContractsDir

this.defaultHook = () => {};
this.onServerReady = config.onServerReady || this.defaultHook;
this.onTestsComplete = config.onTestsComplete || this.defaultHook;
this.onIstanbulComplete = config.onIstanbulComplete || this.defaultHook;

this.server = null;
this.provider = null;
this.defaultPort = 8555;
Expand All @@ -40,6 +45,7 @@ class API {
this.host = config.host || "127.0.0.1";
this.providerOptions = config.providerOptions || {};


this.skipFiles = config.skipFiles || [];

this.log = config.log || console.log;
Expand Down
8 changes: 7 additions & 1 deletion test/integration/projects/test-files/.solcover.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// Testing hooks
const fn = (msg, config) => config.logger.log(msg);

module.exports = {
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text']
istanbulReporter: ['json-summary', 'text'],
onServerReady: fn.bind(null, 'running onServerReady'),
onTestsComplete: fn.bind(null, 'running onTestsComplete'),
onIstanbulComplete: fn.bind(null, 'running onIstanbulComplete')
}
17 changes: 17 additions & 0 deletions test/units/truffle/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ describe('Truffle Plugin: standard use cases', function() {
verify.coverageMissing(missing);
});

it('uses "onServerReady", "onTestsComplete", "onIstanbulComplete"', async function() {
verify.cleanInitialState();

truffleConfig.logger = mock.testLogger;
mock.installFullProject('test-files');

await plugin(truffleConfig);

assert(
mock.loggerOutput.val.includes('running onServerReady') &&
mock.loggerOutput.val.includes('running onTestsComplete') &&
mock.loggerOutput.val.includes('running onIstanbulComplete'),

`Should run "on" hooks : ${mock.loggerOutput.val}`
);
});

it('project with relative path solidity imports', async function() {
verify.cleanInitialState();
mock.installFullProject('import-paths');
Expand Down