File: walk_test.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (63 lines) | stat: -rw-r--r-- 1,533 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package hdfs

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestWalk(t *testing.T) {
	c := getClient(t)

	c.Mkdir("/_test/walk", os.ModePerm)
	c.Mkdir("/_test/walk/dir", os.ModePerm)
	c.Mkdir("/_test/walk/dir/subdir", os.ModePerm)
	c.Create("/_test/walk/walkfile")
	c.Create("/_test/walk/dir/walkfile1")
	c.Create("/_test/walk/dir/walkfile2")
	c.Create("/_test/walk/dir/subdir/walkfile1")
	c.Create("/_test/walk/dir/subdir/walkfile2")

	paths := make([]string, 0, 8)

	err := c.Walk("/_test/walk/", walkFnTest(&paths))
	assert.Nil(t, err, "unexpected error")

	expected := []string{
		"/_test/walk/",
		"/_test/walk/dir",
		"/_test/walk/dir/subdir",
		"/_test/walk/dir/subdir/walkfile1",
		"/_test/walk/dir/subdir/walkfile2",
		"/_test/walk/dir/walkfile1",
		"/_test/walk/dir/walkfile2",
		"/_test/walk/walkfile"}

	assert.Equal(t, expected, paths, "discrepancy between expected and walked paths.")

}

func TestWalkError(t *testing.T) {
	c := getClient(t)
	errors := make([]error, 0, 1)
	c.Walk("/not_existing", walkErrorFn(&errors))
	assert.Equal(t, 1, len(errors), "expected a single error")
}

func walkFnTest(encounteredPaths *[]string) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		*encounteredPaths = append(*encounteredPaths, path)
		return nil
	}
}

func walkErrorFn(errors *[]error) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			*errors = append(*errors, err)
		}
		return nil
	}
}