mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-24 22:15:45 +03:00
d4c3a43bcb
Merge in DNS/adguard-home from add-dnssvc to master
Squashed commit of the following:
commit 55f4f114bab65a03c0d65383e89020a7356cff32
Merge: 95dc28d9 6e63757f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Aug 15 20:53:07 2022 +0300
Merge branch 'master' into add-dnssvc
commit 95dc28d9d77d06e8ac98c1e6772557bffbf1705b
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Aug 15 20:52:50 2022 +0300
all: imp tests, docs
commit 0d9d02950d84afd160b4b1c118da856cee6f12e5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 19:27:59 2022 +0300
all: imp docs
commit 8990e038a81da4430468da12fcebedf79fe14df6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 19:05:29 2022 +0300
all: imp tests more
commit 92730d93a2a1ac77888c2655508e43efaf0e9fde
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 18:37:48 2022 +0300
all: imp tests more
commit 8cd45ba30da7ac310e9dc666fb2af438e577b02d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 18:11:15 2022 +0300
all: add v1 dnssvc stub; refactor tests
144 lines
3.5 KiB
Go
144 lines
3.5 KiB
Go
package aghos_test
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"path"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFileWalker_Walk(t *testing.T) {
|
|
const attribute = `000`
|
|
|
|
makeFileWalker := func(_ string) (fw aghos.FileWalker) {
|
|
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 {
|
|
patterns = append(patterns, path.Join(".", line))
|
|
}
|
|
}
|
|
|
|
return patterns, true, s.Err()
|
|
}
|
|
}
|
|
|
|
const nl = "\n"
|
|
|
|
testCases := []struct {
|
|
testFS fstest.MapFS
|
|
want assert.BoolAssertionFunc
|
|
initPattern string
|
|
name string
|
|
}{{
|
|
name: "simple",
|
|
testFS: fstest.MapFS{
|
|
"simple_0001.txt": &fstest.MapFile{Data: []byte(attribute + nl)},
|
|
},
|
|
initPattern: "simple_0001.txt",
|
|
want: assert.True,
|
|
}, {
|
|
name: "chain",
|
|
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)},
|
|
},
|
|
initPattern: "chain_0001.txt",
|
|
want: assert.True,
|
|
}, {
|
|
name: "several",
|
|
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)},
|
|
},
|
|
initPattern: "several_0001.txt",
|
|
want: assert.True,
|
|
}, {
|
|
name: "no",
|
|
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)},
|
|
},
|
|
initPattern: "no_*",
|
|
want: assert.False,
|
|
}, {
|
|
name: "subdirectory",
|
|
testFS: fstest.MapFS{
|
|
path.Join("dir", "subdir_0002.txt"): &fstest.MapFile{
|
|
Data: []byte(attribute + nl),
|
|
},
|
|
"subdir_0001.txt": &fstest.MapFile{Data: []byte(`dir/*`)},
|
|
},
|
|
initPattern: "subdir_0001.txt",
|
|
want: assert.True,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
fw := makeFileWalker("")
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ok, err := fw.Walk(tc.testFS, tc.initPattern)
|
|
require.NoError(t, err)
|
|
|
|
tc.want(t, ok)
|
|
})
|
|
}
|
|
|
|
t.Run("pattern_malformed", func(t *testing.T) {
|
|
f := fstest.MapFS{}
|
|
ok, err := makeFileWalker("").Walk(f, "[]")
|
|
require.Error(t, err)
|
|
|
|
assert.False(t, ok)
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
|
})
|
|
|
|
t.Run("bad_filename", func(t *testing.T) {
|
|
const filename = "bad_filename.txt"
|
|
|
|
f := fstest.MapFS{
|
|
filename: &fstest.MapFile{Data: []byte("[]")},
|
|
}
|
|
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
|
|
s := bufio.NewScanner(r)
|
|
for s.Scan() {
|
|
patterns = append(patterns, s.Text())
|
|
}
|
|
|
|
return patterns, true, s.Err()
|
|
}).Walk(f, filename)
|
|
require.Error(t, err)
|
|
|
|
assert.False(t, ok)
|
|
assert.ErrorIs(t, err, path.ErrBadPattern)
|
|
})
|
|
|
|
t.Run("itself_error", func(t *testing.T) {
|
|
const rerr errors.Error = "returned error"
|
|
|
|
f := fstest.MapFS{
|
|
"mockfile.txt": &fstest.MapFile{Data: []byte(`mockdata`)},
|
|
}
|
|
|
|
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, ok bool, err error) {
|
|
return nil, true, rerr
|
|
}).Walk(f, "*")
|
|
require.ErrorIs(t, err, rerr)
|
|
|
|
assert.False(t, ok)
|
|
})
|
|
}
|