-
Notifications
You must be signed in to change notification settings - Fork 159
test(polluter): add polluter bisect script #16235
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
Open
IMinchev64
wants to merge
6
commits into
master
Choose a base branch
from
iminchev/test-polluter-bisect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54836c7
feat(polluter): add polluter bisect script
IMinchev64 1ed05e6
fix(test-polluter-bisect): improve failure reporting in runTests func…
IMinchev64 6e6d3cd
chore(polluter): add a README.md file
IMinchev64 dc84bc1
Merge branch 'master' into iminchev/test-polluter-bisect
dkamburov 754962f
Merge branch 'master' into iminchev/test-polluter-bisect
kdinev cd8ae5a
fix(package): update polluter bisect script path
IMinchev64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ yarn-error.log | |
| testem.log | ||
| /typings | ||
| TESTS-**.xml | ||
| polluter-runner.spec.ts | ||
|
|
||
| # System Files | ||
| .DS_Store | ||
|
|
||
This file contains hidden or 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 hidden or 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,35 @@ | ||
| ## Description | ||
| Sometimes tests from the `igniteui-angular` project fail when run as part of the full suite, even though they pass when executed in isolation. | ||
| The reason is that some tests pollute the testing environment (e.g. by leaving behind global state or async handles), which causes later tests to behave incorrectly. | ||
|
|
||
| With a suite of ~200 test files and more than 5000 tests, it’s not feasible to manually track down the culprit file. | ||
|
|
||
| This PR introduces a polluter-bisect script that streamlines the process of identifying polluting tests by: | ||
| - Allowing you to specify a sentinel test file (the test known to fail in polluted environments). | ||
| - Running a binary search over the rest of the suite to narrow down the minimal set of files that trigger the sentinel failure. | ||
| - Supporting two modes: | ||
| - `before` - only considers tests that run before the sentinel. (default) | ||
| - `all` - considers all tests in the suite and runs the sentinel last. | ||
| - Providing a flag to optionally skip the initial full-set scan if you already know the sentinel fails in the suite. | ||
| - Generating a temporary polluter-runner spec file to enforce deterministic execution order (bypassing Karma’s automatic sorting). | ||
|
|
||
| This makes it possible to isolate polluting test files much faster than running the entire suite repeatedly. | ||
| The script is not intended to run in CI; it’s a developer tool to aid in diagnosing flaky tests. | ||
|
|
||
| ## Usage | ||
| From the root of the repo, run: | ||
|
|
||
| ```bash | ||
| # Default: search only in files before the sentinel | ||
| npm run polluter:bisect -- sentinel-file.spec.ts before | ||
|
|
||
| # Search across all test files, with sentinel always last | ||
| npm run polluter:bisect -- sentinel-file.spec.ts all | ||
|
|
||
| # Skip the initial full-set scan (faster if you already know the sentinel fails in the suite) | ||
| npm run polluter:bisect -- sentinel-file.spec.ts before --skip-initial | ||
| ``` | ||
| The script will iteratively run subsets of tests until it identifies the polluting test file that causes the sentinel to fail. | ||
|
|
||
| >NOTE: | ||
| > In order for the script to work correctly you should set only a single test executor in `projects/igniteui-angular/karma.conf.js` under `parallelOptions`. |
This file contains hidden or 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,154 @@ | ||
| import { spawn } from 'child_process'; | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }) | ||
|
|
||
| async function main() { | ||
| const allFiles = getAllSpecFiles("projects/igniteui-angular/src/lib"); | ||
|
|
||
| const sentinelArg = process.argv[2]; | ||
| const mode = process.argv[3] || "before"; | ||
| const skipInitial = process.argv.includes("--skip-initial"); | ||
|
|
||
| if (!sentinelArg) { | ||
| console.error("Usage: node test-polluter-bisect.js <sentinel-file.spec.ts> [before|all]"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const sentinelFile = allFiles.find(f => f.includes(sentinelArg)); | ||
|
|
||
| if (!sentinelFile) { | ||
| console.error(`Sentinel file '${sentinelArg}' not found in the test set.`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`Running polluter search with sentinel: ${sentinelArg}, mode: ${mode}`); | ||
| const culprit = await findPolluter(allFiles, sentinelFile, mode, !skipInitial); | ||
|
|
||
| if (culprit) { | ||
| console.log(`Polluter file is: ${culprit}`); | ||
| } else { | ||
| console.log("No polluter found in the set."); | ||
| } | ||
| } | ||
|
|
||
| async function findPolluter(allFiles, sentinelFile, mode = "before", doInitialScan = true) { | ||
| let suspects; | ||
|
|
||
| if (mode === "before") { | ||
| suspects = allFiles.slice(0, allFiles.indexOf(sentinelFile)); | ||
| } else if (mode === "all") { | ||
| suspects = allFiles.filter(f => f !== sentinelFile); | ||
| } else { | ||
| throw new Error(`Unknown mode: ${mode}`); | ||
| } | ||
|
|
||
| if (doInitialScan) { | ||
| console.log("Initial run with full set..."); | ||
| const initialPass = await runTests([...suspects, sentinelFile], sentinelFile); | ||
|
|
||
| if (initialPass) { | ||
| console.log("Sentinel passed even after full set — no polluter detected."); | ||
| return null; | ||
| } | ||
| } else { | ||
| console.log("Skipping initial full-set scan."); | ||
| } | ||
|
|
||
| while (suspects.length > 1) { | ||
| const mid = Math.floor(suspects.length / 2); | ||
| const left = suspects.slice(0, mid); | ||
| const right = suspects.slice(mid); | ||
|
|
||
| if (await runTests([...left, sentinelFile], sentinelFile)) { | ||
| suspects = right; | ||
| } else { | ||
| suspects = left; | ||
| } | ||
| } | ||
| return suspects[0]; | ||
| } | ||
|
|
||
| function runTests(files, sentinelFile) { | ||
| return new Promise((resolve) => { | ||
| const sentinelNorm = normalizeForNg(sentinelFile); | ||
| const runnerFile = createPolluterRunner(files); | ||
|
|
||
| const args = [ | ||
| "test", | ||
| "igniteui-angular", | ||
| "--watch=false", | ||
| "--include", | ||
| runnerFile | ||
| ]; | ||
|
|
||
| let output = ""; | ||
| let finished = false; | ||
|
|
||
| const finish = (reason) => { | ||
| if (finished) return; | ||
| finished = true; | ||
|
|
||
| const sentinelFailed = path.basename(sentinelNorm); | ||
| const failed = output.includes("FAILED") && output.includes(sentinelFailed); | ||
| console.log(`Sentinel ${sentinelFailed} ${failed ? "FAILED" : "PASSED"} [via ${reason}]`); | ||
| resolve(!failed); | ||
|
|
||
| if (!proc.killed) proc.kill(); | ||
| }; | ||
|
|
||
| const proc = spawn("npx", ["ng", ...args], { shell: true }); | ||
|
|
||
| proc.stdout.on("data", (data) => { | ||
| const text = data.toString(); | ||
| output += text; | ||
| process.stdout.write(text); | ||
|
|
||
| if (text.includes("TOTAL:")) { | ||
| finish("stdout"); | ||
| } | ||
| }); | ||
| proc.stderr.on("data", (data) => { | ||
| const text = data.toString(); | ||
| output += text; | ||
| process.stdout.write(text); | ||
| }); | ||
|
|
||
| proc.on("exit", () => { | ||
| finish("exit"); | ||
| }); | ||
| }) | ||
| } | ||
|
|
||
| function getAllSpecFiles(dir) { | ||
| let files = []; | ||
| fs.readdirSync(dir).forEach((file) => { | ||
| const full = path.join(dir, file); | ||
| if (fs.statSync(full).isDirectory()) { | ||
| files = files.concat(getAllSpecFiles(full)); | ||
| } else if (file.endsWith(".spec.ts")) { | ||
| files.push(full); | ||
| } | ||
| }); | ||
| return files.sort(); | ||
| } | ||
|
|
||
| function normalizeForNg(file) { | ||
| const rel = path.relative(process.cwd(), file); | ||
| return rel.split(path.sep).join("/"); | ||
| } | ||
|
|
||
| function createPolluterRunner(files) { | ||
| const imports = files.map(f => | ||
| `require('${normalizeForNg(f).replace(/\.ts$/, "")}');` | ||
| ).join("\n"); | ||
|
|
||
| const runnerPath = path.join(process.cwd(), "projects/igniteui-angular/src/polluter-runner.spec.ts"); | ||
| fs.mkdirSync(path.dirname(runnerPath), { recursive: true}); | ||
| fs.writeFileSync(runnerPath, imports, "utf8"); | ||
| return runnerPath; | ||
|
IMinchev64 marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.