-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
119 lines (115 loc) · 2.83 KB
/
main_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
package main
import "testing"
func Test_expand(t *testing.T) {
type args struct {
input string
defaultPrefix string
}
tests := []struct {
name string
args args
want string
}{
{
"plain name without project or protocol",
args{"hello", "[email protected]:"},
"hello",
},
{
"absolute HTTPS URI should be unmodified",
args{"https://github.com/joneskoo/git-get", "[email protected]:"},
"https://github.com/joneskoo/git-get",
},
{
"absolute short ssh address should be unmodified",
args{"[email protected]:joneskoo/git-get.git", "[email protected]:"},
"[email protected]:joneskoo/git-get.git",
},
{
"absolute ssh URI should be unmodified",
args{"ssh://[email protected]:joneskoo/git-get.git", "[email protected]:"},
"ssh://[email protected]:joneskoo/git-get.git",
},
{
"relative github path should complete",
args{"joneskoo/git-get", "[email protected]:"},
"[email protected]:joneskoo/git-get.git",
},
{
"relative https path should complete",
args{"joneskoo/git-get", "https://example.com/"},
"https://example.com/joneskoo/git-get.git",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := expand(tt.args.input, tt.args.defaultPrefix); got != tt.want {
t.Errorf("expand(%q, %q) = %q, want %q", tt.args.input, tt.args.defaultPrefix, got, tt.want)
}
})
}
}
func Test_targetDir(t *testing.T) {
type args struct {
cloneURL string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "non-URL should return error",
args: args{"foobar"},
wantErr: true,
},
{
name: "plain hostname is not valid",
args: args{"https://github.com"},
wantErr: true,
},
{
name: "ssh short form address",
args: args{"user@hostname:project/repo"},
want: "hostname/project/repo",
},
{
name: "ssh short form address with git suffix",
args: args{"user@hostname:project/repo.git"},
want: "hostname/project/repo",
},
{
name: "ok https url",
args: args{"https://github.com/joneskoo/git-get"},
want: "github.com/joneskoo/git-get",
},
{
name: "ok https url with git suffix",
args: args{"https://github.com/joneskoo/git-get.git"},
want: "github.com/joneskoo/git-get",
},
{
name: "ok ssh url",
args: args{"ssh://[email protected]/joneskoo/git-get"},
want: "github.com/joneskoo/git-get",
},
{
name: "ok ssh url with git suffix",
args: args{"ssh://[email protected]/joneskoo/git-get.git"},
want: "github.com/joneskoo/git-get",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := targetDir(tt.args.cloneURL)
if (err != nil) != tt.wantErr {
t.Errorf("targetDir() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("targetDir(%q) = %v, want %v", tt.args.cloneURL, got, tt.want)
}
})
}
}