-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractor_test.go
More file actions
273 lines (252 loc) · 7.15 KB
/
extractor_test.go
File metadata and controls
273 lines (252 loc) · 7.15 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package managers
import (
"testing"
"github.com/git-pkgs/managers/definitions"
)
func TestExtractPath_Raw(t *testing.T) {
output := " /path/to/package \n"
result, err := ExtractPath(output, nil, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
if result != "/path/to/package" {
t.Errorf("got %q, want %q", result, "/path/to/package")
}
}
func TestExtractPath_RawExplicit(t *testing.T) {
output := "/path/to/package\n"
result, err := ExtractPath(output, &definitions.Extract{Type: "raw"}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
if result != "/path/to/package" {
t.Errorf("got %q, want %q", result, "/path/to/package")
}
}
func TestExtractPath_JSON(t *testing.T) {
output := `{"Dir": "/home/user/go/pkg/mod/example.com@v1.0.0", "Path": "example.com"}`
result, err := ExtractPath(output, &definitions.Extract{
Type: "json",
Field: "Dir",
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/home/user/go/pkg/mod/example.com@v1.0.0"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_JSON_MissingField(t *testing.T) {
output := `{"Path": "example.com"}`
_, err := ExtractPath(output, &definitions.Extract{
Type: "json",
Field: "Dir",
}, "")
if err == nil {
t.Error("expected error for missing field, got nil")
}
}
func TestExtractPath_LinePrefix(t *testing.T) {
output := `Name: requests
Version: 2.28.1
Summary: Python HTTP for Humans.
Location: /usr/local/lib/python3.9/site-packages
Requires: certifi, charset-normalizer`
result, err := ExtractPath(output, &definitions.Extract{
Type: "line_prefix",
Prefix: "Location: ",
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/usr/local/lib/python3.9/site-packages"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_LinePrefix_NotFound(t *testing.T) {
output := `Name: requests
Version: 2.28.1`
_, err := ExtractPath(output, &definitions.Extract{
Type: "line_prefix",
Prefix: "Location: ",
}, "")
if err == nil {
t.Error("expected error for missing prefix, got nil")
}
}
func TestExtractPath_Regex(t *testing.T) {
output := `Package path: /var/lib/gems/3.0.0/gems/rails-7.0.0`
result, err := ExtractPath(output, &definitions.Extract{
Type: "regex",
Pattern: `Package path: (.+)`,
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/var/lib/gems/3.0.0/gems/rails-7.0.0"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_Regex_NoMatch(t *testing.T) {
output := `some unrelated output`
_, err := ExtractPath(output, &definitions.Extract{
Type: "regex",
Pattern: `Package path: (.+)`,
}, "")
if err == nil {
t.Error("expected error for no match, got nil")
}
}
func TestExtractPath_JSONArray(t *testing.T) {
output := `{
"packages": [
{"name": "serde", "manifest_path": "/home/user/.cargo/registry/src/serde-1.0.0/Cargo.toml"},
{"name": "tokio", "manifest_path": "/home/user/.cargo/registry/src/tokio-1.0.0/Cargo.toml"}
]
}`
result, err := ExtractPath(output, &definitions.Extract{
Type: "json_array",
ArrayField: "packages",
MatchField: "name",
ExtractField: "manifest_path",
}, "serde")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/home/user/.cargo/registry/src/serde-1.0.0/Cargo.toml"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_JSONArray_NotFound(t *testing.T) {
output := `{
"packages": [
{"name": "serde", "manifest_path": "/path/to/serde/Cargo.toml"}
]
}`
_, err := ExtractPath(output, &definitions.Extract{
Type: "json_array",
ArrayField: "packages",
MatchField: "name",
ExtractField: "manifest_path",
}, "tokio")
if err == nil {
t.Error("expected error for package not found, got nil")
}
}
func TestExtractPath_JSONArray_StripFilename(t *testing.T) {
output := `{
"packages": [
{"name": "serde", "manifest_path": "/home/user/.cargo/registry/src/serde-1.0.0/Cargo.toml"}
]
}`
result, err := ExtractPath(output, &definitions.Extract{
Type: "json_array",
ArrayField: "packages",
MatchField: "name",
ExtractField: "manifest_path",
StripFilename: true,
}, "serde")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/home/user/.cargo/registry/src/serde-1.0.0"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_UnknownType(t *testing.T) {
_, err := ExtractPath("output", &definitions.Extract{Type: "invalid"}, "")
if err == nil {
t.Error("expected error for unknown type, got nil")
}
}
func TestExtractPath_GemWhich(t *testing.T) {
// Simulates gem which output
output := `/var/lib/gems/3.0.0/gems/rails-7.0.0/lib/rails.rb`
result, err := ExtractPath(output, &definitions.Extract{
Type: "regex",
Pattern: `^(.+/gems/[^/]+)`,
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/var/lib/gems/3.0.0/gems/rails-7.0.0"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_LuarocksShow(t *testing.T) {
// Simulates luarocks show output
output := `luasocket 3.1.0-1 - Network support for the Lua language
Installed in: /usr/local/lib/luarocks/rocks-5.4
...`
result, err := ExtractPath(output, &definitions.Extract{
Type: "regex",
Pattern: `Installed in:\s+(\S+)`,
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/usr/local/lib/luarocks/rocks-5.4"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_DenoInfo(t *testing.T) {
// Simulates deno info output
output := `local: /Users/user/.cache/deno/deps/https/deno.land/abc123
type: TypeScript
...`
result, err := ExtractPath(output, &definitions.Extract{
Type: "line_prefix",
Prefix: "local: ",
}, "")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "/Users/user/.cache/deno/deps/https/deno.land/abc123"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_Template(t *testing.T) {
// Template extraction computes path from pattern
result, err := ExtractPath("ignored output", &definitions.Extract{
Type: "template",
Pattern: "node_modules/{package}",
}, "lodash")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "node_modules/lodash"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_Template_ScopedPackage(t *testing.T) {
// Template with scoped npm package
result, err := ExtractPath("", &definitions.Extract{
Type: "template",
Pattern: "node_modules/{package}",
}, "@types/node")
if err != nil {
t.Fatalf("ExtractPath failed: %v", err)
}
expected := "node_modules/@types/node"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestExtractPath_Template_MissingPackage(t *testing.T) {
_, err := ExtractPath("", &definitions.Extract{
Type: "template",
Pattern: "deps/{package}",
}, "")
if err == nil {
t.Error("expected error for missing package, got nil")
}
}