Skip to content

Commit 5de038e

Browse files
committed
Add integration tests for coverage reports
- Refine the interplay between the flags "dry run" (-d) and "custom hooks" (-h). Now the custom hooking works even in dry run mode. - Simplify the FuzzedDataProvider test to decrease the chances of the test failing on MacOS (this fuzz test is expected to find an exception within some time)
1 parent 9b8a294 commit 5de038e

File tree

16 files changed

+571
-18
lines changed

16 files changed

+571
-18
lines changed

docs/fuzz-targets.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ Alternatively, you can add a new script to your package.json:
191191
}
192192
```
193193

194-
Only the files matched by the `--include` and `--exclude` flags are included in
195-
the coverage report. It is recommended to disable coverage report generation
196-
during fuzzing, because of a substantial overhead that it adds.
194+
Files matched by the flags `--include` and `--custom_hooks`, and not matched by
195+
the flag `--exclude` will be included in the coverage report. It is recommended
196+
to disable coverage report generation during fuzzing, because of a substantial
197+
overhead that it adds.
197198

198199
### Coverage report directory
199200

docs/jest-integration.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ To generate a coverage report, run jest with the `--coverage` flag:
292292
» npx jest --coverage
293293
```
294294
295-
Only the files matched by the `--include` and `--exclude` flags are included in
296-
the coverage report. These can be specified in the `package.json` or
297-
`.jazzerjsrc.json` config files as described above.
295+
Files matched by the flags `--include` and `--custom_hooks`, and not matched by
296+
the flag `--exclude` will be included in the coverage report. These can be
297+
specified in the `package.json` or `.jazzerjsrc.json` config files as described
298+
above.
298299
299300
By default, the coverage reports can be found in the `./coverage` directory.
300301
This default directory can be changed by setting the flag

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
module.exports = {
1919
preset: "ts-jest",
2020
testEnvironment: "node",
21-
modulePathIgnorePatterns: ["dist", "packages/fuzzer/build"],
21+
modulePathIgnorePatterns: [
22+
"dist",
23+
"packages/fuzzer/build",
24+
"tests/code_coverage",
25+
],
2226
collectCoverageFrom: ["packages/**/*.ts"],
2327
coveragePathIgnorePatterns: ["/node_modules/", "/dist/"],
2428
};

packages/core/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ yargs(process.argv.slice(2))
185185
dryRun: args.dry_run,
186186
sync: args.sync,
187187
fuzzerOptions: args.corpus.concat(args._),
188-
customHooks: args.custom_hooks.map(ensureFilepath),
188+
customHooks: args.custom_hooks,
189189
expectedErrors: args.expected_errors,
190190
coverage: args.coverage,
191191
coverageDirectory: args.coverageDirectory,

packages/core/core.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { ensureFilepath } from "./core";
18+
1819
import path from "path";
1920

2021
describe("core", () => {

packages/core/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ export async function initFuzzing(options: Options) {
6363
registerInstrumentor(
6464
options.includes,
6565
options.excludes,
66+
options.customHooks,
6667
options.coverage,
6768
options.dryRun
6869
);
69-
await Promise.all(options.customHooks.map(importModule));
70+
await Promise.all(options.customHooks.map(ensureFilepath).map(importModule));
7071
}
7172

7273
export function registerGlobals() {

packages/instrumentor/instrument.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export type FilePredicate = (filepath: string) => boolean;
6363
export function registerInstrumentor(
6464
includes: string[],
6565
excludes: string[],
66-
shouldCollectCoverage: boolean,
66+
customHooks: string[],
67+
coverage: boolean,
6768
dryRun: boolean
6869
) {
6970
installSourceMapSupport();
@@ -73,6 +74,11 @@ export function registerInstrumentor(
7374

7475
const shouldInstrument = shouldInstrumentFn(includes, excludes);
7576
const shouldHook = hookManager.hasFunctionsToHook.bind(hookManager);
77+
const shouldCollectCoverage = shouldCollectCoverageFn(
78+
coverage,
79+
customHooks,
80+
shouldInstrument
81+
);
7682
hookRequire(
7783
() => true,
7884
(code: string, options: TransformerOptions): string =>
@@ -123,24 +129,38 @@ export function shouldInstrumentFn(
123129
};
124130
}
125131

132+
function shouldCollectCoverageFn(
133+
coverage: boolean,
134+
customHooks: string[],
135+
shouldInstrument: FilePredicate = () => true
136+
): FilePredicate {
137+
const shouldCoverCustomHooks = shouldInstrumentFn(customHooks, ["nothing"]);
138+
return (filepath: string) => {
139+
return (
140+
coverage &&
141+
(shouldCoverCustomHooks(filepath) || shouldInstrument(filepath))
142+
);
143+
};
144+
}
145+
126146
function instrument(
127147
code: string,
128148
filename: string,
129149
shouldInstrument: FilePredicate,
130150
shouldHook: FilePredicate,
131-
shouldCollectCoverage: boolean,
151+
shouldCollectCoverage: FilePredicate,
132152
dryRun: boolean
133153
) {
134154
const transformations: PluginItem[] = [];
135155
if (!dryRun) {
136156
if (shouldInstrument(filename)) {
137157
transformations.push(codeCoverage, compareHooks);
138158
}
139-
if (shouldHook(filename)) {
140-
transformations.push(functionHooks(filename));
141-
}
142159
}
143-
if (shouldCollectCoverage && shouldInstrument(filename)) {
160+
if (shouldHook(filename)) {
161+
transformations.push(functionHooks(filename));
162+
}
163+
if (shouldCollectCoverage(filename)) {
144164
transformations.push(istanbulCodeCoverage(filename));
145165
}
146166

tests/FuzzedDataProvider/fuzz.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ const { FuzzedDataProvider } = require("@jazzer.js/core");
2222
*/
2323
module.exports.fuzz = function (fuzzerInputData) {
2424
const data = new FuzzedDataProvider(fuzzerInputData);
25-
const s1 = data.consumeString(data.consumeIntegralInRange(1, 15), "utf-8");
25+
const s1 = data.consumeString(data.consumeIntegralInRange(10, 15), "utf-8");
2626
const i1 = data.consumeIntegral(1);
2727
const i2 = data.consumeIntegral(2);
28-
let i3 = 0;
29-
if (data.consumeBoolean()) i3 = data.consumeIntegral(4);
28+
let i3 = data.consumeIntegral(4);
3029

3130
if (i3 === 1000) {
3231
if (s1 === "Hello World!") {

0 commit comments

Comments
 (0)