undocker/internal/tree/tree_test.go

98 lines
2.1 KiB
Go
Raw Normal View History

2021-05-24 00:11:58 +03:00
package tree
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTree(t *testing.T) {
tests := []struct {
name string
paths []string
matchTrue []string
matchFalse []string
}{
{
name: "empty sequence matches nothing",
paths: []string{},
matchFalse: []string{"a", "b"},
},
2021-05-24 00:11:58 +03:00
{
name: "directory",
paths: []string{"a/b"},
matchTrue: []string{"a/b/"},
},
2021-05-24 00:11:58 +03:00
{
name: "a few sequences",
paths: []string{"a", "b", "c/b/a"},
matchTrue: []string{"a", "a/b/c", "c/b/a", "c/b/a/d"},
matchFalse: []string{"c/d", "c", "c/b"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree := New(tt.paths...)
2021-05-24 00:11:58 +03:00
for _, path := range tt.matchTrue {
t.Run(path, func(t *testing.T) {
assert.True(t, tree.HasPrefix(path),
"expected %s to be a prefix of %s", path, tree)
})
}
for _, path := range tt.matchFalse {
t.Run(path, func(t *testing.T) {
assert.False(t, tree.HasPrefix(path),
"expected %s to not be a prefix of %s", path, tree)
})
}
})
}
}
func TestTreeMerge(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree1 := New("bin/ar", "var/cache/apt")
tree2 := New("bin/ar", "bin/busybox", "usr/share/doc")
2021-05-24 00:11:58 +03:00
tree1.Merge(tree2)
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt", tree1.String())
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc", tree2.String())
}
func TestString(t *testing.T) {
tests := []struct {
name string
paths []string
wantStr string
}{
{
name: "empty",
paths: []string{},
wantStr: "<empty>",
},
{
name: "simple path",
paths: []string{"a/b/c"},
wantStr: "./a/b/c",
},
{
name: "duplicate paths",
paths: []string{"a/a", "a//a"},
wantStr: "./a/a",
},
{
name: "a few sequences",
paths: []string{"bin/ar", "bin/busybox", "var/cache/apt/archives"},
wantStr: "./bin/ar:./bin/busybox:./var/cache/apt/archives",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree := New(tt.paths...)
2021-05-24 00:11:58 +03:00
assert.Equal(t, tt.wantStr, tree.String())
})
}
}