fix: test only flag in cli to inclue pre and post test (#5216)

This commit is contained in:
Pooja
2025-08-07 15:50:03 +05:30
committed by GitHub
parent 7cb80abdfc
commit 86901c1e89
3 changed files with 365 additions and 4 deletions

View File

@@ -0,0 +1,44 @@
// Check for meaningful test() calls (not commented out or in strings)
const hasExecutableTestInScript = (script) => {
if (!script) return false;
// Remove single-line comments (// ...) and multi-line comments (/* ... */)
let cleanScript = script
.replace(/\/\/.*$/gm, '') // Remove line comments
.replace(/\/\*[\s\S]*?\*\//g, ''); // Remove block comments
// Remove string literals to avoid matching test() inside strings
cleanScript = cleanScript
.replace(/"(?:[^"\\]|\\.)*"/g, '""') // Remove double-quoted strings
.replace(/'(?:[^'\\]|\\.)*'/g, "''") // Remove single-quoted strings
.replace(/`(?:[^`\\]|\\.)*`/g, '``'); // Remove template literals
// Look for standalone test() calls (not object method calls like obj.test())
// Find all test( occurrences and check they're not preceded by dots
let hasValidTest = false;
let searchFrom = 0;
while (true) {
const index = cleanScript.indexOf('test', searchFrom);
if (index === -1) break;
// Check if this looks like test( with optional whitespace
const afterTest = cleanScript.substring(index + 4);
if (/^\s*\(/.test(afterTest)) {
// Found test( - check if it's not preceded by a dot
if (index === 0 || cleanScript[index - 1] !== '.') {
hasValidTest = true;
break;
}
}
searchFrom = index + 1;
}
return hasValidTest;
};
module.exports = {
hasExecutableTestInScript
};