2022-08-16 13:21:25 +03:00
|
|
|
package aghos_test
|
2021-08-13 19:20:17 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2021-10-14 19:39:21 +03:00
|
|
|
"path"
|
2021-08-13 19:20:17 +03:00
|
|
|
"testing"
|
2021-10-14 19:39:21 +03:00
|
|
|
"testing/fstest"
|
2021-08-13 19:20:17 +03:00
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-08-13 19:20:17 +03: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`
|
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
makeFileWalker := func(_ string) (fw aghos.FileWalker) {
|
2021-08-13 19:20:17 +03: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 19:39:21 +03:00
|
|
|
patterns = append(patterns, path.Join(".", line))
|
2021-08-13 19:20:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, true, s.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nl = "\n"
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-10-14 19:39:21 +03:00
|
|
|
testFS fstest.MapFS
|
|
|
|
want assert.BoolAssertionFunc
|
2021-08-13 19:20:17 +03:00
|
|
|
initPattern string
|
2021-10-14 19:39:21 +03:00
|
|
|
name string
|
2021-08-13 19:20:17 +03:00
|
|
|
}{{
|
|
|
|
name: "simple",
|
2021-10-14 19:39:21 +03:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
"simple_0001.txt": &fstest.MapFile{Data: []byte(attribute + nl)},
|
2021-08-13 19:20:17 +03:00
|
|
|
},
|
|
|
|
initPattern: "simple_0001.txt",
|
2021-10-14 19:39:21 +03:00
|
|
|
want: assert.True,
|
2021-08-13 19:20:17 +03:00
|
|
|
}, {
|
|
|
|
name: "chain",
|
2021-10-14 19:39:21 +03: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 19:20:17 +03:00
|
|
|
},
|
|
|
|
initPattern: "chain_0001.txt",
|
2021-10-14 19:39:21 +03:00
|
|
|
want: assert.True,
|
2021-08-13 19:20:17 +03:00
|
|
|
}, {
|
|
|
|
name: "several",
|
2021-10-14 19:39:21 +03: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 19:20:17 +03:00
|
|
|
},
|
|
|
|
initPattern: "several_0001.txt",
|
2021-10-14 19:39:21 +03:00
|
|
|
want: assert.True,
|
2021-08-13 19:20:17 +03:00
|
|
|
}, {
|
|
|
|
name: "no",
|
2021-10-14 19:39:21 +03: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 19:20:17 +03:00
|
|
|
},
|
|
|
|
initPattern: "no_*",
|
2021-10-14 19:39:21 +03:00
|
|
|
want: assert.False,
|
2021-08-13 19:20:17 +03:00
|
|
|
}, {
|
|
|
|
name: "subdirectory",
|
2021-10-14 19:39:21 +03:00
|
|
|
testFS: fstest.MapFS{
|
|
|
|
path.Join("dir", "subdir_0002.txt"): &fstest.MapFile{
|
|
|
|
Data: []byte(attribute + nl),
|
2021-08-13 19:20:17 +03:00
|
|
|
},
|
2021-10-14 19:39:21 +03:00
|
|
|
"subdir_0001.txt": &fstest.MapFile{Data: []byte(`dir/*`)},
|
2021-08-13 19:20:17 +03:00
|
|
|
},
|
|
|
|
initPattern: "subdir_0001.txt",
|
2021-10-14 19:39:21 +03:00
|
|
|
want: assert.True,
|
2021-08-13 19:20:17 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-10-14 19:39:21 +03:00
|
|
|
fw := makeFileWalker("")
|
2021-08-13 19:20:17 +03:00
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-10-14 19:39:21 +03:00
|
|
|
ok, err := fw.Walk(tc.testFS, tc.initPattern)
|
2021-08-13 19:20:17 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
tc.want(t, ok)
|
2021-08-13 19:20:17 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("pattern_malformed", func(t *testing.T) {
|
2021-10-14 19:39:21 +03:00
|
|
|
f := fstest.MapFS{}
|
|
|
|
ok, err := makeFileWalker("").Walk(f, "[]")
|
2021-08-13 19:20:17 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.False(t, ok)
|
2021-10-14 19:39:21 +03:00
|
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
2021-08-13 19:20:17 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_filename", func(t *testing.T) {
|
2021-10-14 19:39:21 +03:00
|
|
|
const filename = "bad_filename.txt"
|
|
|
|
|
|
|
|
f := fstest.MapFS{
|
|
|
|
filename: &fstest.MapFile{Data: []byte("[]")},
|
|
|
|
}
|
2022-08-16 13:21:25 +03:00
|
|
|
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
|
2021-08-13 19:20:17 +03:00
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
patterns = append(patterns, s.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, true, s.Err()
|
2021-10-14 19:39:21 +03:00
|
|
|
}).Walk(f, filename)
|
2021-08-13 19:20:17 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.False(t, ok)
|
2021-10-14 19:39:21 +03:00
|
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
2021-08-13 19:20:17 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("itself_error", func(t *testing.T) {
|
|
|
|
const rerr errors.Error = "returned error"
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
f := fstest.MapFS{
|
|
|
|
"mockfile.txt": &fstest.MapFile{Data: []byte(`mockdata`)},
|
|
|
|
}
|
2021-08-13 19:20:17 +03:00
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, ok bool, err error) {
|
2021-08-13 19:20:17 +03:00
|
|
|
return nil, true, rerr
|
2021-10-14 19:39:21 +03:00
|
|
|
}).Walk(f, "*")
|
|
|
|
require.ErrorIs(t, err, rerr)
|
2021-08-13 19:20:17 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
assert.False(t, ok)
|
2021-08-13 19:20:17 +03:00
|
|
|
})
|
|
|
|
}
|