Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 0327891

Browse files
authored
utils/fs: move 'os' and 'test' to separate packages. (#93)
* create utils/fs/test package to expose generic test suite to 3rd party fs implementations. * move 'os' to its own package to avoid cyclic dependency (test -> fs -> test, becomes test -> fs, os -> test, os -> fs). * remove TestCreateAndWrite: some of our implementations cannot read a file that was just created, written and not closed yet.
1 parent d0ea76c commit 0327891

File tree

10 files changed

+108
-115
lines changed

10 files changed

+108
-115
lines changed

fixtures/fixtures.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/check.v1"
1212
"gopkg.in/src-d/go-git.v4/core"
1313
"gopkg.in/src-d/go-git.v4/utils/fs"
14+
osfs "gopkg.in/src-d/go-git.v4/utils/fs/os"
1415
)
1516

1617
var RootFolder = ""
@@ -161,7 +162,7 @@ func (f *Fixture) DotGit() fs.Filesystem {
161162
}
162163

163164
folders = append(folders, path)
164-
return fs.NewOS(path)
165+
return osfs.NewOS(path)
165166
}
166167

167168
type Fixtures []*Fixture

remote_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"gopkg.in/src-d/go-git.v4/core"
99
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1010
"gopkg.in/src-d/go-git.v4/storage/memory"
11-
"gopkg.in/src-d/go-git.v4/utils/fs"
11+
osfs "gopkg.in/src-d/go-git.v4/utils/fs/os"
1212

1313
. "gopkg.in/check.v1"
1414
)
@@ -88,7 +88,7 @@ func (s *RemoteSuite) TestFetchObjectStorageWriter(c *C) {
8888
defer os.RemoveAll(dir) // clean up
8989

9090
var sto Storage
91-
sto, err = filesystem.NewStorage(fs.NewOS(dir))
91+
sto, err = filesystem.NewStorage(osfs.NewOS(dir))
9292
c.Assert(err, IsNil)
9393

9494
r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture})

repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"gopkg.in/src-d/go-git.v4/core"
99
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1010
"gopkg.in/src-d/go-git.v4/storage/memory"
11-
"gopkg.in/src-d/go-git.v4/utils/fs"
11+
osfs "gopkg.in/src-d/go-git.v4/utils/fs/os"
1212
)
1313

1414
var (
@@ -33,7 +33,7 @@ func NewMemoryRepository() *Repository {
3333
// based on a fs.OS, if you want to use a custom one you need to use the function
3434
// NewRepository and build you filesystem.Storage
3535
func NewFilesystemRepository(path string) (*Repository, error) {
36-
s, err := filesystem.NewStorage(fs.NewOS(path))
36+
s, err := filesystem.NewStorage(osfs.NewOS(path))
3737
if err != nil {
3838
return nil, err
3939
}

storage/filesystem/internal/dotgit/dotgit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"gopkg.in/src-d/go-git.v4/core"
1111
"gopkg.in/src-d/go-git.v4/fixtures"
12-
"gopkg.in/src-d/go-git.v4/utils/fs"
12+
osfs "gopkg.in/src-d/go-git.v4/utils/fs/os"
1313

1414
. "gopkg.in/check.v1"
1515
)
@@ -27,7 +27,7 @@ func (s *SuiteDotGit) TestSetRefs(c *C) {
2727
c.Assert(err, IsNil)
2828
defer os.RemoveAll(tmp)
2929

30-
fs := fs.NewOS(tmp)
30+
fs := osfs.NewOS(tmp)
3131
dir := New(fs)
3232

3333
err = dir.SetRef(core.NewReferenceFromStrings(
@@ -164,7 +164,7 @@ func (s *SuiteDotGit) TestNewObject(c *C) {
164164
c.Assert(err, IsNil)
165165
defer os.RemoveAll(tmp)
166166

167-
fs := fs.NewOS(tmp)
167+
fs := osfs.NewOS(tmp)
168168
dir := New(fs)
169169
w, err := dir.NewObject()
170170
c.Assert(err, IsNil)

storage/filesystem/internal/dotgit/writers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strconv"
1010

1111
"gopkg.in/src-d/go-git.v4/fixtures"
12-
"gopkg.in/src-d/go-git.v4/utils/fs"
12+
osfs "gopkg.in/src-d/go-git.v4/utils/fs/os"
1313

1414
. "gopkg.in/check.v1"
1515
)
@@ -24,7 +24,7 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) {
2424

2525
defer os.RemoveAll(dir)
2626

27-
fs := fs.NewOS(dir)
27+
fs := osfs.NewOS(dir)
2828
dot := New(fs)
2929

3030
w, err := dot.NewObjectPack()

utils/fs/fs.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
)
99

1010
var (
11-
ErrClosed = errors.New("File: Writing on closed file.")
11+
ErrClosed = errors.New("file: Writing on closed file.")
1212
ErrReadOnly = errors.New("this is a read-only filesystem")
1313
ErrNotSupported = errors.New("feature not supported")
1414
)
1515

1616
type Filesystem interface {
17+
//Create opens a file in write-only mode.
1718
Create(filename string) (File, error)
19+
//Open opens a file in read-only mode.
1820
Open(filename string) (File, error)
1921
Stat(filename string) (FileInfo, error)
2022
ReadDir(path string) ([]FileInfo, error)
@@ -35,18 +37,3 @@ type File interface {
3537
}
3638

3739
type FileInfo os.FileInfo
38-
39-
type BaseFile struct {
40-
filename string
41-
closed bool
42-
}
43-
44-
//Filename returns the filename from the File
45-
func (f *BaseFile) Filename() string {
46-
return f.filename
47-
}
48-
49-
//IsClosed returns if te file is closed
50-
func (f *BaseFile) IsClosed() bool {
51-
return f.closed
52-
}

utils/fs/os.go renamed to utils/fs/os/os.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package fs
1+
package os
22

33
import (
44
"io/ioutil"
55
"os"
66
"path"
77
"path/filepath"
8+
9+
. "gopkg.in/src-d/go-git.v4/utils/fs"
810
)
911

1012
// OS a filesystem base on the os filesystem
@@ -37,10 +39,7 @@ func (fs *OS) Create(filename string) (File, error) {
3739
return nil, err
3840
}
3941

40-
return &OSFile{
41-
BaseFile: BaseFile{filename: filename},
42-
file: f,
43-
}, nil
42+
return newOSFile(filename, f), nil
4443
}
4544

4645
func (fs *OS) createDir(fullpath string) error {
@@ -92,10 +91,7 @@ func (fs *OS) Open(filename string) (File, error) {
9291
return nil, err
9392
}
9493

95-
return &OSFile{
96-
BaseFile: BaseFile{filename: filename},
97-
file: f,
98-
}, nil
94+
return newOSFile(filename, f), nil
9995
}
10096

10197
// Stat returns the FileInfo structure describing file.
@@ -125,10 +121,7 @@ func (fs *OS) TempFile(dir, prefix string) (File, error) {
125121
return nil, err
126122
}
127123

128-
return &OSFile{
129-
BaseFile: BaseFile{filename: filename},
130-
file: f,
131-
}, nil
124+
return newOSFile(filename, f), nil
132125
}
133126

134127
// Join joins the specified elements using the filesystem separator.
@@ -147,30 +140,49 @@ func (fs *OS) Base() string {
147140
return fs.base
148141
}
149142

150-
// OSFile represents a file in the os filesystem
151-
type OSFile struct {
152-
file *os.File
153-
BaseFile
143+
// osFile represents a file in the os filesystem
144+
type osFile struct {
145+
filename string
146+
closed bool
147+
file *os.File
148+
}
149+
150+
func newOSFile(filename string, file *os.File) File {
151+
return &osFile{
152+
filename: filename,
153+
closed: false,
154+
file: file,
155+
}
154156
}
155157

156-
func (f *OSFile) Read(p []byte) (int, error) {
158+
func (f *osFile) Read(p []byte) (int, error) {
157159
return f.file.Read(p)
158160
}
159161

160-
func (f *OSFile) Seek(offset int64, whence int) (int64, error) {
162+
func (f *osFile) Seek(offset int64, whence int) (int64, error) {
161163
return f.file.Seek(offset, whence)
162164
}
163165

164-
func (f *OSFile) Write(p []byte) (int, error) {
166+
func (f *osFile) Write(p []byte) (int, error) {
165167
return f.file.Write(p)
166168
}
167169

168-
func (f *OSFile) Close() error {
170+
func (f *osFile) Close() error {
169171
f.closed = true
170172

171173
return f.file.Close()
172174
}
173175

174-
func (f *OSFile) ReadAt(p []byte, off int64) (int, error) {
176+
func (f *osFile) ReadAt(p []byte, off int64) (n int, err error) {
175177
return f.file.ReadAt(p, off)
176178
}
179+
180+
//Filename returns the filename from the File
181+
func (f *osFile) Filename() string {
182+
return f.filename
183+
}
184+
185+
//IsClosed returns if te file is closed
186+
func (f *osFile) IsClosed() bool {
187+
return f.closed
188+
}

utils/fs/os/os_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package os_test
2+
3+
import (
4+
"io/ioutil"
5+
stdos "os"
6+
"testing"
7+
8+
. "gopkg.in/check.v1"
9+
"gopkg.in/src-d/go-git.v4/utils/fs/os"
10+
"gopkg.in/src-d/go-git.v4/utils/fs/test"
11+
)
12+
13+
func Test(t *testing.T) { TestingT(t) }
14+
15+
type OSSuite struct {
16+
test.FilesystemSuite
17+
path string
18+
}
19+
20+
var _ = Suite(&OSSuite{})
21+
22+
func (s *OSSuite) SetUpTest(c *C) {
23+
s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-git-os-fs-test")
24+
s.FilesystemSuite.Fs = os.NewOS(s.path)
25+
}
26+
func (s *OSSuite) TearDownTest(c *C) {
27+
err := stdos.RemoveAll(s.path)
28+
c.Assert(err, IsNil)
29+
}

utils/fs/os_test.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)