-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_test.go
149 lines (139 loc) · 4.18 KB
/
url_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"errors"
"fmt"
"os/exec"
"strings"
"testing"
"github.com/Songmu/gitconfig"
)
func TestNewURL(t *testing.T) {
testCases := []struct {
name, url, expect, host string
setup func(t *testing.T)
}{{
name: "https", // Does nothing when the URL has scheme part
url: "https://github.com/motemen/pusheen-explorer",
expect: "https://github.com/motemen/pusheen-explorer",
host: "github.com",
}, {
name: "scp", // Convert SCP-like URL to SSH URL
url: "git@github.com:motemen/pusheen-explorer.git",
expect: "ssh://git@github.com/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "scp with root",
url: "git@github.com:/motemen/pusheen-explorer.git",
expect: "ssh://git@github.com/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "scp without user",
url: "github.com:motemen/pusheen-explorer.git",
expect: "ssh://github.com/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "different name repository",
url: "motemen/ghq",
expect: "https://github.com/motemen/ghq",
host: "github.com",
}, {
name: "with authority repository",
url: "github.com/motemen/gore",
expect: "https://github.com/motemen/gore",
host: "github.com",
}, {
name: "with authority repository and go-import",
url: "golang.org/x/crypto",
expect: "https://golang.org/x/crypto",
host: "golang.org",
}, {
name: "fill username",
setup: func(t *testing.T) {
setEnv(t, "GITHUB_USER", "ghq-test")
},
url: "same-name-ghq",
expect: "https://github.com/ghq-test/same-name-ghq",
host: "github.com",
}, {
name: "same name repository",
setup: func(t *testing.T) {
t.Cleanup(gitconfig.WithConfig(t, `[ghq]
completeUser = false`))
},
url: "peco",
expect: "https://github.com/peco/peco",
host: "github.com",
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup(t)
}
repo, err := newURL(tc.url, false, false)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if repo.String() != tc.expect {
t.Errorf("url: got: %s, expect: %s", repo.String(), tc.expect)
}
if repo.Host != tc.host {
t.Errorf("host: got: %s, expect: %s", repo.Host, tc.host)
}
})
}
}
func TestConvertGitURLHTTPToSSH(t *testing.T) {
testCases := []struct {
url, expect string
}{{
url: "https://github.com/motemen/pusheen-explorer",
expect: "ssh://git@github.com/motemen/pusheen-explorer",
}, {
url: "https://ghe.example.com/motemen/pusheen-explorer",
expect: "ssh://git@ghe.example.com/motemen/pusheen-explorer",
}, {
url: "https://motemen@ghe.example.com/motemen/pusheen-explorer",
expect: "ssh://motemen@ghe.example.com/motemen/pusheen-explorer",
}}
for _, tc := range testCases {
t.Run(tc.url, func(t *testing.T) {
httpsURL, err := newURL(tc.url, false, false)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
sshURL, err := convertGitURLHTTPToSSH(httpsURL)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if sshURL.String() != tc.expect {
t.Errorf("got: %s, expect: %s", sshURL.String(), tc.expect)
}
})
}
}
func TestNewURL_err(t *testing.T) {
invalidURL := "http://foo.com/?foo\nbar"
_, err := newURL(invalidURL, false, false)
const wantSub = "net/url: invalid control character in URL"
if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
t.Errorf("newURL(%q, false, false) error = %q; want substring %q", invalidURL, got, wantSub)
}
t.Cleanup(gitconfig.WithConfig(t, `[[[`))
var exitError *exec.ExitError
_, err = newURL("peco", false, false)
if !errors.As(err, &exitError) {
t.Errorf("error should be occurred but nil")
}
}
func TestFillUsernameToPath_err(t *testing.T) {
for _, envStr := range []string{"GITHUB_USER", "GITHUB_TOKEN", "USER", "USERNAME"} {
setEnv(t, envStr, "")
}
setEnv(t, "XDG_CONFIG_HOME", "/dummy/dummy")
usr, err := fillUsernameToPath("peco", false)
t.Log(usr)
const wantSub = "set ghq.user to your gitconfig"
if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
t.Errorf("fillUsernameToPath(peco, false) error = %q; want substring %q", got, wantSub)
}
}