File size: 5,543 Bytes
e36aeda | 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 | // Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package embedtest
import (
"embed"
"io"
"reflect"
"slices"
"testing"
"testing/fstest"
)
//go:embed testdata/h*.txt
//go:embed c*.txt testdata/g*.txt
var global embed.FS
//go:embed c*txt
var concurrency string
//go:embed testdata/g*.txt
var glass []byte
func testFiles(t *testing.T, f embed.FS, name, data string) {
t.Helper()
d, err := f.ReadFile(name)
if err != nil {
t.Error(err)
return
}
if string(d) != data {
t.Errorf("read %v = %q, want %q", name, d, data)
}
}
func testString(t *testing.T, s, name, data string) {
t.Helper()
if s != data {
t.Errorf("%v = %q, want %q", name, s, data)
}
}
func testDir(t *testing.T, f embed.FS, name string, expect ...string) {
t.Helper()
dirs, err := f.ReadDir(name)
if err != nil {
t.Error(err)
return
}
var names []string
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
names = append(names, name)
}
if !slices.Equal(names, expect) {
t.Errorf("readdir %v = %v, want %v", name, names, expect)
}
}
// Tests for issue 49514.
var _ = '"'
var _ = '\''
var _ = '🦆'
func TestGlobal(t *testing.T) {
testFiles(t, global, "concurrency.txt", "Concurrency is not parallelism.\n")
testFiles(t, global, "testdata/hello.txt", "hello, world\n")
testFiles(t, global, "testdata/glass.txt", "I can eat glass and it doesn't hurt me.\n")
if err := fstest.TestFS(global, "concurrency.txt", "testdata/hello.txt"); err != nil {
t.Fatal(err)
}
testString(t, concurrency, "concurrency", "Concurrency is not parallelism.\n")
testString(t, string(glass), "glass", "I can eat glass and it doesn't hurt me.\n")
}
//go:embed testdata
var testDirAll embed.FS
func TestDir(t *testing.T) {
all := testDirAll
testFiles(t, all, "testdata/hello.txt", "hello, world\n")
testFiles(t, all, "testdata/i/i18n.txt", "internationalization\n")
testFiles(t, all, "testdata/i/j/k/k8s.txt", "kubernetes\n")
testFiles(t, all, "testdata/ken.txt", "If a program is too slow, it must have a loop.\n")
testDir(t, all, ".", "testdata/")
testDir(t, all, "testdata/i", "i18n.txt", "j/")
testDir(t, all, "testdata/i/j", "k/")
testDir(t, all, "testdata/i/j/k", "k8s.txt")
}
var (
//go:embed testdata
testHiddenDir embed.FS
//go:embed testdata/*
testHiddenStar embed.FS
)
func TestHidden(t *testing.T) {
dir := testHiddenDir
star := testHiddenStar
t.Logf("//go:embed testdata")
testDir(t, dir, "testdata",
"-not-hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
t.Logf("//go:embed testdata/*")
testDir(t, star, "testdata",
"-not-hidden/", ".hidden/", "_hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
testDir(t, star, "testdata/.hidden",
"fortune.txt", "more/") // but not .more or _more
}
func TestUninitialized(t *testing.T) {
var uninitialized embed.FS
testDir(t, uninitialized, ".")
f, err := uninitialized.Open(".")
if err != nil {
t.Fatal(err)
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
t.Fatal(err)
}
if !fi.IsDir() {
t.Errorf("in uninitialized embed.FS, . is not a directory")
}
}
var (
//go:embed "testdata/hello.txt"
helloT []T
//go:embed "testdata/hello.txt"
helloUint8 []uint8
//go:embed "testdata/hello.txt"
helloEUint8 []EmbedUint8
//go:embed "testdata/hello.txt"
helloBytes EmbedBytes
//go:embed "testdata/hello.txt"
helloString EmbedString
)
type T byte
type EmbedUint8 uint8
type EmbedBytes []byte
type EmbedString string
// golang.org/issue/47735
func TestAliases(t *testing.T) {
all := testDirAll
want, e := all.ReadFile("testdata/hello.txt")
if e != nil {
t.Fatal("ReadFile:", e)
}
check := func(g any) {
got := reflect.ValueOf(g)
for i := 0; i < got.Len(); i++ {
if byte(got.Index(i).Uint()) != want[i] {
t.Fatalf("got %v want %v", got.Bytes(), want)
}
}
}
check(helloT)
check(helloUint8)
check(helloEUint8)
check(helloBytes)
check(helloString)
}
func TestOffset(t *testing.T) {
file, err := testDirAll.Open("testdata/hello.txt")
if err != nil {
t.Fatal("Open:", err)
}
want := "hello, world\n"
// Read the entire file.
got := make([]byte, len(want))
n, err := file.Read(got)
if err != nil {
t.Fatal("Read:", err)
}
if n != len(want) {
t.Fatal("Read:", n)
}
if string(got) != want {
t.Fatalf("Read: %q", got)
}
// Try to read one byte; confirm we're at the EOF.
var buf [1]byte
n, err = file.Read(buf[:])
if err != io.EOF {
t.Fatal("Read:", err)
}
if n != 0 {
t.Fatal("Read:", n)
}
// Use seek to get the offset at the EOF.
seeker := file.(io.Seeker)
off, err := seeker.Seek(0, io.SeekCurrent)
if err != nil {
t.Fatal("Seek:", err)
}
if off != int64(len(want)) {
t.Fatal("Seek:", off)
}
// Use ReadAt to read the entire file, ignoring the offset.
at := file.(io.ReaderAt)
got = make([]byte, len(want))
n, err = at.ReadAt(got, 0)
if err != nil {
t.Fatal("ReadAt:", err)
}
if n != len(want) {
t.Fatalf("ReadAt: got %d bytes, want %d bytes", n, len(want))
}
if string(got) != want {
t.Fatalf("ReadAt: got %q, want %q", got, want)
}
// Use ReadAt with non-zero offset.
off = int64(7)
want = want[off:]
got = make([]byte, len(want))
n, err = at.ReadAt(got, off)
if err != nil {
t.Fatal("ReadAt:", err)
}
if n != len(want) {
t.Fatalf("ReadAt: got %d bytes, want %d bytes", n, len(want))
}
if string(got) != want {
t.Fatalf("ReadAt: got %q, want %q", got, want)
}
}
|