forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempdir.go
72 lines (64 loc) · 1.52 KB
/
tempdir.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
package tempdir
import (
"io/ioutil"
"os"
"path"
"testing"
)
type Dir struct {
Path string
t testing.TB
}
func (d Dir) Cleanup() {
err := os.RemoveAll(d.Path)
if err != nil {
d.t.Errorf("tempdir cleanup failed: %v", err)
}
}
// Check whether the given directory is empty. Marks test failed on
// problems, and on any files seen.
func (d Dir) CheckEmpty() {
f, err := os.Open(d.Path)
if err != nil {
d.t.Errorf("Cannot open temp directory: %v", err)
return
}
junk, err := f.Readdirnames(-1)
if err != nil {
d.t.Errorf("Cannot list temp directory: %v", err)
return
}
if len(junk) != 0 {
d.t.Errorf("Temp directory has unexpected junk: %v", junk)
return
}
}
// Utility function to create a new subdirectory. This can be used to
// have more predictable path names in a test that needs multiple temp
// directories: only make the top one have a random name, name
// subdirectories by their role in the test.
//
// Name must be a valid single path segment, no slashes.
//
// Returns an absolute path.
func (d Dir) Subdir(name string) string {
p := path.Join(d.Path, name)
err := os.Mkdir(p, 0700)
if err != nil {
d.t.Fatal("cannot create subdir of temp dir: %v", err)
}
return p
}
func New(t testing.TB) Dir {
parent := ""
// if we are running under "go test", use its temp dir
arg0 := path.Dir(os.Args[0])
if path.Base(arg0) == "_test" {
parent = arg0
}
dir, err := ioutil.TempDir(parent, "temp-")
if err != nil {
t.Fatalf("cannot create temp directory: %v", err)
}
return Dir{Path: dir, t: t}
}