-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.go
59 lines (44 loc) · 959 Bytes
/
filesystem.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
package test
import (
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
)
func Cd(t tester, name string, runTests func()) {
t.Helper()
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Capture current working directory failed: %v", err)
}
oldpath := cwd
newpath := ExpandPath(t, name)
os.Chdir(newpath)
runTests()
os.Chdir(oldpath)
}
func CreateEmptyFile(t tester, name string) {
CreateFile(t, name, "")
}
func CreateFile(t tester, name string, content string) {
t.Helper()
var perms os.FileMode
path := ExpandPath(t, name)
perms = 0644
logrus.WithFields(logrus.Fields{
"path": path,
"perms": perms,
}).Debug("Created file")
err := ioutil.WriteFile(path, []byte(content), perms)
if err != nil {
t.Fatalf("Write path %s failed: %v", path, err)
}
}
func IsFile(t testHelper, name string) bool {
t.Helper()
path := ExpandPath(t, name)
_, err := os.Stat(path)
if !os.IsNotExist(err) {
return true
}
return false
}