afero/ro_regexp_test.go

97 lines
2.1 KiB
Go
Raw Normal View History

2015-12-13 16:56:00 +03:00
package afero
import (
"regexp"
2015-12-13 16:56:00 +03:00
"testing"
)
func TestFilterReadOnly(t *testing.T) {
fs := &ReadOnlyFs{source: &MemMapFs{}}
2015-12-13 16:56:00 +03:00
_, err := fs.Create("/file.txt")
if err == nil {
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
2015-12-13 16:56:00 +03:00
}
func TestFilterReadonlyRemoveAndRead(t *testing.T) {
2015-12-13 16:56:00 +03:00
mfs := &MemMapFs{}
2022-07-14 14:06:04 +03:00
fh, _ := mfs.Create("/file.txt")
2015-12-13 16:56:00 +03:00
fh.Write([]byte("content here"))
fh.Close()
fs := NewReadOnlyFs(mfs)
2022-07-14 14:06:04 +03:00
err := fs.Remove("/file.txt")
2015-12-13 16:56:00 +03:00
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$`))
2015-12-13 16:56:00 +03:00
_, err := fs.Create("/file.html")
if err == nil {
2015-12-13 16:56:00 +03:00
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
2015-12-13 16:56:00 +03:00
}
func TestFilterRORegexpChain(t *testing.T) {
rofs := &ReadOnlyFs{source: &MemMapFs{}}
fs := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: rofs}
2015-12-13 16:56:00 +03:00
_, err := fs.Create("/file.txt")
if err == nil {
t.Errorf("Did not fail to create file")
}
// t.Logf("ERR=%s", err)
2015-12-13 16:56:00 +03:00
}
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", 0777)
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)
}
}