|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/cli/cli/internal/ghrepo" |
@@ -220,6 +221,101 @@ t001: team(slug:"robots"){id,slug} |
220 | 221 | } |
221 | 222 | } |
222 | 223 |
|
| 224 | +func Test_LabelsByNames(t *testing.T) { |
| 225 | + http := &httpmock.Registry{} |
| 226 | + client := NewClient(ReplaceTripper(http)) |
| 227 | + |
| 228 | + http.StubResponse(200, bytes.NewBufferString(` |
| 229 | + { "data": { "repository": { |
| 230 | + "labels": { |
| 231 | + "nodes": [ |
| 232 | + { "id": "1", "name": "L1" }, |
| 233 | + { "id": "2", "name": "L2" } |
| 234 | + ], |
| 235 | + "pageInfo": { |
| 236 | + "hasNextPage": true, |
| 237 | + "endCursor": "ENDCURSOR" |
| 238 | + } |
| 239 | + } |
| 240 | + } } } |
| 241 | + `)) |
| 242 | + http.StubResponse(200, bytes.NewBufferString(` |
| 243 | + { "data": { "repository": { |
| 244 | + "labels": { |
| 245 | + "nodes": [ |
| 246 | + { "id": "3", "name": "L3"} |
| 247 | + ], |
| 248 | + "pageInfo": { |
| 249 | + "hasNextPage": false, |
| 250 | + "endCursor": "ENDCURSOR" |
| 251 | + } |
| 252 | + } |
| 253 | + } } } |
| 254 | + `)) |
| 255 | + |
| 256 | + repo, _ := ghrepo.FromFullName("OWNER/REPO") |
| 257 | + labels, err := LabelsByNames(client, repo, []string{"l1", "L3"}) |
| 258 | + if err != nil { |
| 259 | + t.Fatalf("unexpected error: %v", err) |
| 260 | + } |
| 261 | + |
| 262 | + eq(t, len(labels), 2) |
| 263 | + eq(t, labels[0].ID, "1") |
| 264 | + eq(t, labels[0].Name, "L1") |
| 265 | + eq(t, labels[1].ID, "3") |
| 266 | + eq(t, labels[1].Name, "L3") |
| 267 | +} |
| 268 | + |
| 269 | +func Test_LabelsByNames_Missing_Name(t *testing.T) { |
| 270 | + http := &httpmock.Registry{} |
| 271 | + client := NewClient(ReplaceTripper(http)) |
| 272 | + |
| 273 | + http.StubResponse(200, bytes.NewBufferString(` |
| 274 | + { "data": { "repository": { |
| 275 | + "labels": { |
| 276 | + "nodes": [ |
| 277 | + { "id": "1", "name": "L1" }, |
| 278 | + { "id": "2", "name": "L2" } |
| 279 | + ], |
| 280 | + "pageInfo": { |
| 281 | + "hasNextPage": false, |
| 282 | + "endCursor": "ENDCURSOR" |
| 283 | + } |
| 284 | + } |
| 285 | + } } } |
| 286 | + `)) |
| 287 | + |
| 288 | + repo, _ := ghrepo.FromFullName("OWNER/REPO") |
| 289 | + _, err := LabelsByNames(client, repo, []string{"l1", "l4"}) |
| 290 | + |
| 291 | + eq(t, err.Error(), "no label found with name: l4") |
| 292 | +} |
| 293 | + |
| 294 | +func Test_LabelsByNames_Missing_Names(t *testing.T) { |
| 295 | + http := &httpmock.Registry{} |
| 296 | + client := NewClient(ReplaceTripper(http)) |
| 297 | + |
| 298 | + http.StubResponse(200, bytes.NewBufferString(` |
| 299 | + { "data": { "repository": { |
| 300 | + "labels": { |
| 301 | + "nodes": [ |
| 302 | + { "id": "1", "name": "L1" }, |
| 303 | + { "id": "2", "name": "L2" } |
| 304 | + ], |
| 305 | + "pageInfo": { |
| 306 | + "hasNextPage": false, |
| 307 | + "endCursor": "ENDCURSOR" |
| 308 | + } |
| 309 | + } |
| 310 | + } } } |
| 311 | + `)) |
| 312 | + |
| 313 | + repo, _ := ghrepo.FromFullName("OWNER/REPO") |
| 314 | + _, err := LabelsByNames(client, repo, []string{"l1", "l4", "L5"}) |
| 315 | + |
| 316 | + eq(t, err.Error(), "no labels found with names: l4, L5") |
| 317 | +} |
| 318 | + |
223 | 319 | func sliceEqual(a, b []string) bool { |
224 | 320 | if len(a) != len(b) { |
225 | 321 | return false |
|
0 commit comments