-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide script to run all internal Jest fuzz tests
These are also executed in the CI system parallel to the normal unit tests.
- Loading branch information
1 parent
9da122c
commit 99439a1
Showing
3 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Internal fuzz tests | ||
|
||
**Note**: These are only internal tests and not intended as examples. | ||
|
||
This directory contains fuzz tests created via the Jest integration that test | ||
internal Jazzer.js functionality. | ||
|
||
## Config | ||
|
||
General fuzz test configuration is done via `.jazzerjsrc` | ||
|
||
## Execution | ||
|
||
Use the provided script `runFuzzTests` to execute all tests in the `fuzztests` | ||
directory. Pass in `async` as first argument to start all tests in parallel. To | ||
change the working dir, pass in the target directory as second parameter. | ||
|
||
**Sync:** | ||
|
||
```shell | ||
./runFuzzTests.js | ||
``` | ||
|
||
**Async:** | ||
|
||
```shell | ||
./runFuzzTests.js async | ||
``` | ||
|
||
**Other dir:** | ||
|
||
```shell | ||
./runFuzzTests.js async otherDirectory | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/node | ||
|
||
// Helper script that searches for Jest fuzz tests in the current directory and | ||
// executes them in new processes using the found fuzz test names. | ||
|
||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const fs = require("fs/promises"); | ||
const { spawn } = require("child_process"); | ||
|
||
const fuzzTestFileExtension = "fuzz.js"; | ||
const fuzzTestNameRegex = /it.fuzz\("(.*)"/g; | ||
|
||
async function findFuzzTestNamesInFile(file) { | ||
const fuzzTestNames = []; | ||
if (file.endsWith(fuzzTestFileExtension)) { | ||
const content = await fs.readFile(file); | ||
for (let match of content.toString().matchAll(fuzzTestNameRegex)) { | ||
fuzzTestNames.push(match[1]); | ||
} | ||
} | ||
return fuzzTestNames; | ||
} | ||
|
||
async function findFuzzTestNamesInDir(dir) { | ||
const files = await fs.readdir(dir); | ||
let fuzzTests = {}; | ||
for (const file of files) { | ||
const fuzzTestNames = await findFuzzTestNamesInFile(file); | ||
if (fuzzTestNames.length) { | ||
fuzzTests[file] = fuzzTestNames; | ||
} | ||
} | ||
return fuzzTests; | ||
} | ||
|
||
async function executeFuzzTest(file, name) { | ||
console.log(`--- Executing fuzz test ${file} > ${name}`); | ||
return new Promise((resolve, reject) => { | ||
process.env["JAZZER_FUZZ"] = "1"; | ||
let test = spawn("npm", ["test", "--", `--testNamePattern='${name}'`], { | ||
stdio: "inherit", | ||
}); | ||
test.on("error", (error) => { | ||
console.log(`ERROR: ${error.message}`); | ||
reject(error); | ||
}); | ||
test.on("close", (code) => { | ||
console.log(`--- Finished fuzz test ${file} > ${name} with code ${code}`); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
async function runFuzzTestsInDir(dir, mode) { | ||
const fuzzTests = await findFuzzTestNamesInDir(dir); | ||
if (mode === "async") { | ||
return Promise.all( | ||
Object.entries(fuzzTests).map(([file, names]) => | ||
Promise.all(names.map((name) => executeFuzzTest(file, name))) | ||
) | ||
); | ||
} else { | ||
for (const [file, names] of Object.entries(fuzzTests)) { | ||
for (let name of names) { | ||
await executeFuzzTest(file, name); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Change into script / requested dir to simplify file handling | ||
process.chdir(process.argv[3] || __dirname); | ||
|
||
runFuzzTestsInDir(".", process.argv[2]).then( | ||
() => { | ||
console.log("DONE"); | ||
}, | ||
(e) => { | ||
console.log("ERROR:", e); | ||
process.exit(79); | ||
} | ||
); |