github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/cmds/build/gobuild_test.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/gobuffalo/buffalo-cli/v2/cli/cmds/build/buildtest"
    12  	"github.com/gobuffalo/plugins"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func Test_Cmd_GoBuilder(t *testing.T) {
    17  	cli := func(n ...string) string {
    18  		cli := filepath.Join(n...)
    19  		if runtime.GOOS == "windows" {
    20  			cli += ".exe"
    21  		}
    22  		return cli
    23  	}
    24  
    25  	mainFolder := "." + string(filepath.Separator) + filepath.Join("cmd", "build")
    26  
    27  	table := []struct {
    28  		args []string
    29  		exp  []string
    30  	}{
    31  		{
    32  			exp: []string{"go", "build", "-o", cli("bin", "build"), mainFolder},
    33  		},
    34  		{
    35  			args: []string{"-o", filepath.Join("bin", "foo")},
    36  			exp:  []string{"go", "build", "-o", cli("bin", "foo"), mainFolder},
    37  		},
    38  		{
    39  			args: []string{"--mod", "vendor"},
    40  			exp:  []string{"go", "build", "-o", cli("bin", "build"), "-mod", "vendor", mainFolder},
    41  		},
    42  		{
    43  			args: []string{"--tags", "a b c"},
    44  			exp:  []string{"go", "build", "-o", cli("bin", "build"), "-tags", "a b c", mainFolder},
    45  		},
    46  		{
    47  			args: []string{"--static"},
    48  			exp:  []string{"go", "build", "-o", cli("bin", "build"), "-ldflags", "-linkmode external -extldflags \"-static\"", mainFolder},
    49  		},
    50  		{
    51  			args: []string{"--ldflags", "linky"},
    52  			exp:  []string{"go", "build", "-o", cli("bin", "build"), "-ldflags", "linky", mainFolder},
    53  		},
    54  	}
    55  
    56  	for _, tt := range table {
    57  		t.Run(strings.Join(tt.args, " "), func(st *testing.T) {
    58  			r := require.New(st)
    59  
    60  			bc := &Cmd{}
    61  
    62  			var act []string
    63  			fn := func(ctx context.Context, root string, cmd *exec.Cmd) error {
    64  				act = cmd.Args
    65  				return nil
    66  			}
    67  			bc.WithPlugins(func() []plugins.Plugin {
    68  				return []plugins.Plugin{
    69  					buildtest.GoBuilder(fn),
    70  				}
    71  			})
    72  
    73  			ctx := context.Background()
    74  			err := bc.Main(ctx, ".", tt.args)
    75  			r.NoError(err)
    76  
    77  			r.Equal(tt.exp, act)
    78  		})
    79  	}
    80  }