github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/bin_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/meta" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func Test_buildCmd(t *testing.T) { 15 envy.Set("GO_BIN", "go") 16 r := require.New(t) 17 18 eq := func(s string, c *exec.Cmd) { 19 if runtime.GOOS == "windows" { 20 s = strings.Replace(s, "bin/build", `bin\build.exe`, 1) 21 s = strings.Replace(s, "bin/foo", `bin\foo.exe`, 1) 22 } 23 r.Equal(s, strings.Join(c.Args, " ")) 24 } 25 26 opts := &Options{ 27 App: meta.New("."), 28 GoCommand: "build", 29 } 30 c, err := buildCmd(opts) 31 r.NoError(err) 32 eq("go build -o bin/build", c) 33 34 opts.Environment = "bar" 35 c, err = buildCmd(opts) 36 r.NoError(err) 37 eq("go build -tags bar -o bin/build", c) 38 39 opts.App.Bin = "bin/foo" 40 c, err = buildCmd(opts) 41 r.NoError(err) 42 eq("go build -tags bar -o bin/foo", c) 43 44 opts.WithSQLite = true 45 c, err = buildCmd(opts) 46 r.NoError(err) 47 eq("go build -tags bar sqlite -o bin/foo", c) 48 49 opts.LDFlags = "-X foo.Bar=baz" 50 c, err = buildCmd(opts) 51 r.NoError(err) 52 eq("go build -tags bar sqlite -o bin/foo -ldflags -X foo.Bar=baz", c) 53 54 opts.Static = true 55 c, err = buildCmd(opts) 56 r.NoError(err) 57 eq("go build -tags bar sqlite -o bin/foo -ldflags -linkmode external -extldflags \"-static\" -X foo.Bar=baz", c) 58 59 opts.LDFlags = "-X main.BuildTime=asdf" 60 c, err = buildCmd(opts) 61 r.NoError(err) 62 eq("go build -tags bar sqlite -o bin/foo -ldflags -linkmode external -extldflags \"-static\" -X main.BuildTime=asdf", c) 63 64 opts.LDFlags = "-X main.BuildVersion=asdf" 65 c, err = buildCmd(opts) 66 r.NoError(err) 67 eq("go build -tags bar sqlite -o bin/foo -ldflags -linkmode external -extldflags \"-static\" -X main.BuildVersion=asdf", c) 68 } 69 70 func Test_buildCmd_Unix_RemovesExe(t *testing.T) { 71 if runtime.GOOS == "windows" { 72 return 73 } 74 envy.Set("GO_BIN", "go") 75 r := require.New(t) 76 77 eq := func(s string, c *exec.Cmd) { 78 r.Equal(s, strings.Join(c.Args, " ")) 79 } 80 app := meta.New(".") 81 app.Bin = "bin/build.exe" 82 opts := &Options{ 83 App: app, 84 GoCommand: "build", 85 } 86 c, err := buildCmd(opts) 87 r.NoError(err) 88 eq("go build -o bin/build", c) 89 } 90 91 func Test_buildCmd_Windows_AddsExe(t *testing.T) { 92 if runtime.GOOS != "windows" { 93 return 94 } 95 envy.Set("GO_BIN", "go") 96 r := require.New(t) 97 98 eq := func(s string, c *exec.Cmd) { 99 r.Equal(s, strings.Join(c.Args, " ")) 100 } 101 102 app := meta.New(".") 103 for _, x := range []string{"bin\\build", "bin\\build.exe"} { 104 app.Bin = x 105 opts := &Options{ 106 App: app, 107 } 108 c, err := buildCmd(opts) 109 r.NoError(err) 110 eq("go build -o bin\\build.exe", c) 111 } 112 } 113 114 func Test_installCmd(t *testing.T) { 115 envy.Set("GO_BIN", "go") 116 r := require.New(t) 117 118 eq := func(s string, c *exec.Cmd) { 119 if runtime.GOOS == "windows" { 120 s = strings.Replace(s, "bin/build", `bin\build.exe`, 1) 121 s = strings.Replace(s, "bin/foo", `bin\foo.exe`, 1) 122 } 123 r.Equal(s, strings.Join(c.Args, " ")) 124 } 125 126 opts := &Options{ 127 App: meta.New("."), 128 GoCommand: "install", 129 } 130 c, err := buildCmd(opts) 131 r.NoError(err) 132 eq("go install", c) 133 134 opts.Environment = "bar" 135 c, err = buildCmd(opts) 136 r.NoError(err) 137 eq("go install -tags bar", c) 138 139 opts.App.Bin = "bin/foo" 140 c, err = buildCmd(opts) 141 r.NoError(err) 142 eq("go install -tags bar", c) 143 144 opts.WithSQLite = true 145 c, err = buildCmd(opts) 146 r.NoError(err) 147 eq("go install -tags bar sqlite", c) 148 149 opts.LDFlags = "-X foo.Bar=baz" 150 c, err = buildCmd(opts) 151 r.NoError(err) 152 eq("go install -tags bar sqlite -ldflags -X foo.Bar=baz", c) 153 154 opts.Static = true 155 c, err = buildCmd(opts) 156 r.NoError(err) 157 eq("go install -tags bar sqlite -ldflags -linkmode external -extldflags \"-static\" -X foo.Bar=baz", c) 158 159 opts.LDFlags = "-X main.BuildTime=asdf" 160 c, err = buildCmd(opts) 161 r.NoError(err) 162 eq("go install -tags bar sqlite -ldflags -linkmode external -extldflags \"-static\" -X main.BuildTime=asdf", c) 163 164 opts.LDFlags = "-X main.BuildVersion=asdf" 165 c, err = buildCmd(opts) 166 r.NoError(err) 167 eq("go install -tags bar sqlite -ldflags -linkmode external -extldflags \"-static\" -X main.BuildVersion=asdf", c) 168 }