-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed_test.go
More file actions
265 lines (214 loc) · 6.58 KB
/
embed_test.go
File metadata and controls
265 lines (214 loc) · 6.58 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package core_test
import (
"bytes"
"compress/gzip"
"encoding/base64"
"testing"
. "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
// --- Mount ---
func mustMountTestFS(t *testing.T, basedir string) *Embed {
t.Helper()
r := Mount(testFS, basedir)
assert.True(t, r.OK)
return r.Value.(*Embed)
}
func TestEmbed_Mount_Good(t *testing.T) {
r := Mount(testFS, "testdata")
assert.True(t, r.OK)
}
func TestEmbed_Mount_Bad(t *testing.T) {
r := Mount(testFS, "nonexistent")
assert.False(t, r.OK)
}
// --- Embed methods ---
func TestEmbed_ReadFile_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.ReadFile("test.txt")
assert.True(t, r.OK)
assert.Equal(t, "hello from testdata\n", string(r.Value.([]byte)))
}
func TestEmbed_ReadString_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.ReadString("test.txt")
assert.True(t, r.OK)
assert.Equal(t, "hello from testdata\n", r.Value.(string))
}
func TestEmbed_Open_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.Open("test.txt")
assert.True(t, r.OK)
}
func TestEmbed_ReadDir_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.ReadDir(".")
assert.True(t, r.OK)
assert.NotEmpty(t, r.Value)
}
func TestEmbed_Sub_Good(t *testing.T) {
emb := mustMountTestFS(t, ".")
r := emb.Sub("testdata")
assert.True(t, r.OK)
sub := r.Value.(*Embed)
r2 := sub.ReadFile("test.txt")
assert.True(t, r2.OK)
}
func TestEmbed_BaseDir_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
assert.Equal(t, "testdata", emb.BaseDirectory())
}
func TestEmbed_FS_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
assert.NotNil(t, emb.FS())
}
func TestEmbed_EmbedFS_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
efs := emb.EmbedFS()
_, err := efs.ReadFile("testdata/test.txt")
assert.NoError(t, err)
}
// --- Extract ---
func TestEmbed_Extract_Good(t *testing.T) {
dir := t.TempDir()
r := Extract(testFS, dir, nil)
assert.True(t, r.OK)
cr := (&Fs{}).New("/").Read(Path(dir, "testdata/test.txt"))
assert.True(t, cr.OK)
assert.Equal(t, "hello from testdata\n", cr.Value)
}
// --- Asset Pack ---
func TestEmbed_AddGetAsset_Good(t *testing.T) {
AddAsset("test-group", "greeting", mustCompress("hello world"))
r := GetAsset("test-group", "greeting")
assert.True(t, r.OK)
assert.Equal(t, "hello world", r.Value.(string))
}
func TestEmbed_GetAsset_Bad(t *testing.T) {
r := GetAsset("missing-group", "missing")
assert.False(t, r.OK)
}
func TestEmbed_GetAssetBytes_Good(t *testing.T) {
AddAsset("bytes-group", "file", mustCompress("binary content"))
r := GetAssetBytes("bytes-group", "file")
assert.True(t, r.OK)
assert.Equal(t, []byte("binary content"), r.Value.([]byte))
}
func TestEmbed_MountEmbed_Good(t *testing.T) {
r := MountEmbed(testFS, "testdata")
assert.True(t, r.OK)
}
// --- ScanAssets ---
func TestEmbed_ScanAssets_Good(t *testing.T) {
r := ScanAssets([]string{"testdata/scantest/sample.go"})
assert.True(t, r.OK)
pkgs := r.Value.([]ScannedPackage)
assert.Len(t, pkgs, 1)
assert.Equal(t, "scantest", pkgs[0].PackageName)
}
func TestEmbed_ScanAssets_Bad(t *testing.T) {
r := ScanAssets([]string{"nonexistent.go"})
assert.False(t, r.OK)
}
func TestEmbed_GeneratePack_Empty_Good(t *testing.T) {
pkg := ScannedPackage{PackageName: "empty"}
r := GeneratePack(pkg)
assert.True(t, r.OK)
assert.Contains(t, r.Value.(string), "package empty")
}
func TestEmbed_GeneratePack_WithFiles_Good(t *testing.T) {
dir := t.TempDir()
assetDir := Path(dir, "mygroup")
(&Fs{}).New("/").EnsureDir(assetDir)
(&Fs{}).New("/").Write(Path(assetDir, "hello.txt"), "hello world")
source := "package test\nimport \"dappco.re/go/core\"\nfunc example() {\n\t_, _ = core.GetAsset(\"mygroup\", \"hello.txt\")\n}\n"
goFile := Path(dir, "test.go")
(&Fs{}).New("/").Write(goFile, source)
sr := ScanAssets([]string{goFile})
assert.True(t, sr.OK)
pkgs := sr.Value.([]ScannedPackage)
r := GeneratePack(pkgs[0])
assert.True(t, r.OK)
assert.Contains(t, r.Value.(string), "core.AddAsset")
}
// --- Extract (template + nested) ---
func TestEmbed_Extract_WithTemplate_Good(t *testing.T) {
dir := t.TempDir()
// Create an in-memory FS with a template file and a plain file
tmplDir := DirFS(t.TempDir())
// Use a real temp dir with files
srcDir := t.TempDir()
(&Fs{}).New("/").Write(Path(srcDir, "plain.txt"), "static content")
(&Fs{}).New("/").Write(Path(srcDir, "greeting.tmpl"), "Hello {{.Name}}!")
(&Fs{}).New("/").EnsureDir(Path(srcDir, "sub"))
(&Fs{}).New("/").Write(Path(srcDir, "sub/nested.txt"), "nested")
_ = tmplDir
fsys := DirFS(srcDir)
data := map[string]string{"Name": "World"}
r := Extract(fsys, dir, data)
assert.True(t, r.OK)
f := (&Fs{}).New("/")
// Plain file copied
cr := f.Read(Path(dir, "plain.txt"))
assert.True(t, cr.OK)
assert.Equal(t, "static content", cr.Value)
// Template processed and .tmpl stripped
gr := f.Read(Path(dir, "greeting"))
assert.True(t, gr.OK)
assert.Equal(t, "Hello World!", gr.Value)
// Nested directory preserved
nr := f.Read(Path(dir, "sub/nested.txt"))
assert.True(t, nr.OK)
assert.Equal(t, "nested", nr.Value)
}
func TestEmbed_Extract_BadTargetDir_Ugly(t *testing.T) {
srcDir := t.TempDir()
(&Fs{}).New("/").Write(Path(srcDir, "f.txt"), "x")
r := Extract(DirFS(srcDir), "/nonexistent/deeply/nested/impossible", nil)
// Should fail gracefully, not panic
_ = r
}
func TestEmbed_PathTraversal_Ugly(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.ReadFile("../../etc/passwd")
assert.False(t, r.OK)
}
func TestEmbed_Sub_BaseDir_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.Sub("scantest")
assert.True(t, r.OK)
sub := r.Value.(*Embed)
assert.Equal(t, ".", sub.BaseDirectory())
}
func TestEmbed_Open_Bad(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.Open("nonexistent.txt")
assert.False(t, r.OK)
}
func TestEmbed_ReadDir_Bad(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
r := emb.ReadDir("nonexistent")
assert.False(t, r.OK)
}
func TestEmbed_EmbedFS_Original_Good(t *testing.T) {
emb := mustMountTestFS(t, "testdata")
efs := emb.EmbedFS()
_, err := efs.ReadFile("testdata/test.txt")
assert.NoError(t, err)
}
func TestEmbed_Extract_NilData_Good(t *testing.T) {
dir := t.TempDir()
srcDir := t.TempDir()
(&Fs{}).New("/").Write(Path(srcDir, "file.txt"), "no template")
r := Extract(DirFS(srcDir), dir, nil)
assert.True(t, r.OK)
}
func mustCompress(input string) string {
var buf bytes.Buffer
b64 := base64.NewEncoder(base64.StdEncoding, &buf)
gz, _ := gzip.NewWriterLevel(b64, gzip.BestCompression)
gz.Write([]byte(input))
gz.Close()
b64.Close()
return buf.String()
}