1package packr 2 3import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "runtime" 8 "sort" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13) 14 15func Test_Box_String(t *testing.T) { 16 r := require.New(t) 17 s := testBox.String("hello.txt") 18 r.Equal("hello world!", strings.TrimSpace(s)) 19} 20 21func Test_Box_MustString(t *testing.T) { 22 r := require.New(t) 23 _, err := testBox.MustString("idontexist.txt") 24 r.Error(err) 25} 26 27func Test_Box_Bytes(t *testing.T) { 28 r := require.New(t) 29 s := testBox.Bytes("hello.txt") 30 r.Equal([]byte("hello world!"), bytes.TrimSpace(s)) 31} 32 33func Test_Box_MustBytes(t *testing.T) { 34 r := require.New(t) 35 _, err := testBox.MustBytes("idontexist.txt") 36 r.Error(err) 37} 38 39func Test_Box_Has(t *testing.T) { 40 r := require.New(t) 41 r.True(testBox.Has("hello.txt")) 42 r.False(testBox.Has("idontexist.txt")) 43} 44 45func Test_Box_Walk_Physical(t *testing.T) { 46 r := require.New(t) 47 count := 0 48 err := testBox.Walk(func(path string, f File) error { 49 count++ 50 return nil 51 }) 52 r.NoError(err) 53 r.Equal(3, count) 54} 55 56func Test_Box_Walk_Virtual(t *testing.T) { 57 r := require.New(t) 58 count := 0 59 err := virtualBox.Walk(func(path string, f File) error { 60 count++ 61 return nil 62 }) 63 r.NoError(err) 64 r.Equal(4, count) 65} 66 67func Test_List_Virtual(t *testing.T) { 68 r := require.New(t) 69 mustHave := []string{"a", "b", "c", "d/a"} 70 actual := virtualBox.List() 71 sort.Strings(actual) 72 r.Equal(mustHave, actual) 73} 74 75func Test_List_Physical(t *testing.T) { 76 r := require.New(t) 77 mustHave := []string{"goodbye.txt", "hello.txt", "index.html"} 78 actual := testBox.List() 79 r.Equal(mustHave, actual) 80} 81 82func Test_Outside_Box(t *testing.T) { 83 r := require.New(t) 84 f, err := ioutil.TempFile("", "") 85 r.NoError(err) 86 defer os.RemoveAll(f.Name()) 87 _, err = testBox.MustString(f.Name()) 88 r.Error(err) 89} 90 91func Test_Box_find(t *testing.T) { 92 box := NewBox("./example") 93 94 onWindows := runtime.GOOS == "windows" 95 table := []struct { 96 name string 97 found bool 98 }{ 99 {"assets/app.css", true}, 100 {"assets\\app.css", onWindows}, 101 {"foo/bar.baz", false}, 102 } 103 104 for _, tt := range table { 105 t.Run(tt.name, func(st *testing.T) { 106 r := require.New(st) 107 _, err := box.find(tt.name) 108 if tt.found { 109 r.True(box.Has(tt.name)) 110 r.NoError(err) 111 } else { 112 r.False(box.Has(tt.name)) 113 r.Error(err) 114 } 115 }) 116 } 117} 118 119func Test_Virtual_Directory_Not_Found(t *testing.T) { 120 r := require.New(t) 121 _, err := virtualBox.find("d") 122 r.NoError(err) 123 _, err = virtualBox.find("does-not-exist") 124 r.Error(err) 125}