| 1 | //go:build windows |
| 2 | |
| 3 | package plugin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestResolveStdioExecutableWindowsPathAndPATHEXT(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | npx := filepath.Join(dir, "npx.cmd") |
| 16 | if err := os.WriteFile(npx, []byte("@echo off\r\n"), 0o644); err != nil { |
| 17 | t.Fatalf("write fake npx.cmd: %v", err) |
| 18 | } |
| 19 | |
| 20 | exe, env, err := resolveStdioExecutable(context.Background(), Spec{Name: "fs", Command: "npx"}, []string{ |
| 21 | "Path=" + dir, |
| 22 | "PATHEXT=.CMD;.EXE", |
| 23 | }) |
| 24 | if err != nil { |
| 25 | t.Fatalf("resolveStdioExecutable: %v", err) |
| 26 | } |
| 27 | if !strings.EqualFold(exe, npx) { |
| 28 | t.Fatalf("resolved executable = %q, want %q", exe, npx) |
| 29 | } |
| 30 | if got, ok := envValue(env, "PATH"); !ok || got != dir { |
| 31 | t.Fatalf("env PATH = %q, %v; want %q, true", got, ok, dir) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestResolveStdioExecutableWindowsUsesCommonNodeFallback(t *testing.T) { |
| 36 | root := t.TempDir() |
| 37 | localAppData := filepath.Join(root, "Local") |
| 38 | nodeDir := filepath.Join(localAppData, "Programs", "nodejs") |
| 39 | if err := os.MkdirAll(nodeDir, 0o755); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | npx := filepath.Join(nodeDir, "npx.cmd") |
| 43 | if err := os.WriteFile(npx, []byte("@echo off\r\n"), 0o644); err != nil { |
| 44 | t.Fatalf("write fake npx.cmd: %v", err) |
| 45 | } |
| 46 | |
| 47 | exe, env, err := resolveStdioExecutable(context.Background(), Spec{Name: "fs", Command: "npx"}, []string{ |
| 48 | "Path=", |
| 49 | "PATHEXT=.CMD;.EXE", |
| 50 | "LOCALAPPDATA=" + localAppData, |
| 51 | }) |
| 52 | if err != nil { |
| 53 | t.Fatalf("resolveStdioExecutable: %v", err) |
| 54 | } |
| 55 | if !strings.EqualFold(exe, npx) { |
| 56 | t.Fatalf("resolved executable = %q, want %q", exe, npx) |
| 57 | } |
| 58 | if got, ok := envValue(env, "PATH"); !ok || !strings.Contains(strings.ToLower(got), strings.ToLower(nodeDir)) { |
| 59 | t.Fatalf("env PATH = %q, %v; want node fallback dir", got, ok) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestSetEnvValueWindowsReplacesPathCaseInsensitively(t *testing.T) { |
| 64 | env := setEnvValue([]string{"Path=C:\\old", "OTHER=x"}, "PATH", "C:\\new") |
| 65 | if got, ok := envValue(env, "Path"); !ok || got != "C:\\new" { |
| 66 | t.Fatalf("env Path = %q, %v; want C:\\new, true", got, ok) |
| 67 | } |
| 68 | if len(env) != 2 { |
| 69 | t.Fatalf("setEnvValue should replace Path instead of appending PATH, got %v", env) |
| 70 | } |
| 71 | } |
| 72 |