|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + forges "github.com/git-pkgs/forge" |
| 7 | +) |
| 8 | + |
| 9 | +func TestParsePath(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + parts []string |
| 13 | + wantOwner string |
| 14 | + wantRepo string |
| 15 | + wantResource forges.ResourceType |
| 16 | + wantNumber int |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "repo only", |
| 21 | + parts: []string{"owner", "repo"}, |
| 22 | + wantOwner: "owner", wantRepo: "repo", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "pull request", |
| 26 | + parts: []string{"owner", "repo", "pull", "123"}, |
| 27 | + wantOwner: "owner", wantRepo: "repo", |
| 28 | + wantResource: forges.ResourceTypePR, wantNumber: 123, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "pull request with extra path", |
| 32 | + parts: []string{"owner", "repo", "pull", "123", "files"}, |
| 33 | + wantOwner: "owner", wantRepo: "repo", |
| 34 | + wantResource: forges.ResourceTypePR, wantNumber: 123, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "issue", |
| 38 | + parts: []string{"owner", "repo", "issues", "456"}, |
| 39 | + wantOwner: "owner", wantRepo: "repo", |
| 40 | + wantResource: forges.ResourceTypeIssue, wantNumber: 456, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "missing repo", |
| 44 | + parts: []string{"owner"}, |
| 45 | + wantErr: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "invalid PR number", |
| 49 | + parts: []string{"owner", "repo", "pull", "abc"}, |
| 50 | + wantErr: true, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + f := &gitHubForge{} |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + ref, err := f.ParsePath(tt.parts) |
| 58 | + if tt.wantErr { |
| 59 | + if err == nil { |
| 60 | + t.Fatal("expected error") |
| 61 | + } |
| 62 | + return |
| 63 | + } |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("unexpected error: %v", err) |
| 66 | + } |
| 67 | + if ref.Owner != tt.wantOwner { |
| 68 | + t.Errorf("owner: got %q, want %q", ref.Owner, tt.wantOwner) |
| 69 | + } |
| 70 | + if ref.Repo != tt.wantRepo { |
| 71 | + t.Errorf("repo: got %q, want %q", ref.Repo, tt.wantRepo) |
| 72 | + } |
| 73 | + if ref.Type != tt.wantResource { |
| 74 | + t.Errorf("resource: got %q, want %q", ref.Type, tt.wantResource) |
| 75 | + } |
| 76 | + if ref.Number != tt.wantNumber { |
| 77 | + t.Errorf("number: got %d, want %d", ref.Number, tt.wantNumber) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments