Files

103 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, '..', '..');
// Scan the JS/TS test locations that exist in this repo: the web-ui tests (packages/web-ui/tests,
// plus anything under its source) and the Node package's test directory. repoRoot/tests is kept as a
// forward-compatible root. A scan that finds no test files fails, so the check cannot pass
// vacuously.
const testsRoots = [
path.join(repoRoot, 'tests'),
path.join(repoRoot, 'packages', 'web-ui', 'src'),
path.join(repoRoot, 'packages', 'web-ui', 'tests'),
path.join(repoRoot, 'packages', 'node-backend', 'test'),
];
const TEST_FILE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts']);
const DISALLOWED_LINE_PATTERNS = [
{
name: 'skip/todo/runIf declaration',
regex: /^\s*(?:await\s+)?(?:it|test|describe)\s*\.\s*(?:skip|todo|skipIf|runIf)\b/,
},
{
name: 'this.skip invocation',
regex: /\bthis\s*\.\s*skip\s*\(/,
},
];
const SKIP_DIR_NAMES = new Set(['node_modules', '.git', 'dist', 'build', '.svelte-kit']);
function listTestFiles(dir) {
if (!fs.existsSync(dir)) return [];
const out = [];
const stack = [dir];
while (stack.length) {
const current = stack.pop();
const entries = fs.readdirSync(current, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(current, entry.name);
if (entry.isDirectory()) {
if (SKIP_DIR_NAMES.has(entry.name)) continue;
stack.push(full);
} else if (entry.isFile()) {
const ext = path.extname(entry.name).toLowerCase();
if (TEST_FILE_EXTENSIONS.has(ext)) {
out.push(full);
}
}
}
}
return out.sort();
}
function collectViolations(filePath) {
const src = fs.readFileSync(filePath, 'utf8');
const lines = src.split(/\r?\n/);
const violations = [];
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
for (const pattern of DISALLOWED_LINE_PATTERNS) {
if (pattern.regex.test(line)) {
violations.push({
line: i + 1,
name: pattern.name,
text: line.trim(),
});
}
}
}
return violations;
}
const files = testsRoots.flatMap(listTestFiles).sort();
if (files.length === 0) {
console.error('[enforce-no-skipped-tests] No test files were found in the scanned roots; refusing to report success.');
process.exit(1);
}
const failures = [];
for (const filePath of files) {
const violations = collectViolations(filePath);
if (violations.length) {
failures.push({ filePath, violations });
}
}
if (failures.length) {
console.error('[enforce-no-skipped-tests] Found disallowed skip-style test usage.');
for (const entry of failures) {
const rel = path.relative(repoRoot, entry.filePath).replace(/\\/g, '/');
for (const v of entry.violations) {
console.error(` - ${rel}:${v.line} (${v.name}) -> ${v.text}`);
}
}
process.exit(1);
}
console.log('[enforce-no-skipped-tests] OK: no skipped/pending test declarations found.');