regexpfs: always use file name instead of path

On `RegexpFs`, some operations apply given regexps with not file *name* but file *path*.
Fix this.
This commit is contained in:
satotake 2021-06-19 21:48:38 +09:00
parent bc94f58bed
commit 2d438d382a
2 changed files with 20 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package afero
import ( import (
"os" "os"
"path/filepath"
"regexp" "regexp"
"syscall" "syscall"
"time" "time"
@ -29,7 +30,7 @@ func (r *RegexpFs) matchesName(name string) error {
if r.re == nil { if r.re == nil {
return nil return nil
} }
if r.re.MatchString(name) { if r.re.MatchString(filepath.Base(name)) {
return nil return nil
} }
return syscall.ENOENT return syscall.ENOENT

View File

@ -94,3 +94,21 @@ func TestFilterRegexReadDir(t *testing.T) {
t.Errorf("Got wrong number of names: %v", names) 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)
}
}