2021-08-13 09:20:17 -07:00
|
|
|
package aghos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2021-10-14 09:39:21 -07:00
|
|
|
"path"
|
2021-08-13 09:20:17 -07:00
|
|
|
"testing"
|
2021-10-14 09:39:21 -07:00
|
|
|
"testing/fstest"
|
2021-08-13 09:20:17 -07:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileWalker_Walk(t *testing.T) {
|
|
|
|
const attribute = `000`
|
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
makeFileWalker := func(_ string) (fw FileWalker) {
|
2021-08-13 09:20:17 -07:00
|
|
|
return func(r io.Reader) (patterns []string, cont bool, err error) {
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
if line == attribute {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) != 0 {
|
2021-10-14 09:39:21 -07:00
|
|
|
patterns = append(patterns, path.Join(".", line))
|
2021-08-13 09:20:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, true, s.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nl = "\n"
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS fstest.MapFS
|
|
|
|
want assert.BoolAssertionFunc
|
2021-08-13 09:20:17 -07:00
|
|
|
initPattern string
|
2021-10-14 09:39:21 -07:00
|
|
|
name string
|
2021-08-13 09:20:17 -07:00
|
|
|
}{{
|
|
|
|
name: "simple",
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
"simple_0001.txt": &fstest.MapFile{Data: []byte(attribute + nl)},
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
|
|
|
initPattern: "simple_0001.txt",
|
2021-10-14 09:39:21 -07:00
|
|
|
want: assert.True,
|
2021-08-13 09:20:17 -07:00
|
|
|
}, {
|
|
|
|
name: "chain",
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
"chain_0001.txt": &fstest.MapFile{Data: []byte(`chain_0002.txt` + nl)},
|
|
|
|
"chain_0002.txt": &fstest.MapFile{Data: []byte(`chain_0003.txt` + nl)},
|
|
|
|
"chain_0003.txt": &fstest.MapFile{Data: []byte(attribute + nl)},
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
|
|
|
initPattern: "chain_0001.txt",
|
2021-10-14 09:39:21 -07:00
|
|
|
want: assert.True,
|
2021-08-13 09:20:17 -07:00
|
|
|
}, {
|
|
|
|
name: "several",
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
"several_0001.txt": &fstest.MapFile{Data: []byte(`several_*` + nl)},
|
|
|
|
"several_0002.txt": &fstest.MapFile{Data: []byte(`several_0001.txt` + nl)},
|
|
|
|
"several_0003.txt": &fstest.MapFile{Data: []byte(attribute + nl)},
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
|
|
|
initPattern: "several_0001.txt",
|
2021-10-14 09:39:21 -07:00
|
|
|
want: assert.True,
|
2021-08-13 09:20:17 -07:00
|
|
|
}, {
|
|
|
|
name: "no",
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
"no_0001.txt": &fstest.MapFile{Data: []byte(nl)},
|
|
|
|
"no_0002.txt": &fstest.MapFile{Data: []byte(nl)},
|
|
|
|
"no_0003.txt": &fstest.MapFile{Data: []byte(nl)},
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
|
|
|
initPattern: "no_*",
|
2021-10-14 09:39:21 -07:00
|
|
|
want: assert.False,
|
2021-08-13 09:20:17 -07:00
|
|
|
}, {
|
|
|
|
name: "subdirectory",
|
2021-10-14 09:39:21 -07:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
path.Join("dir", "subdir_0002.txt"): &fstest.MapFile{
|
|
|
|
Data: []byte(attribute + nl),
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
2021-10-14 09:39:21 -07:00
|
|
|
"subdir_0001.txt": &fstest.MapFile{Data: []byte(`dir/*`)},
|
2021-08-13 09:20:17 -07:00
|
|
|
},
|
|
|
|
initPattern: "subdir_0001.txt",
|
2021-10-14 09:39:21 -07:00
|
|
|
want: assert.True,
|
2021-08-13 09:20:17 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-10-14 09:39:21 -07:00
|
|
|
fw := makeFileWalker("")
|
2021-08-13 09:20:17 -07:00
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
ok, err := fw.Walk(tc.testFS, tc.initPattern)
|
2021-08-13 09:20:17 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
tc.want(t, ok)
|
2021-08-13 09:20:17 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("pattern_malformed", func(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
f := fstest.MapFS{}
|
|
|
|
ok, err := makeFileWalker("").Walk(f, "[]")
|
2021-08-13 09:20:17 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.False(t, ok)
|
2021-10-14 09:39:21 -07:00
|
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
2021-08-13 09:20:17 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_filename", func(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
const filename = "bad_filename.txt"
|
|
|
|
|
|
|
|
f := fstest.MapFS{
|
|
|
|
filename: &fstest.MapFile{Data: []byte("[]")},
|
|
|
|
}
|
|
|
|
ok, err := FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
|
2021-08-13 09:20:17 -07:00
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
patterns = append(patterns, s.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, true, s.Err()
|
2021-10-14 09:39:21 -07:00
|
|
|
}).Walk(f, filename)
|
2021-08-13 09:20:17 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.False(t, ok)
|
2021-10-14 09:39:21 -07:00
|
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
2021-08-13 09:20:17 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("itself_error", func(t *testing.T) {
|
|
|
|
const rerr errors.Error = "returned error"
|
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
f := fstest.MapFS{
|
|
|
|
"mockfile.txt": &fstest.MapFile{Data: []byte(`mockdata`)},
|
|
|
|
}
|
2021-08-13 09:20:17 -07:00
|
|
|
|
|
|
|
ok, err := FileWalker(func(r io.Reader) (patterns []string, ok bool, err error) {
|
|
|
|
return nil, true, rerr
|
2021-10-14 09:39:21 -07:00
|
|
|
}).Walk(f, "*")
|
|
|
|
require.ErrorIs(t, err, rerr)
|
2021-08-13 09:20:17 -07:00
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
assert.False(t, ok)
|
2021-08-13 09:20:17 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
type errFS struct {
|
|
|
|
fs.GlobFS
|
|
|
|
}
|
|
|
|
|
|
|
|
const errErrFSOpen errors.Error = "this error is always returned"
|
|
|
|
|
|
|
|
func (efs *errFS) Open(name string) (fs.File, error) {
|
|
|
|
return nil, errErrFSOpen
|
|
|
|
}
|
|
|
|
|
2021-08-13 09:20:17 -07:00
|
|
|
func TestWalkerFunc_CheckFile(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
emptyFS := fstest.MapFS{}
|
|
|
|
|
2021-08-13 09:20:17 -07:00
|
|
|
t.Run("non-existing", func(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
_, ok, err := checkFile(emptyFS, nil, "lol")
|
2021-08-13 09:20:17 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid_argument", func(t *testing.T) {
|
2021-10-14 09:39:21 -07:00
|
|
|
_, ok, err := checkFile(&errFS{}, nil, "")
|
|
|
|
require.ErrorIs(t, err, errErrFSOpen)
|
2021-08-13 09:20:17 -07:00
|
|
|
|
|
|
|
assert.False(t, ok)
|
2021-10-14 09:39:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ignore_dirs", func(t *testing.T) {
|
|
|
|
const dirName = "dir"
|
|
|
|
|
|
|
|
testFS := fstest.MapFS{
|
|
|
|
path.Join(dirName, "file"): &fstest.MapFile{Data: []byte{}},
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns, ok, err := checkFile(testFS, nil, dirName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Empty(t, patterns)
|
|
|
|
assert.True(t, ok)
|
2021-08-13 09:20:17 -07:00
|
|
|
})
|
|
|
|
}
|