github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/git/url_test.go (about) 1 package git 2 3 import "testing" 4 5 func TestIsURL(t *testing.T) { 6 tests := []struct { 7 name string 8 url string 9 want bool 10 }{ 11 { 12 name: "scp-like", 13 url: "git@example.com:owner/repo", 14 want: true, 15 }, 16 { 17 name: "scp-like with no user", 18 url: "example.com:owner/repo", 19 want: false, 20 }, 21 { 22 name: "ssh", 23 url: "ssh://git@example.com/owner/repo", 24 want: true, 25 }, 26 { 27 name: "git", 28 url: "git://example.com/owner/repo", 29 want: true, 30 }, 31 { 32 name: "git with extension", 33 url: "git://example.com/owner/repo.git", 34 want: true, 35 }, 36 { 37 name: "git+ssh", 38 url: "git+ssh://git@example.com/owner/repo.git", 39 want: true, 40 }, 41 { 42 name: "https", 43 url: "https://example.com/owner/repo.git", 44 want: true, 45 }, 46 { 47 name: "git+https", 48 url: "git+https://example.com/owner/repo.git", 49 want: true, 50 }, 51 { 52 name: "no protocol", 53 url: "example.com/owner/repo", 54 want: false, 55 }, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 if got := IsURL(tt.url); got != tt.want { 60 t.Errorf("IsURL() = %v, want %v", got, tt.want) 61 } 62 }) 63 } 64 } 65 66 func TestParseURL(t *testing.T) { 67 type url struct { 68 Scheme string 69 User string 70 Host string 71 Path string 72 } 73 tests := []struct { 74 name string 75 url string 76 want url 77 wantErr bool 78 }{ 79 { 80 name: "HTTPS", 81 url: "https://example.com/owner/repo.git", 82 want: url{ 83 Scheme: "https", 84 User: "", 85 Host: "example.com", 86 Path: "/owner/repo.git", 87 }, 88 }, 89 { 90 name: "HTTP", 91 url: "http://example.com/owner/repo.git", 92 want: url{ 93 Scheme: "http", 94 User: "", 95 Host: "example.com", 96 Path: "/owner/repo.git", 97 }, 98 }, 99 { 100 name: "git", 101 url: "git://example.com/owner/repo.git", 102 want: url{ 103 Scheme: "git", 104 User: "", 105 Host: "example.com", 106 Path: "/owner/repo.git", 107 }, 108 }, 109 { 110 name: "ssh", 111 url: "ssh://git@example.com/owner/repo.git", 112 want: url{ 113 Scheme: "ssh", 114 User: "git", 115 Host: "example.com", 116 Path: "/owner/repo.git", 117 }, 118 }, 119 { 120 name: "ssh with port", 121 url: "ssh://git@example.com:443/owner/repo.git", 122 want: url{ 123 Scheme: "ssh", 124 User: "git", 125 Host: "example.com", 126 Path: "/owner/repo.git", 127 }, 128 }, 129 { 130 name: "git+ssh", 131 url: "git+ssh://example.com/owner/repo.git", 132 want: url{ 133 Scheme: "ssh", 134 User: "", 135 Host: "example.com", 136 Path: "/owner/repo.git", 137 }, 138 }, 139 { 140 name: "git+https", 141 url: "git+https://example.com/owner/repo.git", 142 want: url{ 143 Scheme: "https", 144 User: "", 145 Host: "example.com", 146 Path: "/owner/repo.git", 147 }, 148 }, 149 { 150 name: "scp-like", 151 url: "git@example.com:owner/repo.git", 152 want: url{ 153 Scheme: "ssh", 154 User: "git", 155 Host: "example.com", 156 Path: "/owner/repo.git", 157 }, 158 }, 159 { 160 name: "scp-like, leading slash", 161 url: "git@example.com:/owner/repo.git", 162 want: url{ 163 Scheme: "ssh", 164 User: "git", 165 Host: "example.com", 166 Path: "/owner/repo.git", 167 }, 168 }, 169 { 170 name: "file protocol", 171 url: "file:///example.com/owner/repo.git", 172 want: url{ 173 Scheme: "file", 174 User: "", 175 Host: "", 176 Path: "/example.com/owner/repo.git", 177 }, 178 }, 179 { 180 name: "file path", 181 url: "/example.com/owner/repo.git", 182 want: url{ 183 Scheme: "", 184 User: "", 185 Host: "", 186 Path: "/example.com/owner/repo.git", 187 }, 188 }, 189 { 190 name: "Windows file path", 191 url: "C:\\example.com\\owner\\repo.git", 192 want: url{ 193 Scheme: "c", 194 User: "", 195 Host: "", 196 Path: "", 197 }, 198 }, 199 } 200 for _, tt := range tests { 201 t.Run(tt.name, func(t *testing.T) { 202 u, err := ParseURL(tt.url) 203 if (err != nil) != tt.wantErr { 204 t.Fatalf("got error: %v", err) 205 } 206 if u.Scheme != tt.want.Scheme { 207 t.Errorf("expected scheme %q, got %q", tt.want.Scheme, u.Scheme) 208 } 209 if u.User.Username() != tt.want.User { 210 t.Errorf("expected user %q, got %q", tt.want.User, u.User.Username()) 211 } 212 if u.Host != tt.want.Host { 213 t.Errorf("expected host %q, got %q", tt.want.Host, u.Host) 214 } 215 if u.Path != tt.want.Path { 216 t.Errorf("expected path %q, got %q", tt.want.Path, u.Path) 217 } 218 }) 219 } 220 }