forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_test.go
64 lines (52 loc) · 2.23 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
package main
import (
. "github.com/onsi/gomega"
"net/url"
"os"
"testing"
)
func TestNewURL(t *testing.T) {
RegisterTestingT(t)
// Does nothing whent the URL has scheme part
httpsUrl, err := NewURL("https://github.com/motemen/pusheen-explorer")
Expect(httpsUrl.String()).To(Equal("https://github.com/motemen/pusheen-explorer"))
Expect(httpsUrl.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
// Convert SCP-like URL to SSH URL
scpUrl, err := NewURL("[email protected]:motemen/pusheen-explorer.git")
Expect(scpUrl.String()).To(Equal("ssh://[email protected]/motemen/pusheen-explorer.git"))
Expect(scpUrl.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
scpUrlWithRoot, err := NewURL("[email protected]:/motemen/pusheen-explorer.git")
Expect(scpUrlWithRoot.String()).To(Equal("ssh://[email protected]/motemen/pusheen-explorer.git"))
Expect(scpUrlWithRoot.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
scpUrlWithoutUser, err := NewURL("github.com:motemen/pusheen-explorer.git")
Expect(scpUrlWithoutUser.String()).To(Equal("ssh://github.com/motemen/pusheen-explorer.git"))
Expect(scpUrlWithoutUser.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
differentNameRepository, err := NewURL("motemen/ghq")
Expect(differentNameRepository.String()).To(Equal("https://github.com/motemen/ghq"))
Expect(differentNameRepository.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
os.Setenv("GITHUB_USER", "ghq-test")
sameNameRepository, err := NewURL("same-name-ghq")
Expect(sameNameRepository.String()).To(Equal("https://github.com/ghq-test/same-name-ghq"))
Expect(sameNameRepository.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
}
func TestConvertGitURLHTTPToSSH(t *testing.T) {
RegisterTestingT(t)
var (
httpsURL, sshURL *url.URL
err error
)
httpsURL, err = NewURL("https://github.com/motemen/pusheen-explorer")
sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
Expect(err).To(BeNil())
Expect(sshURL.String()).To(Equal("ssh://[email protected]/motemen/pusheen-explorer"))
httpsURL, err = NewURL("https://ghe.example.com/motemen/pusheen-explorer")
sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
Expect(err).To(BeNil())
Expect(sshURL.String()).To(Equal("ssh://[email protected]/motemen/pusheen-explorer"))
}