Add onServerReady, onTestsComplete, onIstanbulComplete Hooks (#408)

pull/410/head
cgewecke 5 years ago committed by GitHub
parent d57d6fdc8b
commit 635d9bcd63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      README.md
  2. 7
      dist/truffle.plugin.js
  3. 6
      lib/api.js
  4. 8
      test/integration/projects/test-files/.solcover.js
  5. 17
      test/units/truffle/standard.js

@ -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. | | 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. | | istanbulReporter | *Array* | ['html', 'lcov', 'text'] | Coverage reporters for Istanbul. Optional reporter replaces the default reporters. |
| silent | *Boolean* | false | suppress logging output | | 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 ### FAQ

@ -58,6 +58,9 @@ async function plugin(config){
config.networks[config.network].port config.networks[config.network].port
]); ]);
// Run post-launch server hook;
await api.onServerReady(config);
// Instrument // Instrument
let { let {
targets, targets,
@ -97,8 +100,10 @@ async function plugin(config){
} catch (e) { } catch (e) {
error = e.stack; error = e.stack;
} }
// Run Istanbul
await api.onTestsComplete(config);
await api.report(); await api.report();
await api.onIstanbulComplete(config);
} catch(e){ } catch(e){
error = e; error = e;

@ -32,6 +32,11 @@ class API {
this.cwd = config.cwd || process.cwd(); this.cwd = config.cwd || process.cwd();
this.originalContractsDir = config.originalContractsDir 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.server = null;
this.provider = null; this.provider = null;
this.defaultPort = 8555; this.defaultPort = 8555;
@ -40,6 +45,7 @@ class API {
this.host = config.host || "127.0.0.1"; this.host = config.host || "127.0.0.1";
this.providerOptions = config.providerOptions || {}; this.providerOptions = config.providerOptions || {};
this.skipFiles = config.skipFiles || []; this.skipFiles = config.skipFiles || [];
this.log = config.log || console.log; this.log = config.log || console.log;

@ -1,4 +1,10 @@
// Testing hooks
const fn = (msg, config) => config.logger.log(msg);
module.exports = { module.exports = {
silent: process.env.SILENT ? true : false, 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')
} }

@ -134,6 +134,23 @@ describe('Truffle Plugin: standard use cases', function() {
verify.coverageMissing(missing); 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() { it('project with relative path solidity imports', async function() {
verify.cleanInitialState(); verify.cleanInitialState();
mock.installFullProject('import-paths'); mock.installFullProject('import-paths');

Loading…
Cancel
Save