forked from go-git/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_test.go
135 lines (112 loc) · 3.49 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
package config
import (
. "gopkg.in/check.v1"
)
type URLSuite struct{}
var _ = Suite(&URLSuite{})
func (b *URLSuite) TestValidateInsteadOf(c *C) {
goodURL := URL{
Name: "ssh://github.com",
InsteadOf: []string{"http://github.com"},
}
badURL := URL{}
c.Assert(goodURL.Validate(), IsNil)
c.Assert(badURL.Validate(), NotNil)
}
func (b *URLSuite) TestMarshal(c *C) {
expected := []byte(`[core]
bare = false
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
`)
cfg := NewConfig()
cfg.URLs["ssh://git@github.com/"] = &URL{
Name: "ssh://git@github.com/",
InsteadOf: []string{"https://github.com/"},
}
actual, err := cfg.Marshal()
c.Assert(err, IsNil)
c.Assert(string(actual), Equals, string(expected))
}
func (b *URLSuite) TestMarshalMultipleInsteadOf(c *C) {
expected := []byte(`[core]
bare = false
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
insteadOf = https://google.com/
`)
cfg := NewConfig()
cfg.URLs["ssh://git@github.com/"] = &URL{
Name: "ssh://git@github.com/",
InsteadOf: []string{"https://github.com/", "https://google.com/"},
}
actual, err := cfg.Marshal()
c.Assert(err, IsNil)
c.Assert(string(actual), Equals, string(expected))
}
func (b *URLSuite) TestUnmarshal(c *C) {
input := []byte(`[core]
bare = false
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
`)
cfg := NewConfig()
err := cfg.Unmarshal(input)
c.Assert(err, IsNil)
url := cfg.URLs["ssh://git@github.com/"]
c.Assert(url.Name, Equals, "ssh://git@github.com/")
c.Assert(url.InsteadOf[0], Equals, "https://github.com/")
}
func (b *URLSuite) TestUnmarshalMultipleInsteadOf(c *C) {
input := []byte(`[core]
bare = false
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
insteadOf = https://google.com/
`)
cfg := NewConfig()
err := cfg.Unmarshal(input)
c.Assert(err, IsNil)
url := cfg.URLs["ssh://git@github.com/"]
c.Assert(url.Name, Equals, "ssh://git@github.com/")
c.Assert(url.ApplyInsteadOf("https://github.com/foobar"), Equals, "ssh://git@github.com/foobar")
c.Assert(url.ApplyInsteadOf("https://google.com/foobar"), Equals, "ssh://git@github.com/foobar")
}
func (b *URLSuite) TestUnmarshalDuplicateUrls(c *C) {
input := []byte(`[core]
bare = false
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
[url "ssh://git@github.com/"]
insteadOf = https://google.com/
`)
cfg := NewConfig()
err := cfg.Unmarshal(input)
c.Assert(err, IsNil)
url := cfg.URLs["ssh://git@github.com/"]
c.Assert(url.Name, Equals, "ssh://git@github.com/")
c.Assert(url.ApplyInsteadOf("https://github.com/foobar"), Equals, "ssh://git@github.com/foobar")
c.Assert(url.ApplyInsteadOf("https://google.com/foobar"), Equals, "ssh://git@github.com/foobar")
}
func (b *URLSuite) TestApplyInsteadOf(c *C) {
urlRule := URL{
Name: "ssh://github.com",
InsteadOf: []string{"http://github.com"},
}
c.Assert(urlRule.ApplyInsteadOf("http://google.com"), Equals, "http://google.com")
c.Assert(urlRule.ApplyInsteadOf("http://github.com/myrepo"), Equals, "ssh://github.com/myrepo")
}
func (b *URLSuite) TestFindLongestInsteadOfMatch(c *C) {
urlRules := map[string]*URL{
"ssh://github.com": &URL{
Name: "ssh://github.com",
InsteadOf: []string{"http://github.com"},
},
"ssh://somethingelse.com": &URL{
Name: "ssh://somethingelse.com",
InsteadOf: []string{"http://github.com/foobar"},
},
}
longestUrl := findLongestInsteadOfMatch("http://github.com/foobar/bingbash.git", urlRules)
c.Assert(longestUrl.Name, Equals, "ssh://somethingelse.com")
}