-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_test.go
More file actions
193 lines (171 loc) · 4.32 KB
/
fetch_test.go
File metadata and controls
193 lines (171 loc) · 4.32 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package templar
import (
"testing"
)
// createTestTarGzWithPAX creates an in-memory gzipped tarball with PAX headers
func TestGlobToRegex(t *testing.T) {
tests := []struct {
glob string
input string
expected bool
}{
// Simple wildcards
{"*.html", "file.html", true},
{"*.html", "file.txt", false},
{"*.html", "path/file.html", false}, // * doesn't match /
// Double star (matches any path)
{"**/*.html", "file.html", true},
{"**/*.html", "path/file.html", true},
{"**/*.html", "a/b/c/file.html", true},
{"**/*.html", "file.txt", false},
// Question mark
{"file?.txt", "file1.txt", true},
{"file?.txt", "file12.txt", false},
{"file?.txt", "file.txt", false},
// Complex patterns
{"*_test.go", "main_test.go", true},
{"*_test.go", "utils_test.go", true},
{"*_test.go", "main.go", false},
// Escape special regex chars
{"file.txt", "file.txt", true},
{"file.txt", "fileatxt", false}, // . should be literal, not regex any
}
for _, tt := range tests {
t.Run(tt.glob+"_"+tt.input, func(t *testing.T) {
regex := globToRegex(tt.glob)
patterns := compilePatterns([]string{tt.glob})
result := matchesPatterns(tt.input, patterns, nil)
if result != tt.expected {
t.Errorf("globToRegex(%q) matching %q: expected %v, got %v (regex: %s)",
tt.glob, tt.input, tt.expected, result, regex)
}
})
}
}
func TestMatchesPatterns(t *testing.T) {
tests := []struct {
name string
path string
include []string
exclude []string
expected bool
}{
{
name: "no patterns matches all",
path: "anything.txt",
include: nil,
exclude: nil,
expected: true,
},
{
name: "include pattern matches",
path: "file.html",
include: []string{"*.html"},
exclude: nil,
expected: true,
},
{
name: "include pattern doesn't match",
path: "file.txt",
include: []string{"*.html"},
exclude: nil,
expected: false,
},
{
name: "exclude pattern matches",
path: "test_file.go",
include: nil,
exclude: []string{"test_*"},
expected: false,
},
{
name: "include matches but exclude also matches",
path: "page_test.html",
include: []string{"*.html"},
exclude: []string{"*_test.*"},
expected: false,
},
{
name: "multiple include patterns",
path: "style.css",
include: []string{"*.html", "*.css"},
exclude: nil,
expected: true,
},
{
name: "multiple exclude patterns",
path: "vendor/lib.js",
include: nil,
exclude: []string{"vendor/*", "node_modules/*"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
includePatterns := compilePatterns(tt.include)
excludePatterns := compilePatterns(tt.exclude)
result := matchesPatterns(tt.path, includePatterns, excludePatterns)
if result != tt.expected {
t.Errorf("matchesPatterns(%q, %v, %v): expected %v, got %v",
tt.path, tt.include, tt.exclude, tt.expected, result)
}
})
}
}
func TestIsGitHubURL(t *testing.T) {
tests := []struct {
url string
expected bool
}{
{"github.com/user/repo", true},
{"github.com/org/project", true},
{"gitlab.com/user/repo", false},
{"bitbucket.org/user/repo", false},
{"https://github.com/user/repo", false}, // We expect without https://
{"git@github.com:user/repo.git", false},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
result := isGitHubURL(tt.url)
if result != tt.expected {
t.Errorf("isGitHubURL(%q): expected %v, got %v", tt.url, tt.expected, result)
}
})
}
}
func TestSourceConfig_GetRef(t *testing.T) {
tests := []struct {
name string
source SourceConfig
expected string
}{
{
name: "version takes precedence",
source: SourceConfig{Version: "v1.2.0", Ref: "main"},
expected: "v1.2.0",
},
{
name: "ref when no version",
source: SourceConfig{Ref: "develop"},
expected: "develop",
},
{
name: "default to main",
source: SourceConfig{},
expected: "main",
},
{
name: "commit hash as ref",
source: SourceConfig{Ref: "abc123def"},
expected: "abc123def",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.source.GetRef()
if result != tt.expected {
t.Errorf("GetRef(): expected %q, got %q", tt.expected, result)
}
})
}
}