Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: go/src/infra/tools/cipd/local/fs_test.go

Issue 1154423015: cipd: Extract high level file system operations into the separate interface. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/tools/cipd/local/fs.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package local
6
7 import (
8 "io/ioutil"
9 "os"
10 "path/filepath"
11 "testing"
12
13 . "github.com/smartystreets/goconvey/convey"
14 )
15
16 func TestToAbsPath(t *testing.T) {
17 Convey("ToAbsPath works", t, func() {
18 fs := tempFileSystem()
19 p, err := fs.ToAbsPath(fs.Root())
20 So(err, ShouldBeNil)
21 So(p, ShouldEqual, fs.Root())
22
23 p, err = fs.ToAbsPath(fs.join("abc"))
24 So(err, ShouldBeNil)
25 So(p, ShouldEqual, fs.join("abc"))
26
27 _, err = fs.ToAbsPath(fs.join(".."))
28 So(err, ShouldNotBeNil)
29
30 _, err = fs.ToAbsPath(fs.join("../.."))
31 So(err, ShouldNotBeNil)
32
33 _, err = fs.ToAbsPath(fs.join("../abc"))
34 So(err, ShouldNotBeNil)
35 })
36 }
37
38 func TestEnsureDirectory(t *testing.T) {
39 Convey("EnsureDirectory checks root", t, func() {
40 fs := tempFileSystem()
41 _, err := fs.EnsureDirectory(fs.join(".."))
42 So(err, ShouldNotBeNil)
43 })
44
45 Convey("EnsureDirectory works", t, func() {
46 fs := tempFileSystem()
47 p, err := fs.EnsureDirectory(fs.join("x/../y/z"))
48 So(err, ShouldBeNil)
49 So(p, ShouldEqual, fs.join("y/z"))
50 So(fs.isDir("y/z"), ShouldBeTrue)
51 // Same one.
52 _, err = fs.EnsureDirectory(fs.join("x/../y/z"))
53 So(err, ShouldBeNil)
54 })
55 }
56
57 func TestEnsureSymlink(t *testing.T) {
58 Convey("EnsureSymlink checks root", t, func() {
59 fs := tempFileSystem()
60 So(fs.EnsureSymlink(fs.join(".."), fs.Root()), ShouldNotBeNil)
61 })
62
63 Convey("ensureSymlink creates new symlink", t, func() {
64 fs := tempFileSystem()
65 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil)
66 So(fs.readLink("symlink"), ShouldEqual, "target")
67 })
68
69 Convey("ensureSymlink builds full path", t, func() {
70 fs := tempFileSystem()
71 So(fs.EnsureSymlink(fs.join("a/b/c"), "target"), ShouldBeNil)
72 So(fs.readLink("a/b/c"), ShouldEqual, "target")
73 })
74
75 Convey("ensureSymlink replaces existing one", t, func() {
76 fs := tempFileSystem()
77 // Replace with same one, then with another one.
78 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil)
79 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil)
80 So(fs.EnsureSymlink(fs.join("symlink"), "another"), ShouldBeNil)
81 So(fs.readLink("symlink"), ShouldEqual, "another")
82 })
83 }
84
85 func TestEnsureFileGone(t *testing.T) {
86 Convey("EnsureFileGone checks root", t, func() {
87 fs := tempFileSystem()
88 So(fs.EnsureFileGone(fs.join("../abc")), ShouldNotBeNil)
89 })
90
91 Convey("EnsureFileGone works", t, func() {
92 fs := tempFileSystem()
93 fs.write("abc", "")
94 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil)
95 So(fs.isMissing("abc"), ShouldBeTrue)
96 })
97
98 Convey("EnsureFileGone works with missing file", t, func() {
99 fs := tempFileSystem()
100 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil)
101 })
102
103 Convey("EnsureFileGone works with symlink", t, func() {
104 fs := tempFileSystem()
105 So(fs.EnsureSymlink(fs.join("abc"), "target"), ShouldBeNil)
106 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil)
107 So(fs.isMissing("abc"), ShouldBeTrue)
108 })
109 }
110
111 func TestEnsureDirectoryGone(t *testing.T) {
112 Convey("EnsureDirectoryGone checks root", t, func() {
113 fs := tempFileSystem()
114 So(fs.EnsureDirectoryGone(fs.join("../abc")), ShouldNotBeNil)
115 })
116
117 Convey("EnsureDirectoryGone works", t, func() {
118 fs := tempFileSystem()
119 fs.write("dir/a/1", "")
120 fs.write("dir/a/2", "")
121 fs.write("dir/b/1", "")
122 So(fs.EnsureDirectoryGone(fs.join("dir")), ShouldBeNil)
123 So(fs.isMissing("dir"), ShouldBeTrue)
124 })
125
126 Convey("EnsureDirectoryGone works with missing dir", t, func() {
127 fs := tempFileSystem()
128 So(fs.EnsureDirectoryGone(fs.join("missing")), ShouldBeNil)
129 })
130
131 }
132
133 func TestReplace(t *testing.T) {
134 Convey("Replace checks root", t, func() {
135 fs := tempFileSystem()
136 So(fs.Replace(fs.join("../abc"), fs.join("def")), ShouldNotBeNil )
137 fs.write("def", "")
138 So(fs.Replace(fs.join("def"), fs.join("../abc")), ShouldNotBeNil )
139 })
140
141 Convey("Replace does nothing if oldpath == newpath", t, func() {
142 fs := tempFileSystem()
143 So(fs.Replace(fs.join("abc"), fs.join("abc/d/..")), ShouldBeNil)
144 })
145
146 Convey("Replace recognizes missing oldpath", t, func() {
147 fs := tempFileSystem()
148 So(fs.Replace(fs.join("missing"), fs.join("abc")), ShouldNotBeNi l)
149 })
150
151 Convey("Replace creates file and dir if missing", t, func() {
152 fs := tempFileSystem()
153 fs.write("a/123", "")
154 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
155 So(fs.isMissing("a/123"), ShouldBeTrue)
156 So(fs.isFile("b/c/d/"), ShouldBeTrue)
157 })
158
159 Convey("Replace replaces regular file with a file", t, func() {
160 fs := tempFileSystem()
161 fs.write("a/123", "xxx")
162 fs.write("b/c/d", "yyy")
163 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
164 So(fs.isMissing("a/123"), ShouldBeTrue)
165 So(fs.read("b/c/d"), ShouldEqual, "xxx")
166 })
167
168 Convey("Replace replaces regular file with a dir", t, func() {
169 fs := tempFileSystem()
170 fs.write("a/123/456", "xxx")
171 fs.write("b/c/d", "yyy")
172 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
173 So(fs.isMissing("a/123"), ShouldBeTrue)
174 So(fs.read("b/c/d/456"), ShouldEqual, "xxx")
175 })
176
177 Convey("Replace replaces empty dir with a file", t, func() {
178 fs := tempFileSystem()
179 fs.write("a/123", "xxx")
180 _, err := fs.EnsureDirectory(fs.join("b/c/d"))
181 So(err, ShouldBeNil)
182 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
183 So(fs.isMissing("a/123"), ShouldBeTrue)
184 So(fs.read("b/c/d"), ShouldEqual, "xxx")
185 })
186
187 Convey("Replace replaces empty dir with a dir", t, func() {
188 fs := tempFileSystem()
189 fs.write("a/123/456", "xxx")
190 _, err := fs.EnsureDirectory(fs.join("b/c/d"))
191 So(err, ShouldBeNil)
192 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
193 So(fs.isMissing("a/123"), ShouldBeTrue)
194 So(fs.read("b/c/d/456"), ShouldEqual, "xxx")
195 })
196
197 Convey("Replace replaces dir with a file", t, func() {
198 fs := tempFileSystem()
199 fs.write("a/123", "xxx")
200 fs.write("b/c/d/456", "yyy")
201 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
202 So(fs.isMissing("a/123"), ShouldBeTrue)
203 So(fs.read("b/c/d"), ShouldEqual, "xxx")
204 })
205
206 Convey("Replace replaces dir with a dir", t, func() {
207 fs := tempFileSystem()
208 fs.write("a/123/456", "xxx")
209 fs.write("b/c/d/456", "yyy")
210 So(fs.Replace(fs.join("a/123"), fs.join("b/c/d")), ShouldBeNil)
211 So(fs.isMissing("a/123"), ShouldBeTrue)
212 So(fs.read("b/c/d/456"), ShouldEqual, "xxx")
213 })
214 }
215
216 ///////
217
218 // tempFileSystem returns FileSystem for tests built over a temp directory.
219 func tempFileSystem() *tempFileSystemImpl {
220 tempDir, err := ioutil.TempDir("", "cipd_test")
221 So(err, ShouldBeNil)
222 Reset(func() { os.RemoveAll(tempDir) })
223 return &tempFileSystemImpl{NewFileSystem(tempDir, nil)}
224 }
225
226 type tempFileSystemImpl struct {
227 FileSystem
228 }
229
230 // join returns absolute path given a slash separated path relative to Root().
231 func (f *tempFileSystemImpl) join(path string) string {
232 return filepath.Join(f.Root(), filepath.FromSlash(path))
233 }
234
235 // write creates a file at a given slash separated path relative to Root().
236 func (f *tempFileSystemImpl) write(rel string, data string) {
237 abs := f.join(rel)
238 err := os.MkdirAll(filepath.Dir(abs), 0777)
239 So(err, ShouldBeNil)
240 file, err := os.Create(abs)
241 So(err, ShouldBeNil)
242 file.WriteString(data)
243 file.Close()
244 }
245
246 // read reads an existing file at a given slash separated path relative to Root( ).
247 func (f *tempFileSystemImpl) read(rel string) string {
248 data, err := ioutil.ReadFile(f.join(rel))
249 So(err, ShouldBeNil)
250 return string(data)
251 }
252
253 // readLink reads a symlink at a given slash separated path relative to Root().
254 func (f *tempFileSystemImpl) readLink(rel string) string {
255 val, err := os.Readlink(f.join(rel))
256 So(err, ShouldBeNil)
257 return val
258 }
259
260 // isMissing returns true if there's no file at a given slash separated path
261 // relative to Root().
262 func (f *tempFileSystemImpl) isMissing(rel string) bool {
263 _, err := os.Stat(f.join(rel))
264 return os.IsNotExist(err)
265 }
266
267 // isDir returns true if a file at a given slash separated path relative to
268 // Root() is a directory.
269 func (f *tempFileSystemImpl) isDir(rel string) bool {
270 stat, err := os.Stat(f.join(rel))
271 if err != nil {
272 return false
273 }
274 return stat.IsDir()
275 }
276
277 // isFile returns true if a file at a given slash separated path relative to
278 // Root() is a file (not a directory).
279 func (f *tempFileSystemImpl) isFile(rel string) bool {
280 stat, err := os.Stat(f.join(rel))
281 if err != nil {
282 return false
283 }
284 return !stat.IsDir()
285 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/local/fs.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698