Skip to content

Commit

Permalink
Merge pull request #24 from mcuadros/windows
Browse files Browse the repository at this point in the history
*: windows support
  • Loading branch information
mcuadros committed Apr 17, 2017
2 parents 35732c5 + 829cad3 commit 0661b96
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 42 deletions.
2 changes: 1 addition & 1 deletion osfs/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type OSSuite struct {
var _ = Suite(&OSSuite{})

func (s *OSSuite) SetUpTest(c *C) {
s.path, _ = ioutil.TempDir(os.TempDir(), "go-git-os-fs-test")
s.path, _ = ioutil.TempDir(os.TempDir(), "go-billy-osfs-test")
s.FilesystemSuite.FS = New(s.path)
}

Expand Down
19 changes: 5 additions & 14 deletions subdir/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package subdir
import (
"io"
"path/filepath"
"strings"

"gopkg.in/src-d/go-billy.v2"
)
Expand All @@ -14,9 +13,12 @@ type file struct {
f billy.File
}

func newFile(f billy.File, filename string) billy.File {
func newFile(fs billy.Filesystem, f billy.File, filename string) billy.File {
filename = fs.Join(fs.Base(), filename)
filename, _ = filepath.Rel(fs.Base(), filename)

return &file{
BaseFile: billy.BaseFile{BaseFilename: resolve(filename)},
BaseFile: billy.BaseFile{BaseFilename: filename},
f: f,
}
}
Expand Down Expand Up @@ -46,14 +48,3 @@ func (f *file) Close() error {
defer func() { f.Closed = true }()
return f.f.Close()
}

func resolve(path string) string {
rp := filepath.Clean(path)
if rp == "/" {
rp = "."
} else if strings.HasPrefix(rp, "/") {
rp = rp[1:]
}

return rp
}
27 changes: 12 additions & 15 deletions subdir/subdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *subdirFs) Create(filename string) (billy.File, error) {
return nil, err
}

return newFile(f, filename), nil
return newFile(s, f, filename), nil
}

func (s *subdirFs) Open(filename string) (billy.File, error) {
Expand All @@ -42,7 +42,7 @@ func (s *subdirFs) Open(filename string) (billy.File, error) {
return nil, err
}

return newFile(f, filename), nil
return newFile(s, f, filename), nil
}

func (s *subdirFs) OpenFile(filename string, flag int, mode os.FileMode) (
Expand All @@ -53,7 +53,7 @@ func (s *subdirFs) OpenFile(filename string, flag int, mode os.FileMode) (
return nil, err
}

return newFile(f, filename), nil
return newFile(s, f, filename), nil
}

func (s *subdirFs) TempFile(dir, prefix string) (billy.File, error) {
Expand All @@ -62,7 +62,7 @@ func (s *subdirFs) TempFile(dir, prefix string) (billy.File, error) {
return nil, err
}

return newFile(f, s.Join(dir, filepath.Base(f.Filename()))), nil
return newFile(s, f, s.Join(dir, filepath.Base(f.Filename()))), nil
}

func (s *subdirFs) Rename(from, to string) error {
Expand All @@ -79,8 +79,13 @@ func (s *subdirFs) MkdirAll(filename string, perm os.FileMode) error {
}

func (s *subdirFs) Stat(filename string) (billy.FileInfo, error) {
filename = removeLeadingSlash(filename)
fi, err := s.underlying.Stat(s.underlyingPath(filename))
fullpath := s.underlyingPath(filename)
fi, err := s.underlying.Stat(fullpath)
if err != nil {
return nil, err
}

filename, err = filepath.Rel(s.Base(), fullpath)
if err != nil {
return nil, err
}
Expand All @@ -107,17 +112,9 @@ func (s *subdirFs) Join(elem ...string) string {
}

func (s *subdirFs) Dir(path string) billy.Filesystem {
return New(s, removeLeadingSlash(path))
return New(s.underlying, s.underlyingPath(path))
}

func (s *subdirFs) Base() string {
return s.base
}

func removeLeadingSlash(path string) string {
if strings.HasPrefix(path, "/") {
return path[1:]
}

return path
}
7 changes: 4 additions & 3 deletions subdir/suite_test.go → subdir/subdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type FilesystemSuite struct {
var _ = Suite(&FilesystemSuite{})

func (s *FilesystemSuite) SetUpTest(c *C) {
s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-git-fs-test")
osFs := osfs.New(s.path)
s.cfs = New(osFs, "test-subdir")
s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-billy-subdirfs-test")
fs := osfs.New(s.path)

s.cfs = New(fs, "test-subdir")
s.FilesystemSuite.FS = s.cfs
}

Expand Down
39 changes: 30 additions & 9 deletions test/fs_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -23,6 +24,7 @@ func (s *FilesystemSuite) TestCreate(c *C) {
f, err := s.FS.Create("foo")
c.Assert(err, IsNil)
c.Assert(f.Filename(), Equals, "foo")
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestOpen(c *C) {
Expand All @@ -34,6 +36,7 @@ func (s *FilesystemSuite) TestOpen(c *C) {
f, err = s.FS.Open("foo")
c.Assert(err, IsNil)
c.Assert(f.Filename(), Equals, "foo")
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestOpenNotExists(c *C) {
Expand All @@ -54,13 +57,15 @@ func (s *FilesystemSuite) TestCreateDir(c *C) {
func (s *FilesystemSuite) TestCreateDepth(c *C) {
f, err := s.FS.Create("bar/foo")
c.Assert(err, IsNil)
c.Assert(f.Filename(), Equals, "bar/foo")
c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo"))
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestCreateDepthAbsolute(c *C) {
f, err := s.FS.Create("/bar/foo")
c.Assert(err, IsNil)
c.Assert(f.Filename(), Equals, "bar/foo")
c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo"))
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestCreateOverwrite(c *C) {
Expand All @@ -82,6 +87,7 @@ func (s *FilesystemSuite) TestCreateOverwrite(c *C) {
wrote, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
c.Assert(string(wrote), DeepEquals, "foo2")
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestCreateClose(c *C) {
Expand Down Expand Up @@ -199,13 +205,13 @@ func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) {
}

func (s *FilesystemSuite) TestOpenFileWithModes(c *C) {
f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, customMode)
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)

fi, err := s.FS.Stat("foo")
c.Assert(err, IsNil)
c.Assert(fi.Mode(), Equals, os.FileMode(0755))
c.Assert(fi.Mode(), Equals, os.FileMode(customMode))
}

func (s *FilesystemSuite) testWriteClose(c *C, f File, content string) {
Expand Down Expand Up @@ -281,6 +287,8 @@ func (s *FilesystemSuite) TestFileNonRead(c *C) {

_, err = ioutil.ReadAll(f)
c.Assert(err, NotNil)

c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestFileSeekstart(c *C) {
Expand Down Expand Up @@ -478,11 +486,14 @@ func (s *FilesystemSuite) TestDirStat(c *C) {
}

func (s *FilesystemSuite) TestCreateInDir(c *C) {
dir := s.FS.Dir("foo")
f, err := dir.Create("bar")
f, err := s.FS.Dir("foo").Create("bar")
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)
c.Assert(f.Filename(), Equals, "bar")

f, err = s.FS.Open("foo/bar")
c.Assert(f.Filename(), Equals, s.FS.Join("foo", "bar"))
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestRename(c *C) {
Expand Down Expand Up @@ -531,19 +542,24 @@ func (s *FilesystemSuite) TestRenameDir(c *C) {
func (s *FilesystemSuite) TestTempFile(c *C) {
f, err := s.FS.TempFile("", "bar")
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)

c.Assert(strings.HasPrefix(f.Filename(), "bar"), Equals, true)
}

func (s *FilesystemSuite) TestTempFileWithPath(c *C) {
f, err := s.FS.TempFile("foo", "bar")
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)

c.Assert(strings.HasPrefix(f.Filename(), s.FS.Join("foo", "bar")), Equals, true)
}

func (s *FilesystemSuite) TestTempFileFullWithPath(c *C) {
f, err := s.FS.TempFile("/foo", "bar")
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)

c.Assert(strings.HasPrefix(f.Filename(), s.FS.Join("foo", "bar")), Equals, true)
}

Expand All @@ -558,6 +574,8 @@ func (s *FilesystemSuite) TestOpenAndWrite(c *C) {
n, err := foo.Write([]byte("foo"))
c.Assert(err, NotNil)
c.Assert(n, Equals, 0)

c.Assert(foo.Close(), IsNil)
}

func (s *FilesystemSuite) TestOpenAndStat(c *C) {
Expand All @@ -568,6 +586,7 @@ func (s *FilesystemSuite) TestOpenAndStat(c *C) {
c.Assert(foo, NotNil)
c.Assert(foo.Filename(), Equals, "foo")
c.Assert(err, IsNil)
c.Assert(foo.Close(), IsNil)

stat, err := s.FS.Stat("foo")
c.Assert(stat, NotNil)
Expand Down Expand Up @@ -611,7 +630,7 @@ func (s *FilesystemSuite) TestRemoveTempFile(c *C) {
}

func (s *FilesystemSuite) TestJoin(c *C) {
c.Assert(s.FS.Join("foo", "bar"), Equals, "foo/bar")
c.Assert(s.FS.Join("foo", "bar"), Equals, fmt.Sprintf("foo%cbar", filepath.Separator))
}

func (s *FilesystemSuite) TestBase(c *C) {
Expand Down Expand Up @@ -663,14 +682,14 @@ func (s *FilesystemSuite) TestReadWriteLargeFile(c *C) {
c.Assert(err, IsNil)
c.Assert(n, Equals, size)

err = f.Close()
c.Assert(err, IsNil)
c.Assert(f.Close(), IsNil)

f, err = s.FS.Open("foo")
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
c.Assert(len(b), Equals, size)
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestRemoveAllNonExistent(c *C) {
Expand Down Expand Up @@ -806,4 +825,6 @@ func (s *FilesystemSuite) TestWriteFile(c *C) {
wrote, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
c.Assert(string(wrote), DeepEquals, "bar")

c.Assert(f.Close(), IsNil)
}
7 changes: 7 additions & 0 deletions test/fs_suite_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !windows

package test

import "os"

var customMode os.FileMode = 0755
7 changes: 7 additions & 0 deletions test/fs_suite_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows

package test

import "os"

var customMode os.FileMode = 0666

0 comments on commit 0661b96

Please sign in to comment.