Skip to content

Commit 9eca728

Browse files
wesmclaude
andauthored
Fix hook test portability and robustness (#182)
## Summary - Add `-Encoding UTF8` to `Set-Content` in `pwdCmd` to avoid UTF-16LE on Windows PowerShell 5.1 - Use exact path comparison on Unix, `EqualFold` only on Windows - Add `t.Cleanup` safety net for logger restoration in log capture tests - Fix stale `shellEscape` doc comment to reflect single-quote behavior ## Test plan - [ ] CI passes on all platforms (Ubuntu, macOS, Windows) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 53c9830 commit 9eca728

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

internal/daemon/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func interpolate(cmd string, event Event) string {
170170
}
171171

172172
// shellEscape quotes a value for safe interpolation into a shell command.
173-
// On Unix, wraps in single quotes with embedded single quotes escaped.
174-
// On Windows, wraps in double quotes with embedded double quotes escaped.
173+
// Wraps in single quotes on all platforms, with embedded single quotes escaped.
174+
// On Windows (PowerShell), '' escapes a literal '. On Unix, uses '"'"'.
175175
func shellEscape(s string) string {
176176
if runtime.GOOS == "windows" {
177177
// PowerShell single-quoted strings: only escape is '' for literal '.

internal/daemon/hooks_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func touchCmd(path string) string {
4040
// pwdCmd returns a platform-appropriate shell command to write the cwd to a file.
4141
func pwdCmd(path string) string {
4242
if runtime.GOOS == "windows" {
43-
return "(Get-Location).Path | Set-Content -NoNewline '" + filepath.ToSlash(path) + "'"
43+
return "[IO.File]::WriteAllText('" + filepath.ToSlash(path) + "', (Get-Location).Path)"
4444
}
4545
return "pwd > " + path
4646
}
@@ -407,7 +407,11 @@ func TestHookRunnerWorkingDirectory(t *testing.T) {
407407
want = w
408408
}
409409
}
410-
if !strings.EqualFold(got, want) {
410+
equal := got == want
411+
if runtime.GOOS == "windows" {
412+
equal = strings.EqualFold(got, want)
413+
}
414+
if !equal {
411415
t.Errorf("hook ran in %q, want %q", got, want)
412416
}
413417
return
@@ -674,6 +678,7 @@ func TestHandleEventLogsWhenHooksFired(t *testing.T) {
674678
var buf bytes.Buffer
675679
prevOut := log.Writer()
676680
log.SetOutput(&buf)
681+
t.Cleanup(func() { log.SetOutput(prevOut) })
677682

678683
cfg := &config.Config{
679684
Hooks: []config.HookConfig{
@@ -710,6 +715,7 @@ func TestHandleEventNoLogWhenNoHooksMatch(t *testing.T) {
710715
var buf bytes.Buffer
711716
prevOut := log.Writer()
712717
log.SetOutput(&buf)
718+
t.Cleanup(func() { log.SetOutput(prevOut) })
713719

714720
cfg := &config.Config{
715721
Hooks: []config.HookConfig{

0 commit comments

Comments
 (0)