github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/build_test.go (about)

     1  package build
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/gobuffalo/envy"
    10  	"github.com/gobuffalo/genny/v2"
    11  	"github.com/gobuffalo/genny/v2/gentest"
    12  	"github.com/gobuffalo/meta"
    13  	"github.com/gobuffalo/packr/v2"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // TODO: once `buffalo new` is converted to use genny
    18  // create an integration test that first generates a new application
    19  // and then tries to build using genny/build.
    20  var coke = packr.New("github.com/gobuffalo/buffalo/genny/build/build_test", "../build/_fixtures/coke")
    21  
    22  var cokeRunner = func() *genny.Runner {
    23  	run := gentest.NewRunner()
    24  	run.Disk.AddBox(coke)
    25  	run.Root = coke.Path
    26  	return run
    27  }
    28  
    29  var eq = func(r *require.Assertions, s string, c *exec.Cmd) {
    30  	if runtime.GOOS == "windows" {
    31  		s = strings.Replace(s, "bin/build", `bin\build.exe`, 1)
    32  		s = strings.Replace(s, "bin/foo", `bin\foo.exe`, 1)
    33  	}
    34  	r.Equal(s, strings.Join(c.Args, " "))
    35  }
    36  
    37  func Test_New(t *testing.T) {
    38  	r := require.New(t)
    39  
    40  	run := cokeRunner()
    41  
    42  	opts := &Options{
    43  		WithAssets:    true,
    44  		WithBuildDeps: true,
    45  		Environment:   "bar",
    46  		App:           meta.New("."),
    47  	}
    48  	opts.App.Bin = "bin/foo"
    49  	r.NoError(run.WithNew(New(opts)))
    50  	run.Root = opts.App.Root
    51  
    52  	r.NoError(run.Run())
    53  
    54  	res := run.Results()
    55  
    56  	// we should never leave any files modified or dropped
    57  	r.Len(res.Files, 0)
    58  
    59  	cmds := []string{"go build -tags bar -o bin/foo", "go mod tidy"}
    60  	r.Len(res.Commands, len(cmds))
    61  	for i, c := range res.Commands {
    62  		eq(r, cmds[i], c)
    63  	}
    64  }
    65  
    66  func Test_NewWithoutBuildDeps(t *testing.T) {
    67  	envy.Temp(func() {
    68  		envy.Set(envy.GO111MODULE, "off")
    69  		r := require.New(t)
    70  
    71  		run := cokeRunner()
    72  
    73  		opts := &Options{
    74  			WithAssets:    false,
    75  			WithBuildDeps: false,
    76  			Environment:   "bar",
    77  			App:           meta.New("."),
    78  		}
    79  		opts.App.Bin = "bin/foo"
    80  		r.NoError(run.WithNew(New(opts)))
    81  		run.Root = opts.App.Root
    82  
    83  		r.NoError(run.Run())
    84  
    85  		res := run.Results()
    86  
    87  		cmds := []string{"go build -tags bar -o bin/foo"}
    88  		r.Len(res.Commands, len(cmds))
    89  		for i, c := range res.Commands {
    90  			eq(r, cmds[i], c)
    91  		}
    92  	})
    93  }