-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
206 lines (183 loc) · 4.47 KB
/
examples_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package mktree
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/unix"
)
func Example_dir() {
interpret(`(dir "test")`)
// Output:
// drwxrwxrwx [example]
// drwxrwxrwx [example]/test
}
func Example_file() {
interpret(`(file "test")`)
// Output:
// drwxrwxrwx [example]
// -rw-rw-rw- [example]/test 0
}
func Example_perms() {
interpret(`
(dir "a" (@perms 0700))
(file "b" (@perms 0755))
`)
// Output:
// drwxrwxrwx [example]
// drwx------ [example]/a
// -rwxr-xr-x [example]/b 0
}
func Example_tree_with_perms() {
interpret(`
(dir "a"
(@perms 0700)
(file "b"
(@perms 0755)))
`)
// Output:
// drwxrwxrwx [example]
// drwx------ [example]/a
// -rwxr-xr-x [example]/a/b 0
}
func Example_vars() {
interpretVars(`
(dir "%(dirname)" (@perms %(perms)))
(file "%(filename)" (@perms %(perms)))
(dir "%(root_dir)")
`, map[string]string{
"dirname": "a",
"filename": "b.txt",
"perms": "0555",
})
// Output:
// drwxrwxrwx [example]
// dr-xr-xr-x [example]/a
// drwxrwxrwx [example]/[example]
// -r-xr-xr-x [example]/b.txt 0
}
func Example_parent_dirs() {
interpret(`(file "/a/b/c/d/e.txt")`)
// Output:
// drwxrwxrwx [example]
// -rw-rw-rw- [example]/a/b/c/d/e.txt 0
}
func interpret(source string) {
interpretVars(source, nil)
}
func interpretVars(source string, vars map[string]string) {
i := &Interpreter{
Vars: vars,
Root: "[example]",
}
dir, err := i.InterpretFile(strings.NewReader(source), "")
if err != nil {
panic(err)
}
dir.DebugPrint(os.Stdout)
}
// Executes examples/docs/examples.tree and asserts the output is as expected.
func TestExamples(t *testing.T) {
root, err := ioutil.TempDir("", "mktree")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
i := &Interpreter{
Root: root,
Vars: map[string]string{"my_var": "test"},
}
opts := []Option{
WithTemplateFunction("FileExists", func(_ string) bool { return false }),
WithTemplateFunction("FileContents", func(_ string) string { return "contents" }),
WithTemplateFunction("Now", func() string { return "2022-03-01" }),
WithTemplateFunction("User", func() string { return "test" }),
}
if err := i.ExecFile(nil, "examples/docs/examples.tree", opts...); err != nil {
t.Fatal(err)
}
assertDir(t, filepath.Join(root, "example"), defaultDirMode)
assertFile(t, filepath.Join(root, "original.txt"), defaultFileMode, "")
assertLink(t, filepath.Join(root, "original.txt"), filepath.Join(root, "symbolic.txt"))
assertFile(t, filepath.Join(root, "example.txt"), os.FileMode(0667), "")
assertFile(t, filepath.Join(root, "template_example.txt"), defaultFileMode, strings.TrimSpace(`
[start:now_example]
The current time is 2022-03-01
[end:now_example]
[start:user_example]
The current user is test
[end:user_example]
[start:file_contents_example]
The file "VERSION" contains "contents"
[end:file_contents_example]
[start:file_exists_example]
The file "missing.txt" does not exist
[end:file_exists_example]
[start:var_example]
%(my_var) = test
[end:var_example]
[start:year_example]
The current year is 2022
[end:year_example]
`))
}
func assertDir(t *testing.T, name string, mode os.FileMode) {
t.Helper()
stat, err := os.Stat(name)
if err != nil {
t.Fatal(err)
}
if !stat.IsDir() {
t.Fatalf("not a directory: %s", name)
}
umask := unix.Umask(0)
defer unix.Umask(umask)
// mode = os.FileMode(uint32(umask) ^ uint32(mode))
// fmt.Printf("umask=%#o, mode=%#o\n", umask, mode)
if mode != stat.Mode() {
t.Fatalf("expected dir mode %#o but got %#o", mode, stat.Mode())
}
}
func assertFile(t *testing.T, name string, mode os.FileMode, contents string) {
t.Helper()
stat, err := os.Stat(name)
if err != nil {
t.Fatal(err)
}
if stat.IsDir() {
t.Fatalf("%s is a directory", name)
}
if stat.Mode() != mode {
t.Fatalf("expected file mode %#o but got %#o", mode, stat.Mode())
}
got, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(got), contents); diff != "" {
t.Fatalf("got contents diff (+got,-want):\n%s\n", diff)
}
}
func assertLink(t *testing.T, target, name string) {
t.Helper()
stat, err := os.Lstat(name)
if err != nil {
t.Fatal(err)
}
if stat.IsDir() {
t.Fatalf("%s is a directory", name)
}
gotTarget, err := filepath.EvalSymlinks(name)
if err != nil {
t.Fatal(err)
}
wantTarget, err := filepath.EvalSymlinks(target)
if err != nil {
t.Fatal(err)
}
if gotTarget != wantTarget {
t.Fatalf("got target\n%q\nbut wanted\n%q\n", gotTarget, wantTarget)
}
}