1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/pipe/gofish/gofish_test.go
Carlos Alexandro Becker e8c8a2832f
feat: improve universal binaries on krew/brew/gofish (#2747)
* feat: improve universal binaries on krew/brew/gofish

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* test: OnlyReplacingUnibins

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: archive

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: do not replace single-arch

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
2021-12-07 21:53:39 -03:00

837 lines
20 KiB
Go

package gofish
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}
func createTemplateData() templateData {
binaries := func(ext string) []binary {
return []binary{
{Name: "bin1" + ext, Target: "bin1" + ext},
{Name: "bin2" + ext, Target: "bin2" + ext},
{Name: "bin3" + ext, Target: "bin3" + ext},
}
}
return templateData{
Desc: "Some desc",
Homepage: "https://google.com",
ReleasePackages: []releasePackage{
{
Arch: "amd64",
OS: "darwin",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
Binaries: binaries(""),
},
{
Arch: "arm64",
OS: "darwin",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_arm64.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b349490sadasdsadsadasdasdsd",
Binaries: binaries(""),
},
{
Arch: "amd64",
OS: "linux",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Linux_x86_64.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
Binaries: binaries(""),
},
{
Arch: "arm",
OS: "linux",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm6.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
Binaries: binaries(""),
},
{
Arch: "arm64",
OS: "linux",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm64.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
Binaries: binaries(""),
},
{
Arch: "amd64",
OS: "windows",
DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_windows_amd64.zip",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
Binaries: binaries(".exe"),
},
},
Name: "Test",
Version: "0.1.3",
}
}
func assertDefaultTemplateData(t *testing.T, food string) {
t.Helper()
require.Contains(t, food, "food =")
require.Contains(t, food, `homepage = "https://google.com"`)
require.Contains(t, food, `url = "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`)
require.Contains(t, food, `sha256 = "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`)
require.Contains(t, food, `local version = "0.1.3"`)
}
func TestFullFood(t *testing.T) {
data := createTemplateData()
data.License = "MIT"
food, err := doBuildFood(context.New(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
golden.RequireEqualLua(t, []byte(food))
}
func TestFullFoodLinuxOnly(t *testing.T) {
data := createTemplateData()
for i, v := range data.ReleasePackages {
if v.OS != "linux" {
data.ReleasePackages[i] = releasePackage{}
}
}
food, err := doBuildFood(context.New(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
golden.RequireEqualLua(t, []byte(food))
}
func TestFullFoodWindowsOnly(t *testing.T) {
data := createTemplateData()
for i, v := range data.ReleasePackages {
if v.OS != "windows" {
data.ReleasePackages[i] = releasePackage{}
}
}
food, err := doBuildFood(context.New(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
golden.RequireEqualLua(t, []byte(food))
}
func TestFoodSimple(t *testing.T) {
food, err := doBuildFood(context.New(config.Project{}), createTemplateData())
require.NoError(t, err)
assertDefaultTemplateData(t, food)
}
func TestFullPipe(t *testing.T) {
type testcase struct {
prepare func(ctx *context.Context)
expectedPublishError string
}
for name, tt := range map[string]testcase{
"default": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Rigs[0].Rig.Owner = "test"
ctx.Config.Rigs[0].Rig.Name = "test"
ctx.Config.Rigs[0].Homepage = "https://github.com/goreleaser"
},
},
"default_gitlab": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.Rigs[0].Rig.Owner = "test"
ctx.Config.Rigs[0].Rig.Name = "test"
ctx.Config.Rigs[0].Homepage = "https://gitlab.com/goreleaser"
},
},
"invalid_commit_template": {
prepare: func(ctx *context.Context) {
ctx.Config.Rigs[0].Rig.Owner = "test"
ctx.Config.Rigs[0].Rig.Name = "test"
ctx.Config.Rigs[0].CommitMessageTemplate = "{{ .Asdsa }"
},
expectedPublishError: `template: tmpl:1: unexpected "}" in operand`,
},
} {
t.Run(name, func(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Env: map[string]string{
"FOO": "foo_is_bar",
},
Config: config.Project{
Dist: folder,
ProjectName: name,
Rigs: []config.GoFish{
{
Name: name,
IDs: []string{
"foo",
},
Description: "A run pipe test fish food and FOO={{ .Env.FOO }}",
},
},
},
}
tt.prepare(ctx)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bar_bin.tar.gz",
Path: "doesnt matter",
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "bar",
artifact.ExtraFormat: "tar.gz",
},
})
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"name"},
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := client.NewMock()
distFile := filepath.Join(folder, name+".lua")
require.NoError(t, runAll(ctx, client))
if tt.expectedPublishError != "" {
require.EqualError(t, publishAll(ctx, client), tt.expectedPublishError)
return
}
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualLua(t, []byte(client.Content))
distBts, err := os.ReadFile(distFile)
require.NoError(t, err)
require.Equal(t, client.Content, string(distBts))
})
}
}
func TestRunPipeUniversalBinary(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
Dist: folder,
ProjectName: "unibin",
Rigs: []config.GoFish{
{
Name: "unibin",
Rig: config.RepoRef{
Owner: "unibin",
Name: "bar",
},
IDs: []string{
"unibin",
},
},
},
},
}
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "all",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "unibin",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"unibin"},
artifact.ExtraReplaces: true,
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := client.NewMock()
distFile := filepath.Join(folder, "unibin.lua")
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualLua(t, []byte(client.Content))
distBts, err := os.ReadFile(distFile)
require.NoError(t, err)
require.Equal(t, client.Content, string(distBts))
}
func TestRunPipeUniversalBinaryNotReplacing(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
Dist: folder,
ProjectName: "unibin",
Rigs: []config.GoFish{
{
Name: "unibin",
Rig: config.RepoRef{
Owner: "unibin",
Name: "bar",
},
IDs: []string{
"unibin",
},
},
},
},
}
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin_amd64.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "unibin",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"unibin"},
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin_arm64.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "arm64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "unibin",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"unibin"},
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "all",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "unibin",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"unibin"},
artifact.ExtraReplaces: false,
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := client.NewMock()
distFile := filepath.Join(folder, "unibin.lua")
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualLua(t, []byte(client.Content))
distBts, err := os.ReadFile(distFile)
require.NoError(t, err)
require.Equal(t, client.Content, string(distBts))
}
func TestRunPipeNameTemplate(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Env: map[string]string{
"FOO_BAR": "is_bar",
},
Config: config.Project{
Dist: folder,
ProjectName: "foo",
Rigs: []config.GoFish{
{
Name: "foo_{{ .Env.FOO_BAR }}",
Rig: config.RepoRef{
Owner: "foo",
Name: "bar",
},
IDs: []string{
"foo",
},
},
},
},
}
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"foo"},
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := client.NewMock()
distFile := filepath.Join(folder, "foo_is_bar.lua")
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualLua(t, []byte(client.Content))
distBts, err := os.ReadFile(distFile)
require.NoError(t, err)
require.Equal(t, client.Content, string(distBts))
}
func TestRunPipeMultipleGoFishWithSkip(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Env: map[string]string{
"FOO_BAR": "is_bar",
},
Config: config.Project{
Dist: folder,
ProjectName: "foo",
Rigs: []config.GoFish{
{
Name: "foo",
Rig: config.RepoRef{
Owner: "foo",
Name: "bar",
},
IDs: []string{
"foo",
},
SkipUpload: "true",
},
{
Name: "bar",
Rig: config.RepoRef{
Owner: "foo",
Name: "bar",
},
IDs: []string{
"foo",
},
},
{
Name: "foobar",
Rig: config.RepoRef{
Owner: "foo",
Name: "bar",
},
IDs: []string{
"foo",
},
SkipUpload: "true",
},
},
},
}
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"foo"},
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
cli := client.NewMock()
require.NoError(t, runAll(ctx, cli))
require.EqualError(t, publishAll(ctx, cli), `rig.skip_upload is set`)
require.True(t, cli.CreatedFile)
for _, food := range ctx.Config.Rigs {
distFile := filepath.Join(folder, food.Name+".lua")
_, err := os.Stat(distFile)
require.NoError(t, err, "file should exist: "+distFile)
}
}
func TestRunPipeForMultipleArmVersions(t *testing.T) {
for name, fn := range map[string]func(ctx *context.Context){
"multiple_armv5": func(ctx *context.Context) {
ctx.Config.Rigs[0].Goarm = "5"
},
"multiple_armv6": func(ctx *context.Context) {
ctx.Config.Rigs[0].Goarm = "6"
},
"multiple_armv7": func(ctx *context.Context) {
ctx.Config.Rigs[0].Goarm = "7"
},
} {
t.Run(name, func(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Env: map[string]string{
"FOO": "foo_is_bar",
},
Config: config.Project{
Dist: folder,
ProjectName: name,
Rigs: []config.GoFish{
{
Name: name,
Description: "A run pipe test fish food and FOO={{ .Env.FOO }}",
Rig: config.RepoRef{
Owner: "test",
Name: "test",
},
Homepage: "https://github.com/goreleaser",
},
},
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
},
}
fn(ctx)
for _, a := range []struct {
name string
goos string
goarch string
goarm string
}{
{
name: "bin",
goos: "darwin",
goarch: "amd64",
},
{
name: "arm64",
goos: "linux",
goarch: "arm64",
},
{
name: "armv5",
goos: "linux",
goarch: "arm",
goarm: "5",
},
{
name: "armv6",
goos: "linux",
goarch: "arm",
goarm: "6",
},
{
name: "armv7",
goos: "linux",
goarch: "arm",
goarm: "7",
},
} {
path := filepath.Join(folder, fmt.Sprintf("%s.tar.gz", a.name))
ctx.Artifacts.Add(&artifact.Artifact{
Name: fmt.Sprintf("%s.tar.gz", a.name),
Path: path,
Goos: a.goos,
Goarch: a.goarch,
Goarm: a.goarm,
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: a.name,
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"foo"},
},
})
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
}
client := client.NewMock()
distFile := filepath.Join(folder, name+".lua")
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualLua(t, []byte(client.Content))
distBts, err := os.ReadFile(distFile)
require.NoError(t, err)
require.Equal(t, client.Content, string(distBts))
})
}
}
func TestRunPipeNoBuilds(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
Rigs: []config.GoFish{
{
Rig: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
},
}
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
func TestRunPipeBinaryRelease(t *testing.T) {
folder := t.TempDir()
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.2.1",
},
Version: "1.2.1",
Artifacts: artifact.New(),
Config: config.Project{
Dist: folder,
ProjectName: "foo",
Rigs: []config.GoFish{
{
Name: "foo",
Rig: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
},
}
path := filepath.Join(folder, "dist/foo_darwin_all/foo")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo_macos",
Path: path,
Goos: "darwin",
Goarch: "all",
Type: artifact.UploadableBinary,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "binary",
artifact.ExtraBinary: "foo",
},
})
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := client.NewMock()
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
golden.RequireEqualRb(t, []byte(client.Content))
}
func TestRunPipeNoUpload(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
Rigs: []config.GoFish{
{
Rig: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"foo"},
},
})
client := client.NewMock()
assertNoPublish := func(t *testing.T) {
t.Helper()
require.NoError(t, runAll(ctx, client))
testlib.AssertSkipped(t, publishAll(ctx, client))
require.False(t, client.CreatedFile)
}
t.Run("skip upload true", func(t *testing.T) {
ctx.Config.Rigs[0].SkipUpload = "true"
ctx.Semver.Prerelease = ""
assertNoPublish(t)
})
t.Run("skip upload auto", func(t *testing.T) {
ctx.Config.Rigs[0].SkipUpload = "auto"
ctx.Semver.Prerelease = "beta1"
assertNoPublish(t)
})
}
func TestRunEmptyTokenType(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
Rigs: []config.GoFish{
{
Rig: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
},
})
client := client.NewMock()
require.NoError(t, runAll(ctx, client))
}
func TestDefault(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
Rigs: []config.GoFish{
{},
},
},
}
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Rigs[0].Name)
require.NotEmpty(t, ctx.Config.Rigs[0].CommitAuthor.Name)
require.NotEmpty(t, ctx.Config.Rigs[0].CommitAuthor.Email)
require.NotEmpty(t, ctx.Config.Rigs[0].CommitMessageTemplate)
}
func TestGHFolder(t *testing.T) {
require.Equal(t, "bar.lua", buildFoodPath("", "bar.lua"))
require.Equal(t, "fooo/bar.lua", buildFoodPath("fooo", "bar.lua"))
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
Rigs: []config.GoFish{
{},
},
})
require.False(t, Pipe{}.Skip(ctx))
})
}
func TestRunSkipNoName(t *testing.T) {
ctx := context.New(config.Project{
Rigs: []config.GoFish{{}},
})
client := client.NewMock()
testlib.AssertSkipped(t, runAll(ctx, client))
}