Skip to content

Commit 15304c2

Browse files
committed
test: new test organization based on the interfaces
1 parent ad8207a commit 15304c2

20 files changed

+1775
-1758
lines changed

memfs/memory.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,22 @@ type Memory struct {
2323
tempCount int
2424
}
2525

26-
//New returns a new Memory filesystem
26+
//New returns a new Memory filesystem.
2727
func New() *Memory {
2828
return &Memory{
2929
base: string(separator),
3030
s: newStorage(),
3131
}
3232
}
3333

34-
// Create returns a new file in memory from a given filename.
3534
func (fs *Memory) Create(filename string) (billy.File, error) {
3635
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
3736
}
3837

39-
// Open returns a readonly file from a given name.
4038
func (fs *Memory) Open(filename string) (billy.File, error) {
4139
return fs.OpenFile(filename, os.O_RDONLY, 0)
4240
}
4341

44-
// OpenFile returns the file from a given name with given flag and permits.
4542
func (fs *Memory) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
4643
fullpath := fs.fullpath(filename)
4744
f, has := fs.s.Get(fullpath)
@@ -97,7 +94,6 @@ func isAbs(path string) bool {
9794
return filepath.IsAbs(path) || strings.HasPrefix(path, string(separator))
9895
}
9996

100-
// Stat returns a billy.FileInfo with the information of the requested file.
10197
func (fs *Memory) Stat(filename string) (billy.FileInfo, error) {
10298
fullpath := fs.fullpath(filename)
10399
f, has := fs.s.Get(fullpath)
@@ -132,7 +128,6 @@ func (fs *Memory) Lstat(filename string) (billy.FileInfo, error) {
132128
return f.Stat(), nil
133129
}
134130

135-
// ReadDir returns a list of billy.FileInfo in the given directory.
136131
func (fs *Memory) ReadDir(path string) ([]billy.FileInfo, error) {
137132
fullpath := fs.fullpath(path)
138133
if f, has := fs.s.Get(fullpath); has {
@@ -149,7 +144,6 @@ func (fs *Memory) ReadDir(path string) ([]billy.FileInfo, error) {
149144
return entries, nil
150145
}
151146

152-
// MkdirAll creates a directory.
153147
func (fs *Memory) MkdirAll(path string, perm os.FileMode) error {
154148
fullpath := fs.Join(fs.base, path)
155149

@@ -159,7 +153,6 @@ func (fs *Memory) MkdirAll(path string, perm os.FileMode) error {
159153

160154
var maxTempFiles = 1024 * 4
161155

162-
// TempFile creates a new temporary file.
163156
func (fs *Memory) TempFile(dir, prefix string) (billy.File, error) {
164157
var fullpath string
165158
for {
@@ -182,34 +175,22 @@ func (fs *Memory) getTempFilename(dir, prefix string) string {
182175
return fs.Join(fs.base, dir, filename)
183176
}
184177

185-
// Rename moves a the `from` file to the `to` file.
186178
func (fs *Memory) Rename(from, to string) error {
187179
from = fs.Join(fs.base, from)
188180
to = fs.Join(fs.base, to)
189181

190182
return fs.s.Rename(from, to)
191183
}
192184

193-
// Remove deletes a given file from storage.
194185
func (fs *Memory) Remove(filename string) error {
195186
fullpath := fs.Join(fs.base, filename)
196187
return fs.s.Remove(fullpath)
197188
}
198189

199-
// Join joins any number of path elements into a single path, adding a Separator if necessary.
200190
func (fs *Memory) Join(elem ...string) string {
201191
return filepath.Join(elem...)
202192
}
203193

204-
// Dir creates a new memory filesystem whose root is the given path inside the current
205-
// filesystem.
206-
func (fs *Memory) Dir(path string) billy.Filesystem {
207-
return &Memory{
208-
base: fs.Join(fs.base, path),
209-
s: fs.s,
210-
}
211-
}
212-
213194
func (fs *Memory) Symlink(target, link string) error {
214195
fullpath := clean(fs.Join(fs.base, link))
215196

@@ -244,7 +225,13 @@ func (fs *Memory) Readlink(link string) (string, error) {
244225
return string(f.content.bytes), nil
245226
}
246227

247-
// Base returns the base path for the filesystem.
228+
func (fs *Memory) Dir(path string) billy.Filesystem {
229+
return &Memory{
230+
base: fs.Join(fs.base, path),
231+
s: fs.s,
232+
}
233+
}
234+
248235
func (fs *Memory) Base() string {
249236
return fs.base
250237
}

memfs/memory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type MemorySuite struct {
1717
var _ = Suite(&MemorySuite{})
1818

1919
func (s *MemorySuite) SetUpTest(c *C) {
20-
s.FilesystemSuite.FS = New()
20+
s.FilesystemSuite = test.NewFilesystemSuite(New())
2121
}
2222

2323
func (s *MemorySuite) TestTempFileMaxTempFiles(c *C) {

osfs/os.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,22 @@ const (
1616
defaultCreateMode = 0666
1717
)
1818

19-
// OS is a filesystem based on the os filesystem
19+
// OS is a filesystem based on the os filesystem.
2020
type OS struct {
2121
base string
2222
}
2323

24-
// New returns a new OS filesystem
24+
// New returns a new OS filesystem.
2525
func New(baseDir string) *OS {
2626
return &OS{
2727
base: baseDir,
2828
}
2929
}
3030

31-
// Create creates a file and opens it with standard permissions
32-
// and modes O_RDWR, O_CREATE and O_TRUNC.
3331
func (fs *OS) Create(filename string) (billy.File, error) {
3432
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultCreateMode)
3533
}
3634

37-
// OpenFile is equivalent to standard os.OpenFile.
38-
// If flag os.O_CREATE is set, all parent directories will be created.
3935
func (fs *OS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
4036
fullpath := fs.absolutize(filename)
4137

@@ -69,8 +65,6 @@ func (fs *OS) createDir(fullpath string) error {
6965
return nil
7066
}
7167

72-
// ReadDir returns the filesystem info for all the archives under the specified
73-
// path.
7468
func (fs *OS) ReadDir(path string) ([]billy.FileInfo, error) {
7569
fullpath := fs.absolutize(path)
7670

@@ -87,7 +81,6 @@ func (fs *OS) ReadDir(path string) ([]billy.FileInfo, error) {
8781
return s, nil
8882
}
8983

90-
// Rename moves a file in disk from _from_ to _to_.
9184
func (fs *OS) Rename(from, to string) error {
9285
from = fs.absolutize(from)
9386
to = fs.absolutize(to)
@@ -99,24 +92,20 @@ func (fs *OS) Rename(from, to string) error {
9992
return os.Rename(from, to)
10093
}
10194

102-
// MkdirAll creates a directory.
10395
func (fs *OS) MkdirAll(path string, perm os.FileMode) error {
10496
fullpath := fs.absolutize(path)
10597
return os.MkdirAll(fullpath, defaultDirectoryMode)
10698
}
10799

108-
// Open opens a file in read-only mode.
109100
func (fs *OS) Open(filename string) (billy.File, error) {
110101
return fs.OpenFile(filename, os.O_RDONLY, 0)
111102
}
112103

113-
// Remove deletes a file in disk.
114104
func (fs *OS) Remove(filename string) error {
115105
fullpath := fs.absolutize(filename)
116106
return os.Remove(fullpath)
117107
}
118108

119-
// TempFile creates a new temporal file.
120109
func (fs *OS) TempFile(dir, prefix string) (billy.File, error) {
121110
fullpath := fs.absolutize(dir)
122111
if err := fs.createDir(fullpath + string(os.PathSeparator)); err != nil {
@@ -141,24 +130,18 @@ func (fs *OS) TempFile(dir, prefix string) (billy.File, error) {
141130
return newOSFile(filename, f), nil
142131
}
143132

144-
// Join joins the specified elements using the filesystem separator.
145133
func (fs *OS) Join(elem ...string) string {
146134
return filepath.Join(elem...)
147135
}
148136

149-
// Dir returns a new Filesystem from the same type of fs using as baseDir the
150-
// given path
151137
func (fs *OS) Dir(path string) billy.Filesystem {
152138
return New(fs.absolutize(path))
153139
}
154140

155-
// Base returns the base path of the filesytem
156141
func (fs *OS) Base() string {
157142
return fs.base
158143
}
159144

160-
// RemoveAll removes a file or directory recursively. Removes everything it can,
161-
// but returns the first error.
162145
func (fs *OS) RemoveAll(path string) error {
163146
fullpath := fs.Join(fs.base, path)
164147
return os.RemoveAll(fullpath)
@@ -169,7 +152,6 @@ func (fs *OS) Lstat(filename string) (billy.FileInfo, error) {
169152
return os.Lstat(fullpath)
170153
}
171154

172-
// Symlink imlements billy.Symlinker.Symlink.
173155
func (fs *OS) Symlink(target, link string) error {
174156
target = filepath.FromSlash(target)
175157

@@ -186,7 +168,6 @@ func (fs *OS) Symlink(target, link string) error {
186168
return os.Symlink(target, link)
187169
}
188170

189-
// Readlink implements billy.Symlinker.Readlink.
190171
func (fs *OS) Readlink(link string) (string, error) {
191172
fullpath := fs.Join(fs.base, link)
192173
target, err := os.Readlink(fullpath)
@@ -206,6 +187,13 @@ func (fs *OS) Readlink(link string) (string, error) {
206187
return string(os.PathSeparator) + target, nil
207188
}
208189

190+
func (fs *OS) absolutize(relpath string) string {
191+
fullpath := filepath.FromSlash(filepath.ToSlash(relpath))
192+
193+
fullpath = fs.Join(fs.base, fullpath)
194+
return filepath.Clean(fullpath)
195+
}
196+
209197
// osFile represents a file in the os filesystem
210198
type osFile struct {
211199
billy.BaseFile
@@ -240,10 +228,3 @@ func (f *osFile) Close() error {
240228
func (f *osFile) ReadAt(p []byte, off int64) (int, error) {
241229
return f.file.ReadAt(p, off)
242230
}
243-
244-
func (fs *OS) absolutize(relpath string) string {
245-
fullpath := filepath.FromSlash(filepath.ToSlash(relpath))
246-
247-
fullpath = fs.Join(fs.base, fullpath)
248-
return filepath.Clean(fullpath)
249-
}

osfs/os_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var _ = Suite(&OSSuite{})
2121

2222
func (s *OSSuite) SetUpTest(c *C) {
2323
s.path, _ = ioutil.TempDir(os.TempDir(), "go-billy-osfs-test")
24-
s.FilesystemSuite.FS = New(s.path)
24+
s.FilesystemSuite = test.NewFilesystemSuite(New(s.path))
2525
}
2626

2727
func (s *OSSuite) TearDownTest(c *C) {

0 commit comments

Comments
 (0)