This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
fs_test.go
213 lines (196 loc) · 4.65 KB
/
fs_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
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
package archiver
import (
"bytes"
_ "embed"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path"
"reflect"
"sort"
"testing"
)
func TestPathWithoutTopDir(t *testing.T) {
for i, tc := range []struct {
input, expect string
}{
{
input: "a/b/c",
expect: "b/c",
},
{
input: "b/c",
expect: "c",
},
{
input: "c",
expect: "c",
},
{
input: "",
expect: "",
},
} {
if actual := pathWithoutTopDir(tc.input); actual != tc.expect {
t.Errorf("Test %d (input=%s): Expected '%s' but got '%s'", i, tc.input, tc.expect, actual)
}
}
}
//go:generate zip testdata/test.zip go.mod
//go:generate zip -qr9 testdata/nodir.zip archiver.go go.mod cmd/arc/main.go .github/ISSUE_TEMPLATE/bug_report.md .github/FUNDING.yml README.md .github/workflows/ubuntu-latest.yml
var (
//go:embed testdata/test.zip
testZIP []byte
//go:embed testdata/nodir.zip
nodirZIP []byte
//go:embed testdata/unordered.zip
unorderZip []byte
)
func TestSelfTar(t *testing.T) {
fn := "testdata/self-tar.tar"
fh, err := os.Open(fn)
if err != nil {
t.Errorf("Could not load test tar: %v", fn)
}
fstat, err := os.Stat(fn)
if err != nil {
t.Errorf("Could not stat test tar: %v", fn)
}
fsys := &ArchiveFS{
Stream: io.NewSectionReader(fh, 0, fstat.Size()),
Format: Tar{},
}
var count int
err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if count > 10 {
t.Error("walking test tar appears to be recursing in error")
return fmt.Errorf("recursing tar: %v", fn)
}
count++
return nil
})
if err != nil {
t.Error(err)
}
}
func ExampleArchiveFS_Stream() {
fsys := &ArchiveFS{
Stream: io.NewSectionReader(bytes.NewReader(testZIP), 0, int64(len(testZIP))),
Format: Zip{},
}
// You can serve the contents in a web server:
http.Handle("/static", http.StripPrefix("/static",
http.FileServer(http.FS(fsys))))
// Or read the files using fs functions:
dis, err := fsys.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, di := range dis {
fmt.Println(di.Name())
b, err := fs.ReadFile(fsys, path.Join(".", di.Name()))
if err != nil {
log.Fatal(err)
}
fmt.Println(bytes.Contains(b, []byte("require (")))
}
// Output:
// go.mod
// true
}
func TestArchiveFS_ReadDir(t *testing.T) {
for _, tc := range []struct {
name string
archive ArchiveFS
want map[string][]string
}{
{
name: "test.zip",
archive: ArchiveFS{
Stream: io.NewSectionReader(bytes.NewReader(testZIP), 0, int64(len(testZIP))),
Format: Zip{},
},
// unzip -l testdata/test.zip
want: map[string][]string{
".": {"go.mod"},
},
},
{
name: "nodir.zip",
archive: ArchiveFS{
Stream: io.NewSectionReader(bytes.NewReader(nodirZIP), 0, int64(len(nodirZIP))),
Format: Zip{},
},
// unzip -l testdata/nodir.zip
want: map[string][]string{
".": {".github", "README.md", "archiver.go", "cmd", "go.mod"},
".github": {"FUNDING.yml", "ISSUE_TEMPLATE", "workflows"},
"cmd": {"arc"},
},
},
{
name: "unordered.zip",
archive: ArchiveFS{
Stream: io.NewSectionReader(bytes.NewReader(unorderZip), 0, int64(len(unorderZip))),
Format: Zip{},
},
// unzip -l testdata/unordered.zip, note entry 1/1 and 1/2 are separated by contents of directory 2
want: map[string][]string{
".": {"1", "2"},
"1": {"1", "2"},
"2": {"1"},
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fsys := tc.archive
for baseDir, wantLS := range tc.want {
t.Run(fmt.Sprintf("ReadDir(%q)", baseDir), func(t *testing.T) {
dis, err := fsys.ReadDir(baseDir)
if err != nil {
t.Error(err)
}
dirs := []string{}
for _, di := range dis {
dirs = append(dirs, di.Name())
}
// Stabilize the sort order
sort.Strings(dirs)
if !reflect.DeepEqual(wantLS, dirs) {
t.Errorf("ReadDir() got: %v, want: %v", dirs, wantLS)
}
})
// Uncomment to reproduce https://github.com/mholt/archiver/issues/340.
t.Run(fmt.Sprintf("Open(%s)", baseDir), func(t *testing.T) {
f, err := fsys.Open(baseDir)
if err != nil {
t.Errorf("fsys.Open(%q): %#v %s", baseDir, err, err)
return
}
rdf, ok := f.(fs.ReadDirFile)
if !ok {
t.Errorf("fsys.Open(%q) did not return a fs.ReadDirFile, got: %#v", baseDir, f)
}
dis, err := rdf.ReadDir(-1)
if err != nil {
t.Error(err)
}
dirs := []string{}
for _, di := range dis {
dirs = append(dirs, di.Name())
}
// Stabilize the sort order
sort.Strings(dirs)
if !reflect.DeepEqual(wantLS, dirs) {
t.Errorf("Open().ReadDir(-1) got: %v, want: %v", dirs, wantLS)
}
})
}
})
}
}