aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/fs/fs.go1
-rw-r--r--utils/fs/os_test.go104
2 files changed, 99 insertions, 6 deletions
diff --git a/utils/fs/fs.go b/utils/fs/fs.go
index 4c97340..3bcf88f 100644
--- a/utils/fs/fs.go
+++ b/utils/fs/fs.go
@@ -26,6 +26,7 @@ type Filesystem interface {
type File interface {
Filename() string
+ IsClosed() bool
io.Writer
io.Reader
io.Seeker
diff --git a/utils/fs/os_test.go b/utils/fs/os_test.go
index a040ddf..ea06970 100644
--- a/utils/fs/os_test.go
+++ b/utils/fs/os_test.go
@@ -1,6 +1,7 @@
package fs
import (
+ "io"
"io/ioutil"
"os"
"testing"
@@ -10,11 +11,11 @@ import (
func Test(t *testing.T) { TestingT(t) }
-type WritersSuite struct{}
+type OSSuite struct{}
-var _ = Suite(&WritersSuite{})
+var _ = Suite(&OSSuite{})
-func (s *WritersSuite) TestOSClient_Create(c *C) {
+func (s *OSSuite) TestCreate(c *C) {
path := getTempDir()
client := NewOS(path)
@@ -23,7 +24,16 @@ func (s *WritersSuite) TestOSClient_Create(c *C) {
c.Assert(f.Filename(), Equals, "foo")
}
-func (s *WritersSuite) TestOSClient_Write(c *C) {
+func (s *OSSuite) TestCreateDepth(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+
+ f, err := client.Create("bar/foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Filename(), Equals, "bar/foo")
+}
+
+func (s *OSSuite) TestCreateAndWrite(c *C) {
path := getTempDir()
client := NewOS(path)
@@ -33,21 +43,103 @@ func (s *WritersSuite) TestOSClient_Write(c *C) {
c.Assert(l, Equals, 3)
c.Assert(err, IsNil)
- wrote, _ := ioutil.ReadFile(f.(*OSFile).file.Name())
+ f.Seek(0, io.SeekStart)
+ wrote, err := ioutil.ReadAll(f)
+ c.Assert(err, IsNil)
c.Assert(wrote, DeepEquals, []byte("foo"))
}
-func (s *WritersSuite) TestOSClient_Close(c *C) {
+func (s *OSSuite) TestCreateClose(c *C) {
path := getTempDir()
client := NewOS(path)
f, err := client.Create("foo")
c.Assert(err, IsNil)
+ c.Assert(f.IsClosed(), Equals, false)
+
f.Write([]byte("foo"))
c.Assert(f.Close(), IsNil)
wrote, _ := ioutil.ReadFile(f.(*OSFile).file.Name())
c.Assert(wrote, DeepEquals, []byte("foo"))
+
+ c.Assert(f.IsClosed(), Equals, true)
+}
+
+func (s *OSSuite) TestReadDirAndDir(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+
+ files := []string{"foo", "bar", "qux/baz", "qux/qux"}
+ for _, name := range files {
+ f, err := client.Create(name)
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+ }
+
+ info, err := client.ReadDir("/")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 3)
+
+ info, err = client.ReadDir("/qux")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 2)
+
+ qux := client.Dir("/qux")
+ info, err = qux.ReadDir("/")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 2)
+}
+
+func (s *OSSuite) TestRename(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+
+ f, err := client.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+
+ err = client.Rename("foo", "bar")
+ c.Assert(err, IsNil)
+
+ foo, err := client.Stat("foo")
+ c.Assert(foo, IsNil)
+ c.Assert(err, NotNil)
+
+ bar, err := client.Stat("bar")
+ c.Assert(bar, NotNil)
+ c.Assert(err, IsNil)
+}
+
+func (s *OSSuite) TestOpenAndStat(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+
+ f, err := client.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+
+ foo, err := client.Open("foo")
+ c.Assert(foo, NotNil)
+ c.Assert(foo.Filename(), Equals, "foo")
+ c.Assert(err, IsNil)
+
+ stat, err := client.Stat("foo")
+ c.Assert(stat, NotNil)
+ c.Assert(err, IsNil)
+ c.Assert(stat.Name(), Equals, "foo")
+}
+
+func (s *OSSuite) TestJoin(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+ c.Assert(client.Join("foo", "bar"), Equals, "foo/bar")
+}
+
+func (s *OSSuite) TestBase(c *C) {
+ path := getTempDir()
+ client := NewOS(path)
+ c.Assert(client.Base(), Equals, path)
}
func getTempDir() string {