|
1 | 1 | package ghrepo |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
4 | 6 | "net/url" |
5 | 7 | "testing" |
6 | 8 | ) |
7 | 9 |
|
8 | 10 | func Test_repoFromURL(t *testing.T) { |
9 | | - u, _ := url.Parse("http://github.com/monalisa/octo-cat.git") |
10 | | - repo, err := FromURL(u) |
11 | | - if err != nil { |
12 | | - t.Fatalf("got error %q", err) |
13 | | - } |
14 | | - if repo.RepoOwner() != "monalisa" { |
15 | | - t.Errorf("got owner %q", repo.RepoOwner()) |
16 | | - } |
17 | | - if repo.RepoName() != "octo-cat" { |
18 | | - t.Errorf("got name %q", repo.RepoName()) |
19 | | - } |
20 | | -} |
21 | | - |
22 | | -func Test_repoFromURL_invalid(t *testing.T) { |
23 | | - cases := [][]string{ |
24 | | - []string{ |
25 | | - "https://example.com/one/two", |
26 | | - "unsupported hostname: example.com", |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + result string |
| 15 | + err error |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "github.com URL", |
| 19 | + input: "https://github.com/monalisa/octo-cat.git", |
| 20 | + result: "monalisa/octo-cat", |
| 21 | + err: nil, |
27 | 22 | }, |
28 | | - []string{ |
29 | | - "/path/to/disk", |
30 | | - "unsupported hostname: ", |
| 23 | + { |
| 24 | + name: "www.github.com URL", |
| 25 | + input: "http://www.GITHUB.com/monalisa/octo-cat.git", |
| 26 | + result: "monalisa/octo-cat", |
| 27 | + err: nil, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "unsupported hostname", |
| 31 | + input: "https://example.com/one/two", |
| 32 | + result: "", |
| 33 | + err: errors.New("unsupported hostname: example.com"), |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "filesystem path", |
| 37 | + input: "/path/to/file", |
| 38 | + result: "", |
| 39 | + err: errors.New("unsupported hostname: "), |
31 | 40 | }, |
32 | 41 | } |
33 | | - for _, c := range cases { |
34 | | - u, _ := url.Parse(c[0]) |
35 | | - _, err := FromURL(u) |
36 | | - if err == nil || err.Error() != c[1] { |
37 | | - t.Errorf("got %q", err) |
38 | | - } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + u, err := url.Parse(tt.input) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("got error %q", err) |
| 48 | + } |
| 49 | + |
| 50 | + repo, err := FromURL(u) |
| 51 | + if err != nil { |
| 52 | + if tt.err == nil { |
| 53 | + t.Fatalf("got error %q", err) |
| 54 | + } else if tt.err.Error() == err.Error() { |
| 55 | + return |
| 56 | + } |
| 57 | + t.Fatalf("got error %q", err) |
| 58 | + } |
| 59 | + |
| 60 | + got := fmt.Sprintf("%s/%s", repo.RepoOwner(), repo.RepoName()) |
| 61 | + if tt.result != got { |
| 62 | + t.Errorf("expected %q, got %q", tt.result, got) |
| 63 | + } |
| 64 | + }) |
39 | 65 | } |
40 | 66 | } |
0 commit comments