Skip to content

Add modifierWhitelist option #597

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 1 commit into from
Jan 6, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ module.exports = {
| measureStatementCoverage | *boolean* | `true` | Computes statement (in addition to line) coverage. [More...][34] |
| measureFunctionCoverage | *boolean* | `true` | Computes function coverage. [More...][34] |
| measureModifierCoverage | *boolean* | `true` | Computes each modifier invocation as a code branch. [More...][34] |
| modifierWhitelist | *String[]* | `[]` | List of modifier names (ex: "onlyOwner") to exclude from branch measurement. (Useful for modifiers which prepare something instead of acting as a gate.)) |
| matrixOutputPath | *String* | `./testMatrix.json` | Relative path to write test matrix JSON object to. [More...][38] |
| istanbulFolder | *String* | `./coverage` | Folder location for Istanbul coverage reports. |
| istanbulReporter | *Array* | `['html', 'lcov', 'text', 'json']` | [Istanbul coverage reporters][2] |
Expand Down
3 changes: 2 additions & 1 deletion lib/instrumenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Instrumenter {
constructor(config={}){
this.instrumentationData = {};
this.injector = new Injector();
this.modifierWhitelist = config.modifierWhitelist || [];
this.enabled = {
statements: (config.measureStatementCoverage === false) ? false : true,
functions: (config.measureFunctionCoverage === false) ? false: true,
Expand Down Expand Up @@ -62,7 +63,7 @@ class Instrumenter {
const contract = {};

this.injector.resetModifierMapping();
parse.configure(this.enabled);
parse.configure(this.enabled, this.modifierWhitelist);

contract.source = contractSource;
contract.instrumented = contractSource;
Expand Down
3 changes: 2 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const register = new Registrar();
const parse = {};

// Utilities
parse.configure = function(_enabled){
parse.configure = function(_enabled, _whitelist){
register.enabled = Object.assign(register.enabled, _enabled);
register.modifierWhitelist = _whitelist;
}

// Nodes
Expand Down
10 changes: 9 additions & 1 deletion lib/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Registrar {
branches: true,
lines: true
}

this.modifierWhitelist = [];
}

/**
Expand Down Expand Up @@ -129,7 +131,13 @@ class Registrar {
}

// Add modifier branch coverage
if (!this.enabled.modifiers || expression.isConstructor) continue;
if (
!this.enabled.modifiers ||
expression.isConstructor ||
this.modifierWhitelist.includes(modifier.name)
) {
continue;
}

this.addNewBranch(contract, modifier);
this._createInjectionPoint(
Expand Down
5 changes: 5 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const configSchema = {
type: "array",
items: {type: "string"}
},

modifierWhitelist: {
type: "array",
items: {type: "string"}
}
},
};

Expand Down
24 changes: 23 additions & 1 deletion test/units/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ describe('modifiers', () => {
api = new Api({silent: true});
await api.ganache(client);
})
beforeEach(() => coverage = new Coverage());
beforeEach(() => {
api.config = {};
coverage = new Coverage()
});
after(async() => await api.finish());

async function setupAndRun(solidityFile){
Expand Down Expand Up @@ -97,6 +100,25 @@ describe('modifiers', () => {
});
});

// Same test as above - should have 2 fewer branches
it('should exclude whitelisted modifiers', async function() {
api.config.modifierWhitelist = ['mmm', 'nnn'];
const mapping = await setupAndRun('modifiers/multiple-mods-same-fn');

assert.deepEqual(mapping[util.filePath].l, {
5: 1, 6: 1, 10: 1, 11: 1, 15: 1
});
assert.deepEqual(mapping[util.filePath].b, {
1: [1, 0], 2: [1, 0]
});
assert.deepEqual(mapping[util.filePath].s, {
1: 1, 2: 1, 3: 1
});
assert.deepEqual(mapping[util.filePath].f, {
1: 1, 2: 1, 3: 1
});
});

it('should cover multiple functions which use the same modifier', async function() {
const contract = await util.bootstrapCoverage('modifiers/multiple-fns-same-mod', api);
coverage.addContract(contract.instrumented, util.filePath);
Expand Down
1 change: 1 addition & 0 deletions test/units/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('config validation', () => {
const options = [
"skipFiles",
"istanbulReporter",
"modifierWhitelist"
]

options.forEach(name => {
Expand Down