...
1 package afero
2
3 import (
4 "regexp"
5 "testing"
6 )
7
8 func TestFilterReadOnly(t *testing.T) {
9 fs := &ReadOnlyFs{source: &MemMapFs{}}
10 _, err := fs.Create("/file.txt")
11 if err == nil {
12 t.Errorf("Did not fail to create file")
13 }
14
15 }
16
17 func TestFilterReadonlyRemoveAndRead(t *testing.T) {
18 mfs := &MemMapFs{}
19 fh, _ := mfs.Create("/file.txt")
20 fh.Write([]byte("content here"))
21 fh.Close()
22
23 fs := NewReadOnlyFs(mfs)
24 err := fs.Remove("/file.txt")
25 if err == nil {
26 t.Errorf("Did not fail to remove file")
27 }
28
29 fh, err = fs.Open("/file.txt")
30 if err != nil {
31 t.Errorf("Failed to open file: %s", err)
32 }
33
34 buf := make([]byte, len("content here"))
35 _, err = fh.Read(buf)
36 fh.Close()
37 if string(buf) != "content here" {
38 t.Errorf("Failed to read file: %s", err)
39 }
40
41 err = mfs.Remove("/file.txt")
42 if err != nil {
43 t.Errorf("Failed to remove file")
44 }
45
46 fh, err = fs.Open("/file.txt")
47 if err == nil {
48 fh.Close()
49 t.Errorf("File still present")
50 }
51 }
52
53 func TestFilterRegexp(t *testing.T) {
54 fs := NewRegexpFs(&MemMapFs{}, regexp.MustCompile(`\.txt$`))
55 _, err := fs.Create("/file.html")
56 if err == nil {
57 t.Errorf("Did not fail to create file")
58 }
59
60 }
61
62 func TestFilterRORegexpChain(t *testing.T) {
63 rofs := &ReadOnlyFs{source: &MemMapFs{}}
64 fs := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: rofs}
65 _, err := fs.Create("/file.txt")
66 if err == nil {
67 t.Errorf("Did not fail to create file")
68 }
69
70 }
71
72 func TestFilterRegexReadDir(t *testing.T) {
73 mfs := &MemMapFs{}
74 fs1 := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: mfs}
75 fs := &RegexpFs{re: regexp.MustCompile(`^a`), source: fs1}
76
77 mfs.MkdirAll("/dir/sub", 0o777)
78 for _, name := range []string{"afile.txt", "afile.html", "bfile.txt"} {
79 for _, dir := range []string{"/dir/", "/dir/sub/"} {
80 fh, _ := mfs.Create(dir + name)
81 fh.Close()
82 }
83 }
84
85 files, _ := ReadDir(fs, "/dir")
86 if len(files) != 2 {
87 t.Errorf("Got wrong number of files: %#v", files)
88 }
89
90 f, _ := fs.Open("/dir/sub")
91 names, _ := f.Readdirnames(-1)
92 if len(names) != 1 {
93 t.Errorf("Got wrong number of names: %v", names)
94 }
95 }
96
View as plain text