Skip to content

Commit df3e8e3

Browse files
authored
Merge pull request #23 from src-d/test-improvements
test improvements
2 parents 88d97b7 + 3f6f030 commit df3e8e3

File tree

1 file changed

+191
-40
lines changed

1 file changed

+191
-40
lines changed

test/fs_suite.go

Lines changed: 191 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ func (s *FilesystemSuite) TestCreate(c *C) {
2525
c.Assert(f.Filename(), Equals, "foo")
2626
}
2727

28+
func (s *FilesystemSuite) TestOpen(c *C) {
29+
f, err := s.FS.Create("foo")
30+
c.Assert(err, IsNil)
31+
c.Assert(f.Filename(), Equals, "foo")
32+
c.Assert(f.Close(), IsNil)
33+
34+
f, err = s.FS.Open("foo")
35+
c.Assert(err, IsNil)
36+
c.Assert(f.Filename(), Equals, "foo")
37+
}
38+
39+
func (s *FilesystemSuite) TestOpenNotExists(c *C) {
40+
f, err := s.FS.Open("not-exists")
41+
c.Assert(err, NotNil)
42+
c.Assert(f, IsNil)
43+
}
44+
45+
func (s *FilesystemSuite) TestCreateDir(c *C) {
46+
err := s.FS.MkdirAll("foo", 0644)
47+
c.Assert(err, IsNil)
48+
49+
f, err := s.FS.Create("foo")
50+
c.Assert(err, NotNil)
51+
c.Assert(f, IsNil)
52+
}
53+
2854
func (s *FilesystemSuite) TestCreateDepth(c *C) {
2955
f, err := s.FS.Create("bar/foo")
3056
c.Assert(err, IsNil)
@@ -76,6 +102,36 @@ func (s *FilesystemSuite) TestCreateClose(c *C) {
76102
c.Assert(f.Close(), IsNil)
77103
}
78104

105+
func (s *FilesystemSuite) TestOpenFile(c *C) {
106+
defaultMode := os.FileMode(0666)
107+
108+
f, err := s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
109+
c.Assert(err, IsNil)
110+
s.testWriteClose(c, f, "foo1")
111+
112+
// Truncate if it exists
113+
f, err = s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
114+
c.Assert(err, IsNil)
115+
c.Assert(f.Filename(), Equals, "foo1")
116+
s.testWriteClose(c, f, "foo1overwritten")
117+
118+
// Read-only if it exists
119+
f, err = s.FS.OpenFile("foo1", os.O_RDONLY, defaultMode)
120+
c.Assert(err, IsNil)
121+
c.Assert(f.Filename(), Equals, "foo1")
122+
s.testReadClose(c, f, "foo1overwritten")
123+
124+
// Create when it does exist
125+
f, err = s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
126+
c.Assert(err, IsNil)
127+
c.Assert(f.Filename(), Equals, "foo1")
128+
s.testWriteClose(c, f, "bar")
129+
130+
f, err = s.FS.OpenFile("foo1", os.O_RDONLY, defaultMode)
131+
c.Assert(err, IsNil)
132+
s.testReadClose(c, f, "bar")
133+
}
134+
79135
func (s *FilesystemSuite) TestOpenFileNoTruncate(c *C) {
80136
defaultMode := os.FileMode(0666)
81137

@@ -142,36 +198,6 @@ func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) {
142198
s.testReadClose(c, f, "quxbar")
143199
}
144200

145-
func (s *FilesystemSuite) TestOpenFile(c *C) {
146-
defaultMode := os.FileMode(0666)
147-
148-
f, err := s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
149-
c.Assert(err, IsNil)
150-
s.testWriteClose(c, f, "foo1")
151-
152-
// Truncate if it exists
153-
f, err = s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
154-
c.Assert(err, IsNil)
155-
c.Assert(f.Filename(), Equals, "foo1")
156-
s.testWriteClose(c, f, "foo1overwritten")
157-
158-
// Read-only if it exists
159-
f, err = s.FS.OpenFile("foo1", os.O_RDONLY, defaultMode)
160-
c.Assert(err, IsNil)
161-
c.Assert(f.Filename(), Equals, "foo1")
162-
s.testReadClose(c, f, "foo1overwritten")
163-
164-
// Create when it does exist
165-
f, err = s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode)
166-
c.Assert(err, IsNil)
167-
c.Assert(f.Filename(), Equals, "foo1")
168-
s.testWriteClose(c, f, "bar")
169-
170-
f, err = s.FS.OpenFile("foo1", os.O_RDONLY, defaultMode)
171-
c.Assert(err, IsNil)
172-
s.testReadClose(c, f, "bar")
173-
}
174-
175201
func (s *FilesystemSuite) TestOpenFileWithModes(c *C) {
176202
f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
177203
c.Assert(err, IsNil)
@@ -196,41 +222,114 @@ func (s *FilesystemSuite) testReadClose(c *C, f File, content string) {
196222
c.Assert(f.Close(), IsNil)
197223
}
198224

199-
func (s *FilesystemSuite) TestFileCreateReadSeek(c *C) {
225+
func (s *FilesystemSuite) TestFileWrite(c *C) {
200226
f, err := s.FS.Create("foo")
201227
c.Assert(err, IsNil)
202228

203-
n, err := f.Write([]byte("0123456789abcdefghijklmnopqrstuvwxyz"))
229+
n, err := f.Write([]byte("foo"))
204230
c.Assert(err, IsNil)
205-
c.Assert(n, Equals, 36)
231+
c.Assert(n, Equals, 3)
206232

207-
p, err := f.Seek(10, io.SeekStart)
233+
f.Seek(0, io.SeekStart)
234+
all, err := ioutil.ReadAll(f)
235+
c.Assert(err, IsNil)
236+
c.Assert(string(all), Equals, "foo")
237+
c.Assert(f.Close(), IsNil)
238+
}
239+
240+
func (s *FilesystemSuite) TestFileWriteClose(c *C) {
241+
f, err := s.FS.Create("foo")
242+
c.Assert(err, IsNil)
243+
244+
c.Assert(f.Close(), IsNil)
245+
246+
_, err = f.Write([]byte("foo"))
247+
c.Assert(err, NotNil)
248+
}
249+
250+
func (s *FilesystemSuite) TestFileRead(c *C) {
251+
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
252+
c.Assert(err, IsNil)
253+
254+
f, err := s.FS.Open("foo")
208255
c.Assert(err, IsNil)
209-
c.Assert(int(p), Equals, 10)
210256

211257
all, err := ioutil.ReadAll(f)
212258
c.Assert(err, IsNil)
213-
c.Assert(string(all), Equals, "abcdefghijklmnopqrstuvwxyz")
259+
c.Assert(string(all), Equals, "foo")
214260
c.Assert(f.Close(), IsNil)
215261
}
216262

217-
func (s *FilesystemSuite) TestFileOpenReadSeek(c *C) {
263+
func (s *FilesystemSuite) TestFileClosed(c *C) {
264+
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
265+
c.Assert(err, IsNil)
266+
267+
f, err := s.FS.Open("foo")
268+
c.Assert(err, IsNil)
269+
c.Assert(f.Close(), IsNil)
270+
271+
_, err = ioutil.ReadAll(f)
272+
c.Assert(err, NotNil)
273+
}
274+
275+
func (s *FilesystemSuite) TestFileNonRead(c *C) {
276+
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
277+
c.Assert(err, IsNil)
278+
279+
f, err := s.FS.OpenFile("foo", os.O_WRONLY, 0)
280+
c.Assert(err, IsNil)
281+
282+
_, err = ioutil.ReadAll(f)
283+
c.Assert(err, NotNil)
284+
}
285+
286+
func (s *FilesystemSuite) TestFileSeekstart(c *C) {
287+
s.testFileSeek(c, 10, io.SeekStart)
288+
}
289+
290+
func (s *FilesystemSuite) TestFileSeekCurrent(c *C) {
291+
s.testFileSeek(c, 5, io.SeekCurrent)
292+
}
293+
294+
func (s *FilesystemSuite) TestFileSeekEnd(c *C) {
295+
s.testFileSeek(c, -26, io.SeekEnd)
296+
}
297+
298+
func (s *FilesystemSuite) testFileSeek(c *C, offset int64, whence int) {
218299
err := WriteFile(s.FS, "foo", []byte("0123456789abcdefghijklmnopqrstuvwxyz"), 0644)
219300
c.Assert(err, IsNil)
220301

221302
f, err := s.FS.Open("foo")
222303
c.Assert(err, IsNil)
223304

224-
p, err := f.Seek(10, io.SeekStart)
305+
some := make([]byte, 5)
306+
_, err = f.Read(some)
307+
c.Assert(err, IsNil)
308+
c.Assert(string(some), Equals, "01234")
309+
310+
p, err := f.Seek(offset, whence)
225311
c.Assert(err, IsNil)
226312
c.Assert(int(p), Equals, 10)
227313

228314
all, err := ioutil.ReadAll(f)
229315
c.Assert(err, IsNil)
316+
c.Assert(all, HasLen, 26)
230317
c.Assert(string(all), Equals, "abcdefghijklmnopqrstuvwxyz")
231318
c.Assert(f.Close(), IsNil)
232319
}
233320

321+
func (s *FilesystemSuite) TestFileSeekClosed(c *C) {
322+
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
323+
c.Assert(err, IsNil)
324+
325+
f, err := s.FS.Open("foo")
326+
c.Assert(err, IsNil)
327+
c.Assert(f.Close(), IsNil)
328+
329+
_, err = f.Seek(0, 0)
330+
c.Assert(err, NotNil)
331+
}
332+
234333
func (s *FilesystemSuite) TestFileCloseTwice(c *C) {
235334
f, err := s.FS.Create("foo")
236335
c.Assert(err, IsNil)
@@ -260,6 +359,31 @@ func (s *FilesystemSuite) TestReadDirAndDir(c *C) {
260359
c.Assert(info, HasLen, 2)
261360
}
262361

362+
func (s *FilesystemSuite) TestReadDirAndWithMkDirAll(c *C) {
363+
err := s.FS.MkdirAll("qux", 0644)
364+
c.Assert(err, IsNil)
365+
366+
files := []string{"qux/baz", "qux/qux"}
367+
for _, name := range files {
368+
err := WriteFile(s.FS, name, nil, 0644)
369+
c.Assert(err, IsNil)
370+
}
371+
372+
info, err := s.FS.ReadDir("/")
373+
c.Assert(err, IsNil)
374+
c.Assert(info, HasLen, 1)
375+
c.Assert(info[0].IsDir(), Equals, true)
376+
377+
info, err = s.FS.ReadDir("/qux")
378+
c.Assert(err, IsNil)
379+
c.Assert(info, HasLen, 2)
380+
381+
qux := s.FS.Dir("/qux")
382+
info, err = qux.ReadDir("/")
383+
c.Assert(err, IsNil)
384+
c.Assert(info, HasLen, 2)
385+
}
386+
263387
func (s *FilesystemSuite) TestReadDirFileInfo(c *C) {
264388
err := WriteFile(s.FS, "foo", []byte{'F', 'O', 'O'}, 0644)
265389
c.Assert(err, IsNil)
@@ -357,6 +481,33 @@ func (s *FilesystemSuite) TestRename(c *C) {
357481
c.Assert(err, IsNil)
358482
}
359483

484+
func (s *FilesystemSuite) TestRenameDir(c *C) {
485+
err := s.FS.MkdirAll("foo", 0644)
486+
c.Assert(err, IsNil)
487+
488+
err = WriteFile(s.FS, "foo/bar", nil, 0644)
489+
c.Assert(err, IsNil)
490+
491+
err = s.FS.Rename("foo", "bar")
492+
c.Assert(err, IsNil)
493+
494+
dirfoo, err := s.FS.Stat("foo")
495+
c.Assert(dirfoo, IsNil)
496+
c.Assert(os.IsNotExist(err), Equals, true)
497+
498+
dirbar, err := s.FS.Stat("bar")
499+
c.Assert(err, IsNil)
500+
c.Assert(dirbar, NotNil)
501+
502+
foo, err := s.FS.Stat("foo/bar")
503+
c.Assert(os.IsNotExist(err), Equals, true)
504+
c.Assert(foo, IsNil)
505+
506+
bar, err := s.FS.Stat("bar/bar")
507+
c.Assert(err, IsNil)
508+
c.Assert(bar, NotNil)
509+
}
510+
360511
func (s *FilesystemSuite) TestTempFile(c *C) {
361512
f, err := s.FS.TempFile("", "bar")
362513
c.Assert(err, IsNil)
@@ -585,14 +736,14 @@ func (s *FilesystemSuite) TestMkdirAllNested(c *C) {
585736
}
586737

587738
func (s *FilesystemSuite) TestMkdirAllIdempotent(c *C) {
588-
err := s.FS.MkdirAll("empty", os.FileMode(0755))
739+
err := s.FS.MkdirAll("empty", 0755)
589740
c.Assert(err, IsNil)
590741
fi, err := s.FS.Stat("empty")
591742
c.Assert(err, IsNil)
592743
c.Assert(fi.IsDir(), Equals, true)
593744

594745
// idempotent
595-
err = s.FS.MkdirAll("empty", os.FileMode(0755))
746+
err = s.FS.MkdirAll("empty", 0755)
596747
c.Assert(err, IsNil)
597748
fi, err = s.FS.Stat("empty")
598749
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)