|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + forges "github.com/git-pkgs/forge" |
| 9 | + "github.com/git-pkgs/forge/bitbucket" |
| 10 | + "github.com/git-pkgs/forge/gitea" |
| 11 | + ghforge "github.com/git-pkgs/forge/github" |
| 12 | + glforge "github.com/git-pkgs/forge/gitlab" |
| 13 | + "github.com/git-pkgs/forge/internal/config" |
| 14 | + "github.com/git-pkgs/forge/internal/resolve" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAPIBaseURLFromForge(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + f forges.Forge |
| 21 | + want string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "github", |
| 25 | + f: ghforge.New("", nil), |
| 26 | + want: "https://api.github.com", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "github enterprise", |
| 30 | + f: ghforge.NewWithBase("https://github.enterprise.example.org", "", nil), |
| 31 | + want: "https://github.enterprise.example.org/api/v3", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "gitlab", |
| 35 | + f: glforge.New("https://mylab.example.org", "", nil), |
| 36 | + want: "https://mylab.example.org/api/v4", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "gitea", |
| 40 | + f: gitea.New("https://gitea.example.org", "", nil), |
| 41 | + want: "https://gitea.example.org/api/v1", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "forgejo", |
| 45 | + f: gitea.New("https://forgejo.example.org", "", nil), |
| 46 | + want: "https://forgejo.example.org/api/v1", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "bitbucket", |
| 50 | + f: bitbucket.New("", nil), |
| 51 | + want: "https://api.bitbucket.org/2.0", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + got := apiBaseURL(tt.f, "fallback.example.org") |
| 58 | + if got != tt.want { |
| 59 | + t.Fatalf("APIBaseURL() = %q, want %q", got, tt.want) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestAPIBaseURLFallsBackToLegacyDomainHeuristics(t *testing.T) { |
| 66 | + tests := []struct { |
| 67 | + name string |
| 68 | + domain string |
| 69 | + want string |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "github", |
| 73 | + domain: "github.example.org", |
| 74 | + want: "https://api.github.example.org", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "gitlab", |
| 78 | + domain: "gitlab.example.org", |
| 79 | + want: "https://gitlab.example.org/api/v4", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "bitbucket", |
| 83 | + domain: "bitbucket.example.org", |
| 84 | + want: "https://api.bitbucket.org/2.0", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "default", |
| 88 | + domain: "forge.example.org", |
| 89 | + want: "https://forge.example.org/api/v1", |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + got := apiBaseURL(&mockForge{}, tt.domain) |
| 96 | + if got != tt.want { |
| 97 | + t.Fatalf("apiBaseURL(..., %q) = %q, want %q", tt.domain, got, tt.want) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestAPIBaseURLUsesLegacyFallbackForUnknownDomainWithoutConfiguredForgeType(t *testing.T) { |
| 104 | + got := apiBaseURL(&mockForge{}, "mylab.example.org") |
| 105 | + want := "https://mylab.example.org/api/v1" |
| 106 | + if got != want { |
| 107 | + t.Fatalf("apiBaseURL(..., %q) = %q, want %q", "mylab.example.org", got, want) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestAPIBaseURLUsesConfiguredForgeTypeForUnknownDomain(t *testing.T) { |
| 112 | + config.ResetCache() |
| 113 | + defer config.ResetCache() |
| 114 | + |
| 115 | + xdgConfigHome := t.TempDir() |
| 116 | + t.Setenv("XDG_CONFIG_HOME", xdgConfigHome) |
| 117 | + |
| 118 | + configDir := filepath.Join(xdgConfigHome, "forge") |
| 119 | + if err := os.MkdirAll(configDir, 0700); err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + configPath := filepath.Join(configDir, "config") |
| 123 | + if err := os.WriteFile(configPath, []byte(`[mylab.example.org] |
| 124 | +type = gitlab |
| 125 | +`), 0600); err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + |
| 129 | + forge, _, _, domain, err := resolve.Repo("mylab.example.org/imt/deployments", "") |
| 130 | + if err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | + |
| 134 | + got := apiBaseURL(forge, domain) |
| 135 | + want := "https://mylab.example.org/api/v4" |
| 136 | + if got != want { |
| 137 | + t.Fatalf("apiBaseURL(..., %q) = %q, want %q", domain, got, want) |
| 138 | + } |
| 139 | +} |
0 commit comments