-
Notifications
You must be signed in to change notification settings - Fork 290
/
temp_dir_fixture.go
138 lines (119 loc) · 2.8 KB
/
temp_dir_fixture.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
package tempdir
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/windmilleng/wmclient/pkg/os/temp"
)
type TempDirFixture struct {
t testing.TB
dir *temp.TempDir
}
func NewTempDirFixture(t testing.TB) *TempDirFixture {
name := t.Name()
name = strings.Replace(name, "/", "-", -1)
dir, err := temp.NewDir(name)
if err != nil {
t.Fatalf("Error making temp dir: %v", err)
}
return &TempDirFixture{
t: t,
dir: dir,
}
}
func (f *TempDirFixture) T() testing.TB {
return f.t
}
func (f *TempDirFixture) Path() string {
return f.dir.Path()
}
func (f *TempDirFixture) JoinPath(path ...string) string {
p := []string{}
isAbs := len(path) > 0 && filepath.IsAbs(path[0])
if isAbs {
if !strings.HasPrefix(path[0], f.Path()) {
f.t.Fatalf("Path outside fixture tempdir are forbidden: %s", path[0])
}
} else {
p = append(p, f.Path())
}
p = append(p, path...)
return filepath.Join(p...)
}
func (f *TempDirFixture) JoinPaths(paths []string) []string {
joined := make([]string, len(paths))
for i, p := range paths {
joined[i] = f.JoinPath(p)
}
return joined
}
// Returns the full path to the file written.
func (f *TempDirFixture) WriteFile(path string, contents string) string {
fullPath := f.JoinPath(path)
base := filepath.Dir(fullPath)
err := os.MkdirAll(base, os.FileMode(0777))
if err != nil {
f.t.Fatal(err)
}
err = ioutil.WriteFile(fullPath, []byte(contents), os.FileMode(0777))
if err != nil {
f.t.Fatal(err)
}
return fullPath
}
// Returns the full path to the file written.
func (f *TempDirFixture) CopyFile(originalPath, newPath string) {
contents, err := ioutil.ReadFile(originalPath)
if err != nil {
f.t.Fatal(err)
}
f.WriteFile(newPath, string(contents))
}
func (f *TempDirFixture) WriteSymlink(linkContents, destPath string) {
fullDestPath := f.JoinPath(destPath)
err := os.MkdirAll(filepath.Dir(fullDestPath), os.FileMode(0777))
if err != nil {
f.t.Fatal(err)
}
err = os.Symlink(linkContents, fullDestPath)
if err != nil {
f.t.Fatal(err)
}
}
func (f *TempDirFixture) MkdirAll(path string) {
fullPath := f.JoinPath(path)
err := os.MkdirAll(fullPath, os.FileMode(0777))
if err != nil {
f.t.Fatal(err)
}
}
func (f *TempDirFixture) TouchFiles(paths []string) {
for _, p := range paths {
f.WriteFile(p, "")
}
}
func (f *TempDirFixture) Rm(pathInRepo string) {
fullPath := f.JoinPath(pathInRepo)
err := os.RemoveAll(fullPath)
if err != nil {
f.t.Fatal(err)
}
}
func (f *TempDirFixture) NewFile(prefix string) (*os.File, error) {
return ioutil.TempFile(f.dir.Path(), prefix)
}
func (f *TempDirFixture) TempDir(prefix string) string {
name, err := ioutil.TempDir(f.dir.Path(), prefix)
if err != nil {
f.t.Fatal(err)
}
return name
}
func (f *TempDirFixture) TearDown() {
err := f.dir.TearDown()
if err != nil {
f.t.Fatal(err)
}
}