1
2
3
4
5 package embedtest
6
7 import (
8 "embed"
9 "reflect"
10 "testing"
11 "testing/fstest"
12 )
13
14
15
16 var global embed.FS
17
18
19 var concurrency string
20
21
22 var glass []byte
23
24 func testFiles(t *testing.T, f embed.FS, name, data string) {
25 t.Helper()
26 d, err := f.ReadFile(name)
27 if err != nil {
28 t.Error(err)
29 return
30 }
31 if string(d) != data {
32 t.Errorf("read %v = %q, want %q", name, d, data)
33 }
34 }
35
36 func testString(t *testing.T, s, name, data string) {
37 t.Helper()
38 if s != data {
39 t.Errorf("%v = %q, want %q", name, s, data)
40 }
41 }
42
43 func testDir(t *testing.T, f embed.FS, name string, expect ...string) {
44 t.Helper()
45 dirs, err := f.ReadDir(name)
46 if err != nil {
47 t.Error(err)
48 return
49 }
50 var names []string
51 for _, d := range dirs {
52 name := d.Name()
53 if d.IsDir() {
54 name += "/"
55 }
56 names = append(names, name)
57 }
58 if !reflect.DeepEqual(names, expect) {
59 t.Errorf("readdir %v = %v, want %v", name, names, expect)
60 }
61 }
62
63
64 var _ = '"'
65 var _ = '\''
66 var _ = '🦆'
67
68 func TestGlobal(t *testing.T) {
69 testFiles(t, global, "concurrency.txt", "Concurrency is not parallelism.\n")
70 testFiles(t, global, "testdata/hello.txt", "hello, world\n")
71 testFiles(t, global, "testdata/glass.txt", "I can eat glass and it doesn't hurt me.\n")
72
73 if err := fstest.TestFS(global, "concurrency.txt", "testdata/hello.txt"); err != nil {
74 t.Fatal(err)
75 }
76
77 testString(t, concurrency, "concurrency", "Concurrency is not parallelism.\n")
78 testString(t, string(glass), "glass", "I can eat glass and it doesn't hurt me.\n")
79 }
80
81
82 var testDirAll embed.FS
83
84 func TestDir(t *testing.T) {
85 all := testDirAll
86 testFiles(t, all, "testdata/hello.txt", "hello, world\n")
87 testFiles(t, all, "testdata/i/i18n.txt", "internationalization\n")
88 testFiles(t, all, "testdata/i/j/k/k8s.txt", "kubernetes\n")
89 testFiles(t, all, "testdata/ken.txt", "If a program is too slow, it must have a loop.\n")
90
91 testDir(t, all, ".", "testdata/")
92 testDir(t, all, "testdata/i", "i18n.txt", "j/")
93 testDir(t, all, "testdata/i/j", "k/")
94 testDir(t, all, "testdata/i/j/k", "k8s.txt")
95 }
96
97 var (
98
99 testHiddenDir embed.FS
100
101
102 testHiddenStar embed.FS
103 )
104
105 func TestHidden(t *testing.T) {
106 dir := testHiddenDir
107 star := testHiddenStar
108
109 t.Logf("//go:embed testdata")
110
111 testDir(t, dir, "testdata",
112 "-not-hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
113
114 t.Logf("//go:embed testdata/*")
115
116 testDir(t, star, "testdata",
117 "-not-hidden/", ".hidden/", "_hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
118
119 testDir(t, star, "testdata/.hidden",
120 "fortune.txt", "more/")
121 }
122
123 func TestUninitialized(t *testing.T) {
124 var uninitialized embed.FS
125 testDir(t, uninitialized, ".")
126 f, err := uninitialized.Open(".")
127 if err != nil {
128 t.Fatal(err)
129 }
130 defer f.Close()
131 fi, err := f.Stat()
132 if err != nil {
133 t.Fatal(err)
134 }
135 if !fi.IsDir() {
136 t.Errorf("in uninitialized embed.FS, . is not a directory")
137 }
138 }
139
140 var (
141
142 helloT []T
143
144 helloUint8 []uint8
145
146 helloEUint8 []EmbedUint8
147
148 helloBytes EmbedBytes
149
150 helloString EmbedString
151 )
152
153 type T byte
154 type EmbedUint8 uint8
155 type EmbedBytes []byte
156 type EmbedString string
157
158
159 func TestAliases(t *testing.T) {
160 all := testDirAll
161 want, e := all.ReadFile("testdata/hello.txt")
162 if e != nil {
163 t.Fatal("ReadFile:", e)
164 }
165 check := func(g any) {
166 got := reflect.ValueOf(g)
167 for i := 0; i < got.Len(); i++ {
168 if byte(got.Index(i).Uint()) != want[i] {
169 t.Fatalf("got %v want %v", got.Bytes(), want)
170 }
171 }
172 }
173 check(helloT)
174 check(helloUint8)
175 check(helloEUint8)
176 check(helloBytes)
177 check(helloString)
178 }
179
View as plain text