mirror of
https://github.com/glittercowboy/get-shit-done
synced 2026-05-13 18:46:38 +02:00
* 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
170 lines
6.8 KiB
JavaScript
170 lines
6.8 KiB
JavaScript
// allow-test-rule: pending-migration-to-typed-ir [#2974]
|
|
// Tracked in #2974 for migration to typed-IR assertions per CONTRIBUTING.md
|
|
// "Prohibited: Raw Text Matching on Test Outputs". Per-file review may
|
|
// reclassify some entries as source-text-is-the-product during migration.
|
|
|
|
/**
|
|
* GSD Tests - path replacement in install.js
|
|
*
|
|
* Verifies that global installs produce $HOME/ paths in .md files,
|
|
* so that shell commands expand correctly inside double quotes.
|
|
* ~ does NOT expand inside double quotes in POSIX shells, causing
|
|
* MODULE_NOT_FOUND errors (see #1284).
|
|
*/
|
|
|
|
const { test, describe } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
const repoRoot = path.join(__dirname, '..');
|
|
|
|
// Simulate the pathPrefix computation from install.js (global install)
|
|
function computePathPrefix(homedir, targetDir) {
|
|
const resolvedTarget = path.resolve(targetDir).replace(/\\/g, '/');
|
|
const homeDir = homedir.replace(/\\/g, '/');
|
|
if (resolvedTarget.startsWith(homeDir)) {
|
|
return '$HOME' + resolvedTarget.slice(homeDir.length) + '/';
|
|
}
|
|
return resolvedTarget + '/';
|
|
}
|
|
|
|
describe('pathPrefix computation', () => {
|
|
test('default Claude global install uses $HOME/', () => {
|
|
const homedir = os.homedir();
|
|
const targetDir = path.join(homedir, '.claude');
|
|
const prefix = computePathPrefix(homedir, targetDir);
|
|
assert.strictEqual(prefix, '$HOME/.claude/');
|
|
});
|
|
|
|
test('default Gemini global install uses $HOME/', () => {
|
|
const homedir = os.homedir();
|
|
const targetDir = path.join(homedir, '.gemini');
|
|
const prefix = computePathPrefix(homedir, targetDir);
|
|
assert.strictEqual(prefix, '$HOME/.gemini/');
|
|
});
|
|
|
|
test('custom config dir under home uses $HOME/', () => {
|
|
const homedir = os.homedir();
|
|
const targetDir = path.join(homedir, '.config', 'claude');
|
|
const prefix = computePathPrefix(homedir, targetDir);
|
|
assert.ok(prefix.startsWith('$HOME/'), `Expected $HOME/ prefix, got: ${prefix}`);
|
|
assert.ok(!prefix.includes(homedir), `Should not contain homedir: ${homedir}`);
|
|
});
|
|
|
|
test('Windows-style paths produce $HOME/ not C:/', () => {
|
|
// On Windows, path.resolve returns the input unchanged when it's already absolute.
|
|
// Simulate the string operation directly (can't use path.resolve for Windows paths on macOS/Linux).
|
|
const winHomedir = 'C:\\Users\\matte';
|
|
const winTargetDir = 'C:\\Users\\matte\\.claude';
|
|
const resolvedTarget = winTargetDir.replace(/\\/g, '/');
|
|
const homeDir = winHomedir.replace(/\\/g, '/');
|
|
const prefix = resolvedTarget.startsWith(homeDir)
|
|
? '$HOME' + resolvedTarget.slice(homeDir.length) + '/'
|
|
: resolvedTarget + '/';
|
|
assert.strictEqual(prefix, '$HOME/.claude/');
|
|
assert.ok(!prefix.includes('C:'), `Should not contain drive letter, got: ${prefix}`);
|
|
});
|
|
|
|
test('target outside home uses absolute path', () => {
|
|
const homedir = '/home/user';
|
|
const targetDir = '/opt/gsd/.claude';
|
|
// path.resolve won't change an already-absolute path on the same OS,
|
|
// so simulate the string operation directly
|
|
const resolvedTarget = targetDir.replace(/\\/g, '/');
|
|
const homeDir = homedir.replace(/\\/g, '/');
|
|
const prefix = resolvedTarget.startsWith(homeDir)
|
|
? '$HOME' + resolvedTarget.slice(homeDir.length) + '/'
|
|
: resolvedTarget + '/';
|
|
assert.strictEqual(prefix, '/opt/gsd/.claude/');
|
|
assert.ok(!prefix.includes('$HOME'), `Should not contain $HOME for non-home paths`);
|
|
});
|
|
|
|
test('$HOME expands inside double-quoted shell commands', () => {
|
|
// This is the core regression test for #1284:
|
|
// ~ does NOT expand inside double quotes in POSIX shells,
|
|
// but $HOME does expand inside double quotes.
|
|
const homedir = os.homedir();
|
|
const targetDir = path.join(homedir, '.claude');
|
|
const prefix = computePathPrefix(homedir, targetDir);
|
|
// Verify the prefix uses $HOME, not ~
|
|
assert.ok(!prefix.startsWith('~/'), `pathPrefix must not use ~ (breaks in double-quoted shell commands), got: ${prefix}`);
|
|
assert.ok(prefix.startsWith('$HOME/'), `pathPrefix must use $HOME for shell expansion, got: ${prefix}`);
|
|
});
|
|
});
|
|
|
|
describe('source .md files have no quoted-tilde shell patterns', () => {
|
|
function collectMdFiles(dir) {
|
|
const results = [];
|
|
if (!fs.existsSync(dir)) return results;
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
results.push(...collectMdFiles(fullPath));
|
|
} else if (entry.name.endsWith('.md')) {
|
|
results.push(fullPath);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
const dirsToCheck = ['commands', 'get-shit-done', 'agents'].map(d => path.join(repoRoot, d));
|
|
const mdFiles = dirsToCheck.flatMap(collectMdFiles);
|
|
|
|
test('source .md files exist', () => {
|
|
assert.ok(mdFiles.length > 0, `Expected .md files, found ${mdFiles.length}`);
|
|
});
|
|
|
|
test('no .md file contains node "~/ pattern (quoted tilde breaks shell expansion)', () => {
|
|
const quotedTildePattern = /node\s+"~\//;
|
|
const failures = [];
|
|
for (const file of mdFiles) {
|
|
const content = fs.readFileSync(file, 'utf8');
|
|
if (quotedTildePattern.test(content)) {
|
|
failures.push(path.relative(repoRoot, file));
|
|
}
|
|
}
|
|
assert.deepStrictEqual(failures, [], `Files with quoted-tilde node paths: ${failures.join(', ')}`);
|
|
});
|
|
});
|
|
|
|
describe('installed .md files contain no resolved absolute paths', () => {
|
|
const homedir = os.homedir();
|
|
const targetDir = path.join(homedir, '.claude');
|
|
const pathPrefix = computePathPrefix(homedir, targetDir);
|
|
const claudeDirRegex = /~\/\.claude\//g;
|
|
const claudeHomeRegex = /\$HOME\/\.claude\//g;
|
|
const normalizedHomedir = homedir.replace(/\\/g, '/');
|
|
|
|
function collectMdFiles(dir) {
|
|
const results = [];
|
|
if (!fs.existsSync(dir)) return results;
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
results.push(...collectMdFiles(fullPath));
|
|
} else if (entry.name.endsWith('.md')) {
|
|
results.push(fullPath);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
const dirsToCheck = ['commands', 'get-shit-done', 'agents'].map(d => path.join(repoRoot, d));
|
|
const mdFiles = dirsToCheck.flatMap(collectMdFiles);
|
|
|
|
test('after replacement, no .md file contains os.homedir()', () => {
|
|
const failures = [];
|
|
for (const file of mdFiles) {
|
|
let content = fs.readFileSync(file, 'utf8');
|
|
content = content.replace(claudeDirRegex, pathPrefix);
|
|
content = content.replace(claudeHomeRegex, pathPrefix);
|
|
if (content.includes(normalizedHomedir) && normalizedHomedir !== '$HOME') {
|
|
failures.push(path.relative(repoRoot, file));
|
|
}
|
|
}
|
|
assert.deepStrictEqual(failures, [], `Files with resolved absolute paths: ${failures.join(', ')}`);
|
|
});
|
|
});
|