* feat(#2982): extend no-source-grep lint to catch var-binding readFileSync.includes() The base lint (scripts/lint-no-source-grep.cjs) only catches readFileSync(...).<text-method>() chained directly. The much more common var-binding form escapes it: const src = fs.readFileSync(p, 'utf8'); // 50 lines later if (src.includes('foo')) {} // ← still grep, lint missed it Scan of the test suite found ~141 files using this pattern. Implementation built TDD per #2982 with structured-IR assertions: scripts/lint-no-source-grep-extras.cjs - detectVarBindingViolations(src) — pure detector, two passes: pass 1 collects vars bound from readFileSync, pass 2 finds any <var>.<includes|startsWith|endsWith|match|search>( on those vars. - detectWrappedAssertOkMatch(src) — flags assert.ok(<expr>.match(...)) which escapes the assert.match rule. - VIOLATION enum exposes stable codes for tests to assert on. scripts/lint-no-source-grep.cjs - Wires the new detectors into the existing per-file check; one additional violation row per file with the first 3 sample tokens. tests/bug-2982-lint-var-binding.test.cjs - 13 tests, all assertions on typed VIOLATION enum / structured records. Covers all 5 text-match methods, multi-var, no-bind, string literal (must NOT trigger), wrapped assert.ok(.match), and assert.match (must NOT double-flag). Migration backlog (#2974 expanded scope): - 42 files annotated `// allow-test-rule: source-text-is-the-product` (legitimate — they read .md/.json/.yml files whose deployed text IS the product) - 3 files annotated `// allow-test-rule: pending-migration-to-typed-ir [#2974]` (read .cjs/.js source — clear migration debt) - 95 files annotated `pending-migration-to-typed-ir [#2974]` with `Per-file review may reclassify as source-text-is-the-product during migration` (mixed — manual review under #2974) After this lands the lint reports 0 violations on main; new violations in PRs surface immediately. Closes #2982 Refs #2974 * test(#2982): fix truncated test name per CR The label ended with a bare '(' from a copy-paste mishap. Now reads 'does NOT flag .matchAll(...) — matchAll is not match, so assert.ok(.matchAll(...)) is not flagged'. * chore(#2982): add changeset fragment for PR #2985 * chore(#2982): add changeset fragment for PR #2985
209 lines
7.2 KiB
JavaScript
209 lines
7.2 KiB
JavaScript
// allow-test-rule: source-text-is-the-product
|
|
// Reads .md/.json/.yml product files whose deployed text IS what the
|
|
// runtime loads — testing text content tests the deployed contract.
|
|
|
|
/**
|
|
* Windows Robustness Tests
|
|
*
|
|
* Validates that workflow files, hooks, and core functions handle
|
|
* Windows/cross-platform edge cases correctly:
|
|
*
|
|
* 1. Workflow shell robustness: informational commands guarded with || true
|
|
* 2. Glob loops guarded with [ -e "$var" ] || continue
|
|
* 3. Hook stdin timeout patterns present in all JS hooks
|
|
* 4. findProjectRoot detects .git at same level as .planning/
|
|
* 5. @file: handoff present in all workflows that call init
|
|
*
|
|
* Regression tests for: https://github.com/gsd-build/get-shit-done/issues/1343
|
|
*/
|
|
|
|
const { test, describe } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const WORKFLOWS_DIR = path.join(__dirname, '..', 'get-shit-done', 'workflows');
|
|
const HOOKS_DIR = path.join(__dirname, '..', 'hooks');
|
|
|
|
/**
|
|
* Extract bash code blocks from a markdown file.
|
|
* Returns array of { lineNumber, code } objects.
|
|
*/
|
|
function extractBashBlocks(content) {
|
|
const blocks = [];
|
|
const lines = content.split('\n');
|
|
let inBlock = false;
|
|
let blockStart = 0;
|
|
let blockLines = [];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i];
|
|
if (line.trim().startsWith('```bash')) {
|
|
inBlock = true;
|
|
blockStart = i + 1;
|
|
blockLines = [];
|
|
} else if (inBlock && line.trim() === '```') {
|
|
inBlock = false;
|
|
blocks.push({ lineNumber: blockStart, code: blockLines.join('\n') });
|
|
} else if (inBlock) {
|
|
blockLines.push(line);
|
|
}
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
/**
|
|
* Check if a line is an informational command that can return non-zero on
|
|
* "no results" and should be guarded with || true.
|
|
*
|
|
* Matches: ls, grep, find, cat on optional files — commands at end of line
|
|
* with 2>/dev/null that are NOT already guarded.
|
|
*/
|
|
function findUnguardedInfoCommands(code) {
|
|
const issues = [];
|
|
const lines = code.split('\n');
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i].trim();
|
|
|
|
// Skip comments, empty lines, and lines that are already guarded
|
|
if (!line || line.startsWith('#')) continue;
|
|
if (line.includes('|| true') || line.includes('|| echo') || line.includes('|| continue')) continue;
|
|
|
|
// Lines ending with 2>/dev/null that use informational commands
|
|
if (line.endsWith('2>/dev/null')) {
|
|
// Check if this is an informational command (ls, grep, find, cat on optional files)
|
|
if (/^(ls|grep|find|cat)\s/.test(line) ||
|
|
/\|\s*(ls|grep|find)\s/.test(line)) {
|
|
issues.push({ line: i + 1, content: line });
|
|
}
|
|
}
|
|
}
|
|
return issues;
|
|
}
|
|
|
|
// ─── Workflow Shell Robustness ────────────────────────────────────────────────
|
|
|
|
describe('workflow shell robustness', () => {
|
|
const workflowFiles = fs.readdirSync(WORKFLOWS_DIR)
|
|
.filter(f => f.endsWith('.md'));
|
|
|
|
// Key workflow files that must have || true guards on informational commands
|
|
const criticalWorkflows = [
|
|
'resume-project.md',
|
|
'progress.md',
|
|
'transition.md',
|
|
'verify-phase.md',
|
|
'verify-work.md',
|
|
'discuss-phase.md',
|
|
'plan-phase.md',
|
|
'execute-plan.md',
|
|
'cleanup.md',
|
|
];
|
|
|
|
for (const wf of criticalWorkflows) {
|
|
test(`${wf}: informational commands are guarded with || true`, () => {
|
|
const filePath = path.join(WORKFLOWS_DIR, wf);
|
|
if (!fs.existsSync(filePath)) return; // skip if workflow doesn't exist
|
|
const content = fs.readFileSync(filePath, 'utf-8');
|
|
const blocks = extractBashBlocks(content);
|
|
const allIssues = [];
|
|
|
|
for (const block of blocks) {
|
|
const issues = findUnguardedInfoCommands(block.code);
|
|
for (const issue of issues) {
|
|
allIssues.push(`Line ~${block.lineNumber + issue.line}: ${issue.content}`);
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(
|
|
allIssues.length, 0,
|
|
`${wf} has unguarded informational commands that may fail on Windows:\n ${allIssues.join('\n ')}`
|
|
);
|
|
});
|
|
}
|
|
|
|
test('glob loops in resume-project.md have existence guard', () => {
|
|
const content = fs.readFileSync(path.join(WORKFLOWS_DIR, 'resume-project.md'), 'utf-8');
|
|
const blocks = extractBashBlocks(content);
|
|
|
|
for (const block of blocks) {
|
|
// Look for `for ... in .planning/` glob loops
|
|
const forLoopMatch = block.code.match(/for\s+\w+\s+in\s+\.planning\/[^;]+;\s*do/);
|
|
if (forLoopMatch) {
|
|
// The loop body should contain [ -e "$var" ] || continue
|
|
assert.ok(
|
|
block.code.includes('|| continue'),
|
|
`Glob loop at line ~${block.lineNumber} missing existence guard ([ -e "$var" ] || continue):\n${forLoopMatch[0]}`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// ─── Hook Stdin Timeout ──────────────────────────────────────────────────────
|
|
|
|
describe('hook stdin timeout patterns', () => {
|
|
test('all JS hooks have stdin timeout guard', () => {
|
|
if (!fs.existsSync(HOOKS_DIR)) return;
|
|
|
|
const hookFiles = fs.readdirSync(HOOKS_DIR)
|
|
.filter(f => f.endsWith('.js'));
|
|
|
|
for (const hook of hookFiles) {
|
|
const content = fs.readFileSync(path.join(HOOKS_DIR, hook), 'utf-8');
|
|
|
|
// Hooks that read stdin must have a timeout
|
|
if (content.includes('process.stdin')) {
|
|
assert.ok(
|
|
content.includes('setTimeout') || content.includes('stdinTimeout'),
|
|
`${hook} reads stdin but lacks a timeout guard — will hang on Windows if stdin pipe doesn't close`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('no JS hooks use synchronous readFileSync on /dev/stdin', () => {
|
|
if (!fs.existsSync(HOOKS_DIR)) return;
|
|
|
|
const hookFiles = fs.readdirSync(HOOKS_DIR)
|
|
.filter(f => f.endsWith('.js'));
|
|
|
|
for (const hook of hookFiles) {
|
|
const content = fs.readFileSync(path.join(HOOKS_DIR, hook), 'utf-8');
|
|
assert.ok(
|
|
!content.includes("readFileSync('/dev/stdin')") &&
|
|
!content.includes('readFileSync("/dev/stdin")'),
|
|
`${hook} uses readFileSync('/dev/stdin') which hangs on Windows — use async process.stdin with timeout instead`
|
|
);
|
|
}
|
|
});
|
|
});
|
|
|
|
// ─── @file: Handoff ─────────────────────────────────────────────────────────
|
|
|
|
describe('@file: handoff in workflows', () => {
|
|
test('all workflows calling gsd-tools init have @file: handler', () => {
|
|
const workflowFiles = fs.readdirSync(WORKFLOWS_DIR)
|
|
.filter(f => f.endsWith('.md'));
|
|
|
|
const missing = [];
|
|
for (const wf of workflowFiles) {
|
|
const content = fs.readFileSync(path.join(WORKFLOWS_DIR, wf), 'utf-8');
|
|
|
|
// Check if this workflow calls gsd-tools.cjs init
|
|
if (/INIT=\$\(node.*gsd-tools.*\binit\b/.test(content)) {
|
|
// Must have @file: handler
|
|
if (!content.includes('@file:')) {
|
|
missing.push(wf);
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(
|
|
missing.length, 0,
|
|
`Workflows calling gsd-tools init without @file: handler (large output will be truncated):\n ${missing.join('\n ')}`
|
|
);
|
|
});
|
|
});
|