Skip to content

Commit

Permalink
osfs: fix js implementation based on menfs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 22, 2021
1 parent 43a34ba commit 42ead41
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
12 changes: 3 additions & 9 deletions osfs/os_js.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build js

package osfs

import (
Expand All @@ -9,15 +11,7 @@ import (
// globalMemFs is the global memory fs
var globalMemFs = memfs.New()

const (
defaultDirectoryMode = 0755
defaultCreateMode = 0666
)

// OS is a filesystem shim for js.
type OS struct{}

// New returns a new OS filesystem.
func New(baseDir string) billy.Filesystem {
return chroot.New(globalMemFs, baseDir)
return chroot.New(globalMemFs, globalMemFs.Join("/", baseDir))
}
47 changes: 47 additions & 0 deletions osfs/os_js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// +build js

package osfs

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/test"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type OSSuite struct {
test.FilesystemSuite
path string
tempCounter int
}

var _ = Suite(&OSSuite{})

func (s *OSSuite) SetUpTest(c *C) {
s.tempCounter++
s.path = fmt.Sprintf("test_%d", s.tempCounter)
s.FilesystemSuite = test.NewFilesystemSuite(New(s.path))
}

func (s *OSSuite) TestOpenDoesNotCreateDir(c *C) {
_, err := s.FS.Open("dir/non-existent")
c.Assert(err, NotNil)

_, err = s.FS.Stat(filepath.Join(s.path, "dir"))
c.Assert(os.IsNotExist(err), Equals, true)
}

func (s *OSSuite) TestCapabilities(c *C) {
_, ok := s.FS.(billy.Capable)
c.Assert(ok, Equals, true)

caps := billy.Capabilities(s.FS)
c.Assert(caps, Equals, billy.DefaultCapabilities&^billy.LockCapability)
}
2 changes: 2 additions & 0 deletions osfs/os_plan9.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build plan9

package osfs

import (
Expand Down
2 changes: 2 additions & 0 deletions osfs/os_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !js

package osfs

import (
Expand Down

0 comments on commit 42ead41

Please sign in to comment.