github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/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/JarrahG/buffalocli/internal/genny/newapp/web"
    10  	"github.com/JarrahG/buffalocli/internal/genny/testrunner"
    11  	"github.com/gobuffalo/envy"
    12  	"github.com/gobuffalo/meta"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  var eq = func(r *require.Assertions, s string, c *exec.Cmd) {
    17  	if runtime.GOOS == "windows" {
    18  		s = strings.Replace(s, "bin/build", `bin\build.exe`, 1)
    19  		s = strings.Replace(s, "bin/foo", `bin\foo.exe`, 1)
    20  	}
    21  	r.Equal(s, strings.Join(c.Args, " "))
    22  }
    23  
    24  func Test_New(t *testing.T) {
    25  	r := require.New(t)
    26  
    27  	run, err := testrunner.WebApp(&web.Options{})
    28  	r.NoError(err)
    29  
    30  	opts := &Options{
    31  		WithAssets:    true,
    32  		WithBuildDeps: true,
    33  		Environment:   "bar",
    34  		App:           meta.New("."),
    35  	}
    36  	opts.App.Bin = "bin/foo"
    37  	r.NoError(run.WithNew(New(opts)))
    38  	run.Root = opts.App.Root
    39  	r.NoError(run.Run())
    40  
    41  	res := run.Results()
    42  	r.Len(res.Files, 0)
    43  
    44  	cmds := []string{
    45  		"go mod tidy",
    46  		"go build -tags bar -o bin/foo " + appBinaryFolder(),
    47  		"go mod tidy",
    48  	}
    49  	r.Len(res.Commands, len(cmds))
    50  	for i, c := range res.Commands {
    51  		eq(r, cmds[i], c)
    52  	}
    53  }
    54  
    55  func Test_NewWithoutBuildDeps(t *testing.T) {
    56  	envy.Temp(func() {
    57  		r := require.New(t)
    58  
    59  		run, err := testrunner.WebApp(&web.Options{})
    60  		r.NoError(err)
    61  
    62  		opts := &Options{
    63  			WithAssets:    false,
    64  			WithBuildDeps: false,
    65  			Environment:   "bar",
    66  			App:           meta.New("."),
    67  		}
    68  		opts.App.Bin = "bin/foo"
    69  		r.NoError(run.WithNew(New(opts)))
    70  		run.Root = opts.App.Root
    71  
    72  		r.NoError(run.Run())
    73  
    74  		res := run.Results()
    75  
    76  		cmds := []string{"go mod tidy", "go build -tags bar -o bin/foo " + appBinaryFolder()}
    77  		r.Len(res.Commands, len(cmds))
    78  		for i, c := range res.Commands {
    79  			eq(r, cmds[i], c)
    80  		}
    81  	})
    82  }