forked from spf13/afero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ro_regexp_test.go
95 lines (81 loc) · 2.1 KB
/
ro_regexp_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
package afero
import (
"regexp"
"testing"
)
func TestFilterReadOnly(t *testing.T) {
fs := &ReadOnlyFs{source: &MemMapFs{}}
_, err := fs.Create("/file.txt")
if err == nil {
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
}
func TestFilterReadonlyRemoveAndRead(t *testing.T) {
mfs := &MemMapFs{}
fh, _ := mfs.Create("/file.txt")
fh.Write([]byte("content here"))
fh.Close()
fs := NewReadOnlyFs(mfs)
err := fs.Remove("/file.txt")
if err == nil {
t.Errorf("Did not fail to remove file")
}
fh, err = fs.Open("/file.txt")
if err != nil {
t.Errorf("Failed to open file: %s", err)
}
buf := make([]byte, len("content here"))
_, err = fh.Read(buf)
fh.Close()
if string(buf) != "content here" {
t.Errorf("Failed to read file: %s", err)
}
err = mfs.Remove("/file.txt")
if err != nil {
t.Errorf("Failed to remove file")
}
fh, err = fs.Open("/file.txt")
if err == nil {
fh.Close()
t.Errorf("File still present")
}
}
func TestFilterRegexp(t *testing.T) {
fs := NewRegexpFs(&MemMapFs{}, regexp.MustCompile(`\.txt$`))
_, err := fs.Create("/file.html")
if err == nil {
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
}
func TestFilterRORegexpChain(t *testing.T) {
rofs := &ReadOnlyFs{source: &MemMapFs{}}
fs := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: rofs}
_, err := fs.Create("/file.txt")
if err == nil {
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
}
func TestFilterRegexReadDir(t *testing.T) {
mfs := &MemMapFs{}
fs1 := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: mfs}
fs := &RegexpFs{re: regexp.MustCompile(`^a`), source: fs1}
mfs.MkdirAll("/dir/sub", 0o777)
for _, name := range []string{"afile.txt", "afile.html", "bfile.txt"} {
for _, dir := range []string{"/dir/", "/dir/sub/"} {
fh, _ := mfs.Create(dir + name)
fh.Close()
}
}
files, _ := ReadDir(fs, "/dir")
if len(files) != 2 { // afile.txt, sub
t.Errorf("Got wrong number of files: %#v", files)
}
f, _ := fs.Open("/dir/sub")
names, _ := f.Readdirnames(-1)
if len(names) != 1 {
t.Errorf("Got wrong number of names: %v", names)
}
}