Black Lives Matter. Support the Equal Justice Initiative.

Source file src/embed/internal/embedtest/embed_test.go

Documentation: embed/internal/embedtest

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package embedtest
     6  
     7  import (
     8  	"embed"
     9  	"reflect"
    10  	"testing"
    11  	"testing/fstest"
    12  )
    13  
    14  //go:embed testdata/h*.txt
    15  //go:embed c*.txt testdata/g*.txt
    16  var global embed.FS
    17  
    18  //go:embed c*txt
    19  var concurrency string
    20  
    21  //go:embed testdata/g*.txt
    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  func TestGlobal(t *testing.T) {
    64  	testFiles(t, global, "concurrency.txt", "Concurrency is not parallelism.\n")
    65  	testFiles(t, global, "testdata/hello.txt", "hello, world\n")
    66  	testFiles(t, global, "testdata/glass.txt", "I can eat glass and it doesn't hurt me.\n")
    67  
    68  	if err := fstest.TestFS(global, "concurrency.txt", "testdata/hello.txt"); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	testString(t, concurrency, "concurrency", "Concurrency is not parallelism.\n")
    73  	testString(t, string(glass), "glass", "I can eat glass and it doesn't hurt me.\n")
    74  }
    75  
    76  //go:embed testdata
    77  var testDirAll embed.FS
    78  
    79  func TestDir(t *testing.T) {
    80  	all := testDirAll
    81  	testFiles(t, all, "testdata/hello.txt", "hello, world\n")
    82  	testFiles(t, all, "testdata/i/i18n.txt", "internationalization\n")
    83  	testFiles(t, all, "testdata/i/j/k/k8s.txt", "kubernetes\n")
    84  	testFiles(t, all, "testdata/ken.txt", "If a program is too slow, it must have a loop.\n")
    85  
    86  	testDir(t, all, ".", "testdata/")
    87  	testDir(t, all, "testdata/i", "i18n.txt", "j/")
    88  	testDir(t, all, "testdata/i/j", "k/")
    89  	testDir(t, all, "testdata/i/j/k", "k8s.txt")
    90  }
    91  
    92  //go:embed testdata
    93  var testHiddenDir embed.FS
    94  
    95  //go:embed testdata/*
    96  var testHiddenStar embed.FS
    97  
    98  func TestHidden(t *testing.T) {
    99  	dir := testHiddenDir
   100  	star := testHiddenStar
   101  
   102  	t.Logf("//go:embed testdata")
   103  
   104  	testDir(t, dir, "testdata",
   105  		"-not-hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
   106  
   107  	t.Logf("//go:embed testdata/*")
   108  
   109  	testDir(t, star, "testdata",
   110  		"-not-hidden/", ".hidden/", "_hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")
   111  
   112  	testDir(t, star, "testdata/.hidden",
   113  		"fortune.txt", "more/") // but not .more or _more
   114  }
   115  
   116  func TestUninitialized(t *testing.T) {
   117  	var uninitialized embed.FS
   118  	testDir(t, uninitialized, ".")
   119  	f, err := uninitialized.Open(".")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	defer f.Close()
   124  	fi, err := f.Stat()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	if !fi.IsDir() {
   129  		t.Errorf("in uninitialized embed.FS, . is not a directory")
   130  	}
   131  }
   132  

View as plain text