|
| 1 | +package shim |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/CodingWithCalvin/dtvem.cli/src/internal/constants" |
| 11 | +) |
| 12 | + |
| 13 | +// touch creates an empty file at path, making any missing parent directories. |
| 14 | +// On Unix it sets the executable bit so callers can rely on the file behaving |
| 15 | +// like a real binary for path-resolution tests. |
| 16 | +func touch(t *testing.T, path string) { |
| 17 | + t.Helper() |
| 18 | + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 19 | + t.Fatalf("mkdir %s: %v", filepath.Dir(path), err) |
| 20 | + } |
| 21 | + f, err := os.Create(path) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("create %s: %v", path, err) |
| 24 | + } |
| 25 | + _ = f.Close() |
| 26 | + if runtime.GOOS != constants.OSWindows { |
| 27 | + if err := os.Chmod(path, 0755); err != nil { |
| 28 | + t.Fatalf("chmod %s: %v", path, err) |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// runtimeBin returns the conventional name for a primary runtime binary on the |
| 34 | +// current platform — e.g. "python.exe" on Windows, "python" on Unix. |
| 35 | +func runtimeBin(name string) string { |
| 36 | + if runtime.GOOS == constants.OSWindows { |
| 37 | + return name + constants.ExtExe |
| 38 | + } |
| 39 | + return name |
| 40 | +} |
| 41 | + |
| 42 | +// secondaryBin returns the conventional name for a secondary executable on the |
| 43 | +// current platform. The .ext argument is the Windows extension to use; on Unix |
| 44 | +// the extension is dropped because Unix scripts are typically extensionless. |
| 45 | +func secondaryBin(name, ext string) string { |
| 46 | + if runtime.GOOS == constants.OSWindows { |
| 47 | + return name + ext |
| 48 | + } |
| 49 | + return name |
| 50 | +} |
| 51 | + |
| 52 | +func TestFindSecondaryExecutable_FoundAlongsideRuntime(t *testing.T) { |
| 53 | + dir := t.TempDir() |
| 54 | + runtimePath := filepath.Join(dir, runtimeBin("python")) |
| 55 | + touch(t, runtimePath) |
| 56 | + secondary := filepath.Join(dir, secondaryBin("pip", constants.ExtExe)) |
| 57 | + touch(t, secondary) |
| 58 | + |
| 59 | + got, err := FindSecondaryExecutable(runtimePath, "pip") |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("unexpected error: %v", err) |
| 62 | + } |
| 63 | + if got != secondary { |
| 64 | + t.Errorf("got %q, want %q", got, secondary) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestFindSecondaryExecutable_FoundInScriptsSubdir(t *testing.T) { |
| 69 | + if runtime.GOOS != constants.OSWindows { |
| 70 | + t.Skip("Scripts/ subdirectory layout is Windows-specific (Python on Windows)") |
| 71 | + } |
| 72 | + dir := t.TempDir() |
| 73 | + runtimePath := filepath.Join(dir, runtimeBin("python")) |
| 74 | + touch(t, runtimePath) |
| 75 | + secondary := filepath.Join(dir, "Scripts", secondaryBin("uv", constants.ExtExe)) |
| 76 | + touch(t, secondary) |
| 77 | + |
| 78 | + got, err := FindSecondaryExecutable(runtimePath, "uv") |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("unexpected error: %v", err) |
| 81 | + } |
| 82 | + if got != secondary { |
| 83 | + t.Errorf("got %q, want %q", got, secondary) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestFindSecondaryExecutable_FoundInParentScriptsSubdir(t *testing.T) { |
| 88 | + if runtime.GOOS != constants.OSWindows { |
| 89 | + t.Skip("Scripts/ subdirectory layout is Windows-specific") |
| 90 | + } |
| 91 | + root := t.TempDir() |
| 92 | + binDir := filepath.Join(root, "bin") |
| 93 | + runtimePath := filepath.Join(binDir, runtimeBin("python")) |
| 94 | + touch(t, runtimePath) |
| 95 | + secondary := filepath.Join(root, "Scripts", secondaryBin("uv", constants.ExtExe)) |
| 96 | + touch(t, secondary) |
| 97 | + |
| 98 | + got, err := FindSecondaryExecutable(runtimePath, "uv") |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("unexpected error: %v", err) |
| 101 | + } |
| 102 | + // filepath.Clean should have collapsed the "..". Compare on cleaned form. |
| 103 | + if filepath.Clean(got) != filepath.Clean(secondary) { |
| 104 | + t.Errorf("got %q, want %q", got, secondary) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestFindSecondaryExecutable_PrefersCmdOverExeOnWindows(t *testing.T) { |
| 109 | + if runtime.GOOS != constants.OSWindows { |
| 110 | + t.Skip("Windows extension preference is Windows-specific") |
| 111 | + } |
| 112 | + dir := t.TempDir() |
| 113 | + runtimePath := filepath.Join(dir, runtimeBin("node")) |
| 114 | + touch(t, runtimePath) |
| 115 | + cmdPath := filepath.Join(dir, "npm"+constants.ExtCmd) |
| 116 | + exePath := filepath.Join(dir, "npm"+constants.ExtExe) |
| 117 | + touch(t, cmdPath) |
| 118 | + touch(t, exePath) |
| 119 | + |
| 120 | + got, err := FindSecondaryExecutable(runtimePath, "npm") |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("unexpected error: %v", err) |
| 123 | + } |
| 124 | + if got != cmdPath { |
| 125 | + t.Errorf("got %q, want %q (.cmd should be preferred)", got, cmdPath) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestFindSecondaryExecutable_NotFoundReturnsError(t *testing.T) { |
| 130 | + dir := t.TempDir() |
| 131 | + runtimePath := filepath.Join(dir, runtimeBin("python")) |
| 132 | + touch(t, runtimePath) |
| 133 | + |
| 134 | + got, err := FindSecondaryExecutable(runtimePath, "uv") |
| 135 | + if err == nil { |
| 136 | + t.Fatalf("expected error, got nil and path %q", got) |
| 137 | + } |
| 138 | + if !errors.Is(err, ErrSecondaryExecutableNotFound) { |
| 139 | + t.Errorf("expected ErrSecondaryExecutableNotFound, got %v", err) |
| 140 | + } |
| 141 | + if got != "" { |
| 142 | + t.Errorf("expected empty path on error, got %q", got) |
| 143 | + } |
| 144 | +} |
0 commit comments