github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/upx/upx_test.go (about) 1 package upx 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 "github.com/goreleaser/goreleaser/internal/artifact" 11 "github.com/goreleaser/goreleaser/internal/testctx" 12 "github.com/goreleaser/goreleaser/internal/testlib" 13 "github.com/goreleaser/goreleaser/pkg/config" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestStringer(t *testing.T) { 18 require.NotEmpty(t, Pipe{}.String()) 19 } 20 21 func TestDefault(t *testing.T) { 22 ctx := testctx.NewWithCfg(config.Project{ 23 UPXs: []config.UPX{ 24 {}, 25 }, 26 }) 27 require.NoError(t, Pipe{}.Default(ctx)) 28 require.Len(t, ctx.Config.UPXs, 1) 29 require.Equal(t, "upx", ctx.Config.UPXs[0].Binary) 30 } 31 32 func TestSkip(t *testing.T) { 33 t.Run("skip", func(t *testing.T) { 34 ctx := testctx.NewWithCfg(config.Project{ 35 UPXs: []config.UPX{}, 36 }) 37 require.True(t, Pipe{}.Skip(ctx)) 38 }) 39 t.Run("do not skip", func(t *testing.T) { 40 ctx := testctx.NewWithCfg(config.Project{ 41 UPXs: []config.UPX{ 42 {}, 43 }, 44 }) 45 require.False(t, Pipe{}.Skip(ctx)) 46 }) 47 } 48 49 func TestRun(t *testing.T) { 50 ctx := testctx.NewWithCfg(config.Project{ 51 UPXs: []config.UPX{ 52 { 53 Enabled: "true", 54 IDs: []string{"1"}, 55 }, 56 { 57 Enabled: "true", 58 IDs: []string{"2"}, 59 Compress: "best", 60 }, 61 { 62 Enabled: "true", 63 IDs: []string{"3"}, 64 Compress: "9", 65 }, 66 { 67 Enabled: "true", 68 IDs: []string{"4"}, 69 Compress: "8", 70 LZMA: true, 71 }, 72 { 73 Enabled: `{{ eq .Env.UPX "1" }}`, 74 IDs: []string{"5"}, 75 Brute: true, 76 }, 77 }, 78 }, testctx.WithEnv(map[string]string{"UPX": "1"})) 79 80 tmp := t.TempDir() 81 main := filepath.Join(tmp, "main.go") 82 require.NoError(t, os.WriteFile(main, []byte("package main\nfunc main(){ println(1) }"), 0o644)) 83 84 for _, goos := range []string{"linux", "windows", "darwin"} { 85 for _, goarch := range []string{"386", "amd64", "arm64"} { 86 ext := "" 87 if goos == "windows" { 88 ext = ".exe" 89 } 90 path := filepath.Join(tmp, fmt.Sprintf("bin_%s_%s%s", goos, goarch, ext)) 91 cmd := exec.Command("go", "build", "-o", path, main) 92 cmd.Env = append([]string{ 93 "CGO_ENABLED=0", 94 "GOOS=" + goos, 95 "GOARCH=" + goarch, 96 }, cmd.Environ()...) 97 if cmd.Run() != nil { 98 // ignore unsupported arches 99 continue 100 } 101 102 for i := 1; i <= 5; i++ { 103 ctx.Artifacts.Add(&artifact.Artifact{ 104 Name: "bin", 105 Path: path, 106 Goos: goos, 107 Goarch: goarch, 108 Type: artifact.Binary, 109 Extra: map[string]any{ 110 artifact.ExtraID: fmt.Sprintf("%d", i), 111 }, 112 }) 113 } 114 115 } 116 } 117 118 require.NoError(t, Pipe{}.Default(ctx)) 119 require.NoError(t, Pipe{}.Run(ctx)) 120 } 121 122 func TestEnabled(t *testing.T) { 123 t.Run("no config", func(t *testing.T) { 124 ctx := testctx.NewWithCfg(config.Project{ 125 UPXs: []config.UPX{ 126 {}, 127 }, 128 }) 129 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 130 }) 131 t.Run("tmpl", func(t *testing.T) { 132 ctx := testctx.NewWithCfg(config.Project{ 133 UPXs: []config.UPX{ 134 { 135 Enabled: `{{ printf "false" }}`, 136 }, 137 }, 138 }) 139 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 140 }) 141 t.Run("invalid template", func(t *testing.T) { 142 ctx := testctx.NewWithCfg(config.Project{ 143 UPXs: []config.UPX{ 144 { 145 Enabled: `{{ .Foo }}`, 146 }, 147 }, 148 }) 149 testlib.RequireTemplateError(t, Pipe{}.Run(ctx)) 150 }) 151 } 152 153 func TestUpxNotInstalled(t *testing.T) { 154 ctx := testctx.NewWithCfg(config.Project{ 155 UPXs: []config.UPX{ 156 { 157 Enabled: "true", 158 Binary: "fakeupx", 159 }, 160 }, 161 }) 162 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 163 } 164 165 func TestFindBinaries(t *testing.T) { 166 ctx := testctx.New() 167 tmp := t.TempDir() 168 main := filepath.Join(tmp, "main.go") 169 require.NoError(t, os.WriteFile(main, []byte("package main\nfunc main(){ println(1) }"), 0o644)) 170 for _, goos := range []string{"linux", "windows", "darwin"} { 171 for _, goarch := range []string{"386", "amd64", "arm64", "arm", "mips"} { 172 ext := "" 173 goarm := "" 174 gomips := "" 175 goamd64 := "" 176 switch goos { 177 case "windows": 178 ext = ".exe" 179 } 180 if goos == "windows" { 181 ext = ".exe" 182 } 183 switch goarch { 184 case "arm": 185 goarm = "7" 186 if goos != "linux" { 187 continue 188 } 189 case "mips": 190 gomips = "softfloat" 191 if goos != "linux" { 192 continue 193 } 194 case "arm64": 195 if goos == "windows" { 196 continue 197 } 198 case "amd64": 199 goamd64 = "v1" 200 case "386": 201 if goos == "darwin" { 202 continue 203 } 204 } 205 path := filepath.Join(tmp, fmt.Sprintf("bin_%s_%s%s", goos, goarch, ext)) 206 cmd := exec.Command("go", "build", "-o", path, main) 207 cmd.Env = append([]string{ 208 "CGO_ENABLED=0", 209 "GOOS=" + goos, 210 "GOARCH=" + goarch, 211 "GOAMD64=" + goamd64, 212 "GOARM=" + goarm, 213 "GOMIPS=" + gomips, 214 }, cmd.Environ()...) 215 if cmd.Run() != nil { 216 // ignore unsupported arches 217 continue 218 } 219 220 for i := 1; i <= 5; i++ { 221 ctx.Artifacts.Add(&artifact.Artifact{ 222 Name: "bin", 223 Path: path, 224 Goos: goos, 225 Goarch: goarch, 226 Goarm: goarm, 227 Gomips: gomips, 228 Goamd64: goamd64, 229 Type: artifact.Binary, 230 Extra: map[string]any{ 231 artifact.ExtraID: fmt.Sprintf("%d", i), 232 }, 233 }) 234 } 235 236 } 237 } 238 239 t.Run("only ids", func(t *testing.T) { 240 require.Len(t, findBinaries(ctx, config.UPX{ 241 IDs: []string{"1", "2", "3"}, 242 }), 27) 243 }) 244 245 t.Run("id and goos", func(t *testing.T) { 246 require.Len(t, findBinaries(ctx, config.UPX{ 247 IDs: []string{"4"}, 248 Goos: []string{"windows", "darwin"}, 249 }), 4) // amd64, 386 250 }) 251 252 t.Run("id, goos goarch", func(t *testing.T) { 253 require.Len(t, findBinaries(ctx, config.UPX{ 254 IDs: []string{"3"}, 255 Goos: []string{"windows"}, 256 Goarch: []string{"386", "amd64"}, 257 }), 2) 258 }) 259 260 t.Run("goamd64", func(t *testing.T) { 261 require.Empty(t, findBinaries(ctx, config.UPX{ 262 IDs: []string{"2"}, 263 Goos: []string{"linux"}, 264 Goarch: []string{"amd64"}, 265 Goamd64: []string{"v3"}, 266 })) 267 require.Len(t, findBinaries(ctx, config.UPX{ 268 IDs: []string{"2"}, 269 Goos: []string{"linux"}, 270 Goarch: []string{"amd64"}, 271 Goamd64: []string{"v1", "v2", "v3", "v4"}, 272 }), 1) 273 }) 274 275 t.Run("goarm", func(t *testing.T) { 276 require.Empty(t, findBinaries(ctx, config.UPX{ 277 IDs: []string{"2"}, 278 Goos: []string{"linux"}, 279 Goarch: []string{"arm"}, 280 Goarm: []string{"6"}, 281 })) 282 require.Len(t, findBinaries(ctx, config.UPX{ 283 IDs: []string{"2"}, 284 Goos: []string{"linux"}, 285 Goarch: []string{"arm"}, 286 Goarm: []string{"7"}, 287 }), 1) 288 require.Len(t, findBinaries(ctx, config.UPX{ 289 IDs: []string{"2"}, 290 Goarm: []string{"6", "7"}, 291 }), 1) 292 }) 293 }