diff options
Diffstat (limited to '')
15 files changed, 310 insertions, 0 deletions
diff --git a/src/embed/internal/embedtest/concurrency.txt b/src/embed/internal/embedtest/concurrency.txt new file mode 100644 index 0000000..0814741 --- /dev/null +++ b/src/embed/internal/embedtest/concurrency.txt @@ -0,0 +1 @@ +Concurrency is not parallelism. diff --git a/src/embed/internal/embedtest/embed_test.go b/src/embed/internal/embedtest/embed_test.go new file mode 100644 index 0000000..cbd58ee --- /dev/null +++ b/src/embed/internal/embedtest/embed_test.go @@ -0,0 +1,178 @@ +// 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" + "reflect" + "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 !reflect.DeepEqual(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) +} diff --git a/src/embed/internal/embedtest/embedx_test.go b/src/embed/internal/embedtest/embedx_test.go new file mode 100644 index 0000000..27fa116 --- /dev/null +++ b/src/embed/internal/embedtest/embedx_test.go @@ -0,0 +1,92 @@ +// 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_test + +import ( + "embed" + "os" + "testing" +) + +var ( + global2 = global + concurrency2 = concurrency + glass2 = glass + sbig2 = sbig + bbig2 = bbig +) + +//go:embed testdata/*.txt +var global embed.FS + +//go:embed c*txt +var concurrency string + +//go:embed testdata/g*.txt +var glass []byte + +//go:embed testdata/ascii.txt +var sbig string + +//go:embed testdata/ascii.txt +var bbig []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 TestXGlobal(t *testing.T) { + testFiles(t, global, "testdata/hello.txt", "hello, world\n") + 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") + testString(t, concurrency2, "concurrency2", "Concurrency is not parallelism.\n") + testString(t, string(glass2), "glass2", "I can eat glass and it doesn't hurt me.\n") + + big, err := os.ReadFile("testdata/ascii.txt") + if err != nil { + t.Fatal(err) + } + testString(t, sbig, "sbig", string(big)) + testString(t, sbig2, "sbig2", string(big)) + testString(t, string(bbig), "bbig", string(big)) + testString(t, string(bbig2), "bbig", string(big)) + + if t.Failed() { + return + } + + // Could check &glass[0] == &glass2[0] but also want to make sure write does not fault + // (data must not be in read-only memory). + old := glass[0] + glass[0]++ + if glass2[0] != glass[0] { + t.Fatalf("glass and glass2 do not share storage") + } + glass[0] = old + + // Could check &bbig[0] == &bbig2[0] but also want to make sure write does not fault + // (data must not be in read-only memory). + old = bbig[0] + bbig[0]++ + if bbig2[0] != bbig[0] { + t.Fatalf("bbig and bbig2 do not share storage") + } + bbig[0] = old +} diff --git a/src/embed/internal/embedtest/testdata/-not-hidden/fortune.txt b/src/embed/internal/embedtest/testdata/-not-hidden/fortune.txt new file mode 100644 index 0000000..31f2013 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/-not-hidden/fortune.txt @@ -0,0 +1,2 @@ +WARNING: terminal is not fully functional + - (press RETURN) diff --git a/src/embed/internal/embedtest/testdata/.hidden/.more/tip.txt b/src/embed/internal/embedtest/testdata/.hidden/.more/tip.txt new file mode 100644 index 0000000..71b9c69 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/.hidden/.more/tip.txt @@ -0,0 +1 @@ +#define struct union /* Great space saver */ diff --git a/src/embed/internal/embedtest/testdata/.hidden/_more/tip.txt b/src/embed/internal/embedtest/testdata/.hidden/_more/tip.txt new file mode 100644 index 0000000..71b9c69 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/.hidden/_more/tip.txt @@ -0,0 +1 @@ +#define struct union /* Great space saver */ diff --git a/src/embed/internal/embedtest/testdata/.hidden/fortune.txt b/src/embed/internal/embedtest/testdata/.hidden/fortune.txt new file mode 100644 index 0000000..31f2013 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/.hidden/fortune.txt @@ -0,0 +1,2 @@ +WARNING: terminal is not fully functional + - (press RETURN) diff --git a/src/embed/internal/embedtest/testdata/.hidden/more/tip.txt b/src/embed/internal/embedtest/testdata/.hidden/more/tip.txt new file mode 100644 index 0000000..71b9c69 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/.hidden/more/tip.txt @@ -0,0 +1 @@ +#define struct union /* Great space saver */ diff --git a/src/embed/internal/embedtest/testdata/_hidden/fortune.txt b/src/embed/internal/embedtest/testdata/_hidden/fortune.txt new file mode 100644 index 0000000..31f2013 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/_hidden/fortune.txt @@ -0,0 +1,2 @@ +WARNING: terminal is not fully functional + - (press RETURN) diff --git a/src/embed/internal/embedtest/testdata/ascii.txt b/src/embed/internal/embedtest/testdata/ascii.txt new file mode 100644 index 0000000..0cfebf6 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/ascii.txt @@ -0,0 +1,25 @@ + !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn +!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno +"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnop +#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopq +$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqr +%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrs +&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrst +'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu +()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuv +)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvw +*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwx ++,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxy +,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz +-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{ +./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{| +/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} +0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} +123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} ! +23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !" +3456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"# +456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$ +56789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$% +6789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$%& +789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$%&' +89:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$%&'( diff --git a/src/embed/internal/embedtest/testdata/glass.txt b/src/embed/internal/embedtest/testdata/glass.txt new file mode 100644 index 0000000..8350baf --- /dev/null +++ b/src/embed/internal/embedtest/testdata/glass.txt @@ -0,0 +1 @@ +I can eat glass and it doesn't hurt me. diff --git a/src/embed/internal/embedtest/testdata/hello.txt b/src/embed/internal/embedtest/testdata/hello.txt new file mode 100644 index 0000000..4b5fa63 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/hello.txt @@ -0,0 +1 @@ +hello, world diff --git a/src/embed/internal/embedtest/testdata/i/i18n.txt b/src/embed/internal/embedtest/testdata/i/i18n.txt new file mode 100644 index 0000000..5ee27c6 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/i/i18n.txt @@ -0,0 +1 @@ +internationalization diff --git a/src/embed/internal/embedtest/testdata/i/j/k/k8s.txt b/src/embed/internal/embedtest/testdata/i/j/k/k8s.txt new file mode 100644 index 0000000..807e21b --- /dev/null +++ b/src/embed/internal/embedtest/testdata/i/j/k/k8s.txt @@ -0,0 +1 @@ +kubernetes diff --git a/src/embed/internal/embedtest/testdata/ken.txt b/src/embed/internal/embedtest/testdata/ken.txt new file mode 100644 index 0000000..bb25981 --- /dev/null +++ b/src/embed/internal/embedtest/testdata/ken.txt @@ -0,0 +1 @@ +If a program is too slow, it must have a loop. |