102 lines
3.3 KiB
JavaScript
102 lines
3.3 KiB
JavaScript
#!/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, '..', '..');
|
||
|
|
|
||
|
|
// The monorepo kept its canonical JS/TS test sources under a single repo-root tests/ dir, which
|
||
|
|
// doesn't exist in MediaEngine. Scan the real locations JS/TS test files can currently live in
|
||
|
|
// this repo instead: webui/src (SvelteKit app) and shared/packages/*/test(s) (vendored from the
|
||
|
|
// shared repo). repoRoot/tests is kept as a forward-compatible root in case one is added later.
|
||
|
|
// As of the MediaEngine extraction none of these contain vitest/mocha/jest-style test files yet
|
||
|
|
// (shared/packages/node-backend/test/auth.test.js uses plain `node --test`, which shares the same
|
||
|
|
// describe/it/test .skip API surface these patterns already match), so this currently passes
|
||
|
|
// trivially rather than being a dead check.
|
||
|
|
const testsRoots = [
|
||
|
|
path.join(repoRoot, 'tests'),
|
||
|
|
path.join(repoRoot, 'media-webui', 'src'),
|
||
|
|
path.join(repoRoot, 'shared', 'packages'),
|
||
|
|
];
|
||
|
|
|
||
|
|
const TEST_FILE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs']);
|
||
|
|
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();
|
||
|
|
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.');
|