|
5 | 5 | "encoding/json" |
6 | 6 | "io" |
7 | 7 | "os" |
| 8 | + "reflect" |
8 | 9 | "testing" |
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/cli/cli/v2/api" |
11 | 13 | "github.com/cli/cli/v2/internal/ghrepo" |
@@ -239,3 +241,213 @@ func TestChecksRun_web(t *testing.T) { |
239 | 241 | }) |
240 | 242 | } |
241 | 243 | } |
| 244 | + |
| 245 | +func TestEliminateDupulicates(t *testing.T) { |
| 246 | + tests := []struct { |
| 247 | + name string |
| 248 | + checkContexts []api.CheckContext |
| 249 | + want []api.CheckContext |
| 250 | + }{ |
| 251 | + { |
| 252 | + name: "duplicate CheckRun (lint)", |
| 253 | + checkContexts: []api.CheckContext{ |
| 254 | + { |
| 255 | + TypeName: "CheckRun", |
| 256 | + Name: "build (ubuntu-latest)", |
| 257 | + Status: "COMPLETED", |
| 258 | + Conclusion: "SUCCESS", |
| 259 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 260 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 261 | + DetailsURL: "https://github.com/cli/cli/runs/1", |
| 262 | + }, |
| 263 | + { |
| 264 | + TypeName: "CheckRun", |
| 265 | + Name: "lint", |
| 266 | + Status: "COMPLETED", |
| 267 | + Conclusion: "FAILURE", |
| 268 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 269 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 270 | + DetailsURL: "https://github.com/cli/cli/runs/2", |
| 271 | + }, |
| 272 | + { |
| 273 | + TypeName: "CheckRun", |
| 274 | + Name: "lint", |
| 275 | + Status: "COMPLETED", |
| 276 | + Conclusion: "SUCCESS", |
| 277 | + StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 278 | + CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 279 | + DetailsURL: "https://github.com/cli/cli/runs/3", |
| 280 | + }, |
| 281 | + }, |
| 282 | + want: []api.CheckContext{ |
| 283 | + { |
| 284 | + TypeName: "CheckRun", |
| 285 | + Name: "lint", |
| 286 | + Status: "COMPLETED", |
| 287 | + Conclusion: "SUCCESS", |
| 288 | + StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 289 | + CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 290 | + DetailsURL: "https://github.com/cli/cli/runs/3", |
| 291 | + }, |
| 292 | + { |
| 293 | + TypeName: "CheckRun", |
| 294 | + Name: "build (ubuntu-latest)", |
| 295 | + Status: "COMPLETED", |
| 296 | + Conclusion: "SUCCESS", |
| 297 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 298 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 299 | + DetailsURL: "https://github.com/cli/cli/runs/1", |
| 300 | + }, |
| 301 | + }, |
| 302 | + }, |
| 303 | + { |
| 304 | + name: "duplicate StatusContext (Windows GPU)", |
| 305 | + checkContexts: []api.CheckContext{ |
| 306 | + { |
| 307 | + TypeName: "StatusContext", |
| 308 | + Name: "", |
| 309 | + Context: "Windows GPU", |
| 310 | + State: "FAILURE", |
| 311 | + Status: "", |
| 312 | + Conclusion: "", |
| 313 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 314 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 315 | + DetailsURL: "", |
| 316 | + TargetURL: "https://github.com/cli/cli/2", |
| 317 | + }, |
| 318 | + { |
| 319 | + TypeName: "StatusContext", |
| 320 | + Name: "", |
| 321 | + Context: "Windows GPU", |
| 322 | + State: "SUCCESS", |
| 323 | + Status: "", |
| 324 | + Conclusion: "", |
| 325 | + StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 326 | + CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 327 | + DetailsURL: "", |
| 328 | + TargetURL: "https://github.com/cli/cli/3", |
| 329 | + }, |
| 330 | + { |
| 331 | + TypeName: "StatusContext", |
| 332 | + Name: "", |
| 333 | + Context: "Linux GPU", |
| 334 | + State: "SUCCESS", |
| 335 | + Status: "", |
| 336 | + Conclusion: "", |
| 337 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 338 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 339 | + DetailsURL: "", |
| 340 | + TargetURL: "https://github.com/cli/cli/1", |
| 341 | + }, |
| 342 | + }, |
| 343 | + want: []api.CheckContext{ |
| 344 | + { |
| 345 | + TypeName: "StatusContext", |
| 346 | + Name: "", |
| 347 | + Context: "Windows GPU", |
| 348 | + State: "SUCCESS", |
| 349 | + Status: "", |
| 350 | + Conclusion: "", |
| 351 | + StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 352 | + CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC), |
| 353 | + DetailsURL: "", |
| 354 | + TargetURL: "https://github.com/cli/cli/3", |
| 355 | + }, |
| 356 | + { |
| 357 | + TypeName: "StatusContext", |
| 358 | + Name: "", |
| 359 | + Context: "Linux GPU", |
| 360 | + State: "SUCCESS", |
| 361 | + Status: "", |
| 362 | + Conclusion: "", |
| 363 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 364 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 365 | + DetailsURL: "", |
| 366 | + TargetURL: "https://github.com/cli/cli/1", |
| 367 | + }, |
| 368 | + }, |
| 369 | + }, |
| 370 | + { |
| 371 | + name: "unique CheckContext", |
| 372 | + checkContexts: []api.CheckContext{ |
| 373 | + { |
| 374 | + TypeName: "CheckRun", |
| 375 | + Name: "build (ubuntu-latest)", |
| 376 | + Status: "COMPLETED", |
| 377 | + Conclusion: "SUCCESS", |
| 378 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 379 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 380 | + DetailsURL: "https://github.com/cli/cli/runs/1", |
| 381 | + }, |
| 382 | + { |
| 383 | + TypeName: "StatusContext", |
| 384 | + Name: "", |
| 385 | + Context: "Windows GPU", |
| 386 | + State: "SUCCESS", |
| 387 | + Status: "", |
| 388 | + Conclusion: "", |
| 389 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 390 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 391 | + DetailsURL: "", |
| 392 | + TargetURL: "https://github.com/cli/cli/2", |
| 393 | + }, |
| 394 | + { |
| 395 | + TypeName: "StatusContext", |
| 396 | + Name: "", |
| 397 | + Context: "Linux GPU", |
| 398 | + State: "SUCCESS", |
| 399 | + Status: "", |
| 400 | + Conclusion: "", |
| 401 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 402 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 403 | + DetailsURL: "", |
| 404 | + TargetURL: "https://github.com/cli/cli/3", |
| 405 | + }, |
| 406 | + }, |
| 407 | + want: []api.CheckContext{ |
| 408 | + { |
| 409 | + TypeName: "CheckRun", |
| 410 | + Name: "build (ubuntu-latest)", |
| 411 | + Status: "COMPLETED", |
| 412 | + Conclusion: "SUCCESS", |
| 413 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 414 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 415 | + DetailsURL: "https://github.com/cli/cli/runs/1", |
| 416 | + }, |
| 417 | + { |
| 418 | + TypeName: "StatusContext", |
| 419 | + Name: "", |
| 420 | + Context: "Windows GPU", |
| 421 | + State: "SUCCESS", |
| 422 | + Status: "", |
| 423 | + Conclusion: "", |
| 424 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 425 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 426 | + DetailsURL: "", |
| 427 | + TargetURL: "https://github.com/cli/cli/2", |
| 428 | + }, |
| 429 | + { |
| 430 | + TypeName: "StatusContext", |
| 431 | + Name: "", |
| 432 | + Context: "Linux GPU", |
| 433 | + State: "SUCCESS", |
| 434 | + Status: "", |
| 435 | + Conclusion: "", |
| 436 | + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 437 | + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), |
| 438 | + DetailsURL: "", |
| 439 | + TargetURL: "https://github.com/cli/cli/3", |
| 440 | + }, |
| 441 | + }, |
| 442 | + }, |
| 443 | + } |
| 444 | + |
| 445 | + for _, tt := range tests { |
| 446 | + t.Run(tt.name, func(t *testing.T) { |
| 447 | + got := eliminateDuplicates(tt.checkContexts) |
| 448 | + if !reflect.DeepEqual(tt.want, got) { |
| 449 | + t.Errorf("got eliminateDuplicates %+v, want %+v\n", got, tt.want) |
| 450 | + } |
| 451 | + }) |
| 452 | + } |
| 453 | +} |
0 commit comments