Debian Codegraph @

internal/url/url_test.go

raw
1 package url
2
3 import (
4 "runtime"
5 "testing"
6
7 . "gopkg.in/check.v1"
8 )
9
10 func Test(t *testing.T) { TestingT(t) }
11
12 type URLSuite struct{}
13
14 var _ = Suite(&URLSuite{})
15
16 func (s *URLSuite) TestMatchesScpLike(c *C) {
17 // See https://github.com/git/git/blob/master/Documentation/urls.txt#L37
18 examples := []string{
19 // Most-extended case
20 "[email protected]:james/bond",
21 // Most-extended case with port
22 "[email protected]:22:james/bond",
23 // Most-extended case with numeric path
24 "[email protected]:007/bond",
25 // Most-extended case with port and numeric "username"
26 "[email protected]:22:007/bond",
27 // Single repo path
28 "[email protected]:bond",
29 // Single repo path with port
30 "[email protected]:22:bond",
31 // Single repo path with port and numeric repo
32 "[email protected]:22:007",
33 // Repo path ending with .git and starting with _
34 "[email protected]:22:_007.git",
35 "[email protected]:_007.git",
36 "[email protected]:_james.git",
37 "[email protected]:_james/bond.git",
38 }
39
40 for _, url := range examples {
41 c.Check(MatchesScpLike(url), Equals, true)
42 }
43 }
44
45 func (s *URLSuite) TestFindScpLikeComponents(c *C) {
46 testCases := []struct {
47 url, user, host, port, path string
48 }{
49 {
50 // Most-extended case
51 url: "[email protected]:james/bond", user: "git", host: "github.com", port: "", path: "james/bond",
52 },
53 {
54 // Most-extended case with port
55 url: "[email protected]:22:james/bond", user: "git", host: "github.com", port: "22", path: "james/bond",
56 },
57 {
58 // Most-extended case with numeric path
59 url: "[email protected]:007/bond", user: "git", host: "github.com", port: "", path: "007/bond",
60 },
61 {
62 // Most-extended case with port and numeric path
63 url: "[email protected]:22:007/bond", user: "git", host: "github.com", port: "22", path: "007/bond",
64 },
65 {
66 // Single repo path
67 url: "[email protected]:bond", user: "git", host: "github.com", port: "", path: "bond",
68 },
69 {
70 // Single repo path with port
71 url: "[email protected]:22:bond", user: "git", host: "github.com", port: "22", path: "bond",
72 },
73 {
74 // Single repo path with port and numeric path
75 url: "[email protected]:22:007", user: "git", host: "github.com", port: "22", path: "007",
76 },
77 {
78 // Repo path ending with .git and starting with _
79 url: "[email protected]:22:_007.git", user: "git", host: "github.com", port: "22", path: "_007.git",
80 },
81 {
82 // Repo path ending with .git and starting with _
83 url: "[email protected]:_007.git", user: "git", host: "github.com", port: "", path: "_007.git",
84 },
85 {
86 // Repo path ending with .git and starting with _
87 url: "[email protected]:_james.git", user: "git", host: "github.com", port: "", path: "_james.git",
88 },
89 {
90 // Repo path ending with .git and starting with _
91 url: "[email protected]:_james/bond.git", user: "git", host: "github.com", port: "", path: "_james/bond.git",
92 },
93 }
94
95 for _, tc := range testCases {
96 user, host, port, path := FindScpLikeComponents(tc.url)
97
98 logf := func(ok bool) {
99 if ok {
100 return
101 }
102 c.Logf("%q check failed", tc.url)
103 }
104
105 logf(c.Check(user, Equals, tc.user))
106 logf(c.Check(host, Equals, tc.host))
107 logf(c.Check(port, Equals, tc.port))
108 logf(c.Check(path, Equals, tc.path))
109 }
110 }
111
112 func (s *URLSuite) TestMatchesScpLikeRejectsLocalPaths(c *C) {
113 // Cases that look superficially SCP-like but are actually local
114 // paths per canonical Git's url_is_local_not_ssh — a `/` before
115 // the first `:` means a local path. See
116 // https://github.com/git/git/blob/v2.54.0/connect.c#L710-L716.
117 for _, url := range []string{
118 "/abs/path/with:colon/file",
119 "./relative:path",
120 "./relative/with:colon",
121 "sub/dir:foo",
122 } {
123 c.Check(MatchesScpLike(url), Equals, false, Commentf("%q", url))
124 }
125 }
126
127 func (s *URLSuite) TestMatchesScpLikeWindowsDrivePrefix(c *C) {
128 // On Windows, drive-letter paths (`C:foo`, `C:/foo`, `C:\foo`)
129 // match the SCP regex's host=`C` pattern but are local. Canonical
130 // Git rejects them via has_dos_drive_prefix; we mirror that.
131 if runtime.GOOS != "windows" {
132 c.Skip("Windows-only: drive-prefix disambiguation is platform-specific")
133 }
134 for _, url := range []string{
135 "C:foo",
136 "C:/path/to/repo",
137 `C:\path\to\repo`,
138 "d:relative",
139 } {
140 c.Check(MatchesScpLike(url), Equals, false, Commentf("%q", url))
141 }
142 }
143
144 func (s *URLSuite) TestMatchesScpLikeStillAcceptsRealSCP(c *C) {
145 // Regression-guard: the new disambiguation logic must not reject
146 // canonical SCP forms.
147 for _, url := range []string{
148 "[email protected]:james/bond",
149 "[email protected]:path/to/repo.git",
150 "host:path",
151 } {
152 c.Check(MatchesScpLike(url), Equals, true, Commentf("%q", url))
153 }
154 }