-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathgit_repo_test.go
More file actions
145 lines (142 loc) · 3.75 KB
/
git_repo_test.go
File metadata and controls
145 lines (142 loc) · 3.75 KB
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
package github
import (
"net/url"
"testing"
)
func TestExtractRepoDetailsFromSourceURL(t *testing.T) {
tests := []struct {
name string
input string
wantOwner string
wantRepo string
wantBranch string
wantRoot string
wantErr bool
}{
{
name: "owner and repo only",
input: "git://github.com/meshery/meshkit",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "/**",
wantErr: false,
},
{
name: "owner repo and branch",
input: "git://github.com/meshery/meshkit/master",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "/**",
wantErr: false,
},
{
name: "owner repo branch and path",
input: "git://github.com/meshery/meshkit/master/install/kubernetes",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "install/kubernetes",
wantErr: false,
},
{
name: "invalid single component",
input: "git://github.com/meshery",
wantErr: true,
},
{
name: "trailing slash on repo",
input: "git://github.com/meshery/meshkit/",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "/**",
wantErr: false,
},
// AFter gemini review these should gail
{
name: "trailing slash triggers default branch",
input: "git://github.com/meshery/meshkit/", // Trailing slash
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master", // The old code would return "" here
wantRoot: "/**",
wantErr: false,
},
{
name: "nested path depth greater than 4",
input: "git://github.com/meshery/meshkit/master/install/kubernetes/helm/meshery",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "install/kubernetes/helm/meshery", // The old code would cut this off
wantErr: false,
},
{
name: "multiple slashes in path",
input: "git://github.com/meshery/meshkit///master/",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "/**",
wantErr: false,
},
{
name: "GitHub Browser URL - Tree",
input: "https://github.com/meshery/meshkit/tree/master/install",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "install",
wantErr: false,
},
{
name: "GitHub Browser URL - Blob",
input: "https://github.com/meshery/meshkit/blob/master/models/meshmodel.yaml",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "master",
wantRoot: "models/meshmodel.yaml",
wantErr: false,
},
{
name: "GitHub Release URL",
input: "https://github.com/meshery/meshkit/releases/tag/v0.6.0",
wantOwner: "meshery",
wantRepo: "meshkit",
wantBranch: "v0.6.0",
wantRoot: "/**",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := url.Parse(tt.input)
if err != nil {
t.Fatalf("failed to parse url %s: %v", tt.input, err)
}
gr := GitRepo{URL: u}
owner, repo, branch, root, err := gr.ExtractRepoDetailsFromSourceURL()
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error state: got err=%v, wantErr=%v", err, tt.wantErr)
}
if err != nil {
// on error we are done
return
}
if owner != tt.wantOwner {
t.Fatalf("owner: got %q want %q", owner, tt.wantOwner)
}
if repo != tt.wantRepo {
t.Fatalf("repo: got %q want %q", repo, tt.wantRepo)
}
if branch != tt.wantBranch {
t.Fatalf("branch: got %q want %q", branch, tt.wantBranch)
}
if root != tt.wantRoot {
t.Fatalf("root: got %q want %q", root, tt.wantRoot)
}
})
}
}