forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
154 lines (124 loc) · 3.25 KB
/
files_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
// Copyright (c) 2019 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
package util
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func inTempDir(fun func(dir string)) error {
curdir, err := os.Getwd()
if err != nil {
return err
}
dir, err := ioutil.TempDir("", "awgo-util-")
if err != nil {
return err
}
// TempDir() returns a symlink on my macOS :(
if dir, err = filepath.EvalSymlinks(dir); err != nil {
return err
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}()
// Change to temporary directory
if err := os.Chdir(dir); err != nil {
return err
}
// Change back after we're done
defer func() {
if err := os.Chdir(curdir); err != nil {
panic(err)
}
}()
fun(dir)
return nil
}
func TestMustExist(t *testing.T) {
err := inTempDir(func(dir string) {
name := "testdir"
// Create directory
s := MustExist(name)
assert.Equal(t, name, s, "unexpected dirname")
_, err := os.Stat(s)
assert.Nil(t, err, "stat dir failed")
// Check path is as expected
p := filepath.Join(dir, name)
p2, err := filepath.Abs(s)
require.Nil(t, err, "filepath.Abs failed")
assert.Equal(t, p, p2, "unexpected path")
})
require.Nil(t, err, "inTempDir failed")
}
func TestPathExists(t *testing.T) {
t.Parallel()
err := inTempDir(func(dir string) {
name := "existingdir"
path := filepath.Join(dir, name)
badName := "nodir"
badPath := filepath.Join(dir, badName)
require.Nil(t, os.MkdirAll(name, 0700), "MkdirAll failed")
data := []struct {
p string
x bool
}{
{dir, true},
{name, true},
{path, true},
{badName, false},
{badPath, false},
}
for _, td := range data {
assert.Equal(t, td.x, PathExists(td.p), "unexpected result")
}
})
require.Nil(t, err, "inTempDir failed")
}
func TestClearDirectory(t *testing.T) {
err := inTempDir(func(dir string) {
names := []string{"./root/one", "./root/two", "./root/three"}
for _, s := range names {
require.Nil(t, os.MkdirAll(s, 0700), "MkdirAll failed")
}
for _, s := range names {
_, err := os.Stat(s)
assert.Nil(t, err, "stat failed")
}
assert.Nil(t, ClearDirectory("./root"), "ClearDirectory failed")
for _, s := range names {
s := s
t.Run(s, func(t *testing.T) {
_, err := os.Stat(s)
assert.True(t, os.IsNotExist(err), "file exists")
})
}
})
require.Nil(t, err, "inTempDir failed")
}
func TestWriteFile(t *testing.T) {
err := inTempDir(func(dir string) {
var (
name = "test.txt"
content = []byte(`test`)
)
require.False(t, PathExists(name), "path already exists")
require.Nil(t, WriteFile(name, content, 0600), "WriteFile failed")
require.True(t, PathExists(name), "path does not exist")
data, err := ioutil.ReadFile(name)
require.Nil(t, err, "read file failed")
assert.Equal(t, content, data, "unexpected file content")
fi, err := os.Stat(name)
require.Nil(t, err, "stat file failed")
assert.Equal(t, os.FileMode(0600), fi.Mode(), "unexpected file mode")
infos, err := ioutil.ReadDir(".")
require.Nil(t, err, "ReadDir failed")
assert.Equal(t, 1, len(infos), "unexpected no. of files")
})
require.Nil(t, err, "inTempDir failed")
}