-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_test.go
79 lines (69 loc) · 1.64 KB
/
fs_test.go
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
package ofs_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/db47h/ofs"
)
type testData struct {
path string
contents string
}
var td = []testData{
{"testdata/foodir/foobar", "foobar"},
{"testdata/foodir/foofoo", "foofoo_fromdir"},
{"testdata/foo", "foo_fromdir"},
{"testdata/bar", "bar_cache"},
}
func Test_Overlay(t *testing.T) {
var ovl ofs.Overlay
// create cache
tmp, err := ioutil.TempDir("", "goovl")
if err != nil {
panic(err)
}
t.Log("created temp dir ", tmp)
defer os.RemoveAll(tmp)
err = os.Mkdir(filepath.Join(tmp, "testdata"), 0700)
if err != nil {
t.Fatal("failed to create temp dir:", err)
}
err = ovl.Add(true, "testdata.zip", ".", tmp)
if err != nil {
t.Fatal("failed to create overlay:", err)
}
// test create
f, err := ovl.Create("testdata/bar")
if err != nil {
t.Fatal("failed to create file in overlay:", err)
}
_, err = f.Write([]byte("bar_cache"))
f.Close()
if err != nil {
t.Fatal("failed to write data to cache:", err)
}
// check that the file go created in the cache
cached := filepath.Join(tmp, "testdata/bar")
fi, err := os.Stat(cached)
if err != nil {
t.Fatal("failed to stat cached file", err)
}
if fi.Size() != 9 {
t.Fatalf("bad cached data: expected size %d, got %d", 9, fi.Size())
}
// now test reading
for _, d := range td {
f, err := ovl.Open(d.path)
if err != nil {
t.Errorf("failed to open %s: %v", d.path, err)
}
c, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("failed to read from %s: %v", d.path, err)
}
if string(c) != d.contents {
t.Errorf("%s: bad file contents. Expected %s, got %s", d.path, d.contents, c)
}
}
}