aboutsummaryrefslogtreecommitdiffstats
path: root/utils/fs/os_test.go
blob: ea0697063e32bea18d9f1d589b8fd6c6f462a0ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package fs

import (
	"io"
	"io/ioutil"
	"os"
	"testing"

	. "gopkg.in/check.v1"
)

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

type OSSuite struct{}

var _ = Suite(&OSSuite{})

func (s *OSSuite) TestCreate(c *C) {
	path := getTempDir()
	client := NewOS(path)

	f, err := client.Create("foo")
	c.Assert(err, IsNil)
	c.Assert(f.Filename(), Equals, "foo")
}

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)

	f, err := client.Create("foo")
	c.Assert(err, IsNil)
	l, err := f.Write([]byte("foo"))
	c.Assert(l, Equals, 3)
	c.Assert(err, IsNil)

	f.Seek(0, io.SeekStart)
	wrote, err := ioutil.ReadAll(f)
	c.Assert(err, IsNil)
	c.Assert(wrote, DeepEquals, []byte("foo"))
}

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 {
	dir, _ := ioutil.TempDir(os.TempDir(), "--OSClientTest--")
	return dir
}