-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp_dir_test.go
269 lines (212 loc) · 7.07 KB
/
temp_dir_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2017-2019 Vlad Didenko. All rights reserved.
// See the included LICENSE.md file for licensing information
package fst // import "go.didenko.com/fst"
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
func ExampleTempInitDir() {
lg := log.New(os.Stderr, "ExampleTempInitDir", log.LUTC|log.Ldate|log.Ltime)
root, cleanup := TempInitDir(lg)
defer cleanup()
fmt.Printf("Here goes the code using the temporary directory at %s\n", root)
}
func TestTempInitDir(t *testing.T) {
// Get the values and create the test root dir to be tested
testRootDir, cleanup := TempInitDir(t)
// Check that the returned testRootDir is Stat-able
testRootInfo, err := os.Stat(testRootDir)
if err != nil {
t.Fatalf("While learning about the temporary directory %q: %s", testRootDir, err)
}
// Check that the returned testRootDir is a directory
if !testRootInfo.IsDir() {
t.Errorf("Returned temporary path \"%s\" is not a directory", testRootDir)
}
// run the resulting cleaup function for tests below
cleanup()
// Check that the testRootDir does not exist after the cleanup
_, err = os.Stat(testRootDir)
_, ok := err.(*os.PathError)
if err == nil && !ok {
t.Fatalf("Temporary directory \"%s\" remained after cleanup", testRootDir)
}
}
func TestTempInitChdir(t *testing.T) {
// Capture the old workdir
origWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
// Get the values and create the test root dir to be tested
old, cleanup := TempInitChdir(t)
if origWD != old {
t.Fatalf("Got \"%s\" as an old directory instead of the expected \"%s\"\n", old, origWD)
}
// Capture the temporary workdir
tempWD, err := os.Getwd()
if err != nil {
t.Fatalf("Workind directory is indetermined: %s", err)
}
// Check that we are in an empty directory
files, err := ioutil.ReadDir(".")
if err != nil {
t.Fatal(err)
}
if len(files) > 0 {
t.Fatalf("The current, supposedly new, directory \"%s\" is not empty\n", tempWD)
}
// run the resulting cleaup function for tests below
cleanup()
// Check that we returned into the original directory after the cleanup
currWD, err := os.Getwd()
if err != nil {
t.Fatalf("Learning about the original directory after the tests: %s", err)
}
if currWD != origWD {
t.Fatalf("Expected to return to the \"%s\" directory after the cleanup. Instead we are in \"%s\"\n", origWD, currWD)
}
}
func TestTempInitDirRestrictedPermissions(t *testing.T) {
root, cleanup := TempInitDir(t)
d := filepath.Join(root, "d")
err := os.Mkdir(d, 0700)
if err != nil {
t.Fatalf("Creating a test directory %q: %s", d, err)
}
f := filepath.Join(d, "f")
err = ioutil.WriteFile(f, []byte{}, 0700)
if err != nil {
t.Fatalf("Creating a test directory %q: %s", f, err)
}
// Set fully restricted permissions on the file and directory
// so that it is clear that the cleanup removes them
err = os.Chmod(f, 0)
if err != nil {
t.Fatalf("Permissions on a test directory %q: %s", f, err)
}
err = os.Chmod(d, 0)
if err != nil {
t.Fatalf("Permissions on a test directory %q: %s", d, err)
}
// run the resulting cleaup function for tests below
cleanup()
// Check that the testRootDir does not exist after the cleanup
_, err = os.Stat(root)
_, ok := err.(*os.PathError)
if !ok {
t.Fatalf("Temporary directory \"%s\" remained after cleanup", root)
}
}
func ExampleTempCloneDir() {
lg := log.New(os.Stderr, "ExampleTempCloneDir", log.LUTC|log.Ldate|log.Ltime)
root, cleanup := TempCloneDir(lg, "./mock")
defer cleanup()
fmt.Printf("Here goes the code using the temporary directory at %s\n", root)
}
func TestTempCloneDir(t *testing.T) {
const src string = "./testdata/temp_dir_mocks"
// Get the values and create the test root dir to be tested
testRootDir, cleanup := TempCloneDir(t, src)
// Check that the returned testRootDir is Stat-able
testRootInfo, err := os.Stat(testRootDir)
if err != nil {
t.Fatal(err)
}
// Check that the returned testRootDir is a directory
if !testRootInfo.IsDir() {
t.Errorf("Returned temporary path \"%s\" is not a directory", testRootDir)
}
diffs := TreeDiff(t, src, testRootDir, ByName, ByDir, BySize, ByPerm, ByTime, ByContent(t))
if diffs != nil {
t.Errorf("Trees at \"%s\" and \"%s\" differ unexpectedly: %v", src, testRootDir, diffs)
}
// run the resulting cleaup function for tests below
cleanup()
// Check that the testRootDir does not exist after the cleanup
_, err = os.Stat(testRootDir)
_, ok := err.(*os.PathError)
if !ok {
t.Fatalf("Cloned directory \"%s\" remained after cleanup", testRootDir)
}
}
func TestTempCloneChdir(t *testing.T) {
// Capture the old workdir
origWD, err := os.Getwd()
if err != nil {
t.Fatalf("Original working directory is indetermined: %s", err)
}
// Get the values and create the test root dir to be tested
src := "./testdata/temp_dir_mocks"
// Get the values and clone the test root dir to be tested
old, cleanup := TempCloneChdir(t, src)
if origWD != old {
t.Fatalf("Got \"%s\" as an old directory instead of the expected \"%s\"\n", old, origWD)
}
// Capture the temporary workdir
tempWD, err := os.Getwd()
if err != nil {
t.Fatalf("Temporary working directory is indetermined: %s", err)
}
src = filepath.Join(origWD, src)
diffs := TreeDiff(t, src, tempWD, ByName, ByDir, BySize, ByPerm, ByTime, ByContent(t))
if diffs != nil {
t.Errorf("Trees at \"%s\" and \"%s\" differ unexpectedly: %v", src, tempWD, diffs)
}
// run the resulting cleaup function for tests below
cleanup()
// Check that we returned into the original directory after the cleanup
currWD, err := os.Getwd()
if err != nil {
t.Fatalf("Original working directory is indetermined after cleanup: %s", err)
}
if currWD != origWD {
t.Fatalf("Expected to return to the \"%s\" directory after the cleanup. Instead we are in \"%s\"\n", origWD, currWD)
}
}
func ExampleTempCreateChdir() {
dirMark := func(fi os.FileInfo) string {
if fi.IsDir() {
return "/"
}
return ""
}
lg := log.New(os.Stderr, "ExampleTempCreateChdir", log.LUTC|log.Ldate|log.Ltime)
nodes := []*Node{
&Node{0750, Rfc3339(lg, "2001-01-01T01:01:01Z"), "a/", ""},
&Node{0750, Rfc3339(lg, "2001-01-01T01:01:01Z"), "b/", ""},
&Node{0700, Rfc3339(lg, "2001-01-01T01:01:01Z"), "c.txt", "This is a two line\nfile with\ta tab\n"},
&Node{0700, Rfc3339(lg, "2001-01-01T01:01:01Z"), "d.txt", "A single line without tabs"},
&Node{0700, Rfc3339(lg, "2002-01-01T01:01:01Z"), "has\ttab/", ""},
&Node{0700, Rfc3339(lg, "2002-01-01T01:01:01Z"), "has\ttab/e.mb", "# Markdown...\n\n... also ***possible***\n"},
&Node{0700, Rfc3339(lg, "2002-01-01T01:01:01Z"), "\u263asmiles\u263a/", ""},
}
_, cleanup := TempCreateChdir(lg, nodes)
defer cleanup()
files, err := ioutil.ReadDir(".")
if err != nil {
log.Printf("%v\n", err)
return
}
fmt.Printf(
"%v | %v | %s%s\n",
files[1].ModTime().UTC(),
files[1].Mode().Perm(),
files[1].Name(),
dirMark(files[1]),
)
fmt.Printf(
"%v | %v | %s%s\n",
files[2].ModTime().UTC(),
files[2].Mode().Perm(),
files[2].Name(),
dirMark(files[2]),
)
// Output:
// 2001-01-01 01:01:01 +0000 UTC | -rwxr-x--- | b/
// 2001-01-01 01:01:01 +0000 UTC | -rwx------ | c.txt
}