sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/gerrit/source/source_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package source contains functions that help with Gerrit source control 18 // specific logics. 19 package source 20 21 import ( 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 ) 26 27 func TestCloneURIFromOrgRepo(t *testing.T) { 28 tests := []struct { 29 name string 30 org string 31 repo string 32 want string 33 }{ 34 { 35 name: "base", 36 org: "foo.baz", 37 repo: "bar", 38 want: "https://foo.baz/bar", 39 }, 40 { 41 name: "with-https", 42 org: "https://foo.baz", 43 repo: "bar", 44 want: "https://foo.baz/bar", 45 }, 46 { 47 name: "with-http", 48 org: "http://foo.baz", 49 repo: "bar", 50 want: "http://foo.baz/bar", 51 }, 52 { 53 name: "org-extra-slash", 54 org: "https://foo.baz/", 55 repo: "bar", 56 want: "https://foo.baz/bar", 57 }, 58 { 59 name: "repo-extra-slash", 60 org: "https://foo.baz", 61 repo: "/bar/", 62 want: "https://foo.baz/bar", 63 }, 64 } 65 66 for _, tc := range tests { 67 tc := tc 68 if got, want := CloneURIFromOrgRepo(tc.org, tc.repo), tc.want; got != want { 69 t.Errorf("CloneURI mismatch. Want: '%s', got: '%s'", want, got) 70 } 71 } 72 } 73 74 func TestNormalizeOrg(t *testing.T) { 75 tests := []struct { 76 name string 77 org string 78 want string 79 }{ 80 { 81 name: "base", 82 org: "foo.baz", 83 want: "https://foo.baz", 84 }, 85 { 86 name: "with-https", 87 org: "https://foo.baz", 88 want: "https://foo.baz", 89 }, 90 { 91 name: "with-http", 92 org: "http://foo.baz", 93 want: "http://foo.baz", 94 }, 95 { 96 name: "org-extra-slash", 97 org: "https://foo.baz/", 98 want: "https://foo.baz", 99 }, 100 } 101 102 for _, tc := range tests { 103 tc := tc 104 if got, want := NormalizeOrg(tc.org), tc.want; got != want { 105 t.Errorf("CloneURI mismatch. Want: '%s', got: '%s'", want, got) 106 } 107 } 108 } 109 110 func TestNormalizeCloneURI(t *testing.T) { 111 tests := []struct { 112 name string 113 cloneURI string 114 want string 115 }{ 116 { 117 name: "base", 118 cloneURI: "foo.baz", 119 want: "https://foo.baz", 120 }, 121 { 122 name: "with-https", 123 cloneURI: "https://foo.baz", 124 want: "https://foo.baz", 125 }, 126 { 127 name: "with-http", 128 cloneURI: "http://foo.baz", 129 want: "http://foo.baz", 130 }, 131 { 132 name: "org-extra-slash", 133 cloneURI: "https://foo.baz/", 134 want: "https://foo.baz", 135 }, 136 } 137 138 for _, tc := range tests { 139 tc := tc 140 t.Run(tc.name, func(t *testing.T) { 141 if got, want := NormalizeCloneURI(tc.cloneURI), tc.want; got != want { 142 t.Errorf("CloneURI mismatch. Want: '%s', got: '%s'", want, got) 143 } 144 }) 145 } 146 } 147 148 func TestOrgRepoFromCloneURI(t *testing.T) { 149 type orgRepo struct { 150 Org, Repo string 151 } 152 tests := []struct { 153 name string 154 cloneURI string 155 want orgRepo 156 wantErr error 157 }{ 158 { 159 name: "base", 160 cloneURI: "foo.baz/bar", 161 want: orgRepo{"https://foo.baz", "bar"}, 162 }, 163 { 164 name: "with-https", 165 cloneURI: "https://foo.baz/bar", 166 want: orgRepo{"https://foo.baz", "bar"}, 167 }, 168 { 169 name: "with-http", 170 cloneURI: "http://foo.baz/bar", 171 want: orgRepo{"http://foo.baz", "bar"}, 172 }, 173 { 174 name: "org-extra-slash", 175 cloneURI: "https://foo.baz/bar//", 176 want: orgRepo{"https://foo.baz", "bar"}, 177 }, 178 } 179 180 for _, tc := range tests { 181 tc := tc 182 t.Run(tc.name, func(t *testing.T) { 183 gotOrg, gotRepo, gotErr := OrgRepoFromCloneURI(tc.cloneURI) 184 if tc.wantErr != nil { 185 if gotErr != tc.wantErr { 186 t.Fatalf("Error mismatch. Want: %v, got: %v", tc.wantErr, gotErr) 187 } 188 return 189 } 190 if diff := cmp.Diff(tc.want, orgRepo{gotOrg, gotRepo}); diff != "" { 191 t.Errorf("CloneURI mismatch. Want(-), got(+):\n%s", diff) 192 } 193 }) 194 } 195 } 196 197 func TestCodeURL(t *testing.T) { 198 tests := []struct { 199 name string 200 in string 201 want string 202 wantErr bool 203 }{ 204 { 205 name: "base", 206 in: "https://foo-review.googlesource.com", 207 want: "https://foo.googlesource.com", 208 }, 209 { 210 name: "with-repo", 211 in: "https://foo-review.googlesource.com/bar", 212 want: "https://foo.googlesource.com/bar", 213 }, 214 { 215 name: "invalid", 216 in: "https://foo.googlesource.com", 217 wantErr: true, 218 }, 219 { 220 name: "non-url", 221 in: "other-review", 222 wantErr: true, 223 }, 224 } 225 226 for _, tc := range tests { 227 tc := tc 228 t.Run(tc.name, func(t *testing.T) { 229 got, gotErr := CodeURL(tc.in) 230 if tc.wantErr { 231 if gotErr == nil { 232 t.Fatal("Want error, got nil") 233 } 234 return 235 } 236 if gotErr != nil { 237 t.Fatalf("Want no error, got: %v", gotErr) 238 } 239 if want, got := tc.want, got; want != got { 240 t.Fatalf("Want: %s, got: %s", want, got) 241 } 242 }) 243 } 244 } 245 246 func TestEnsureCodeURL(t *testing.T) { 247 tests := []struct { 248 name string 249 in string 250 want string 251 }{ 252 { 253 name: "base", 254 in: "https://foo-review.googlesource.com", 255 want: "https://foo.googlesource.com", 256 }, 257 { 258 name: "with-repo", 259 in: "https://foo-review.googlesource.com/bar", 260 want: "https://foo.googlesource.com/bar", 261 }, 262 { 263 name: "other", 264 in: "https://foo.googlesource.com", 265 want: "https://foo.googlesource.com", 266 }, 267 { 268 name: "wrong-place", 269 in: "https://foo.googlesource-review.com", 270 want: "https://foo.googlesource-review.com", 271 }, 272 { 273 name: "non-url", 274 in: "other-review", 275 want: "other-review", 276 }, 277 } 278 279 for _, tc := range tests { 280 t.Run(tc.name, func(t *testing.T) { 281 got := EnsureCodeURL(tc.in) 282 if want, got := tc.want, got; want != got { 283 t.Errorf("Want: %s, got: %s", want, got) 284 } 285 }) 286 } 287 }