| 1 | package hook |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/pluginpkg" |
| 8 | ) |
| 9 | |
| 10 | func TestWindowsExtensionlessPOSIXPluginHookUsesBash(t *testing.T) { |
| 11 | root := filepath.Join(t.TempDir(), "plugin root") |
| 12 | script := filepath.Join(root, "hooks", "session-start-codex") |
| 13 | writeHookTestFile(t, script, "#!/usr/bin/env bash\necho ok\n") |
| 14 | |
| 15 | config := pluginHookExecutionConfigForPlatform(pluginpkg.Hook{Command: "hooks/session-start-codex"}, root, "windows") |
| 16 | if config.ExecutionMode != ExecutionShell || config.Shell != "bash" { |
| 17 | t.Fatalf("execution contract = mode %q shell %q, want Bash shell form", config.ExecutionMode, config.Shell) |
| 18 | } |
| 19 | if want := bashSingleQuote(filepath.ToSlash(script)); config.Command != want { |
| 20 | t.Fatalf("command = %q, want safely quoted script path %q", config.Command, want) |
| 21 | } |
| 22 | if !requiresWindowsBash(config) { |
| 23 | t.Fatal("extensionless POSIX plugin hook should report a Windows Bash dependency") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestWindowsPluginHookDoesNotGuessBashForNativeOrNonShellFiles(t *testing.T) { |
| 28 | root := filepath.Join(t.TempDir(), "plugin root") |
| 29 | writeHookTestFile(t, filepath.Join(root, "hooks", "native-tool"), "MZ\x00not a shell script\n") |
| 30 | writeHookTestFile(t, filepath.Join(root, "hooks", "run-hook.cmd"), "@echo off\r\n") |
| 31 | |
| 32 | for _, command := range []string{"hooks/native-tool", "hooks/run-hook.cmd"} { |
| 33 | config := pluginHookExecutionConfigForPlatform(pluginpkg.Hook{Command: command}, root, "windows") |
| 34 | if config.ExecutionMode != ExecutionLegacy || config.Shell != "" { |
| 35 | t.Fatalf("command %q guessed shell execution: mode %q shell %q", command, config.ExecutionMode, config.Shell) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestWindowsImplicitPluginPOSIXScriptUsesBashPath(t *testing.T) { |
| 41 | root := filepath.Join(t.TempDir(), "plugin root") |
| 42 | script := filepath.Join(root, "hooks", "start.sh") |
| 43 | writeHookTestFile(t, script, "#!/usr/bin/env bash\necho ok\n") |
| 44 | config := pluginHookExecutionConfigForPlatform(pluginpkg.Hook{ |
| 45 | Command: `"${CLAUDE_PLUGIN_ROOT}/hooks/start.sh"`, |
| 46 | ShellCommand: true, |
| 47 | }, root, "windows") |
| 48 | if config.ExecutionMode != ExecutionShell || config.Shell != "bash" { |
| 49 | t.Fatalf("implicit shell contract = mode %q shell %q, want explicit Bash", config.ExecutionMode, config.Shell) |
| 50 | } |
| 51 | if want := bashSingleQuote(filepath.ToSlash(script)); config.Command != want { |
| 52 | t.Fatalf("implicit shell command = %q, want %q", config.Command, want) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestWindowsLegacyPOSIXScriptSpawnInputUsesBash(t *testing.T) { |
| 57 | root := filepath.Join(t.TempDir(), "workspace root") |
| 58 | script := filepath.Join(root, "hooks", "session-start") |
| 59 | writeHookTestFile(t, script, "#!/usr/bin/env bash\necho ok\n") |
| 60 | |
| 61 | got := normalizeWindowsHookSpawnInputForPlatform(SpawnInput{ |
| 62 | Command: "hooks/session-start", |
| 63 | Mode: ExecutionLegacy, |
| 64 | Cwd: root, |
| 65 | }, "windows") |
| 66 | if got.Mode != ExecutionShell || got.Shell != "bash" { |
| 67 | t.Fatalf("normalized spawn contract = mode %q shell %q, want Bash shell", got.Mode, got.Shell) |
| 68 | } |
| 69 | if want := bashSingleQuote(filepath.ToSlash(script)); got.Command != want { |
| 70 | t.Fatalf("normalized command = %q, want %q", got.Command, want) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestWindowsLegacyPOSIXScriptRuntimeRequiresBash(t *testing.T) { |
| 75 | root := filepath.Join(t.TempDir(), "workspace root") |
| 76 | script := filepath.Join(root, "hooks", "session-start") |
| 77 | writeHookTestFile(t, script, "#!/usr/bin/env bash\necho ok\n") |
| 78 | |
| 79 | config := HookConfig{Command: "hooks/session-start", Cwd: root, ExecutionMode: ExecutionLegacy} |
| 80 | if !requiresWindowsBash(config) { |
| 81 | t.Fatal("legacy POSIX script hook should require Bash on Windows") |
| 82 | } |
| 83 | } |
| 84 |