git.wit.org/jcarr/packr@v1.10.8/builder/builder_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/gobuffalo/packr"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func Test_Builder_Run(t *testing.T) {
    17  	r := require.New(t)
    18  
    19  	root := filepath.Join("..", "example")
    20  	defer Clean(root)
    21  
    22  	exPackr := filepath.Join(root, "example-packr.go")
    23  	r.False(fileExists(exPackr))
    24  
    25  	fooPackr := filepath.Join(root, "foo", "foo-packr.go")
    26  	r.False(fileExists(fooPackr))
    27  
    28  	b := New(context.Background(), root)
    29  	err := b.Run()
    30  	r.NoError(err)
    31  
    32  	r.True(fileExists(exPackr))
    33  	r.True(fileExists(fooPackr))
    34  
    35  	bb, err := ioutil.ReadFile(exPackr)
    36  	r.NoError(err)
    37  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.css", "\"Ym9ke`)))
    38  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.js", "\"YWxlcn`)))
    39  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./templates", "index.html", "\"PCFET0NUWVBF`)))
    40  
    41  	bb, err = ioutil.ReadFile(fooPackr)
    42  	r.NoError(err)
    43  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.css", "\"Ym9keS`)))
    44  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.js", "\"YWxlcn`)))
    45  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"PCFET0NUW`)))
    46  }
    47  
    48  func Test_Builder_Run_Compress(t *testing.T) {
    49  	r := require.New(t)
    50  
    51  	root := filepath.Join("..", "example")
    52  	defer Clean(root)
    53  
    54  	exPackr := filepath.Join(root, "example-packr.go")
    55  	r.False(fileExists(exPackr))
    56  
    57  	fooPackr := filepath.Join(root, "foo", "foo-packr.go")
    58  	r.False(fileExists(fooPackr))
    59  
    60  	b := New(context.Background(), root)
    61  	b.Compress = true
    62  	err := b.Run()
    63  	r.NoError(err)
    64  
    65  	r.True(fileExists(exPackr))
    66  	r.True(fileExists(fooPackr))
    67  
    68  	bb, err := ioutil.ReadFile(exPackr)
    69  	r.NoError(err)
    70  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.css", "\"H4sIAAAAAAAA/0rKT`)))
    71  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.js", "\"H4sIAAAAAAAA/0rMSS`)))
    72  
    73  	bb, err = ioutil.ReadFile(fooPackr)
    74  	r.NoError(err)
    75  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.css", "\"H4sIAAAAAAAA/0rKT`)))
    76  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.js", "\"H4sIAAAAAAAA/0rMSS`)))
    77  	r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"H4sIAAAAAAAA`)))
    78  }
    79  
    80  func Test_Binary_Builds(t *testing.T) {
    81  	r := require.New(t)
    82  	pwd, _ := os.Getwd()
    83  	defer os.Chdir(pwd)
    84  
    85  	root := "../example"
    86  	defer Clean(root)
    87  	defer os.RemoveAll(filepath.Join(root, "bin"))
    88  
    89  	b := New(context.Background(), root)
    90  	err := b.Run()
    91  	r.NoError(err)
    92  
    93  	os.Chdir(root)
    94  	cmd := exec.Command(packr.GoBin(), "build", "-v", "-o", "bin/example")
    95  	err = cmd.Run()
    96  	r.NoError(err)
    97  
    98  	r.True(fileExists("bin/example"))
    99  }
   100  
   101  func fileExists(path string) bool {
   102  	_, err := os.Stat(path)
   103  	return err == nil
   104  }