Skip to content

Commit 0661b96

Browse files
authored
Merge pull request #24 from mcuadros/windows
*: windows support
2 parents 35732c5 + 829cad3 commit 0661b96

File tree

7 files changed

+66
-42
lines changed

7 files changed

+66
-42
lines changed

osfs/os_test.go

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

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

subdir/file.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package subdir
33
import (
44
"io"
55
"path/filepath"
6-
"strings"
76

87
"gopkg.in/src-d/go-billy.v2"
98
)
@@ -14,9 +13,12 @@ type file struct {
1413
f billy.File
1514
}
1615

17-
func newFile(f billy.File, filename string) billy.File {
16+
func newFile(fs billy.Filesystem, f billy.File, filename string) billy.File {
17+
filename = fs.Join(fs.Base(), filename)
18+
filename, _ = filepath.Rel(fs.Base(), filename)
19+
1820
return &file{
19-
BaseFile: billy.BaseFile{BaseFilename: resolve(filename)},
21+
BaseFile: billy.BaseFile{BaseFilename: filename},
2022
f: f,
2123
}
2224
}
@@ -46,14 +48,3 @@ func (f *file) Close() error {
4648
defer func() { f.Closed = true }()
4749
return f.f.Close()
4850
}
49-
50-
func resolve(path string) string {
51-
rp := filepath.Clean(path)
52-
if rp == "/" {
53-
rp = "."
54-
} else if strings.HasPrefix(rp, "/") {
55-
rp = rp[1:]
56-
}
57-
58-
return rp
59-
}

subdir/subdir.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *subdirFs) Create(filename string) (billy.File, error) {
3333
return nil, err
3434
}
3535

36-
return newFile(f, filename), nil
36+
return newFile(s, f, filename), nil
3737
}
3838

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

45-
return newFile(f, filename), nil
45+
return newFile(s, f, filename), nil
4646
}
4747

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

56-
return newFile(f, filename), nil
56+
return newFile(s, f, filename), nil
5757
}
5858

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

65-
return newFile(f, s.Join(dir, filepath.Base(f.Filename()))), nil
65+
return newFile(s, f, s.Join(dir, filepath.Base(f.Filename()))), nil
6666
}
6767

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

8181
func (s *subdirFs) Stat(filename string) (billy.FileInfo, error) {
82-
filename = removeLeadingSlash(filename)
83-
fi, err := s.underlying.Stat(s.underlyingPath(filename))
82+
fullpath := s.underlyingPath(filename)
83+
fi, err := s.underlying.Stat(fullpath)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
filename, err = filepath.Rel(s.Base(), fullpath)
8489
if err != nil {
8590
return nil, err
8691
}
@@ -107,17 +112,9 @@ func (s *subdirFs) Join(elem ...string) string {
107112
}
108113

109114
func (s *subdirFs) Dir(path string) billy.Filesystem {
110-
return New(s, removeLeadingSlash(path))
115+
return New(s.underlying, s.underlyingPath(path))
111116
}
112117

113118
func (s *subdirFs) Base() string {
114119
return s.base
115120
}
116-
117-
func removeLeadingSlash(path string) string {
118-
if strings.HasPrefix(path, "/") {
119-
return path[1:]
120-
}
121-
122-
return path
123-
}

subdir/suite_test.go renamed to subdir/subdir_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ type FilesystemSuite struct {
2323
var _ = Suite(&FilesystemSuite{})
2424

2525
func (s *FilesystemSuite) SetUpTest(c *C) {
26-
s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-git-fs-test")
27-
osFs := osfs.New(s.path)
28-
s.cfs = New(osFs, "test-subdir")
26+
s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-billy-subdirfs-test")
27+
fs := osfs.New(s.path)
28+
29+
s.cfs = New(fs, "test-subdir")
2930
s.FilesystemSuite.FS = s.cfs
3031
}
3132

test/fs_suite.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"testing"
1112

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

2830
func (s *FilesystemSuite) TestOpen(c *C) {
@@ -34,6 +36,7 @@ func (s *FilesystemSuite) TestOpen(c *C) {
3436
f, err = s.FS.Open("foo")
3537
c.Assert(err, IsNil)
3638
c.Assert(f.Filename(), Equals, "foo")
39+
c.Assert(f.Close(), IsNil)
3740
}
3841

3942
func (s *FilesystemSuite) TestOpenNotExists(c *C) {
@@ -54,13 +57,15 @@ func (s *FilesystemSuite) TestCreateDir(c *C) {
5457
func (s *FilesystemSuite) TestCreateDepth(c *C) {
5558
f, err := s.FS.Create("bar/foo")
5659
c.Assert(err, IsNil)
57-
c.Assert(f.Filename(), Equals, "bar/foo")
60+
c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo"))
61+
c.Assert(f.Close(), IsNil)
5862
}
5963

6064
func (s *FilesystemSuite) TestCreateDepthAbsolute(c *C) {
6165
f, err := s.FS.Create("/bar/foo")
6266
c.Assert(err, IsNil)
63-
c.Assert(f.Filename(), Equals, "bar/foo")
67+
c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo"))
68+
c.Assert(f.Close(), IsNil)
6469
}
6570

6671
func (s *FilesystemSuite) TestCreateOverwrite(c *C) {
@@ -82,6 +87,7 @@ func (s *FilesystemSuite) TestCreateOverwrite(c *C) {
8287
wrote, err := ioutil.ReadAll(f)
8388
c.Assert(err, IsNil)
8489
c.Assert(string(wrote), DeepEquals, "foo2")
90+
c.Assert(f.Close(), IsNil)
8591
}
8692

8793
func (s *FilesystemSuite) TestCreateClose(c *C) {
@@ -199,13 +205,13 @@ func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) {
199205
}
200206

201207
func (s *FilesystemSuite) TestOpenFileWithModes(c *C) {
202-
f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
208+
f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, customMode)
203209
c.Assert(err, IsNil)
204210
c.Assert(f.Close(), IsNil)
205211

206212
fi, err := s.FS.Stat("foo")
207213
c.Assert(err, IsNil)
208-
c.Assert(fi.Mode(), Equals, os.FileMode(0755))
214+
c.Assert(fi.Mode(), Equals, os.FileMode(customMode))
209215
}
210216

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

282288
_, err = ioutil.ReadAll(f)
283289
c.Assert(err, NotNil)
290+
291+
c.Assert(f.Close(), IsNil)
284292
}
285293

286294
func (s *FilesystemSuite) TestFileSeekstart(c *C) {
@@ -478,11 +486,14 @@ func (s *FilesystemSuite) TestDirStat(c *C) {
478486
}
479487

480488
func (s *FilesystemSuite) TestCreateInDir(c *C) {
481-
dir := s.FS.Dir("foo")
482-
f, err := dir.Create("bar")
489+
f, err := s.FS.Dir("foo").Create("bar")
483490
c.Assert(err, IsNil)
484491
c.Assert(f.Close(), IsNil)
485492
c.Assert(f.Filename(), Equals, "bar")
493+
494+
f, err = s.FS.Open("foo/bar")
495+
c.Assert(f.Filename(), Equals, s.FS.Join("foo", "bar"))
496+
c.Assert(f.Close(), IsNil)
486497
}
487498

488499
func (s *FilesystemSuite) TestRename(c *C) {
@@ -531,19 +542,24 @@ func (s *FilesystemSuite) TestRenameDir(c *C) {
531542
func (s *FilesystemSuite) TestTempFile(c *C) {
532543
f, err := s.FS.TempFile("", "bar")
533544
c.Assert(err, IsNil)
545+
c.Assert(f.Close(), IsNil)
534546

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

538550
func (s *FilesystemSuite) TestTempFileWithPath(c *C) {
539551
f, err := s.FS.TempFile("foo", "bar")
540552
c.Assert(err, IsNil)
553+
c.Assert(f.Close(), IsNil)
554+
541555
c.Assert(strings.HasPrefix(f.Filename(), s.FS.Join("foo", "bar")), Equals, true)
542556
}
543557

544558
func (s *FilesystemSuite) TestTempFileFullWithPath(c *C) {
545559
f, err := s.FS.TempFile("/foo", "bar")
546560
c.Assert(err, IsNil)
561+
c.Assert(f.Close(), IsNil)
562+
547563
c.Assert(strings.HasPrefix(f.Filename(), s.FS.Join("foo", "bar")), Equals, true)
548564
}
549565

@@ -558,6 +574,8 @@ func (s *FilesystemSuite) TestOpenAndWrite(c *C) {
558574
n, err := foo.Write([]byte("foo"))
559575
c.Assert(err, NotNil)
560576
c.Assert(n, Equals, 0)
577+
578+
c.Assert(foo.Close(), IsNil)
561579
}
562580

563581
func (s *FilesystemSuite) TestOpenAndStat(c *C) {
@@ -568,6 +586,7 @@ func (s *FilesystemSuite) TestOpenAndStat(c *C) {
568586
c.Assert(foo, NotNil)
569587
c.Assert(foo.Filename(), Equals, "foo")
570588
c.Assert(err, IsNil)
589+
c.Assert(foo.Close(), IsNil)
571590

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

613632
func (s *FilesystemSuite) TestJoin(c *C) {
614-
c.Assert(s.FS.Join("foo", "bar"), Equals, "foo/bar")
633+
c.Assert(s.FS.Join("foo", "bar"), Equals, fmt.Sprintf("foo%cbar", filepath.Separator))
615634
}
616635

617636
func (s *FilesystemSuite) TestBase(c *C) {
@@ -663,14 +682,14 @@ func (s *FilesystemSuite) TestReadWriteLargeFile(c *C) {
663682
c.Assert(err, IsNil)
664683
c.Assert(n, Equals, size)
665684

666-
err = f.Close()
667-
c.Assert(err, IsNil)
685+
c.Assert(f.Close(), IsNil)
668686

669687
f, err = s.FS.Open("foo")
670688
c.Assert(err, IsNil)
671689
b, err := ioutil.ReadAll(f)
672690
c.Assert(err, IsNil)
673691
c.Assert(len(b), Equals, size)
692+
c.Assert(f.Close(), IsNil)
674693
}
675694

676695
func (s *FilesystemSuite) TestRemoveAllNonExistent(c *C) {
@@ -806,4 +825,6 @@ func (s *FilesystemSuite) TestWriteFile(c *C) {
806825
wrote, err := ioutil.ReadAll(f)
807826
c.Assert(err, IsNil)
808827
c.Assert(string(wrote), DeepEquals, "bar")
828+
829+
c.Assert(f.Close(), IsNil)
809830
}

test/fs_suite_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !windows
2+
3+
package test
4+
5+
import "os"
6+
7+
var customMode os.FileMode = 0755

test/fs_suite_windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build windows
2+
3+
package test
4+
5+
import "os"
6+
7+
var customMode os.FileMode = 0666

0 commit comments

Comments
 (0)