This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
binclude_test.go
227 lines (182 loc) · 4.27 KB
/
binclude_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package binclude_test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/lu4p/binclude"
"github.com/lu4p/binclude/example"
)
var BinFS = example.BinFS
func ExampleFileSystem_Open() {
binclude.Include("./assets")
f, _ := BinFS.Open("./assets/asset1.txt")
data, _ := ioutil.ReadAll(f)
fmt.Println(string(data))
// Output: asset1
}
func ExampleFileSystem_ReadFile() {
binclude.Include("file.txt")
data, _ := BinFS.ReadFile("file.txt")
fmt.Println(string(data))
// Output: file.txt
}
func ExampleFileSystem_ReadDir() {
binclude.Include("./assets")
infos, _ := BinFS.ReadDir("./assets")
for _, info := range infos {
fmt.Println(info.Name())
}
// Output: asset1.txt
// asset2.txt
// logo_nocompress.png
// subdir
}
func ExampleFileSystem_CopyFile() {
BinFS.CopyFile("./assets/asset1.txt", "asset1.txt")
c, _ := ioutil.ReadFile("asset1.txt")
fmt.Println(string(c))
// Output: asset1
}
func ExampleIncludeGlob() {
binclude.IncludeGlob("./assets/*.txt")
f, _ := BinFS.Open("./assets/asset1.txt")
data, _ := ioutil.ReadAll(f)
fmt.Println(string(data))
// Output: asset1
}
func TestCopyFile(t *testing.T) {
err := BinFS.CopyFile("./assets/asset1.txt", "asset1.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove("asset1.txt")
c, err := ioutil.ReadFile("asset1.txt")
if err != nil {
t.Fatal(err)
}
if string(c) != "asset1" {
t.Fatal("content doesn't match", c)
}
err = BinFS.CopyFile("nonexistent.txt", "nonexistent.txt")
if err == nil {
t.Fatal("can copy nonexistent file")
}
}
func TestCompression(t *testing.T) {
testPath := "assets/asset1.txt"
startContent, err := BinFS.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
err = BinFS.Compress(binclude.None)
if err != nil {
t.Fatal(err)
}
nocompressContent, err := BinFS.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
if string(startContent) != string(nocompressContent) {
t.Fatal("Compression with binclude.None should be a noop")
}
err = BinFS.Compress(binclude.Gzip)
if err != nil {
t.Fatal(err)
}
if BinFS.Files["assets/logo_nocompress.png"].Compression != binclude.None {
t.Fatal("Unexpected compressed png")
}
gzipContent, err := BinFS.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
if string(startContent) == string(gzipContent) {
t.Fatal("Gzip didn't compress")
}
err = BinFS.Decompress()
if err != nil {
t.Fatal(err)
}
decGzipContent, err := BinFS.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
if string(startContent) != string(decGzipContent) {
t.Fatal("File differs after compression and decompression.")
}
}
func TestReadFile(t *testing.T) {
_, err := BinFS.ReadFile("nonexistent.txt")
if err == nil {
t.Fatal("shouldn't be able to read nonexistent file")
}
}
func TestOpen(t *testing.T) {
f, err := BinFS.Open("file.txt")
if err != nil {
t.Fatal(err)
}
data, _ := ioutil.ReadAll(f)
if string(data) != "file.txt" {
t.Fatal("content does not match")
}
f.Close()
f, err = BinFS.Open("nonexistent.txt")
if err == nil {
f.Close()
t.Fatal("nonexistent file can be opened")
}
binclude.Debug = true
f, err = BinFS.Open("go.mod")
if err != nil {
t.Fatal("cannot use os filesystem")
}
f.Close()
binclude.Debug = false
}
func TestReadDir(t *testing.T) {
_, err := BinFS.ReadDir("./assets/asset1.txt")
if err != nil {
t.Fatal("cannot read directory of file")
}
_, err = BinFS.ReadDir("./nonexistent")
if err == nil {
t.Fatal("shouldn't be able to read nonexistent dir")
}
}
func TestStat(t *testing.T) {
_, err := BinFS.Stat("./assets/asset1.txt")
if err != nil {
t.Fatal("cannot stat file")
}
_, err = BinFS.Stat("./nonexistent")
if err == nil {
t.Fatal("shouldn't be able to stat nonexistent dir")
}
}
func TestFileInfo(t *testing.T) {
file := BinFS.Files["assets/asset1.txt"]
info, err := BinFS.Stat("./assets/asset1.txt")
if err != nil {
t.Fatal("cannot stat file")
}
if info.Name() != file.Filename {
t.Fatal("Name does not match")
}
if int(info.Size()) != len(file.Content) {
t.Fatal("Size does not match")
}
if info.Mode() != file.Mode {
t.Fatal("Mode does not match")
}
if info.IsDir() != file.Mode.IsDir() {
t.Fatal("IsDir does not match")
}
if info.ModTime() != file.ModTime {
t.Fatal("ModTime does not match")
}
if info.Sys() != nil {
t.Fatal("Sys return should be nil")
}
}