forked from gobuffalo/packr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
box_test.go
129 lines (107 loc) · 2.54 KB
/
box_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
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
package packr
import (
"bytes"
"io/ioutil"
"os"
"runtime"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Box_FindString(t *testing.T) {
r := require.New(t)
s, err := testBox.FindString("hello.txt")
r.NoError(err)
r.Equal("hello world!", strings.TrimSpace(s))
_, err = testBox.Find("idontexist.txt")
r.Error(err)
}
func Test_Box_FindBytes(t *testing.T) {
r := require.New(t)
s, err := testBox.Find("hello.txt")
r.NoError(err)
r.Equal([]byte("hello world!"), bytes.TrimSpace(s))
_, err = testBox.Find("idontexist.txt")
r.Error(err)
}
func Test_Box_Has(t *testing.T) {
r := require.New(t)
r.True(testBox.Has("hello.txt"))
r.False(testBox.Has("idontexist.txt"))
}
func Test_List_Virtual(t *testing.T) {
r := require.New(t)
mustHave := []string{"a", "b", "c", "d/a"}
actual := virtualBox.List()
sort.Strings(actual)
r.Equal(mustHave, actual)
}
func Test_List_Physical(t *testing.T) {
r := require.New(t)
mustHave := osPaths("MyFile.txt", "foo/a.txt", "foo/bar/b.txt", "goodbye.txt", "hello.txt", "index.html")
actual := testBox.List()
r.Equal(mustHave, actual)
}
func Test_Outside_Box(t *testing.T) {
r := require.New(t)
f, err := ioutil.TempFile("", "")
r.NoError(err)
defer os.RemoveAll(f.Name())
_, err = testBox.FindString(f.Name())
r.Error(err)
}
func Test_Box_find(t *testing.T) {
box := NewBox("./example")
onWindows := runtime.GOOS == "windows"
table := []struct {
name string
found bool
}{
{"assets/app.css", true},
{"assets\\app.css", onWindows},
{"foo/bar.baz", false},
{"bar", true},
{"bar/sub", true},
{"bar/foo", false},
{"bar/sub/sub.html", true},
}
for _, tt := range table {
t.Run(tt.name, func(st *testing.T) {
r := require.New(st)
_, err := box.find(tt.name, false)
if tt.found {
r.True(box.Has(tt.name))
r.NoError(err)
} else {
r.False(box.Has(tt.name))
r.Error(err)
}
})
}
}
func Test_Virtual_Directory_Not_Found(t *testing.T) {
r := require.New(t)
_, err := virtualBox.find("d", false)
r.NoError(err)
_, err = virtualBox.find("does-not-exist", false)
r.Error(err)
}
func Test_AddString(t *testing.T) {
r := require.New(t)
_, err := virtualBox.Find("string")
r.Error(err)
virtualBox.AddString("string", "hello")
s, err := virtualBox.FindString("string")
r.NoError(err)
r.Equal("hello", s)
}
func Test_AddBytes(t *testing.T) {
r := require.New(t)
_, err := virtualBox.Find("bytes")
r.Error(err)
virtualBox.AddBytes("bytes", []byte("hello"))
s, err := virtualBox.Find("bytes")
r.NoError(err)
r.Equal([]byte("hello"), s)
}