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 (
"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

View File

@ -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)
}
}