| 1 | package project |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | func TestNormalizeRepoName(t *testing.T) { |
| 8 | reg := &Registry{ |
| 9 | Projects: map[string]string{ |
| 10 | "site": "/home/user/src/site", |
| 11 | "klaus": "/home/user/src/klaus", |
| 12 | }, |
| 13 | } |
| 14 | |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | ref string |
| 18 | reg *Registry |
| 19 | want string |
| 20 | }{ |
| 21 | { |
| 22 | name: "project name stays as-is", |
| 23 | ref: "site", |
| 24 | reg: reg, |
| 25 | want: "site", |
| 26 | }, |
| 27 | { |
| 28 | name: "owner/repo matching project returns project name", |
| 29 | ref: "maintainer/site", |
| 30 | reg: reg, |
| 31 | want: "site", |
| 32 | }, |
| 33 | { |
| 34 | name: "owner/repo not matching project stays as owner/repo", |
| 35 | ref: "maintainer/other-thing", |
| 36 | reg: reg, |
| 37 | want: "maintainer/other-thing", |
| 38 | }, |
| 39 | { |
| 40 | name: "full HTTPS URL matching project returns project name", |
| 41 | ref: "https://github.com/maintainer/site", |
| 42 | reg: reg, |
| 43 | want: "site", |
| 44 | }, |
| 45 | { |
| 46 | name: "full HTTPS URL with .git matching project", |
| 47 | ref: "https://github.com/maintainer/site.git", |
| 48 | reg: reg, |
| 49 | want: "site", |
| 50 | }, |
| 51 | { |
| 52 | name: "SSH URL matching project returns project name", |
| 53 | ref: "git@github.com:maintainer/klaus.git", |
| 54 | reg: reg, |
| 55 | want: "klaus", |
| 56 | }, |
| 57 | { |
| 58 | name: "full URL not matching project returns owner/repo", |
| 59 | ref: "https://github.com/someorg/newrepo", |
| 60 | reg: reg, |
| 61 | want: "someorg/newrepo", |
| 62 | }, |
| 63 | { |
| 64 | name: "empty ref returns empty", |
| 65 | ref: "", |
| 66 | reg: reg, |
| 67 | want: "", |
| 68 | }, |
| 69 | { |
| 70 | name: "nil registry with project-like name", |
| 71 | ref: "site", |
| 72 | reg: nil, |
| 73 | want: "site", |
| 74 | }, |
| 75 | { |
| 76 | name: "nil registry with owner/repo", |
| 77 | ref: "maintainer/site", |
| 78 | reg: nil, |
| 79 | want: "maintainer/site", |
| 80 | }, |
| 81 | { |
| 82 | name: "owner/repo with trailing slash", |
| 83 | ref: "maintainer/site/", |
| 84 | reg: reg, |
| 85 | want: "site", |
| 86 | }, |
| 87 | { |
| 88 | name: "HTTP URL (not HTTPS)", |
| 89 | ref: "http://github.com/maintainer/site", |
| 90 | reg: reg, |
| 91 | want: "site", |
| 92 | }, |
| 93 | { |
| 94 | name: "unknown bare name with no registry match", |
| 95 | ref: "unknown-project", |
| 96 | reg: reg, |
| 97 | want: "unknown-project", |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | for _, tt := range tests { |
| 102 | t.Run(tt.name, func(t *testing.T) { |
| 103 | got := NormalizeRepoName(tt.ref, tt.reg) |
| 104 | if got != tt.want { |
| 105 | t.Errorf("NormalizeRepoName(%q) = %q, want %q", tt.ref, got, tt.want) |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | } |