dispatch_windows_test.go

 1//go:build windows
 2
 3package shell
 4
 5import (
 6	"os"
 7	"path/filepath"
 8	"testing"
 9)
10
11// TestResolveInterpreter_PermissiveFallback_Windows is the Windows-native
12// counterpart to the POSIX permissive-fallback test. It proves the one
13// behavior that makes `#!/bin/bash` hooks work on a stock Windows box
14// with Git for Windows installed: when the literal interpreter path does
15// not exist, we fall back to a PATH-lookup on the basename and that
16// lookup accepts any executable extension Windows honors (here, `.bat`).
17//
18// We plant a bash.bat in a tempdir rather than a .exe because producing
19// a .exe would require a toolchain step; LookPath on Windows resolves
20// PATHEXT extensions, so .bat is just as valid for the lookup codepath.
21func TestResolveInterpreter_PermissiveFallback_Windows(t *testing.T) {
22	dir := t.TempDir()
23	fake := filepath.Join(dir, "bash.bat")
24	contents := "@echo off\r\nexit /b 0\r\n"
25	if err := os.WriteFile(fake, []byte(contents), 0o755); err != nil {
26		t.Fatalf("write fake bash.bat: %v", err)
27	}
28	t.Setenv("PATH", dir)
29	t.Setenv("PATHEXT", ".BAT;.CMD;.EXE")
30
31	// Literal path must be absent so the stat fails with ENOENT.
32	missing := filepath.Join(dir, "definitely-not-here-"+randSuffix(), "bash")
33	resolved, err := resolveInterpreter(missing)
34	if err != nil {
35		t.Fatalf("expected fallback to succeed, got: %v", err)
36	}
37	if resolved != fake {
38		t.Fatalf("resolved = %q, want %q", resolved, fake)
39	}
40}