diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7626d38..c2386ad 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: Lint SKILL.md +name: Lint and guard tests on: pull_request: diff --git a/CHANGELOG.md b/CHANGELOG.md index ed9b57c..5d84d76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `docs/*`, `ci/*` and `perf/*` working branches for single-type changes: base and merge target `develop` (or an epic), accepted by the guard's default `branchTypes` and `pr-base` table (#103) - Small-issue bundles: two or three small, related issues may share one branch and one PR, with atomic commits per issue and one `Closes #N` line each (#98) +### Fixed + +- Guard rough edges: uninstall no longer leaves an empty `"hooks": {}`, install backs up an unparsable user config like repair, a non-array `harnesses` value is ignored, `/cygdrive//` paths map on Windows, bash installers reject an invalid `--scope` (#110) + ## [1.5.0] - 2026-11-19 ### Added diff --git a/README.md b/README.md index e250890..039bf97 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ The install scripts register the guard right after the skill symlink when `node` .\scripts\install_windows.ps1 -Scope all # -NoHooks and -Uninstall work too ``` -You can also ask Claude Code ("install the guard", "is the guard working?") or run the guard directly: `node hooks/guard.mjs install|update|doctor|repair|uninstall|enable|disable`. `install` accepts `--scope opt-in|all` and `--harness claude,codex`. Re-running `install` without `--scope` keeps the scope you chose earlier. Run `doctor` to check the registration. +You can also ask Claude Code ("install the guard", "is the guard working?") or run the guard directly: `node /path/to/clone/hooks/guard.mjs install|update|doctor|repair|uninstall|enable|disable` (use the absolute path; `install` prints it, and `enable`/`disable` act on the repository of the current directory). `install` accepts `--scope opt-in|all` and `--harness claude,codex`. Re-running `install` without `--scope` keeps the scope you chose earlier. Run `doctor` to check the registration. ### Scope and per-repository config diff --git a/hooks/lib/manage/commands.mjs b/hooks/lib/manage/commands.mjs index 0a41acd..2d65a7c 100644 --- a/hooks/lib/manage/commands.mjs +++ b/hooks/lib/manage/commands.mjs @@ -24,6 +24,16 @@ function codexFeatureEnabled(env) { } catch { return false; } } +// Keep the user's broken file: a typo must not silently reset scope or other settings. +function backUpInvalid(user, lines) { + if (user.value || user.missing) return; + fs.copyFileSync(user.file, `${user.file}.bak`); + lines.push(`invalid user config ${user.file} (${user.error}) backed up to ${user.file}.bak`); +} + +const mergeHarnesses = (existing, registered) => + [...new Set([...(Array.isArray(existing) ? existing : []), ...registered])]; + export function install({ scope, harnesses, env = process.env } = {}) { if (scope !== undefined && scope !== 'opt-in' && scope !== 'all') throw new Error(`invalid scope ${JSON.stringify(scope)}; use opt-in or all`); const lines = []; @@ -43,13 +53,15 @@ export function install({ scope, harnesses, env = process.env } = {}) { lines.push(`${name}: error: ${e.message}; left untouched`); } } - const file = userConfigPath(env); - const existing = readUserConfig(env).value; + const user = readUserConfig(env); + backUpInvalid(user, lines); + const file = user.file; + const existing = user.value; const kept = scope === undefined && (existing?.scope === 'all' || existing?.scope === 'opt-in'); const finalScope = scope ?? (kept ? existing.scope : 'opt-in'); const cfg = { scope: finalScope, version: skillVersion(), guardPath: GUARD_PATH, - harnesses: [...new Set([...(Array.isArray(existing?.harnesses) ? existing.harnesses : []), ...registered])], + harnesses: mergeHarnesses(existing?.harnesses, registered), }; writeFileThrough(file, `${JSON.stringify(cfg, null, 2)}\n`); lines.push(`user config written to ${file} (scope: ${finalScope}${kept ? ', kept' : ''})`); @@ -263,13 +275,9 @@ export function repair({ env = process.env } = {}) { } } const user = readUserConfig(env); - if (!user.value && !user.missing) { - // Keep the user's broken file: a typo must not silently reset scope or other settings. - fs.copyFileSync(user.file, `${user.file}.bak`); - lines.push(`invalid user config ${user.file} (${user.error}) backed up to ${user.file}.bak`); - } + backUpInvalid(user, lines); const base = user.value ?? {}; - const cfg = { ...base, scope: base.scope === 'all' ? 'all' : 'opt-in', version: skillVersion(), guardPath: GUARD_PATH, harnesses: [...new Set([...(base.harnesses ?? []), ...registered])] }; + const cfg = { ...base, scope: base.scope === 'all' ? 'all' : 'opt-in', version: skillVersion(), guardPath: GUARD_PATH, harnesses: mergeHarnesses(base.harnesses, registered) }; writeFileThrough(user.file, `${JSON.stringify(cfg, null, 2)}\n`); lines.push(`user config rewritten at ${user.file} (version ${cfg.version})`); return lines; diff --git a/hooks/lib/manage/registry.mjs b/hooks/lib/manage/registry.mjs index 2810692..80ea164 100644 --- a/hooks/lib/manage/registry.mjs +++ b/hooks/lib/manage/registry.mjs @@ -118,6 +118,8 @@ export function unregister(harness, env = process.env) { changed = true; if (kept.length) cfg.hooks[event] = kept; else delete cfg.hooks[event]; } + // Leave no empty "hooks": {} behind when ours were the only hooks. + if (changed && Object.keys(cfg.hooks).length === 0) delete cfg.hooks; if (changed) writeConfig(file, cfg); return { changed }; } diff --git a/hooks/lib/paths.mjs b/hooks/lib/paths.mjs index 8448982..2bc2cea 100644 --- a/hooks/lib/paths.mjs +++ b/hooks/lib/paths.mjs @@ -3,10 +3,11 @@ import path from 'node:path'; export const pathApi = (platform = process.platform) => (platform === 'win32' ? path.win32 : path.posix); -// Git Bash (how Claude Code and Codex run commands on Windows) names drives as /c/... or /C; +// Git Bash (how Claude Code and Codex run commands on Windows) names drives as /c/... or /C, +// Cygwin as /cygdrive/c/...; // Node would read that as \c\... on the current drive. Other platforms are left untouched. export function fromGitBash(p, platform = process.platform) { if (platform !== 'win32' || typeof p !== 'string') return p; - const m = /^\/([a-zA-Z])(?:\/|$)/.exec(p); + const m = /^\/(?:cygdrive\/)?([a-zA-Z])(?:\/|$)/.exec(p); return m ? `${m[1].toUpperCase()}:/${p.slice(m[0].length)}` : p; } diff --git a/hooks/test/check.test.mjs b/hooks/test/check.test.mjs index 7dc86f8..cedb894 100644 --- a/hooks/test/check.test.mjs +++ b/hooks/test/check.test.mjs @@ -16,7 +16,7 @@ const mkRepo = (name, optIn = true) => { return dir; }; const repo = mkRepo('repo'); -const env = { XDG_CONFIG_HOME: xdg, HOME: tmp }; +const env = { XDG_CONFIG_HOME: xdg, APPDATA: xdg, HOME: tmp, USERPROFILE: tmp }; const spy = (impl) => { const calls = []; diff --git a/hooks/test/doctor.test.mjs b/hooks/test/doctor.test.mjs index cd11700..d0f2eb6 100644 --- a/hooks/test/doctor.test.mjs +++ b/hooks/test/doctor.test.mjs @@ -64,6 +64,17 @@ test('registered path pointing to a deleted file fails claude-path; repair fixes assert.ok(after.hooks.PreToolUse.some((e) => e.hooks[0].command === 'echo foreign'), 'foreign entry kept'); }); +test('repair ignores a non-array harnesses list', () => { + const env = makeEnv(); + install({ scope: 'all', env }); + const file = userConfigPath(env); + fs.writeFileSync(file, JSON.stringify({ scope: 'all', harnesses: 'claude' })); + repair({ env }); + const { harnesses } = JSON.parse(fs.readFileSync(file, 'utf8')); + assert.ok(Array.isArray(harnesses)); + assert.ok(!harnesses.includes('c'), JSON.stringify(harnesses)); +}); + test('repair backs up an invalid user config before rewriting it', () => { const env = makeEnv(); install({ scope: 'all', env }); diff --git a/hooks/test/levels.test.mjs b/hooks/test/levels.test.mjs index b04b93b..d83d05e 100644 --- a/hooks/test/levels.test.mjs +++ b/hooks/test/levels.test.mjs @@ -47,7 +47,7 @@ test('runCheck: "on" denies attribution; raw fallback honours warn and off', asy const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'guard-levels-')); const repo = path.join(tmp, 'repo'); fs.mkdirSync(path.join(repo, '.git'), { recursive: true }); - const env = { XDG_CONFIG_HOME: path.join(tmp, 'xdg'), HOME: tmp }; + const env = { XDG_CONFIG_HOME: path.join(tmp, 'xdg'), APPDATA: path.join(tmp, 'xdg'), HOME: tmp, USERPROFILE: tmp }; const exec = async () => ({ code: 1, stdout: '', stderr: '' }); const run = (command, rules) => { fs.writeFileSync(path.join(repo, '.git-workflow.json'), JSON.stringify({ rules })); diff --git a/hooks/test/manage.test.mjs b/hooks/test/manage.test.mjs index 1616a6a..268717a 100644 --- a/hooks/test/manage.test.mjs +++ b/hooks/test/manage.test.mjs @@ -87,6 +87,27 @@ test('register writes through a symlinked settings file and keeps its mode', { s assert.equal(fs.lstatSync(file).isSymbolicLink(), true); }); +test('unregister drops the hooks key when only our entries were in it', () => { + const env = makeEnv(); + const file = HARNESSES.claude.configPath(env); + fs.writeFileSync(file, JSON.stringify({ theme: 'dark' })); + register('claude', GUARD, env); + assert.equal(unregister('claude', env).changed, true); + assert.deepEqual(readJson(file), { theme: 'dark' }); +}); + +test('install backs up an invalid user config before rewriting it', () => { + const env = makeEnv(); + install({ scope: 'all', env }); + const file = userConfigPath(env); + fs.writeFileSync(file, '{"scope": "all",'); + const lines = install({ env }); + assert.equal(fs.readFileSync(`${file}.bak`, 'utf8'), '{"scope": "all",'); + assert.ok(lines.some((l) => l.includes(`${file}.bak`)), lines.join('\n')); + assert.equal(readJson(file).scope, 'opt-in'); + assert.ok(!install({ env }).some((l) => l.includes('.bak'))); +}); + test('unregister removes only ours', () => { const env = makeEnv(); const file = HARNESSES.claude.configPath(env); diff --git a/hooks/test/shell.test.mjs b/hooks/test/shell.test.mjs index f97bc5c..cea965d 100644 --- a/hooks/test/shell.test.mjs +++ b/hooks/test/shell.test.mjs @@ -175,3 +175,12 @@ test('cd to a Git Bash drive path maps to the drive on win32 only', () => { const l = parseCommandLine('cd /c/Users/x && git status', { cwd: '/r', platform: 'linux' }); assert.equal(l.commands[1].cwd, '/c/Users/x'); }); + +test('cd to a Cygwin drive path maps to the drive on win32 only', () => { + const w = parseCommandLine('cd /cygdrive/c/Users/x && git status; cd /cygdrive/d && git log; cd /cygdrive && git log', { cwd: 'C:\\r', platform: 'win32' }); + assert.equal(w.commands[1].cwd, 'C:/Users/x'); + assert.equal(w.commands[3].cwd, 'D:/'); + assert.notEqual(w.commands[5].cwd, 'C:/'); + const l = parseCommandLine('cd /cygdrive/c/Users/x && git status', { cwd: '/r', platform: 'linux' }); + assert.equal(l.commands[1].cwd, '/cygdrive/c/Users/x'); +}); diff --git a/scripts/install_linux.sh b/scripts/install_linux.sh index 2de1f8c..99196ec 100755 --- a/scripts/install_linux.sh +++ b/scripts/install_linux.sh @@ -105,6 +105,11 @@ while [[ $# -gt 0 ]]; do shift done +case "$SCOPE" in + ""|opt-in|all) ;; + *) fail "Invalid --scope value: $SCOPE (use opt-in or all)"; usage ;; +esac + if [[ "$ACTION" == "uninstall" ]]; then uninstall else diff --git a/scripts/install_macos.sh b/scripts/install_macos.sh index 563873f..8bb2102 100755 --- a/scripts/install_macos.sh +++ b/scripts/install_macos.sh @@ -116,6 +116,11 @@ while [[ $# -gt 0 ]]; do shift done +case "$SCOPE" in + ""|opt-in|all) ;; + *) fail "Invalid --scope value: $SCOPE (use opt-in or all)"; usage ;; +esac + if [[ "$ACTION" == "uninstall" ]]; then uninstall else