|
1 | 1 | package resolve |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 | ) |
6 | 9 |
|
@@ -106,3 +109,106 @@ func TestDomainFromForgeTypeWithForgeHost(t *testing.T) { |
106 | 109 | t.Errorf("expected FORGE_HOST override for empty type, got %q", got) |
107 | 110 | } |
108 | 111 | } |
| 112 | + |
| 113 | +func TestRemoteDefaultsToOrigin(t *testing.T) { |
| 114 | + if remoteName != "origin" { |
| 115 | + t.Errorf("default remote should be origin, got %q", remoteName) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestSetRemote(t *testing.T) { |
| 120 | + old := remoteName |
| 121 | + defer func() { remoteName = old }() |
| 122 | + |
| 123 | + SetRemote("upstream") |
| 124 | + if remoteName != "upstream" { |
| 125 | + t.Errorf("SetRemote did not update remoteName, got %q", remoteName) |
| 126 | + } |
| 127 | + |
| 128 | + // Empty string should leave the default alone so callers can pass |
| 129 | + // a flag value unconditionally without resetting to "". |
| 130 | + SetRemote("") |
| 131 | + if remoteName != "upstream" { |
| 132 | + t.Errorf("SetRemote(\"\") should be a no-op, got %q", remoteName) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestRemoteSelectsCorrectGitURL(t *testing.T) { |
| 137 | + if _, err := exec.LookPath("git"); err != nil { |
| 138 | + t.Skip("git not installed") |
| 139 | + } |
| 140 | + |
| 141 | + dir := t.TempDir() |
| 142 | + t.Chdir(dir) |
| 143 | + |
| 144 | + mustGit(t, "init", "-q") |
| 145 | + mustGit(t, "remote", "add", "origin", "https://gitea.example.com/owner/origin-repo.git") |
| 146 | + mustGit(t, "remote", "add", "mirror", "https://github.com/owner/mirror-repo.git") |
| 147 | + |
| 148 | + old := remoteName |
| 149 | + defer func() { remoteName = old }() |
| 150 | + |
| 151 | + tests := []struct { |
| 152 | + remote string |
| 153 | + wantDomain string |
| 154 | + wantRepo string |
| 155 | + }{ |
| 156 | + {"origin", "gitea.example.com", "origin-repo"}, |
| 157 | + {"mirror", "github.com", "mirror-repo"}, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tt := range tests { |
| 161 | + t.Run(tt.remote, func(t *testing.T) { |
| 162 | + SetRemote(tt.remote) |
| 163 | + domain, owner, repo, err := resolveRemote() |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("resolveRemote: %v", err) |
| 166 | + } |
| 167 | + if domain != tt.wantDomain { |
| 168 | + t.Errorf("domain = %q, want %q", domain, tt.wantDomain) |
| 169 | + } |
| 170 | + if owner != "owner" { |
| 171 | + t.Errorf("owner = %q, want owner", owner) |
| 172 | + } |
| 173 | + if repo != tt.wantRepo { |
| 174 | + t.Errorf("repo = %q, want %q", repo, tt.wantRepo) |
| 175 | + } |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestRemoteUnknownNameError(t *testing.T) { |
| 181 | + if _, err := exec.LookPath("git"); err != nil { |
| 182 | + t.Skip("git not installed") |
| 183 | + } |
| 184 | + |
| 185 | + dir := t.TempDir() |
| 186 | + t.Chdir(dir) |
| 187 | + |
| 188 | + mustGit(t, "init", "-q") |
| 189 | + mustGit(t, "remote", "add", "origin", "https://github.com/owner/repo.git") |
| 190 | + |
| 191 | + old := remoteName |
| 192 | + defer func() { remoteName = old }() |
| 193 | + |
| 194 | + SetRemote("doesnotexist") |
| 195 | + _, _, _, err := resolveRemote() |
| 196 | + if err == nil { |
| 197 | + t.Fatal("expected error for unknown remote") |
| 198 | + } |
| 199 | + if !strings.Contains(err.Error(), "doesnotexist") { |
| 200 | + t.Errorf("error should mention the remote name, got: %v", err) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func mustGit(t *testing.T, args ...string) { |
| 205 | + t.Helper() |
| 206 | + cmd := exec.Command("git", args...) |
| 207 | + cmd.Env = append(os.Environ(), |
| 208 | + "GIT_CONFIG_GLOBAL=/dev/null", |
| 209 | + "GIT_CONFIG_SYSTEM=/dev/null", |
| 210 | + ) |
| 211 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 212 | + t.Fatalf("git %v: %v\n%s", args, err, out) |
| 213 | + } |
| 214 | +} |
0 commit comments