package nfs_test import ( "fmt" "git.noahlan.cn/noahlan/ntool/nfs" "git.noahlan.cn/noahlan/ntool/ntest/assert" "io/fs" "strings" "testing" ) func TestSearchNameUp(t *testing.T) { p := nfs.SearchNameUp("testdata", "finder") assert.NotEmpty(t, p) assert.True(t, strings.HasSuffix(p, "nfs")) p = nfs.SearchNameUp("testdata", ".dotdir") assert.NotEmpty(t, p) assert.True(t, strings.HasSuffix(p, "testdata")) p = nfs.SearchNameUp("testdata", "test.txt") assert.NotEmpty(t, p) assert.True(t, strings.HasSuffix(p, "testdata")) p = nfs.SearchNameUp("testdata", "not-exists") assert.Empty(t, p) } type dirEnt struct { typ fs.FileMode isDir bool name string } func (d *dirEnt) Name() string { return d.name } func (d *dirEnt) IsDir() bool { return d.isDir } func (d *dirEnt) Type() fs.FileMode { return d.typ } func (d *dirEnt) Info() (fs.FileInfo, error) { panic("implement me") } func TestApplyFilters(t *testing.T) { e1 := &dirEnt{name: "some-backup"} f1 := nfs.ExcludeSuffix("-backup") assert.False(t, f1("", e1)) assert.True(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{f1})) assert.True(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{nfs.OnlyFindDir})) assert.False(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{nfs.OnlyFindFile})) assert.False(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{nfs.ExcludeDotFile})) assert.False(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{nfs.IncludeSuffix("-backup")})) assert.True(t, nfs.ApplyFilters("", e1, []nfs.FilterFunc{nfs.ExcludeNames("some-backup")})) } func TestFindInDir(t *testing.T) { err := nfs.FindInDir("path-not-exist", nil) assert.NoErr(t, err) err = nfs.FindInDir("testdata/test.txt", nil) assert.NoErr(t, err) files := make([]string, 0, 8) err = nfs.FindInDir("testdata", func(fPath string, de fs.DirEntry) error { files = append(files, fPath) return nil }) //dump.P(files) assert.NoErr(t, err) assert.True(t, len(files) > 0) files = files[:0] err = nfs.FindInDir("testdata", func(fPath string, de fs.DirEntry) error { files = append(files, fPath) return nil }, func(fPath string, de fs.DirEntry) bool { return !strings.HasPrefix(de.Name(), ".") }) assert.NoErr(t, err) assert.True(t, len(files) > 0) err = nfs.FindInDir("testdata", func(fPath string, de fs.DirEntry) error { return fmt.Errorf("handle error") }) assert.Err(t, err) }