forked from karrick/godirwalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scandir_test.go
100 lines (81 loc) · 1.9 KB
/
scandir_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package godirwalk
import (
"os"
"path/filepath"
"testing"
)
func TestScanner(t *testing.T) {
t.Run("collect names", func(t *testing.T) {
var actual []string
scanner, err := NewScanner(filepath.Join(scaffolingRoot, "d0"))
ensureError(t, err)
for scanner.Scan() {
actual = append(actual, scanner.Name())
}
ensureError(t, scanner.Err())
expected := []string{maxName, "d1", "f1", "skips", "symlinks"}
ensureStringSlicesMatch(t, actual, expected)
})
t.Run("collect dirents", func(t *testing.T) {
var actual []*Dirent
testroot := filepath.Join(scaffolingRoot, "d0")
scanner, err := NewScanner(testroot)
ensureError(t, err)
for scanner.Scan() {
dirent, err := scanner.Dirent()
ensureError(t, err)
actual = append(actual, dirent)
}
ensureError(t, scanner.Err())
expected := Dirents{
&Dirent{
name: maxName,
path: testroot,
modeType: os.FileMode(0),
},
&Dirent{
name: "d1",
path: testroot,
modeType: os.ModeDir,
},
&Dirent{
name: "f1",
path: testroot,
modeType: os.FileMode(0),
},
&Dirent{
name: "skips",
path: testroot,
modeType: os.ModeDir,
},
&Dirent{
name: "symlinks",
path: testroot,
modeType: os.ModeDir,
},
}
ensureDirentsMatch(t, actual, expected)
})
t.Run("symlink to directory", func(t *testing.T) {
scanner, err := NewScanner(filepath.Join(scaffolingRoot, "d0/symlinks"))
ensureError(t, err)
var found bool
for scanner.Scan() {
if scanner.Name() != "toD1" {
continue
}
found = true
de, err := scanner.Dirent()
ensureError(t, err)
got, err := de.IsDirOrSymlinkToDir()
ensureError(t, err)
if want := true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
}
ensureError(t, scanner.Err())
if got, want := found, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
})
}