-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_fs_test.go
More file actions
191 lines (169 loc) · 5.09 KB
/
fetch_fs_test.go
File metadata and controls
191 lines (169 loc) · 5.09 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
package templar
import (
"testing"
)
// TestExtractTarGzFS verifies that extractTarGzFS writes files to a MemFS
// from an in-memory tarball — zero disk I/O.
func TestExtractTarGzFS(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc123"}, map[string]string{
"hello.txt": "world",
"sub/nested.html": "<h1>Nested</h1>",
})
mfs := NewMemFS()
count, err := extractTarGzFS(mfs, tarball, "vendor/src", "", nil, nil)
if err != nil {
t.Fatal(err)
}
if count != 2 {
t.Errorf("extracted %d files, want 2", count)
}
// Verify files written to FS
data, err := mfs.ReadFile("vendor/src/hello.txt")
if err != nil {
t.Fatal(err)
}
if string(data) != "world" {
t.Errorf("hello.txt = %q, want world", data)
}
data, err = mfs.ReadFile("vendor/src/sub/nested.html")
if err != nil {
t.Fatal(err)
}
if string(data) != "<h1>Nested</h1>" {
t.Errorf("nested.html = %q", data)
}
}
// TestExtractTarGzFSWithSubPath verifies subPath filtering.
func TestExtractTarGzFSWithSubPath(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc123"}, map[string]string{
"templates/a.html": "A",
"templates/b.html": "B",
"other/c.txt": "C",
})
mfs := NewMemFS()
count, err := extractTarGzFS(mfs, tarball, "out", "templates", nil, nil)
if err != nil {
t.Fatal(err)
}
if count != 2 {
t.Errorf("extracted %d files, want 2 (only templates/)", count)
}
if !mfs.HasFile("out/a.html") {
t.Error("missing a.html")
}
if mfs.HasFile("out/c.txt") {
t.Error("c.txt should not be extracted (outside subPath)")
}
}
// TestExtractTarGzFSWithPatterns verifies include/exclude filtering.
func TestExtractTarGzFSWithPatterns(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc123"}, map[string]string{
"a.html": "A",
"b.css": "B",
"c.html": "C",
})
mfs := NewMemFS()
count, err := extractTarGzFS(mfs, tarball, "out", "", []string{"*.html"}, nil)
if err != nil {
t.Fatal(err)
}
if count != 2 {
t.Errorf("extracted %d files, want 2 (only *.html)", count)
}
if mfs.HasFile("out/b.css") {
t.Error("b.css should be excluded by include pattern")
}
}
// TestExtractTarGzFSWithPAX verifies that PAX global headers are skipped.
func TestExtractTarGzFSWithPAX(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc", IncludePAX: true}, map[string]string{
"hello.txt": "world",
})
mfs := NewMemFS()
count, err := extractTarGzFS(mfs, tarball, "out", "", nil, nil)
if err != nil {
t.Fatal(err)
}
if count != 1 {
t.Errorf("extracted %d, want 1", count)
}
if mfs.HasFile("out/pax_global_header") {
t.Error("PAX header should not be extracted")
}
}
// TestExtractTarGzFSWithExclude verifies exclude pattern filtering.
func TestExtractTarGzFSWithExclude(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc"}, map[string]string{
"keep.html": "keep", "remove.css": "rm", "also.html": "also",
})
mfs := NewMemFS()
count, _ := extractTarGzFS(mfs, tarball, "out", "", nil, []string{"*.css"})
if count != 2 {
t.Errorf("extracted %d, want 2", count)
}
if mfs.HasFile("out/remove.css") {
t.Error("*.css should be excluded")
}
}
// TestExtractTarGzFSIncludeAndExclude verifies both filters together.
func TestExtractTarGzFSIncludeAndExclude(t *testing.T) {
tarball := makeTestTarGz(t, TarGzOpts{TopDir: "repo-abc"}, map[string]string{
"a.html": "A", "b.html": "B", "c.css": "C", "test.html": "test",
})
mfs := NewMemFS()
count, _ := extractTarGzFS(mfs, tarball, "out", "", []string{"*.html"}, []string{"test*"})
if count != 2 {
t.Errorf("extracted %d, want 2 (a.html, b.html)", count)
}
if mfs.HasFile("out/c.css") {
t.Error("c.css excluded by include")
}
if mfs.HasFile("out/test.html") {
t.Error("test.html excluded by exclude")
}
}
// TestWriteLockFileFS verifies lock file writing to MemFS.
func TestWriteLockFileFS(t *testing.T) {
mfs := NewMemFS()
lock := &VendorLock{
Version: 1,
Sources: map[string]LockedSource{
"test": {URL: "github.com/test/repo", ResolvedCommit: "abc123"},
},
}
info := ToolInfo{Name: "test", ConfigNames: []string{"test.yaml"}, VendorDir: "modules", FetchCmd: "test fetch"}
err := WriteLockFileFS(mfs, "test.lock", lock, info)
if err != nil {
t.Fatal(err)
}
if !mfs.HasFile("test.lock") {
t.Fatal("lock file not written")
}
// Verify it can be loaded back
loaded, err := LoadLockFileFS(mfs, "test.lock")
if err != nil {
t.Fatal(err)
}
if loaded.Version != 1 {
t.Errorf("version = %d, want 1", loaded.Version)
}
if loaded.Sources["test"].ResolvedCommit != "abc123" {
t.Errorf("commit = %q", loaded.Sources["test"].ResolvedCommit)
}
}
// TestWriteVendorReadmeFS verifies readme writing to MemFS.
func TestWriteVendorReadmeFS(t *testing.T) {
mfs := NewMemFS()
info := ToolInfo{Name: "slyds", ConfigNames: []string{".slyds.yaml"}, VendorDir: ".slyds-modules", FetchCmd: "slyds update", ProjectURL: "https://github.com/panyam/slyds"}
err := WriteVendorReadmeFS(mfs, ".slyds-modules", info)
if err != nil {
t.Fatal(err)
}
data, err := mfs.ReadFile(".slyds-modules/README.md")
if err != nil {
t.Fatal(err)
}
if len(data) == 0 {
t.Error("readme is empty")
}
}