| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Resolving a git launcher names its repository by URL, so the configuration |
| 13 | // of whatever repository the process happens to sit in must not reach it. |
| 14 | func TestResolveGitLocatorIgnoresTheWorkingDirectoryRepository(t *testing.T) { |
| 15 | if runtime.GOOS == "windows" { |
| 16 | t.Skip("payload script is POSIX shell") |
| 17 | } |
| 18 | if _, err := exec.LookPath("git"); err != nil { |
| 19 | t.Skip("git not installed") |
| 20 | } |
| 21 | repo := t.TempDir() |
| 22 | if out, err := exec.Command("git", "init", "-q", repo).CombinedOutput(); err != nil { |
| 23 | t.Fatalf("git init: %v: %s", err, out) |
| 24 | } |
| 25 | marker := filepath.Join(t.TempDir(), "executed") |
| 26 | payload := filepath.Join(t.TempDir(), "payload.sh") |
| 27 | if err := os.WriteFile(payload, []byte("#!/bin/sh\necho ran >> '"+marker+"'\nexit 1\n"), 0o755); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if out, err := exec.Command("git", "-C", repo, "config", "core.sshCommand", payload).CombinedOutput(); err != nil { |
| 31 | t.Fatalf("git config: %v: %s", err, out) |
| 32 | } |
| 33 | t.Setenv("GIT_SSH_COMMAND", "") |
| 34 | _ = os.Unsetenv("GIT_SSH_COMMAND") |
| 35 | t.Chdir(repo) |
| 36 | |
| 37 | falseBin, err := exec.LookPath("false") |
| 38 | if err != nil { |
| 39 | t.Skip("no false binary") |
| 40 | } |
| 41 | t.Setenv("GIT_SSH", falseBin) |
| 42 | if _, _, err := resolveGitLocator(context.Background(), Spec{}, "git+ssh://example.invalid/server.git@main"); err == nil { |
| 43 | t.Fatal("resolution succeeded against an unreachable remote") |
| 44 | } |
| 45 | if _, err := os.Stat(marker); err == nil { |
| 46 | t.Fatal("resolving a launcher ran a program named by the working directory's repository") |
| 47 | } |
| 48 | } |
| 49 |