From 2d438d382a86a7386d911c39efe87745b2f649d7 Mon Sep 17 00:00:00 2001 From: satotake Date: Sat, 19 Jun 2021 21:48:38 +0900 Subject: [PATCH] regexpfs: always use file name instead of path On `RegexpFs`, some operations apply given regexps with not file *name* but file *path*. Fix this. --- regexpfs.go | 3 ++- ro_regexp_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/regexpfs.go b/regexpfs.go index ac359c6..38f095f 100644 --- a/regexpfs.go +++ b/regexpfs.go @@ -2,6 +2,7 @@ package afero import ( "os" + "path/filepath" "regexp" "syscall" "time" @@ -29,7 +30,7 @@ func (r *RegexpFs) matchesName(name string) error { if r.re == nil { return nil } - if r.re.MatchString(name) { + if r.re.MatchString(filepath.Base(name)) { return nil } return syscall.ENOENT diff --git a/ro_regexp_test.go b/ro_regexp_test.go index ef8a35d..16f5f53 100644 --- a/ro_regexp_test.go +++ b/ro_regexp_test.go @@ -94,3 +94,21 @@ func TestFilterRegexReadDir(t *testing.T) { t.Errorf("Got wrong number of names: %v", names) } } + +func TestFilterRegexTarget(t *testing.T) { + mfs := &MemMapFs{} + fs := &RegexpFs{re: regexp.MustCompile(`^a`), source: mfs} + + mfs.MkdirAll("/dir/", 0777) + + _, err := fs.Create("a.txt") + if err != nil { + t.Errorf("Got unexpected error: %#err", err) + } + + // regexp is applied with file name (not file path) + _, err = fs.Create("/dir/a.txt") + if err != nil { + t.Errorf("Got unexpected error: %#err", err) + } +}