|
7 | 7 | "fmt" |
8 | 8 | "net/http" |
9 | 9 | "net/http/httptest" |
| 10 | + "strings" |
10 | 11 | "testing" |
11 | 12 |
|
12 | 13 | "github.com/google/go-cmp/cmp" |
@@ -455,3 +456,72 @@ func mockDirectoriesInReposResults(t *testing.T, filesPerRepo filesInRepos) (cli |
455 | 456 |
|
456 | 457 | return mockGraphQLClient(string(rawRes)) |
457 | 458 | } |
| 459 | + |
| 460 | +func TestService_ValidateChangesetSpecs(t *testing.T) { |
| 461 | + repo1 := &graphql.Repository{ID: "repo-graphql-id-1", Name: "github.com/sourcegraph/src-cli"} |
| 462 | + repo2 := &graphql.Repository{ID: "repo-graphql-id-2", Name: "github.com/sourcegraph/sourcegraph"} |
| 463 | + |
| 464 | + tests := map[string]struct { |
| 465 | + repos []*graphql.Repository |
| 466 | + specs []*ChangesetSpec |
| 467 | + |
| 468 | + wantErrInclude string |
| 469 | + }{ |
| 470 | + "no errors": { |
| 471 | + repos: []*graphql.Repository{repo1, repo2}, |
| 472 | + specs: []*ChangesetSpec{ |
| 473 | + {CreatedChangeset: &CreatedChangeset{ |
| 474 | + HeadRepository: repo1.ID, HeadRef: "refs/heads/branch-1"}, |
| 475 | + }, |
| 476 | + {CreatedChangeset: &CreatedChangeset{ |
| 477 | + HeadRepository: repo1.ID, HeadRef: "refs/heads/branch-2"}, |
| 478 | + }, |
| 479 | + {CreatedChangeset: &CreatedChangeset{ |
| 480 | + HeadRepository: repo2.ID, HeadRef: "refs/heads/branch-1"}, |
| 481 | + }, |
| 482 | + {CreatedChangeset: &CreatedChangeset{ |
| 483 | + HeadRepository: repo2.ID, HeadRef: "refs/heads/branch-2"}, |
| 484 | + }, |
| 485 | + }, |
| 486 | + }, |
| 487 | + |
| 488 | + "duplicate branches": { |
| 489 | + repos: []*graphql.Repository{repo1, repo2}, |
| 490 | + specs: []*ChangesetSpec{ |
| 491 | + {CreatedChangeset: &CreatedChangeset{ |
| 492 | + HeadRepository: repo1.ID, HeadRef: "refs/heads/branch-1"}, |
| 493 | + }, |
| 494 | + {CreatedChangeset: &CreatedChangeset{ |
| 495 | + HeadRepository: repo1.ID, HeadRef: "refs/heads/branch-2"}, |
| 496 | + }, |
| 497 | + {CreatedChangeset: &CreatedChangeset{ |
| 498 | + HeadRepository: repo2.ID, HeadRef: "refs/heads/branch-1"}, |
| 499 | + }, |
| 500 | + {CreatedChangeset: &CreatedChangeset{ |
| 501 | + HeadRepository: repo2.ID, HeadRef: "refs/heads/branch-1"}, |
| 502 | + }, |
| 503 | + }, |
| 504 | + wantErrInclude: `github.com/sourcegraph/sourcegraph: 2 changeset specs have the branch "branch-1"`, |
| 505 | + }, |
| 506 | + } |
| 507 | + |
| 508 | + for name, tt := range tests { |
| 509 | + t.Run(name, func(t *testing.T) { |
| 510 | + svc := &Service{} |
| 511 | + haveErr := svc.ValidateChangesetSpecs(tt.repos, tt.specs) |
| 512 | + if tt.wantErrInclude != "" { |
| 513 | + if haveErr == nil { |
| 514 | + t.Fatalf("expected %q to be included in error, but got none", tt.wantErrInclude) |
| 515 | + } else { |
| 516 | + if !strings.Contains(haveErr.Error(), tt.wantErrInclude) { |
| 517 | + t.Fatalf("expected %q to be included in error, but was not. error=%q", tt.wantErrInclude, haveErr.Error()) |
| 518 | + } |
| 519 | + } |
| 520 | + } else { |
| 521 | + if haveErr != nil { |
| 522 | + t.Fatalf("unexpected error: %s", haveErr) |
| 523 | + } |
| 524 | + } |
| 525 | + }) |
| 526 | + } |
| 527 | +} |
0 commit comments