-
Notifications
You must be signed in to change notification settings - Fork 89
/
file_test.go
269 lines (249 loc) · 7.06 KB
/
file_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 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/utils/v4"
)
type fileSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&fileSuite{})
func (*fileSuite) TestNormalizePath(c *gc.C) {
home := filepath.FromSlash(c.MkDir())
err := utils.SetHome(home)
c.Assert(err, gc.IsNil)
// TODO (frankban) bug 1324841: improve the isolation of this suite.
currentUser, err := user.Current()
c.Assert(err, gc.IsNil)
for i, test := range []struct {
path string
expected string
err string
}{{
path: filepath.FromSlash("/var/lib/juju"),
expected: filepath.FromSlash("/var/lib/juju"),
}, {
path: "~/foo",
expected: filepath.Join(home, "foo"),
}, {
path: "~/foo//../bar",
expected: filepath.Join(home, "bar"),
}, {
path: "~",
expected: home,
}, {
path: "~" + currentUser.Username,
expected: currentUser.HomeDir,
}, {
path: "~" + currentUser.Username + "/foo",
expected: filepath.Join(currentUser.HomeDir, "foo"),
}, {
path: "~" + currentUser.Username + "/foo//../bar",
expected: filepath.Join(currentUser.HomeDir, "bar"),
}, {
path: filepath.FromSlash("foo~bar/baz"),
expected: filepath.FromSlash("foo~bar/baz"),
}, {
path: "~foobar/path",
err: ".*" + utils.NoSuchUserErrRegexp,
}} {
c.Logf("test %d: %s", i, test.path)
actual, err := utils.NormalizePath(test.path)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
c.Check(actual, gc.Equals, test.expected)
}
}
}
func (*fileSuite) TestExpandPath(c *gc.C) {
home := filepath.FromSlash(c.MkDir())
err := utils.SetHome(home)
c.Assert(err, gc.IsNil)
currentUser, err := user.Current()
c.Assert(err, gc.IsNil)
cwd, err := os.Getwd()
c.Assert(err, gc.IsNil)
for i, test := range []struct {
path string
expected string
err string
}{{
path: filepath.FromSlash("/var/lib/juju"),
expected: filepath.FromSlash("/var/lib/juju"),
}, {
path: "~/foo",
expected: filepath.Join(home, "foo"),
}, {
path: "~/foo//../bar",
expected: filepath.Join(home, "bar"),
}, {
path: "~",
expected: home,
}, {
path: "~" + currentUser.Username,
expected: currentUser.HomeDir,
}, {
path: "~" + currentUser.Username + "/foo",
expected: filepath.Join(currentUser.HomeDir, "foo"),
}, {
path: "~" + currentUser.Username + "/foo//../bar",
expected: filepath.Join(currentUser.HomeDir, "bar"),
}, {
path: filepath.FromSlash("foo~bar/baz"),
expected: filepath.Join(cwd, "foo~bar/baz"),
}, {
path: filepath.FromSlash("foo/bar"),
expected: filepath.Join(cwd, "foo", "bar"),
}, {
path: filepath.FromSlash("foo/../bar"),
expected: filepath.Join(cwd, "bar"),
}, {
path: filepath.FromSlash("foo/./bar"),
expected: filepath.Join(cwd, "foo", "bar"),
}, {
path: "~foobar/path",
err: ".*" + utils.NoSuchUserErrRegexp,
}} {
c.Logf("test %d: %s", i, test.path)
actual, err := utils.ExpandPath(test.path)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
c.Check(actual, gc.Equals, test.expected)
c.Check(filepath.IsAbs(actual), jc.IsTrue)
}
}
}
func (*fileSuite) TestCopyFile(c *gc.C) {
dir := c.MkDir()
f, err := ioutil.TempFile(dir, "source")
c.Assert(err, gc.IsNil)
defer f.Close()
_, err = f.Write([]byte("hello world"))
c.Assert(err, gc.IsNil)
dest := filepath.Join(dir, "dest")
err = utils.CopyFile(dest, f.Name())
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(dest)
c.Assert(err, gc.IsNil)
c.Assert(string(data), gc.Equals, "hello world")
}
var atomicWriteFileTests = []struct {
summary string
change func(filename string, contents []byte) error
check func(c *gc.C, fileInfo os.FileInfo)
expectErr string
}{{
summary: "atomic file write and chmod 0644",
change: func(filename string, contents []byte) error {
return utils.AtomicWriteFile(filename, contents, 0765)
},
check: func(c *gc.C, fi os.FileInfo) {
c.Assert(fi.Mode(), gc.Equals, 0765)
},
}, {
summary: "atomic file write and change",
change: func(filename string, contents []byte) error {
chmodChange := func(f string) error {
// FileMod.Chmod() is not implemented on Windows, however, os.Chmod() is
return os.Chmod(f, 0700)
}
return utils.AtomicWriteFileAndChange(filename, contents, chmodChange)
},
check: func(c *gc.C, fi os.FileInfo) {
c.Assert(fi.Mode(), gc.Equals, 0700)
},
}, {
summary: "atomic file write empty contents",
change: func(filename string, contents []byte) error {
nopChange := func(string) error {
return nil
}
return utils.AtomicWriteFileAndChange(filename, contents, nopChange)
},
}, {
summary: "atomic file write and failing change func",
change: func(filename string, contents []byte) error {
errChange := func(string) error {
return fmt.Errorf("pow!")
}
return utils.AtomicWriteFileAndChange(filename, contents, errChange)
},
expectErr: "pow!",
}}
func (*fileSuite) TestAtomicWriteFile(c *gc.C) {
dir := c.MkDir()
name := "test.file"
path := filepath.Join(dir, name)
assertDirContents := func(names ...string) {
fis, err := ioutil.ReadDir(dir)
c.Assert(err, gc.IsNil)
c.Assert(fis, gc.HasLen, len(names))
for i, name := range names {
c.Assert(fis[i].Name(), gc.Equals, name)
}
}
assertNotExist := func(path string) {
_, err := os.Lstat(path)
c.Assert(err, jc.Satisfies, os.IsNotExist)
}
for i, test := range atomicWriteFileTests {
c.Logf("test %d: %s", i, test.summary)
// First - test with file not already there.
assertDirContents()
assertNotExist(path)
contents := []byte("some\ncontents")
err := test.change(path, contents)
if test.expectErr == "" {
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(path)
c.Assert(err, gc.IsNil)
c.Assert(data, jc.DeepEquals, contents)
assertDirContents(name)
} else {
c.Assert(err, gc.ErrorMatches, test.expectErr)
assertDirContents()
continue
}
// Second - test with a file already there.
contents = []byte("new\ncontents")
err = test.change(path, contents)
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(path)
c.Assert(err, gc.IsNil)
c.Assert(data, jc.DeepEquals, contents)
assertDirContents(name)
// Remove the file to reset scenario.
c.Assert(os.Remove(path), gc.IsNil)
}
}
func (*fileSuite) TestMoveFile(c *gc.C) {
d := c.MkDir()
dest := filepath.Join(d, "foo")
f1Name := filepath.Join(d, ".foo1")
f2Name := filepath.Join(d, ".foo2")
err := ioutil.WriteFile(f1Name, []byte("macaroni"), 0644)
c.Assert(err, gc.IsNil)
err = ioutil.WriteFile(f2Name, []byte("cheese"), 0644)
c.Assert(err, gc.IsNil)
ok, err := utils.MoveFile(f1Name, dest)
c.Assert(ok, gc.Equals, true)
c.Assert(err, gc.IsNil)
ok, err = utils.MoveFile(f2Name, dest)
c.Assert(ok, gc.Equals, false)
c.Assert(err, gc.NotNil)
contents, err := ioutil.ReadFile(dest)
c.Assert(err, gc.IsNil)
c.Assert(contents, gc.DeepEquals, []byte("macaroni"))
}