-
Notifications
You must be signed in to change notification settings - Fork 0
/
symwalk_test.go
95 lines (84 loc) · 2.11 KB
/
symwalk_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
package symwalk
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestWalk(t *testing.T) {
dir, err := ioutil.TempDir("", "walktest")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(dir)
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
t.Fatalf("failed to eval temp dir: %v", err)
}
mkdir(t, filepath.Join(dir, "a"))
mkdir(t, filepath.Join(dir, "b", "c"))
symlink(t, filepath.Join(dir, "a"), filepath.Join(dir, "b", "c", "Y"))
symlink(t, filepath.Join(dir, "b", "c"), filepath.Join(dir, "a", "X"))
touch(t, filepath.Join(dir, "a", "d"))
var res strings.Builder
Walk(dir, func(path string, info os.FileInfo, err error) error {
fmt.Fprintln(&res, path[len(dir):])
return nil
})
const expected = `
/a
/a/X
/b/c
/b/c/Y
/a/d
/b
`
if res.String() != expected {
t.Errorf("walk did not match expected dirs:\nExpected:\n%s---\nBut found:\n%s", expected, res.String())
}
}
func TestLoop(t *testing.T) {
dir, err := ioutil.TempDir("", "walklooptest")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(dir)
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
t.Fatalf("failed to eval temp dir: %v", err)
}
symlink(t, filepath.Join(dir, "a"), filepath.Join(dir, "b"))
symlink(t, filepath.Join(dir, "b"), filepath.Join(dir, "a"))
var res strings.Builder
Walk(dir, func(path string, info os.FileInfo, err error) error {
fmt.Fprintln(&res, path[len(dir):])
return nil
})
const expected = `
/a
`
if res.String() != expected {
t.Errorf("walk did not match expected dirs:\nExpected:\n%s---\nBut found:\n%s", expected, res.String())
}
}
func mkdir(t *testing.T, name string) {
err := os.MkdirAll(name, 0755)
if err != nil {
t.Fatalf("failed to create dir %v: %v", name, err)
}
}
func symlink(t *testing.T, from, to string) {
err := os.Symlink(from, to)
if err != nil {
t.Fatalf("failed to create symbolic link %v->%v: %v", from, to, err)
}
}
func touch(t *testing.T, name string) {
f, err := os.Create(name)
if err != nil {
t.Fatalf("failed to create file %v: %v", name, err)
}
f.Close()
}