rclone/fs/dirtree/dirtree_test.go
Nick Craig-Wood 401cf81034 build: modernize Go usage
This commit modernizes Go usage. This was done with:

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

Then files needed to be `go fmt`ed and a few comments needed to be
restored.

The modernizations include replacing

- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
2025-02-28 11:31:14 +00:00

222 lines
3.8 KiB
Go

package dirtree
import (
"fmt"
"testing"
"github.com/rclone/rclone/fstest/mockdir"
"github.com/rclone/rclone/fstest/mockobject"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
dt := New()
assert.Equal(t, "", dt.String())
}
func TestParentDir(t *testing.T) {
assert.Equal(t, "root/parent", parentDir("root/parent/file"))
assert.Equal(t, "parent", parentDir("parent/file"))
assert.Equal(t, "", parentDir("parent"))
assert.Equal(t, "", parentDir(""))
}
func TestDirTreeAdd(t *testing.T) {
dt := New()
o := mockobject.New("potato")
dt.Add(o)
assert.Equal(t, `/
potato
`, dt.String())
o = mockobject.New("dir/subdir/sausage")
dt.Add(o)
assert.Equal(t, `/
potato
dir/subdir/
sausage
`, dt.String())
}
func TestDirTreeAddDir(t *testing.T) {
dt := New()
d := mockdir.New("potato")
dt.Add(d)
assert.Equal(t, `/
potato/
`, dt.String())
d = mockdir.New("dir/subdir/sausage")
dt.AddDir(d)
assert.Equal(t, `/
potato/
dir/subdir/
sausage/
dir/subdir/sausage/
`, dt.String())
d = mockdir.New("")
dt.AddDir(d)
assert.Equal(t, `/
potato/
dir/subdir/
sausage/
dir/subdir/sausage/
`, dt.String())
}
func TestDirTreeAddEntry(t *testing.T) {
dt := New()
d := mockdir.New("dir/subdir/sausagedir")
dt.AddEntry(d)
o := mockobject.New("dir/subdir2/sausage2")
dt.AddEntry(o)
assert.Equal(t, `/
dir/
dir/
subdir/
subdir2/
dir/subdir/
sausagedir/
dir/subdir/sausagedir/
dir/subdir2/
sausage2
`, dt.String())
}
func TestDirTreeFind(t *testing.T) {
dt := New()
parent, foundObj := dt.Find("dir/subdir/sausage")
assert.Equal(t, "dir/subdir", parent)
assert.Nil(t, foundObj)
o := mockobject.New("dir/subdir/sausage")
dt.Add(o)
parent, foundObj = dt.Find("dir/subdir/sausage")
assert.Equal(t, "dir/subdir", parent)
assert.Equal(t, o, foundObj)
}
func TestDirTreeCheckParent(t *testing.T) {
dt := New()
o := mockobject.New("dir/subdir/sausage")
dt.Add(o)
assert.Equal(t, `dir/subdir/
sausage
`, dt.String())
dt.checkParent("", "dir/subdir", nil)
assert.Equal(t, `/
dir/
dir/
subdir/
dir/subdir/
sausage
`, dt.String())
}
func TestDirTreeCheckParents(t *testing.T) {
dt := New()
dt.Add(mockobject.New("dir/subdir/sausage"))
dt.Add(mockobject.New("dir/subdir2/sausage2"))
dt.CheckParents("")
dt.Sort() // sort since the exact order of adding parents is not defined
assert.Equal(t, `/
dir/
dir/
subdir/
subdir2/
dir/subdir/
sausage
dir/subdir2/
sausage2
`, dt.String())
}
func TestDirTreeSort(t *testing.T) {
dt := New()
dt.Add(mockobject.New("dir/subdir/B"))
dt.Add(mockobject.New("dir/subdir/A"))
assert.Equal(t, `dir/subdir/
B
A
`, dt.String())
dt.Sort()
assert.Equal(t, `dir/subdir/
A
B
`, dt.String())
}
func TestDirTreeDirs(t *testing.T) {
dt := New()
dt.Add(mockobject.New("dir/subdir/sausage"))
dt.Add(mockobject.New("dir/subdir2/sausage2"))
dt.CheckParents("")
assert.Equal(t, []string{
"",
"dir",
"dir/subdir",
"dir/subdir2",
}, dt.Dirs())
}
func TestDirTreePrune(t *testing.T) {
dt := New()
dt.Add(mockobject.New("file"))
dt.Add(mockobject.New("dir/subdir/sausage"))
dt.Add(mockobject.New("dir/subdir2/sausage2"))
dt.Add(mockobject.New("dir/file"))
dt.Add(mockobject.New("dir2/file"))
dt.CheckParents("")
err := dt.Prune(map[string]bool{
"dir": true,
})
require.NoError(t, err)
assert.Equal(t, `/
file
dir2/
dir2/
file
`, dt.String())
}
func BenchmarkCheckParents(b *testing.B) {
for _, N := range []int{1e2, 1e3, 1e4, 1e5, 1e6} {
b.Run(fmt.Sprintf("%d", N), func(b *testing.B) {
b.StopTimer()
dt := New()
for i := range N {
remote := fmt.Sprintf("dir%09d/file%09d.txt", i, 1)
o := mockobject.New(remote)
dt.Add(o)
}
b.StartTimer()
for n := 0; n < b.N; n++ {
dt.CheckParents("")
}
})
}
}