1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

test: testctx pkg (#3807)

alternative to #3806 

the idea is that both `context.New` and `context.Context{}` are never
used in tests.

not sure yet how much I like it, so far code does look a bit more
readable though.

---------

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2023-03-02 00:01:11 -03:00
committed by GitHub
parent e6159a1283
commit f544c5ce69
71 changed files with 2480 additions and 3121 deletions

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -46,7 +47,7 @@ func TestSetupPipeline(t *testing.T) {
require.Equal(
t,
pipeline.BuildCmdPipeline,
setupPipeline(context.New(config.Project{}), buildOpts{}),
setupPipeline(testctx.New(), buildOpts{}),
)
})
@@ -54,7 +55,7 @@ func TestSetupPipeline(t *testing.T) {
require.Equal(
t,
pipeline.BuildCmdPipeline,
setupPipeline(context.New(config.Project{}), buildOpts{
setupPipeline(testctx.New(), buildOpts{
singleTarget: true,
}),
)
@@ -64,7 +65,7 @@ func TestSetupPipeline(t *testing.T) {
require.Equal(
t,
pipeline.BuildCmdPipeline,
setupPipeline(context.New(config.Project{}), buildOpts{
setupPipeline(testctx.New(), buildOpts{
singleTarget: true,
ids: []string{"foo"},
}),
@@ -75,7 +76,7 @@ func TestSetupPipeline(t *testing.T) {
require.Equal(
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{"foobar"}),
setupPipeline(context.New(config.Project{}), buildOpts{
setupPipeline(testctx.New(), buildOpts{
singleTarget: true,
ids: []string{"foo"},
output: ".",
@@ -88,7 +89,7 @@ func TestSetupPipeline(t *testing.T) {
t,
pipeline.BuildCmdPipeline,
setupPipeline(
context.New(config.Project{
testctx.NewWithCfg(config.Project{
Builds: []config.Build{{}},
}),
buildOpts{
@@ -103,7 +104,7 @@ func TestSetupPipeline(t *testing.T) {
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{"foobar"}),
setupPipeline(
context.New(config.Project{}),
testctx.New(),
buildOpts{
singleTarget: true,
ids: []string{"foo"},
@@ -118,7 +119,7 @@ func TestSetupPipeline(t *testing.T) {
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{"zaz"}),
setupPipeline(
context.New(config.Project{
testctx.NewWithCfg(config.Project{
Builds: []config.Build{{}},
}),
buildOpts{
@@ -132,7 +133,7 @@ func TestSetupPipeline(t *testing.T) {
func TestBuildFlags(t *testing.T) {
setup := func(opts buildOpts) *context.Context {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, setupBuildContext(ctx, opts))
return ctx
}
@@ -194,7 +195,7 @@ func TestBuildFlags(t *testing.T) {
t.Run("id", func(t *testing.T) {
t.Run("match", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "default",
@@ -210,7 +211,7 @@ func TestBuildFlags(t *testing.T) {
})
t.Run("match-multiple", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "default",
@@ -226,7 +227,7 @@ func TestBuildFlags(t *testing.T) {
})
t.Run("match-partial", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "default",
@@ -242,7 +243,7 @@ func TestBuildFlags(t *testing.T) {
})
t.Run("dont match", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -258,14 +259,14 @@ func TestBuildFlags(t *testing.T) {
})
t.Run("default config", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, setupBuildContext(ctx, buildOpts{
ids: []string{"aaa"},
}))
})
t.Run("single build config", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -280,7 +281,7 @@ func TestBuildFlags(t *testing.T) {
}
func TestBuildSingleTargetWithSpecificTargets(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{
@@ -303,7 +304,7 @@ func TestBuildSingleTargetWithSpecificTargets(t *testing.T) {
}
func TestBuildSingleTargetRemoveOtherOptions(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{

View File

@@ -4,7 +4,7 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -56,7 +56,7 @@ func TestReleaseBrokenProject(t *testing.T) {
func TestReleaseFlags(t *testing.T) {
setup := func(tb testing.TB, opts releaseOpts) *context.Context {
tb.Helper()
ctx := context.New(config.Project{})
ctx := testctx.New()
setupReleaseContext(ctx, opts)
return ctx
}

View File

@@ -4,16 +4,16 @@ import (
"testing"
"time"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestEval(t *testing.T) {
now := time.Now().Truncate(time.Second)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"OWNER=carlos", "FOLDER=d"},
})
ctx.Git.CommitDate = now

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
api "github.com/goreleaser/goreleaser/pkg/build"
@@ -235,13 +236,11 @@ func TestWithDefaults(t *testing.T) {
if testcase.build.GoBinary != "" && testcase.build.GoBinary != "go" {
createFakeGoBinaryWithVersion(t, testcase.build.GoBinary, "go1.18")
}
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
testcase.build,
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
build, err := Default.WithDefaults(ctx.Config.Builds[0])
require.NoError(t, err)
require.ElementsMatch(t, build.Targets, testcase.targets)
@@ -331,12 +330,11 @@ func TestInvalidTargets(t *testing.T) {
},
} {
t.Run(s, func(t *testing.T) {
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
tc.build,
},
}
ctx := context.New(config)
})
_, err := Default.WithDefaults(ctx.Config.Builds[0])
require.EqualError(t, err, tc.expectedErr)
})
@@ -346,7 +344,8 @@ func TestInvalidTargets(t *testing.T) {
func TestBuild(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"GO_FLAGS=-v"},
Builds: []config.Build{
{
ID: "foo",
@@ -380,11 +379,7 @@ func TestBuild(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Env["GO_FLAGS"] = "-v"
ctx.Git.CurrentTag = "v5.6.7"
ctx.Version = ctx.Git.CurrentTag
}, testctx.WithCurrentTag("v5.6.7"), testctx.WithVersion("v5.6.7"))
build := ctx.Config.Builds[0]
for _, target := range build.Targets {
var ext string
@@ -543,7 +538,7 @@ func TestBuild(t *testing.T) {
func TestBuildInvalidEnv(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -558,9 +553,7 @@ func TestBuildInvalidEnv(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
build := ctx.Config.Builds[0]
err := Default.Build(ctx, build, api.Options{
Target: runtimeTarget,
@@ -577,7 +570,7 @@ func TestBuildCodeInSubdir(t *testing.T) {
err := os.Mkdir(subdir, 0o755)
require.NoError(t, err)
writeGoodMain(t, subdir)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -593,9 +586,7 @@ func TestBuildCodeInSubdir(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
build := ctx.Config.Builds[0]
err = Default.Build(ctx, build, api.Options{
Target: runtimeTarget,
@@ -610,7 +601,7 @@ func TestBuildWithDotGoDir(t *testing.T) {
folder := testlib.Mktmp(t)
require.NoError(t, os.Mkdir(filepath.Join(folder, ".go"), 0o755))
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -623,9 +614,7 @@ func TestBuildWithDotGoDir(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
build := ctx.Config.Builds[0]
require.NoError(t, Default.Build(ctx, build, api.Options{
Target: runtimeTarget,
@@ -638,7 +627,7 @@ func TestBuildWithDotGoDir(t *testing.T) {
func TestBuildFailed(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "buildid",
@@ -652,9 +641,7 @@ func TestBuildFailed(t *testing.T) {
Command: "build",
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
err := Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: "darwin_amd64",
})
@@ -665,7 +652,7 @@ func TestBuildFailed(t *testing.T) {
func TestRunInvalidAsmflags(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Binary: "nametest",
@@ -677,9 +664,7 @@ func TestRunInvalidAsmflags(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
err := Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
@@ -689,7 +674,7 @@ func TestRunInvalidAsmflags(t *testing.T) {
func TestRunInvalidGcflags(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Binary: "nametest",
@@ -701,9 +686,7 @@ func TestRunInvalidGcflags(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
err := Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
@@ -713,7 +696,7 @@ func TestRunInvalidGcflags(t *testing.T) {
func TestRunInvalidLdflags(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Binary: "nametest",
@@ -726,9 +709,7 @@ func TestRunInvalidLdflags(t *testing.T) {
},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
err := Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
@@ -738,7 +719,7 @@ func TestRunInvalidLdflags(t *testing.T) {
func TestRunInvalidFlags(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Binary: "nametest",
@@ -750,8 +731,7 @@ func TestRunInvalidFlags(t *testing.T) {
},
},
},
}
ctx := context.New(config)
})
err := Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
@@ -763,11 +743,9 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
t.Helper()
folder := testlib.Mktmp(t)
writeMainWithoutMainFunc(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{{Binary: "no-main"}},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
return ctx
}
t.Run("empty", func(t *testing.T) {
@@ -814,19 +792,17 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
func TestBuildTests(t *testing.T) {
folder := testlib.Mktmp(t)
writeTest(t, folder)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{{
Binary: "foo.test",
Command: "test",
BuildDetails: config.BuildDetails{
Flags: []string{"-c"},
},
NoMainCheck: true,
}},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
ctx.Config.Builds[0].NoMainCheck = true
build, err := Default.WithDefaults(config.Builds[0])
}, testctx.WithCurrentTag("5.6.7"))
build, err := Default.WithDefaults(ctx.Config.Builds[0])
require.NoError(t, err)
require.NoError(t, Default.Build(ctx, build, api.Options{
Target: runtimeTarget,
@@ -859,7 +835,7 @@ import _ "github.com/goreleaser/goreleaser"
cmd.Dir = proxied
require.NoError(t, cmd.Run())
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
Proxy: true,
},
@@ -877,8 +853,7 @@ import _ "github.com/goreleaser/goreleaser"
Command: "build",
},
},
}
ctx := context.New(config)
})
require.NoError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
@@ -892,7 +867,7 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
[]byte("package main\nfunc main() {println(0)}"),
0o644,
))
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Binary: "foo",
@@ -907,9 +882,7 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
Command: "build",
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "5.6.7"
}, testctx.WithCurrentTag("5.6.7"))
t.Run("empty", func(t *testing.T) {
ctx.Config.Builds[0].Main = ""
require.NoError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
@@ -933,17 +906,14 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
func TestLdFlagsFullTemplate(t *testing.T) {
run := time.Now().UTC()
commit := time.Now().AddDate(-1, 0, 0)
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.2.3",
Commit: "123",
CommitDate: commit,
},
Date: run,
Version: "1.2.3",
Env: map[string]string{"FOO": "123"},
}
ctx := testctx.New(
testctx.WithCurrentTag("v1.2.3"),
testctx.WithCommit("123"),
testctx.WithCommitDate(commit),
testctx.WithVersion("1.2.3"),
testctx.WithEnv(map[string]string{"FOO": "123"}),
testctx.WithDate(run),
)
artifact := &artifact.Artifact{Goarch: "amd64"}
flags, err := tmpl.New(ctx).WithArtifact(artifact).
Apply(`-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}" -X main.time={{ time "20060102" }} -X main.arch={{.Arch}} -X main.commitDate={{.CommitDate}}`)
@@ -965,8 +935,7 @@ func TestInvalidTemplate(t *testing.T) {
"{{.Env.NOPE}}",
} {
t.Run(template, func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "3.4.1"
ctx := testctx.New(testctx.WithCurrentTag("3.4.1"))
flags, err := tmpl.New(ctx).Apply(template)
testlib.RequireTemplateError(t, err)
require.Empty(t, flags)
@@ -975,10 +944,10 @@ func TestInvalidTemplate(t *testing.T) {
}
func TestProcessFlags(t *testing.T) {
ctx := &context.Context{
Version: "1.2.3",
}
ctx.Git.CurrentTag = "5.6.7"
ctx := testctx.New(
testctx.WithVersion("1.2.3"),
testctx.WithCurrentTag("5.6.7"),
)
artifact := &artifact.Artifact{
Name: "name",
@@ -1017,12 +986,10 @@ func TestProcessFlags(t *testing.T) {
}
func TestProcessFlagsInvalid(t *testing.T) {
ctx := &context.Context{}
ctx := testctx.New()
source := []string{
"{{.Version}",
}
flags, err := processFlags(ctx, &artifact.Artifact{}, []string{}, source, "-testflag=")
testlib.RequireTemplateError(t, err)
require.Nil(t, flags)
@@ -1035,9 +1002,10 @@ func TestBuildModTimestamp(t *testing.T) {
folder := testlib.Mktmp(t)
writeGoodMain(t, folder)
config := config.Project{
Builds: []config.Build{
{
ctx := testctx.NewWithCfg(
config.Project{
Env: []string{"GO_FLAGS=-v"},
Builds: []config.Build{{
ID: "foo",
Binary: "bin/foo-{{ .Version }}",
Targets: []string{
@@ -1058,13 +1026,11 @@ func TestBuildModTimestamp(t *testing.T) {
ModTimestamp: fmt.Sprintf("%d", modTime.Unix()),
GoBinary: "go",
Command: "build",
},
}},
},
}
ctx := context.New(config)
ctx.Env["GO_FLAGS"] = "-v"
ctx.Git.CurrentTag = "v5.6.7"
ctx.Version = ctx.Git.CurrentTag
testctx.WithCurrentTag("v5.6.7"),
testctx.WithVersion("5.6.7"),
)
build := ctx.Config.Builds[0]
for _, target := range build.Targets {
var ext string
@@ -1102,15 +1068,15 @@ func TestBuildModTimestamp(t *testing.T) {
func TestBuildGoBuildLine(t *testing.T) {
requireEqualCmd := func(tb testing.TB, build config.Build, expected []string) {
tb.Helper()
cfg := config.Project{
Builds: []config.Build{build},
}
ctx := context.New(cfg)
ctx.Version = "1.2.3"
ctx.Git.Commit = "aaa"
ctx := testctx.NewWithCfg(
config.Project{
Builds: []config.Build{build},
},
testctx.WithVersion("1.2.3"),
testctx.WithGitInfo(context.GitInfo{Commit: "aaa"}),
)
options := api.Options{
Path: cfg.Builds[0].Binary,
Path: ctx.Config.Builds[0].Binary,
Goos: "linux",
Goarch: "amd64",
}
@@ -1244,7 +1210,7 @@ func TestBuildGoBuildLine(t *testing.T) {
func TestOverrides(t *testing.T) {
t.Run("linux amd64", func(t *testing.T) {
dets, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetails: config.BuildDetails{
Ldflags: []string{"original"},
@@ -1272,7 +1238,7 @@ func TestOverrides(t *testing.T) {
t.Run("single sided", func(t *testing.T) {
dets, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetails: config.BuildDetails{},
BuildDetailsOverrides: []config.BuildDetailsOverride{
@@ -1304,7 +1270,7 @@ func TestOverrides(t *testing.T) {
t.Run("with template", func(t *testing.T) {
dets, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetails: config.BuildDetails{
Ldflags: []string{"original"},
@@ -1334,7 +1300,7 @@ func TestOverrides(t *testing.T) {
t.Run("with invalid template", func(t *testing.T) {
_, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetailsOverrides: []config.BuildDetailsOverride{
{
@@ -1351,7 +1317,7 @@ func TestOverrides(t *testing.T) {
t.Run("with goarm", func(t *testing.T) {
dets, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetails: config.BuildDetails{
Ldflags: []string{"original"},
@@ -1381,7 +1347,7 @@ func TestOverrides(t *testing.T) {
t.Run("with gomips", func(t *testing.T) {
dets, err := withOverrides(
context.New(config.Project{}),
testctx.New(),
config.Build{
BuildDetails: config.BuildDetails{
Ldflags: []string{"original"},

View File

@@ -4,30 +4,27 @@ import (
"math/rand"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestClientEmpty(t *testing.T) {
ctx := &context.Context{}
ctx := testctx.New()
client, err := New(ctx)
require.Nil(t, client)
require.EqualError(t, err, `invalid client token type: ""`)
}
func TestClientNewGitea(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
// TODO: use a mocked http server to cover version api
API: "https://gitea.com/api/v1",
Download: "https://gitea.com",
},
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
// TODO: use a mocked http server to cover version api
API: "https://gitea.com/api/v1",
Download: "https://gitea.com",
},
TokenType: context.TokenTypeGitea,
Token: "giteatoken",
}
}, testctx.GiteaTokenType)
client, err := New(ctx)
require.NoError(t, err)
_, ok := client.(*giteaClient)
@@ -35,25 +32,18 @@ func TestClientNewGitea(t *testing.T) {
}
func TestClientNewGiteaInvalidURL(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "://gitea.com/api/v1",
},
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "://gitea.com/api/v1",
},
TokenType: context.TokenTypeGitea,
Token: "giteatoken",
}
}, testctx.GiteaTokenType)
client, err := New(ctx)
require.Error(t, err)
require.Nil(t, client)
}
func TestClientNewGitLab(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Token: "gitlabtoken",
}
ctx := testctx.New(testctx.GitLabTokenType)
client, err := New(ctx)
require.NoError(t, err)
_, ok := client.(*gitlabClient)
@@ -66,35 +56,24 @@ func TestCheckBodyMaxLength(t *testing.T) {
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
ctx := context.New(config.Project{})
ctx.ReleaseNotes = string(b)
out := truncateReleaseBody(string(b))
require.Len(t, out, maxReleaseBodyLength)
}
func TestNewIfToken(t *testing.T) {
t.Run("valid", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Token: "gitlabtoken",
}
ctx := testctx.New(testctx.GitLabTokenType)
client, err := New(ctx)
require.NoError(t, err)
_, ok := client.(*gitlabClient)
require.True(t, ok)
ctx = &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
ctx = testctx.NewWithCfg(config.Project{
Env: []string{"VAR=token"},
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
TokenType: context.TokenTypeGitea,
Token: "giteatoken",
Env: map[string]string{"VAR": "token"},
}
}, testctx.GiteaTokenType)
client, err = NewIfToken(ctx, client, "{{ .Env.VAR }}")
require.NoError(t, err)
_, ok = client.(*giteaClient)
@@ -102,10 +81,7 @@ func TestNewIfToken(t *testing.T) {
})
t.Run("empty", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Token: "gitlabtoken",
}
ctx := testctx.New(testctx.GitLabTokenType)
client, err := New(ctx)
require.NoError(t, err)
@@ -117,11 +93,7 @@ func TestNewIfToken(t *testing.T) {
})
t.Run("invalid tmpl", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Token: "gitlabtoken",
}
ctx := testctx.New(testctx.GitLabTokenType)
_, err := NewIfToken(ctx, nil, "nope")
require.EqualError(t, err, `expected {{ .Env.VAR_NAME }} only (no plain-text or other interpolation)`)
})
@@ -129,10 +101,9 @@ func TestNewIfToken(t *testing.T) {
func TestNewWithToken(t *testing.T) {
t.Run("gitlab", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Env: map[string]string{"TK": "token"},
}
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"TK=token"},
}, testctx.GitLabTokenType)
cli, err := newWithToken(ctx, "{{ .Env.TK }}")
require.NoError(t, err)
@@ -142,15 +113,12 @@ func TestNewWithToken(t *testing.T) {
})
t.Run("gitea", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitea,
Env: map[string]string{"TK": "token"},
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"TK=token"},
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
}
}, testctx.GiteaTokenType)
cli, err := newWithToken(ctx, "{{ .Env.TK }}")
require.NoError(t, err)
@@ -160,11 +128,9 @@ func TestNewWithToken(t *testing.T) {
})
t.Run("invalid", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenType("nope"),
Env: map[string]string{"TK": "token"},
}
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"TK=token"},
}, testctx.WithTokenType(context.TokenType("nope")))
cli, err := newWithToken(ctx, "{{ .Env.TK }}")
require.EqualError(t, err, `invalid client token type: "nope"`)
require.Nil(t, cli)

View File

@@ -11,6 +11,7 @@ import (
"code.gitea.io/sdk/gitea"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/jarcoal/httpmock"
@@ -25,7 +26,7 @@ type GetInstanceURLSuite struct {
func (s *GetInstanceURLSuite) TestWithScheme() {
t := s.T()
rootURL := "https://gitea.com"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: rootURL + "/api/v1",
},
@@ -38,7 +39,7 @@ func (s *GetInstanceURLSuite) TestWithScheme() {
func (s *GetInstanceURLSuite) TestParseError() {
t := s.T()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "://wrong.gitea.com",
},
@@ -51,7 +52,7 @@ func (s *GetInstanceURLSuite) TestParseError() {
func (s *GetInstanceURLSuite) TestNoScheme() {
t := s.T()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "gitea.com",
},
@@ -64,7 +65,7 @@ func (s *GetInstanceURLSuite) TestNoScheme() {
func (s *GetInstanceURLSuite) TestEmpty() {
t := s.T()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "",
},
@@ -78,7 +79,7 @@ func (s *GetInstanceURLSuite) TestEmpty() {
func (s *GetInstanceURLSuite) TestTemplate() {
t := s.T()
rootURL := "https://gitea.mycompany.com"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
fmt.Sprintf("GORELEASER_TEST_GITAEA_URLS_API=%s", rootURL),
},
@@ -94,7 +95,7 @@ func (s *GetInstanceURLSuite) TestTemplate() {
func (s *GetInstanceURLSuite) TestTemplateMissingValue() {
t := s.T()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
@@ -107,7 +108,7 @@ func (s *GetInstanceURLSuite) TestTemplateMissingValue() {
func (s *GetInstanceURLSuite) TestTemplateInvalid() {
t := s.T()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "{{.dddddddddd",
},
@@ -157,9 +158,8 @@ func (s *GiteaReleasesTestSuite) SetupTest() {
s.commit = "some commit hash"
s.isDraft = false
s.isPrerelease = true
s.ctx = &context.Context{
Version: "6.6.6",
Config: config.Project{
s.ctx = testctx.NewWithCfg(
config.Project{
ProjectName: "project",
Release: config.Release{
NameTemplate: "{{ .ProjectName }}_{{ .Version }}",
@@ -170,20 +170,18 @@ func (s *GiteaReleasesTestSuite) SetupTest() {
Draft: s.isDraft,
},
},
Env: context.Env{},
Semver: context.Semver{
Major: 6,
Minor: 6,
Patch: 6,
},
Git: context.GitInfo{
testctx.WithVersion("6.6.6"),
testctx.WithGitInfo(context.GitInfo{
CurrentTag: s.tag,
Commit: s.commit,
ShortCommit: s.commit[0:2],
URL: "https://gitea.com/goreleaser/goreleaser.git",
}),
func(ctx *context.Context) {
ctx.PreRelease = s.isPrerelease
},
PreRelease: s.isPrerelease,
}
testctx.WithSemver(6, 6, 6, ""),
)
s.releaseID = 666
s.releaseURL = fmt.Sprintf("%v/%v", s.releasesURL, s.releaseID)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/api/v1/version", s.url), httpmock.NewStringResponder(200, "{\"version\":\"1.12.0\"}"))
@@ -507,7 +505,7 @@ func TestGiteaReleaseURLTemplate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"GORELEASER_TEST_GITEA_URLS_DOWNLOAD=https://gitea.mycompany.com",
},
@@ -553,7 +551,7 @@ func TestGiteaGetDefaultBranch(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: srv.URL,
},
@@ -584,7 +582,7 @@ func TestGiteaGetDefaultBranchErr(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: srv.URL,
},
@@ -611,7 +609,7 @@ func TestGiteaChangelog(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: srv.URL,
},

View File

@@ -10,15 +10,15 @@ import (
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestNewGitHubClient(t *testing.T) {
t.Run("good urls", func(t *testing.T) {
githubURL := "https://github.mycompany.com"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: githubURL + "/api",
Upload: githubURL + "/upload",
@@ -35,7 +35,7 @@ func TestNewGitHubClient(t *testing.T) {
})
t.Run("bad api url", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: "://github.mycompany.com/api",
Upload: "https://github.mycompany.com/upload",
@@ -47,7 +47,7 @@ func TestNewGitHubClient(t *testing.T) {
})
t.Run("bad upload url", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "not a url:4994",
@@ -60,7 +60,7 @@ func TestNewGitHubClient(t *testing.T) {
t.Run("template", func(t *testing.T) {
githubURL := "https://github.mycompany.com"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_API=%s/api", githubURL),
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_UPLOAD=%s/upload", githubURL),
@@ -81,7 +81,7 @@ func TestNewGitHubClient(t *testing.T) {
})
t.Run("template invalid api", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
@@ -92,7 +92,7 @@ func TestNewGitHubClient(t *testing.T) {
})
t.Run("template invalid upload", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "{{ .Env.GORELEASER_NOT_EXISTS }}",
@@ -104,7 +104,7 @@ func TestNewGitHubClient(t *testing.T) {
})
t.Run("template invalid", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: "{{.dddddddddd",
},
@@ -116,7 +116,7 @@ func TestNewGitHubClient(t *testing.T) {
}
func TestGitHubUploadReleaseIDNotInt(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
@@ -158,7 +158,7 @@ func TestGitHubReleaseURLTemplate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"GORELEASER_TEST_GITHUB_URLS_DOWNLOAD=https://github.mycompany.com",
},
@@ -188,7 +188,7 @@ func TestGitHubReleaseURLTemplate(t *testing.T) {
}
func TestGitHubCreateReleaseWrongNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
NameTemplate: "{{.dddddddddd",
},
@@ -213,7 +213,7 @@ func TestGithubGetDefaultBranch(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: srv.URL + "/",
},
@@ -243,7 +243,7 @@ func TestGithubGetDefaultBranchErr(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: srv.URL + "/",
},
@@ -274,7 +274,7 @@ func TestChangelog(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: srv.URL + "/",
},
@@ -306,7 +306,7 @@ func TestReleaseNotes(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: srv.URL + "/",
},
@@ -339,7 +339,7 @@ func TestCloseMilestone(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
API: srv.URL + "/",
},

View File

@@ -13,8 +13,8 @@ import (
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -66,7 +66,7 @@ func TestGitLabReleaseURLTemplate(t *testing.T) {
}
for _, tt := range tests {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
},
@@ -118,7 +118,7 @@ func TestGitLabURLsAPITemplate(t *testing.T) {
gitlabURLs.API = "{{ .Env.GORELEASER_TEST_GITLAB_URLS_API }}"
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: envs,
GitLabURLs: gitlabURLs,
})
@@ -134,7 +134,7 @@ func TestGitLabURLsAPITemplate(t *testing.T) {
}
t.Run("no_env_specified", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
@@ -145,7 +145,7 @@ func TestGitLabURLsAPITemplate(t *testing.T) {
})
t.Run("invalid_template", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{.dddddddddd",
},
@@ -190,7 +190,7 @@ func TestGitLabURLsDownloadTemplate(t *testing.T) {
},
{
name: "url_registry",
wantURL: "/api/v4/projects/test%2Ftest/packages/generic/projectname/v1%2E0%2E0/test",
wantURL: "/api/v4/projects/test%2Ftest/packages/generic/projectname/1%2E0%2E0/test",
usePackageRegistry: true,
},
}
@@ -219,7 +219,7 @@ func TestGitLabURLsDownloadTemplate(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "projectname",
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
@@ -235,9 +235,7 @@ func TestGitLabURLsDownloadTemplate(t *testing.T) {
Download: tt.downloadURL,
UsePackageRegistry: tt.usePackageRegistry,
},
})
ctx.Version = "v1.0.0"
}, testctx.WithVersion("1.0.0"))
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)
@@ -256,7 +254,7 @@ func TestGitLabURLsDownloadTemplate(t *testing.T) {
}
func TestGitLabCreateReleaseUknownHost(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Owner: "owner",
@@ -310,7 +308,7 @@ func TestGitLabCreateReleaseReleaseNotExists(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -363,7 +361,7 @@ func TestGitLabCreateReleaseReleaseExists(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -391,7 +389,7 @@ func TestGitLabCreateReleaseUnkownHTTPError(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -416,7 +414,7 @@ func TestGitlabGetDefaultBranch(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -444,7 +442,7 @@ func TestGitlabGetDefaultBranchErr(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -474,7 +472,7 @@ func TestGitlabChangelog(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -521,7 +519,7 @@ func TestGitlabCreateFile(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -580,7 +578,7 @@ func TestCloseMileston(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
@@ -646,7 +644,7 @@ func TestCheckUseJobToken(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("CI_JOB_TOKEN", tt.ciToken)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitLabURLs: config.GitLabURLs{
UseJobToken: tt.useJobToken,
},

View File

@@ -3,14 +3,14 @@ package commitauthor
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestGet(t *testing.T) {
t.Run("valid", func(t *testing.T) {
author, err := Get(context.New(config.Project{
author, err := Get(testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "MAIL=foo@bar"},
}), config.CommitAuthor{
Name: "{{.Env.NAME}}",
@@ -25,7 +25,7 @@ func TestGet(t *testing.T) {
t.Run("invalid name tmpl", func(t *testing.T) {
_, err := Get(
context.New(config.Project{}),
testctx.New(),
config.CommitAuthor{
Name: "{{.Env.NOPE}}",
Email: "a",
@@ -35,7 +35,7 @@ func TestGet(t *testing.T) {
t.Run("invalid email tmpl", func(t *testing.T) {
_, err := Get(
context.New(config.Project{}),
testctx.New(),
config.CommitAuthor{
Name: "a",
Email: "{{.Env.NOPE}}",

View File

@@ -7,8 +7,7 @@ import (
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/muesli/termenv"
"github.com/stretchr/testify/require"
)
@@ -20,7 +19,7 @@ func TestNotice(t *testing.T) {
log.Log = log.New(&w)
log.Info("first")
ctx := context.New(config.Project{})
ctx := testctx.New()
Notice(ctx, "foo.bar.whatever: foobar")
log.Info("last")
require.True(t, ctx.Deprecated)
@@ -35,7 +34,7 @@ func TestNoticeCustom(t *testing.T) {
log.Log = log.New(&w)
log.Info("first")
ctx := context.New(config.Project{})
ctx := testctx.New()
NoticeCustom(ctx, "something-else", "some custom template with a url {{ .URL }}")
log.Info("last")
require.True(t, ctx.Deprecated)

View File

@@ -8,13 +8,13 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestExecute(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "blah",
Archives: []config.Archive{
{
@@ -23,13 +23,13 @@ func TestExecute(t *testing.T) {
},
},
},
})
ctx.Env["TEST_A_SECRET"] = "x"
ctx.Env["TEST_A_USERNAME"] = "u2"
ctx.Version = "2.1.0"
Env: []string{
"TEST_A_SECRET=x",
"TEST_A_USERNAME=u2",
},
}, testctx.WithVersion("2.1.0"))
// Preload artifacts
ctx.Artifacts = artifact.New()
folder := t.TempDir()
for _, a := range []struct {
id string

View File

@@ -3,8 +3,8 @@ package extrafiles
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,8 +13,9 @@ func TestTemplate(t *testing.T) {
{Glob: "./testdata/file{{ .Env.ONE }}.golden"},
}
ctx := context.New(config.Project{})
ctx.Env["ONE"] = "1"
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"ONE=1"},
})
files, err := Find(ctx, globs)
require.NoError(t, err)
require.Len(t, files, 1)
@@ -26,7 +27,7 @@ func TestBadTemplate(t *testing.T) {
{Glob: "./testdata/file{{ .Env.NOPE }}.golden"},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to glob "./testdata/file{{ .Env.NOPE }}.golden": template: tmpl:1:22: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
@@ -39,7 +40,7 @@ func TestShouldGetSpecificFile(t *testing.T) {
{Glob: "./testdata/file1.golden"},
}
files, err := Find(context.New(config.Project{}), globs)
files, err := Find(testctx.New(), globs)
require.NoError(t, err)
require.Len(t, files, 1)
@@ -51,7 +52,7 @@ func TestFailToGetSpecificFile(t *testing.T) {
{Glob: "./testdata/file453.golden"},
}
files, err := Find(context.New(config.Project{}), globs)
files, err := Find(testctx.New(), globs)
require.EqualError(t, err, "globbing failed for pattern ./testdata/file453.golden: matching \"./testdata/file453.golden\": file does not exist")
require.Empty(t, files)
}
@@ -61,7 +62,7 @@ func TestShouldGetFilesWithSuperStar(t *testing.T) {
{Glob: "./**/file?.golden"},
}
files, err := Find(context.New(config.Project{}), globs)
files, err := Find(testctx.New(), globs)
require.NoError(t, err)
require.Len(t, files, 3)
require.Equal(t, "testdata/file2.golden", files["file2.golden"])
@@ -74,7 +75,7 @@ func TestShouldGetAllFilesWithGoldenExtension(t *testing.T) {
{Glob: "./testdata/*.golden"},
}
files, err := Find(context.New(config.Project{}), globs)
files, err := Find(testctx.New(), globs)
require.NoError(t, err)
require.Len(t, files, 2)
require.Equal(t, "testdata/file1.golden", files["file1.golden"])
@@ -86,7 +87,7 @@ func TestShouldGetAllFilesInsideTestdata(t *testing.T) {
{Glob: "./testdata/*"},
}
files, err := Find(context.New(config.Project{}), globs)
files, err := Find(testctx.New(), globs)
require.NoError(t, err)
require.Len(t, files, 4)
require.Equal(t, "testdata/sub3/file1.golden", files["file1.golden"])
@@ -103,8 +104,7 @@ func TestTargetName(t *testing.T) {
},
}
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.New(testctx.WithCurrentTag("v1.0.0"))
files, err := Find(ctx, globs)
require.NoError(t, err)
require.Len(t, files, 1)
@@ -120,7 +120,7 @@ func TestTargetInvalidNameTemplate(t *testing.T) {
},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to name "file1_{{.Env.HONK}}.golden": template: tmpl:1:12: executing "tmpl" at <.Env.HONK>: map has no entry for key "HONK"`)
@@ -134,7 +134,7 @@ func TestTargetNameMatchesMultipleFiles(t *testing.T) {
},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to add extra_file: "./testdata/*" -> "file1.golden": glob matches multiple files`)
@@ -148,7 +148,7 @@ func TestTargetNameNoMatches(t *testing.T) {
},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `globbing failed for pattern ./testdata/file1.silver: matching "./testdata/file1.silver": file does not exist`)
@@ -159,7 +159,7 @@ func TestGlobEvalsToEmpty(t *testing.T) {
{Glob: `{{ printf "" }}`},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.NoError(t, err)
@@ -170,7 +170,7 @@ func TestTargetNameNoGlob(t *testing.T) {
{NameTemplate: "file1.golden"},
}
ctx := context.New(config.Project{})
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.NoError(t, err)

View File

@@ -16,6 +16,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -81,8 +82,10 @@ func TestDefaults(t *testing.T) {
}
func TestCheckConfig(t *testing.T) {
ctx := context.New(config.Project{ProjectName: "blah"})
ctx.Env["TEST_A_SECRET"] = "x"
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "blah",
Env: []string{"TEST_A_SECRET=x"},
})
type args struct {
ctx *context.Context
upload *config.Upload
@@ -224,7 +227,7 @@ func TestUpload(t *testing.T) {
}
return fmt.Errorf("unexpected http status code: %v", r.StatusCode)
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "blah",
Archives: []config.Archive{
{
@@ -233,11 +236,11 @@ func TestUpload(t *testing.T) {
},
},
},
})
ctx.Env["TEST_A_SECRET"] = "x"
ctx.Env["TEST_A_USERNAME"] = "u2"
ctx.Version = "2.1.0"
ctx.Artifacts = artifact.New()
Env: []string{
"TEST_A_SECRET=x",
"TEST_A_USERNAME=u2",
},
}, testctx.WithVersion("2.1.0"))
folder := t.TempDir()
for _, a := range []struct {
ext string

View File

@@ -4,8 +4,8 @@ import (
"errors"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/require"
)
@@ -15,7 +15,7 @@ func TestDescription(t *testing.T) {
}
func TestAnnounce(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Twitter: config.Twitter{
Enabled: true,
@@ -34,55 +34,51 @@ func TestAnnounce(t *testing.T) {
}
func TestAnnounceAllDisabled(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipAnnounce = true
ctx := testctx.New(testctx.SkipAnnounce)
b, err := Pipe{}.Skip(ctx)
require.NoError(t, err)
require.True(t, b)
})
t.Run("skip on patches", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Skip: "{{gt .Patch 0}}",
},
})
ctx.Semver.Patch = 1
}, testctx.WithSemver(0, 0, 1, ""))
b, err := Pipe{}.Skip(ctx)
require.NoError(t, err)
require.True(t, b)
})
t.Run("invalid template", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Skip: "{{if eq .Patch 123}",
},
})
ctx.Semver.Patch = 1
}, testctx.WithSemver(0, 0, 1, ""))
_, err := Pipe{}.Skip(ctx)
require.Error(t, err)
})
t.Run("dont skip", func(t *testing.T) {
b, err := Pipe{}.Skip(context.New(config.Project{}))
b, err := Pipe{}.Skip(testctx.New())
require.NoError(t, err)
require.False(t, b)
})
t.Run("dont skip based on template", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Skip: "{{gt .Patch 0}}",
},
})
ctx.Semver.Patch = 0
b, err := Pipe{}.Skip(ctx)
require.NoError(t, err)
require.False(t, b)

View File

@@ -4,15 +4,15 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestMeta(t *testing.T) {
t.Run("good", func(t *testing.T) {
dist := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
Archives: []config.Archive{
{
@@ -36,7 +36,7 @@ func TestMeta(t *testing.T) {
t.Run("bad tmpl", func(t *testing.T) {
dist := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
Archives: []config.Archive{
{
@@ -55,7 +55,7 @@ func TestMeta(t *testing.T) {
t.Run("no files", func(t *testing.T) {
dist := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
Archives: []config.Archive{
{

View File

@@ -11,10 +11,10 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/archive"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -76,7 +76,7 @@ func TestRunPipe(t *testing.T) {
f, err := os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
ProjectName: "foobar",
@@ -267,7 +267,7 @@ func TestRunPipeDifferentBinaryCount(t *testing.T) {
createFakeBinary(t, dist, arch, "bin/mybin")
}
createFakeBinary(t, dist, "darwinamd64", "bin/foobar")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
ProjectName: "foobar",
Archives: []config.Archive{
@@ -334,15 +334,13 @@ func TestRunPipeNoBinaries(t *testing.T) {
folder := testlib.Mktmp(t)
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
ProjectName: "foobar",
Archives: []config.Archive{{
Builds: []string{"not-default"},
}},
})
ctx.Version = "0.0.1"
ctx.Git.CurrentTag = "v0.0.1"
}, testctx.WithVersion("0.0.1"), testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "linux",
Goarch: "amd64",
@@ -432,7 +430,7 @@ func TestRunPipeBinary(t *testing.T) {
f, err = os.Create(filepath.Join(folder, "README.md"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -443,9 +441,9 @@ func TestRunPipeBinary(t *testing.T) {
},
},
},
testctx.WithVersion("0.0.1"),
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Version = "0.0.1"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -519,7 +517,7 @@ func TestRunPipeBinary(t *testing.T) {
}
func TestRunPipeDistRemoved(t *testing.T) {
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: "/tmp/path/to/nope",
Archives: []config.Archive{
@@ -530,8 +528,8 @@ func TestRunPipeDistRemoved(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
@@ -556,7 +554,7 @@ func TestRunPipeInvalidGlob(t *testing.T) {
f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -570,6 +568,7 @@ func TestRunPipeInvalidGlob(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
@@ -594,7 +593,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -605,8 +604,8 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -629,7 +628,7 @@ func TestRunPipeInvalidFilesNameTemplate(t *testing.T) {
f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -643,8 +642,8 @@ func TestRunPipeInvalidFilesNameTemplate(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -667,7 +666,7 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -679,8 +678,8 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -706,7 +705,7 @@ func TestRunPipeWrap(t *testing.T) {
f, err = os.Create(filepath.Join(folder, "README.md"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
@@ -724,8 +723,8 @@ func TestRunPipeWrap(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -762,11 +761,9 @@ func TestRunPipeWrap(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NotEmpty(t, ctx.Config.Archives[0].NameTemplate)
require.Equal(t, "tar.gz", ctx.Config.Archives[0].Format)
@@ -775,20 +772,18 @@ func TestDefault(t *testing.T) {
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{
{
Builds: []string{"default"},
NameTemplate: "foo",
Format: "zip",
Files: []config.File{
{Source: "foo"},
},
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{
{
Builds: []string{"default"},
NameTemplate: "foo",
Format: "zip",
Files: []config.File{
{Source: "foo"},
},
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "foo", ctx.Config.Archives[0].NameTemplate)
require.Equal(t, "zip", ctx.Config.Archives[0].Format)
@@ -797,52 +792,46 @@ func TestDefaultSet(t *testing.T) {
}
func TestDefaultNoFiles(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{
{
Format: "tar.gz",
},
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{
{
Format: "tar.gz",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, defaultNameTemplate, ctx.Config.Archives[0].NameTemplate)
require.False(t, ctx.Config.Archives[0].RLCP)
}
func TestDefaultFormatBinary(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{
{
Format: "binary",
},
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{
{
Format: "binary",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, defaultBinaryNameTemplate, ctx.Config.Archives[0].NameTemplate)
require.False(t, ctx.Config.Archives[0].RLCP)
}
func TestFormatFor(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{
{
Builds: []string{"default"},
Format: "tar.gz",
FormatOverrides: []config.FormatOverride{
{
Goos: "windows",
Format: "zip",
},
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{
{
Builds: []string{"default"},
Format: "tar.gz",
FormatOverrides: []config.FormatOverride{
{
Goos: "windows",
Format: "zip",
},
},
},
},
}
})
require.Equal(t, "zip", packageFormat(ctx.Config.Archives[0], "windows"))
require.Equal(t, "tar.gz", packageFormat(ctx.Config.Archives[0], "linux"))
}
@@ -864,7 +853,7 @@ func TestBinaryOverride(t *testing.T) {
require.NoError(t, f.Close())
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(t *testing.T) {
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
ProjectName: "foobar",
@@ -884,8 +873,8 @@ func TestBinaryOverride(t *testing.T) {
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@@ -940,7 +929,7 @@ func TestRunPipeSameArchiveFilename(t *testing.T) {
f, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
ProjectName: "foobar",
@@ -1036,18 +1025,16 @@ func TestWrapInDirectory(t *testing.T) {
}
func TestSeveralArchivesWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Archives: []config.Archive{
{
ID: "a",
},
{
ID: "a",
},
ctx := testctx.NewWithCfg(config.Project{
Archives: []config.Archive{
{
ID: "a",
},
{
ID: "a",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 archives with the ID 'a', please fix your config")
}
@@ -1057,7 +1044,7 @@ func TestArchive_globbing(t *testing.T) {
bin, err := os.CreateTemp(t.TempDir(), "binary")
require.NoError(t, err)
dist := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
Archives: []config.Archive{
{
@@ -1135,7 +1122,7 @@ func TestArchive_globbing(t *testing.T) {
}
func TestInvalidFormat(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
Archives: []config.Archive{
{
@@ -1150,7 +1137,7 @@ func TestInvalidFormat(t *testing.T) {
}
func TestIssue3803(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
Archives: []config.Archive{
{

View File

@@ -10,9 +10,9 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -166,7 +166,7 @@ func TestRunPipe_ModeBinary(t *testing.T) {
}`)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -183,14 +183,12 @@ func TestRunPipe_ModeBinary(t *testing.T) {
Username: "productionuser",
},
},
Archives: []config.Archive{
{},
Archives: []config.Archive{{}},
Env: []string{
"ARTIFACTORY_PRODUCTION-US_SECRET=deployuser-secret",
"ARTIFACTORY_PRODUCTION-EU_SECRET=productionuser-apikey",
},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION-US_SECRET": "deployuser-secret",
"ARTIFACTORY_PRODUCTION-EU_SECRET": "productionuser-apikey",
}
for _, goos := range []string{"linux", "darwin"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
@@ -217,7 +215,7 @@ func TestRunPipe_ModeArchive(t *testing.T) {
require.NoError(t, err)
require.NoError(t, debfile.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "goreleaser",
Dist: folder,
Artifactories: []config.Upload{
@@ -228,14 +226,9 @@ func TestRunPipe_ModeArchive(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Version = "1.0.0"
Archives: []config.Archive{{}},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
}, testctx.WithVersion("1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -317,7 +310,7 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
require.NoError(t, err)
require.NoError(t, tarfile.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "goreleaser",
Dist: folder,
Artifactories: []config.Upload{
@@ -328,11 +321,8 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
Username: "deployuser",
},
},
})
ctx.Version = "2.0.0"
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
}, testctx.WithVersion("2.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -350,7 +340,7 @@ func TestRunPipe_TargetTemplateError(t *testing.T) {
dist := filepath.Join(folder, "dist")
binPath := filepath.Join(dist, "mybin", "mybin")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -362,13 +352,9 @@ func TestRunPipe_TargetTemplateError(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -409,7 +395,7 @@ func TestRunPipe_BadCredentials(t *testing.T) {
}`)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -420,13 +406,9 @@ func TestRunPipe_BadCredentials(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -464,7 +446,7 @@ func TestRunPipe_UnparsableErrorResponse(t *testing.T) {
fmt.Fprint(w, `<body><h1>error</h1></body>`)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -475,13 +457,9 @@ func TestRunPipe_UnparsableErrorResponse(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
Archives: []config.Archive{{}},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -495,7 +473,7 @@ func TestRunPipe_UnparsableErrorResponse(t *testing.T) {
}
func TestRunPipe_FileNotFound(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: "archivetest/dist",
Artifactories: []config.Upload{
@@ -506,13 +484,9 @@ func TestRunPipe_FileNotFound(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
Archives: []config.Archive{{}},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: "archivetest/dist/mybin/mybin",
@@ -534,7 +508,7 @@ func TestRunPipe_UnparsableTarget(t *testing.T) {
d1 := []byte("hello\ngo\n")
require.NoError(t, os.WriteFile(binPath, d1, 0o666))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -545,13 +519,9 @@ func TestRunPipe_UnparsableTarget(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -571,7 +541,7 @@ func TestRunPipe_DirUpload(t *testing.T) {
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
binPath := filepath.Join(dist, "mybin")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Artifactories: []config.Upload{
@@ -582,13 +552,9 @@ func TestRunPipe_DirUpload(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: filepath.Dir(binPath),
@@ -606,45 +572,35 @@ func TestDescription(t *testing.T) {
}
func TestArtifactoriesWithoutTarget(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Username: "deployuser",
},
},
}
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Publish(ctx))
}
func TestArtifactoriesWithoutUsername(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
},
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
},
},
}
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Publish(ctx))
}
func TestArtifactoriesWithoutName(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Username: "deployuser",
@@ -657,7 +613,7 @@ func TestArtifactoriesWithoutName(t *testing.T) {
}
func TestArtifactoriesWithoutSecret(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Name: "production",
@@ -671,38 +627,31 @@ func TestArtifactoriesWithoutSecret(t *testing.T) {
}
func TestArtifactoriesWithInvalidMode(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Mode: "does-not-exists",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Mode: "does-not-exists",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
},
}
Env: []string{"ARTIFACTORY_PRODUCTION_SECRET=deployuser-secret"},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Error(t, Pipe{}.Publish(ctx))
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Artifactories, 1)
@@ -711,26 +660,22 @@ func TestDefault(t *testing.T) {
}
func TestDefaultNoArtifactories(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Artifactories: []config.Upload{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Artifactories)
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Artifactories: []config.Upload{
{
Mode: "custom",
ChecksumHeader: "foo",
},
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{
{
Mode: "custom",
ChecksumHeader: "foo",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Artifactories, 1)
artifactory := ctx.Config.Artifactories[0]
@@ -740,11 +685,11 @@ func TestDefaultSet(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Artifactories: []config.Upload{{}},
})
require.False(t, Pipe{}.Skip(ctx))

View File

@@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -100,7 +101,7 @@ func createTemplateData() templateData {
func TestFullAur(t *testing.T) {
data := createTemplateData()
pkg, err := applyTemplate(context.New(config.Project{
pkg, err := applyTemplate(testctx.NewWithCfg(config.Project{
ProjectName: "foo",
}), aurTemplateData, data)
require.NoError(t, err)
@@ -109,7 +110,7 @@ func TestFullAur(t *testing.T) {
}
func TestAurSimple(t *testing.T) {
pkg, err := applyTemplate(context.New(config.Project{}), aurTemplateData, createTemplateData())
pkg, err := applyTemplate(testctx.New(), aurTemplateData, createTemplateData())
require.NoError(t, err)
require.Contains(t, pkg, `# Maintainer: Ciclano <ciclano@example.com>`)
require.Contains(t, pkg, `# Maintainer: Cicrano <cicrano@example.com>`)
@@ -125,7 +126,7 @@ func TestAurSimple(t *testing.T) {
func TestFullSrcInfo(t *testing.T) {
data := createTemplateData()
data.License = "MIT"
pkg, err := applyTemplate(context.New(config.Project{
pkg, err := applyTemplate(testctx.NewWithCfg(config.Project{
ProjectName: "foo",
}), srcInfoTemplate, data)
require.NoError(t, err)
@@ -134,7 +135,7 @@ func TestFullSrcInfo(t *testing.T) {
}
func TestSrcInfoSimple(t *testing.T) {
pkg, err := applyTemplate(context.New(config.Project{}), srcInfoTemplate, createTemplateData())
pkg, err := applyTemplate(testctx.New(), srcInfoTemplate, createTemplateData())
require.NoError(t, err)
require.Contains(t, pkg, `pkgbase = test-bin`)
require.Contains(t, pkg, `pkgname = test-bin`)
@@ -241,33 +242,26 @@ func TestFullPipe(t *testing.T) {
key := makeKey(t, keygen.Ed25519, nil)
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: name,
AURs: []config.AUR{
{
Name: name,
IDs: []string{"foo"},
PrivateKey: key,
License: "MIT",
GitURL: url,
Description: "A run pipe test fish food and FOO={{ .Env.FOO }}",
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
AURs: []config.AUR{
{
Name: name,
IDs: []string{"foo"},
PrivateKey: key,
License: "MIT",
GitURL: url,
Description: "A run pipe test fish food and FOO={{ .Env.FOO }}",
},
},
Env: []string{"FOO=foo_is_bar"},
},
})
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1-foo",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 1,
Prerelease: "foo",
}
ctx.Version = "1.0.1-foo"
ctx.Env = map[string]string{
"FOO": "foo_is_bar",
}
testctx.WithCurrentTag("v1.0.1-foo"),
testctx.WithSemver(1, 0, 1, "foo"),
testctx.WithVersion("1.0.1-foo"),
)
tt.prepare(ctx)
ctx.Artifacts.Add(&artifact.Artifact{
@@ -340,44 +334,36 @@ func TestRunPipe(t *testing.T) {
key := makeKey(t, keygen.Ed25519, nil)
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
AURs: []config.AUR{
{
License: "MIT",
Description: "A run pipe test aur and FOO={{ .Env.FOO }}",
Homepage: "https://github.com/goreleaser",
IDs: []string{"foo"},
GitURL: url,
PrivateKey: key,
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
AURs: []config.AUR{
{
License: "MIT",
Description: "A run pipe test aur and FOO={{ .Env.FOO }}",
Homepage: "https://github.com/goreleaser",
IDs: []string{"foo"},
GitURL: url,
PrivateKey: key,
},
},
},
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
Env: []string{"FOO=foo_is_bar"},
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 1,
}
ctx.Version = "1.0.1"
ctx.Artifacts = artifact.New()
ctx.Env = map[string]string{
"FOO": "foo_is_bar",
}
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithSemver(1, 0, 1, ""),
testctx.WithVersion("1.0.1"),
)
for _, a := range []struct {
name string
@@ -469,13 +455,10 @@ func TestRunPipe(t *testing.T) {
}
func TestRunPipeNoBuilds(t *testing.T) {
ctx := context.New(
config.Project{
ProjectName: "foo",
AURs: []config.AUR{{}},
},
)
ctx.TokenType = context.TokenTypeGitHub
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
AURs: []config.AUR{{}},
}, testctx.GitHubTokenType)
client := client.NewMock()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
@@ -486,24 +469,19 @@ func TestRunPipeBinaryRelease(t *testing.T) {
url := makeBareRepo(t)
key := makeKey(t, keygen.Ed25519, nil)
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
AURs: []config.AUR{{
GitURL: url,
PrivateKey: key,
}},
})
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.1",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 2,
Patch: 1,
}
ctx.Version = "1.2.1"
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
AURs: []config.AUR{{
GitURL: url,
PrivateKey: key,
}},
},
testctx.WithVersion("1.2.1"),
testctx.WithCurrentTag("v1.2.1"),
testctx.WithSemver(1, 2, 1, ""),
)
path := filepath.Join(folder, "dist/foo_linux_amd64/foo")
ctx.Artifacts.Add(&artifact.Artifact{
@@ -535,19 +513,17 @@ func TestRunPipeBinaryRelease(t *testing.T) {
func TestRunPipeNoUpload(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
AURs: []config.AUR{{}},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 1,
}
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
AURs: []config.AUR{{}},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithSemver(1, 0, 1, ""),
)
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
@@ -590,20 +566,18 @@ func TestRunPipeNoUpload(t *testing.T) {
func TestRunEmptyTokenType(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
AURs: []config.AUR{
{},
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
AURs: []config.AUR{
{},
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 1,
}
testctx.WithGitInfo(context.GitInfo{CurrentTag: "v1.0.1"}),
testctx.WithSemver(1, 0, 1, ""),
)
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -626,15 +600,10 @@ func TestRunEmptyTokenType(t *testing.T) {
func TestDefault(t *testing.T) {
t.Run("empty", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
AURs: []config.AUR{
{},
},
},
}
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
AURs: []config.AUR{{}},
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.AUR{
Name: "myproject-bin",
@@ -652,17 +621,14 @@ func TestDefault(t *testing.T) {
})
t.Run("name-without-bin-suffix", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
AURs: []config.AUR{
{
Name: "foo",
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
AURs: []config.AUR{
{
Name: "foo",
},
},
}
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.AUR{
Name: "foo-bin",
@@ -680,18 +646,15 @@ func TestDefault(t *testing.T) {
})
t.Run("partial", func(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
AURs: []config.AUR{
{
Conflicts: []string{"somethingelse"},
Goamd64: "v3",
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
AURs: []config.AUR{
{
Conflicts: []string{"somethingelse"},
Goamd64: "v3",
},
},
}
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.AUR{
Name: "myproject-bin",
@@ -711,11 +674,11 @@ func TestDefault(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
AURs: []config.AUR{
{},
},
@@ -786,7 +749,7 @@ func makeBareRepo(tb testing.TB) string {
tb.Helper()
dir := tb.TempDir()
_, err := git.Run(
context.New(config.Project{}),
testctx.New(),
"-C", dir,
"-c", "init.defaultBranch=master",
"init",
@@ -810,7 +773,7 @@ func makeKey(tb testing.TB, algo keygen.KeyType, pass []byte) string {
func requireEqualRepoFiles(tb testing.TB, folder, name, url string) {
tb.Helper()
dir := tb.TempDir()
_, err := git.Run(context.New(config.Project{}), "-C", dir, "clone", url, "repo")
_, err := git.Run(testctx.New(), "-C", dir, "clone", url, "repo")
require.NoError(tb, err)
for reponame, ext := range map[string]string{

View File

@@ -6,8 +6,8 @@ import (
"testing"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -29,7 +29,7 @@ func TestRunPipe(t *testing.T) {
{"go version", "go list"},
{`bash -c "go version; echo \"lala spaces and such\""`},
} {
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Before: config.Before{
Hooks: tc,
@@ -41,7 +41,7 @@ func TestRunPipe(t *testing.T) {
}
func TestRunPipeInvalidCommand(t *testing.T) {
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Before: config.Before{
Hooks: []string{`bash -c "echo \"unterminated command\"`},
@@ -56,7 +56,7 @@ func TestRunPipeFail(t *testing.T) {
"hook failed: go tool foobar: exit status 2; output: go: no such tool \"foobar\"\n": {"go tool foobar"},
"hook failed: sh ./testdata/foo.sh: exit status 1; output: lalala\n": {"sh ./testdata/foo.sh"},
} {
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Before: config.Before{
Hooks: tc,
@@ -69,7 +69,7 @@ func TestRunPipeFail(t *testing.T) {
func TestRunWithEnv(t *testing.T) {
f := filepath.Join(t.TempDir(), "testfile")
require.NoError(t, Pipe{}.Run(context.New(
require.NoError(t, Pipe{}.Run(testctx.NewWithCfg(
config.Project{
Env: []string{
"TEST_FILE=" + f,
@@ -83,7 +83,7 @@ func TestRunWithEnv(t *testing.T) {
}
func TestInvalidTemplate(t *testing.T) {
require.EqualError(t, Pipe{}.Run(context.New(
require.EqualError(t, Pipe{}.Run(testctx.NewWithCfg(
config.Project{
Before: config.Before{
Hooks: []string{"touch {{ .fasdsd }"},
@@ -94,11 +94,11 @@ func TestInvalidTemplate(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip before", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Before: config.Before{
Hooks: []string{""},
},
@@ -108,7 +108,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Before: config.Before{
Hooks: []string{""},
},

View File

@@ -10,6 +10,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -80,7 +81,7 @@ func TestMinioUpload(t *testing.T) {
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))
require.NoError(t, os.WriteFile(sigpath, []byte("fake\nsig"), 0o744))
require.NoError(t, os.WriteFile(certpath, []byte("fake\ncert"), 0o744))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "testupload",
Blobs: []config.Blob{
@@ -97,8 +98,7 @@ func TestMinioUpload(t *testing.T) {
},
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.Checksum,
Name: "checksum.txt",
@@ -169,7 +169,7 @@ func TestMinioUploadCustomBucketID(t *testing.T) {
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))
// Set custom BUCKET_ID env variable.
require.NoError(t, os.Setenv("BUCKET_ID", name))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "testupload",
Blobs: []config.Blob{
@@ -179,8 +179,7 @@ func TestMinioUploadCustomBucketID(t *testing.T) {
Endpoint: "http://" + listen,
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -204,7 +203,7 @@ func TestMinioUploadRootFolder(t *testing.T) {
debpath := filepath.Join(folder, "bin.deb")
require.NoError(t, os.WriteFile(tgzpath, []byte("fake\ntargz"), 0o744))
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "testupload",
Blobs: []config.Blob{
@@ -215,8 +214,7 @@ func TestMinioUploadRootFolder(t *testing.T) {
Endpoint: "http://" + listen,
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -239,7 +237,7 @@ func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
debpath := filepath.Join(folder, "bin.deb")
require.NoError(t, os.WriteFile(tgzpath, []byte("fake\ntargz"), 0o744))
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "testupload",
Blobs: []config.Blob{
@@ -249,8 +247,7 @@ func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
Endpoint: "http://" + listen,
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.1.0"}
}, testctx.WithCurrentTag("v1.1.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",

View File

@@ -4,9 +4,9 @@ import (
"fmt"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -35,7 +35,7 @@ func TestErrors(t *testing.T) {
func TestDefaultsNoConfig(t *testing.T) {
errorString := "bucket or provider cannot be empty"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{{}},
})
require.EqualError(t, Pipe{}.Default(ctx), errorString)
@@ -43,7 +43,7 @@ func TestDefaultsNoConfig(t *testing.T) {
func TestDefaultsNoBucket(t *testing.T) {
errorString := "bucket or provider cannot be empty"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{
{
Provider: "azblob",
@@ -55,7 +55,7 @@ func TestDefaultsNoBucket(t *testing.T) {
func TestDefaultsNoProvider(t *testing.T) {
errorString := "bucket or provider cannot be empty"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{
{
Bucket: "goreleaser-bucket",
@@ -66,7 +66,7 @@ func TestDefaultsNoProvider(t *testing.T) {
}
func TestDefaults(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{
{
Bucket: "foo",
@@ -96,7 +96,7 @@ func TestDefaults(t *testing.T) {
}
func TestDefaultsWithProvider(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{
{
Bucket: "foo",
@@ -117,7 +117,7 @@ func TestDefaultsWithProvider(t *testing.T) {
func TestURL(t *testing.T) {
t.Run("s3 with opts", func(t *testing.T) {
url, err := urlFor(context.New(config.Project{}), config.Blob{
url, err := urlFor(testctx.New(), config.Blob{
Bucket: "foo",
Provider: "s3",
Region: "us-west-1",
@@ -130,7 +130,7 @@ func TestURL(t *testing.T) {
})
t.Run("s3 with some opts", func(t *testing.T) {
url, err := urlFor(context.New(config.Project{}), config.Blob{
url, err := urlFor(testctx.New(), config.Blob{
Bucket: "foo",
Provider: "s3",
Region: "us-west-1",
@@ -141,7 +141,7 @@ func TestURL(t *testing.T) {
})
t.Run("gs with opts", func(t *testing.T) {
url, err := urlFor(context.New(config.Project{}), config.Blob{
url, err := urlFor(testctx.New(), config.Blob{
Bucket: "foo",
Provider: "gs",
Region: "us-west-1",
@@ -154,7 +154,7 @@ func TestURL(t *testing.T) {
})
t.Run("s3 no opts", func(t *testing.T) {
url, err := urlFor(context.New(config.Project{}), config.Blob{
url, err := urlFor(testctx.New(), config.Blob{
Bucket: "foo",
Provider: "s3",
})
@@ -163,7 +163,7 @@ func TestURL(t *testing.T) {
})
t.Run("gs no opts", func(t *testing.T) {
url, err := urlFor(context.New(config.Project{}), config.Blob{
url, err := urlFor(testctx.New(), config.Blob{
Bucket: "foo",
Provider: "gs",
})
@@ -173,20 +173,20 @@ func TestURL(t *testing.T) {
t.Run("template errors", func(t *testing.T) {
t.Run("provider", func(t *testing.T) {
_, err := urlFor(context.New(config.Project{}), config.Blob{
_, err := urlFor(testctx.New(), config.Blob{
Provider: "{{ .Nope }}",
})
testlib.RequireTemplateError(t, err)
})
t.Run("bucket", func(t *testing.T) {
_, err := urlFor(context.New(config.Project{}), config.Blob{
_, err := urlFor(testctx.New(), config.Blob{
Bucket: "{{ .Nope }}",
Provider: "gs",
})
testlib.RequireTemplateError(t, err)
})
t.Run("endpoint", func(t *testing.T) {
_, err := urlFor(context.New(config.Project{}), config.Blob{
_, err := urlFor(testctx.New(), config.Blob{
Bucket: "foobar",
Endpoint: "{{.Env.NOPE}}",
Provider: "s3",
@@ -194,7 +194,7 @@ func TestURL(t *testing.T) {
testlib.RequireTemplateError(t, err)
})
t.Run("region", func(t *testing.T) {
_, err := urlFor(context.New(config.Project{}), config.Blob{
_, err := urlFor(testctx.New(), config.Blob{
Bucket: "foobar",
Region: "{{.Env.NOPE}}",
Provider: "s3",
@@ -206,11 +206,11 @@ func TestURL(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Blobs: []config.Blob{{}},
})
require.False(t, Pipe{}.Skip(ctx))

View File

@@ -9,6 +9,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -106,7 +107,7 @@ func TestFullFormulae(t *testing.T) {
data.PostInstall = []string{`touch "/tmp/foo"`, `system "echo", "done"`}
data.CustomBlock = []string{"devel do", ` url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`, ` sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`, "end"}
data.Tests = []string{`system "#{bin}/{{.ProjectName}}", "-version"`}
formulae, err := doBuildFormula(context.New(config.Project{
formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
@@ -117,7 +118,7 @@ func TestFullFormulae(t *testing.T) {
func TestFullFormulaeLinuxOnly(t *testing.T) {
data := defaultTemplateData
data.MacOSPackages = []releasePackage{}
formulae, err := doBuildFormula(context.New(config.Project{
formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
@@ -128,7 +129,7 @@ func TestFullFormulaeLinuxOnly(t *testing.T) {
func TestFullFormulaeMacOSOnly(t *testing.T) {
data := defaultTemplateData
data.LinuxPackages = []releasePackage{}
formulae, err := doBuildFormula(context.New(config.Project{
formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
ProjectName: "foo",
}), data)
require.NoError(t, err)
@@ -137,7 +138,7 @@ func TestFullFormulaeMacOSOnly(t *testing.T) {
}
func TestFormulaeSimple(t *testing.T) {
formulae, err := doBuildFormula(context.New(config.Project{}), defaultTemplateData)
formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{}), defaultTemplateData)
require.NoError(t, err)
assertDefaultTemplateData(t, formulae)
require.NotContains(t, formulae, "def caveats")
@@ -258,16 +259,8 @@ func TestFullPipe(t *testing.T) {
} {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
Brews: []config.Homebrew{
@@ -292,8 +285,11 @@ func TestFullPipe(t *testing.T) {
Goamd64: "v1",
},
},
Env: []string{"FOO=foo_is_bar"},
},
}
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
tt.prepare(ctx)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bar_bin.tar.gz",
@@ -374,16 +370,8 @@ func TestFullPipe(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Brews: []config.Homebrew{
@@ -402,8 +390,11 @@ func TestRunPipeNameTemplate(t *testing.T) {
},
},
},
Env: []string{"FOO_BAR=is_bar"},
},
}
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
@@ -435,17 +426,8 @@ func TestRunPipeNameTemplate(t *testing.T) {
func TestRunPipeMultipleBrewsWithSkip(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",
"SKIP_UPLOAD": "true",
},
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Brews: []config.Homebrew{
@@ -497,8 +479,14 @@ func TestRunPipeMultipleBrewsWithSkip(t *testing.T) {
SkipUpload: "{{ .Env.SKIP_UPLOAD }}",
},
},
Env: []string{
"FOO_BAR=is_bar",
"SKIP_UPLOAD=true",
},
},
}
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
@@ -546,17 +534,8 @@ func TestRunPipeForMultipleAmd64Versions(t *testing.T) {
} {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
Brews: []config.Homebrew{
@@ -580,8 +559,12 @@ func TestRunPipeForMultipleAmd64Versions(t *testing.T) {
Name: "test",
},
},
Env: []string{"FOO=foo_is_bar"},
},
}
testctx.GitHubTokenType,
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
fn(ctx)
for _, a := range []struct {
name string
@@ -671,17 +654,8 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
} {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
Brews: []config.Homebrew{
@@ -710,8 +684,12 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
Name: "test",
},
},
Env: []string{"FOO=foo_is_bar"},
},
}
testctx.GitHubTokenType,
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
fn(ctx)
for _, a := range []struct {
name string
@@ -782,39 +760,33 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
}
func TestRunPipeNoBuilds(t *testing.T) {
ctx := context.New(
config.Project{
Brews: []config.Homebrew{
{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
ctx := testctx.NewWithCfg(config.Project{
Brews: []config.Homebrew{
{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
)
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
ctx := context.New(
config.Project{
Brews: []config.Homebrew{
{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
ctx := testctx.NewWithCfg(config.Project{
Brews: []config.Homebrew{
{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
)
}, testctx.GitHubTokenType)
ctx.TokenType = context.TokenTypeGitHub
f, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)
t.Cleanup(func() {
@@ -949,13 +921,8 @@ func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Brews: []config.Homebrew{
@@ -970,8 +937,9 @@ func TestRunPipeBinaryRelease(t *testing.T) {
},
},
},
}
testctx.WithVersion("1.2.1"),
testctx.WithCurrentTag("v1.2.1"),
)
path := filepath.Join(folder, "dist/foo_darwin_all/foo")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo_macos",
@@ -1000,7 +968,7 @@ func TestRunPipeBinaryRelease(t *testing.T) {
func TestRunPipeNoUpload(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
@@ -1013,12 +981,8 @@ func TestRunPipeNoUpload(t *testing.T) {
Goamd64: "v1",
},
},
})
ctx.Env = map[string]string{
"SKIP_UPLOAD": "true",
}
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
Env: []string{"SKIP_UPLOAD=true"},
}, testctx.WithCurrentTag("v1.0.1"), testctx.GitHubTokenType)
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -1062,7 +1026,7 @@ func TestRunPipeNoUpload(t *testing.T) {
func TestRunEmptyTokenType(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
@@ -1075,8 +1039,7 @@ func TestRunEmptyTokenType(t *testing.T) {
Goamd64: "v1",
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
}, testctx.WithCurrentTag("v1.0.0"))
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -1099,16 +1062,12 @@ func TestRunEmptyTokenType(t *testing.T) {
func TestDefault(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
Brews: []config.Homebrew{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
Brews: []config.Homebrew{
{},
},
}
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Brews[0].Name)
require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Name)
@@ -1123,11 +1082,11 @@ func TestGHFolder(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Brews: []config.Homebrew{
{},
},
@@ -1137,7 +1096,7 @@ func TestSkip(t *testing.T) {
}
func TestRunSkipNoName(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Brews: []config.Homebrew{{}},
})
@@ -1148,7 +1107,7 @@ func TestRunSkipNoName(t *testing.T) {
func TestInstalls(t *testing.T) {
t.Run("provided", func(t *testing.T) {
install, err := installs(
context.New(config.Project{}),
testctx.New(),
config.Homebrew{Install: "bin.install \"foo\"\nbin.install \"bar\""},
&artifact.Artifact{},
)
@@ -1161,8 +1120,7 @@ func TestInstalls(t *testing.T) {
t.Run("from archives", func(t *testing.T) {
install, err := installs(
context.New(config.Project{}),
testctx.New(),
config.Homebrew{},
&artifact.Artifact{
Type: artifact.UploadableArchive,
@@ -1180,7 +1138,7 @@ func TestInstalls(t *testing.T) {
t.Run("from binary", func(t *testing.T) {
install, err := installs(
context.New(config.Project{}),
testctx.New(),
config.Homebrew{},
&artifact.Artifact{
Name: "foo_macos",
@@ -1198,7 +1156,7 @@ func TestInstalls(t *testing.T) {
t.Run("from template", func(t *testing.T) {
install, err := installs(
context.New(config.Project{}),
testctx.New(),
config.Homebrew{
Install: `bin.install "foo_{{.Os}}" => "foo"`,
},
@@ -1217,13 +1175,8 @@ func TestInstalls(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "unibin",
Brews: []config.Homebrew{
@@ -1242,7 +1195,9 @@ func TestRunPipeUniversalBinary(t *testing.T) {
},
},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
@@ -1275,13 +1230,8 @@ func TestRunPipeUniversalBinary(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "unibin",
Brews: []config.Homebrew{
@@ -1301,7 +1251,10 @@ func TestRunPipeUniversalBinaryNotReplacing(t *testing.T) {
},
},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin_amd64.tar.gz",

View File

@@ -8,6 +8,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
api "github.com/goreleaser/goreleaser/pkg/build"
@@ -78,15 +79,15 @@ func TestBuild(t *testing.T) {
},
},
}
ctx := &context.Context{
Artifacts: artifact.New(),
Git: context.GitInfo{
ctx := testctx.NewWithCfg(
config,
testctx.WithVersion("1.2.3"),
testctx.WithGitInfo(context.GitInfo{
CurrentTag: "v1.2.3",
Commit: "123",
},
Version: "1.2.3",
Config: config,
}
}),
)
opts, err := buildOptionsForTarget(ctx, ctx.Config.Builds[0], "darwin_amd64")
require.NoError(t, err)
error := doBuild(ctx, ctx.Config.Builds[0], *opts)
@@ -95,7 +96,7 @@ func TestBuild(t *testing.T) {
func TestRunPipe(t *testing.T) {
folder := testlib.Mktmp(t)
config := config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Builds: []config.Build{
{
@@ -108,9 +109,7 @@ func TestRunPipe(t *testing.T) {
Targets: []string{"linux_amd64"},
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "2.4.5"
}, testctx.WithCurrentTag("2.4.5"))
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, ctx.Artifacts.List(), []*artifact.Artifact{{
Name: "testing",
@@ -149,8 +148,7 @@ func TestRunFullPipe(t *testing.T) {
},
Dist: folder,
}
ctx := context.New(config)
ctx.Git.CurrentTag = "2.4.5"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("2.4.5"))
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, ctx.Artifacts.List(), []*artifact.Artifact{{
@@ -189,8 +187,7 @@ func TestRunFullPipeFail(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "2.4.5"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("2.4.5"))
require.EqualError(t, Pipe{}.Run(ctx), errFailedBuild.Error())
require.Empty(t, ctx.Artifacts.List())
require.FileExists(t, pre)
@@ -210,15 +207,13 @@ func TestRunPipeFailingHooks(t *testing.T) {
},
}
t.Run("pre-hook", func(t *testing.T) {
ctx := context.New(cfg)
ctx.Git.CurrentTag = "2.3.4"
ctx := testctx.NewWithCfg(cfg, testctx.WithCurrentTag("2.4.5"))
ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "exit 1"}}
ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "echo post"}}
require.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: failed to run 'exit 1': exec: \"exit\": executable file not found in $PATH")
})
t.Run("post-hook", func(t *testing.T) {
ctx := context.New(cfg)
ctx.Git.CurrentTag = "2.3.4"
ctx := testctx.NewWithCfg(cfg, testctx.WithCurrentTag("2.4.5"))
ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "echo pre"}}
ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "exit 1"}}
require.EqualError(t, Pipe{}.Run(ctx), `post hook failed: failed to run 'exit 1': exec: "exit": executable file not found in $PATH`)
@@ -226,9 +221,7 @@ func TestRunPipeFailingHooks(t *testing.T) {
}
func TestDefaultNoBuilds(t *testing.T) {
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
}
@@ -242,40 +235,36 @@ func TestDefaultFail(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx := testctx.NewWithCfg(config)
require.EqualError(t, Pipe{}.Default(ctx), errFailedDefault.Error())
require.Empty(t, ctx.Artifacts.List())
}
func TestDefaultExpandEnv(t *testing.T) {
require.NoError(t, os.Setenv("XBAR", "FOOBAR"))
ctx := &context.Context{
Config: config.Project{
Builds: []config.Build{
{
BuildDetails: config.BuildDetails{
Env: []string{
"XFOO=bar_$XBAR",
},
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
BuildDetails: config.BuildDetails{
Env: []string{
"XFOO=bar_$XBAR",
},
},
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
env := ctx.Config.Builds[0].Env[0]
require.Equal(t, "XFOO=bar_FOOBAR", env)
}
func TestDefaultEmptyBuild(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "foo",
Builds: []config.Build{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Builds: []config.Build{
{},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
build := ctx.Config.Builds[0]
require.Equal(t, ctx.Config.ProjectName, build.ID)
@@ -292,19 +281,17 @@ func TestDefaultEmptyBuild(t *testing.T) {
}
func TestDefaultBuildID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "foo",
Builds: []config.Build{
{
Binary: "{{.Env.FOO}}",
},
{
Binary: "bar",
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Builds: []config.Build{
{
Binary: "{{.Env.FOO}}",
},
{
Binary: "bar",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'foo', please fix your config")
build1 := ctx.Config.Builds[0].ID
build2 := ctx.Config.Builds[1].ID
@@ -313,45 +300,41 @@ func TestDefaultBuildID(t *testing.T) {
}
func TestSeveralBuildsWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "a",
Binary: "bar",
},
{
ID: "a",
Binary: "foo",
},
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "a",
Binary: "bar",
},
{
ID: "a",
Binary: "foo",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'a', please fix your config")
}
func TestDefaultPartialBuilds(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "build1",
Binary: "bar",
Goos: []string{"linux"},
Main: "./cmd/main.go",
},
{
ID: "build2",
Binary: "foo",
Dir: "baz",
BuildDetails: config.BuildDetails{
Ldflags: []string{"-s -w"},
},
Goarch: []string{"386"},
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "build1",
Binary: "bar",
Goos: []string{"linux"},
Main: "./cmd/main.go",
},
{
ID: "build2",
Binary: "foo",
Dir: "baz",
BuildDetails: config.BuildDetails{
Ldflags: []string{"-s -w"},
},
Goarch: []string{"386"},
},
},
}
})
// Create any 'Dir' paths necessary for builds.
cwd, err := os.Getwd()
require.NoError(t, err)
@@ -391,14 +374,12 @@ func TestDefaultPartialBuilds(t *testing.T) {
func TestDefaultFillSingleBuild(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
Config: config.Project{
ProjectName: "foo",
SingleBuild: config.Build{
Main: "testreleaser",
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
SingleBuild: config.Build{
Main: "testreleaser",
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Builds, 1)
require.Equal(t, ctx.Config.Builds[0].Binary, "foo")
@@ -412,7 +393,7 @@ func TestDefaultFailSingleBuild(t *testing.T) {
Builder: "fakeFailDefault",
},
}
ctx := context.New(config)
ctx := testctx.NewWithCfg(config)
require.EqualError(t, Pipe{}.Default(ctx), errFailedDefault.Error())
require.Empty(t, ctx.Artifacts.List())
}
@@ -427,8 +408,7 @@ func TestSkipBuild(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "2.4.5"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("2.4.5"))
require.NoError(t, Pipe{}.Run(ctx))
require.Len(t, ctx.Artifacts.List(), 0)
}
@@ -477,13 +457,12 @@ func TestExtOthers(t *testing.T) {
}
func TestTemplate(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
Commit: "123",
}
ctx.Version = "1.2.3"
ctx.Env = map[string]string{"FOO": "123"}
ctx := testctx.New(
testctx.WithEnv(map[string]string{"FOO": "123"}),
testctx.WithVersion("1.2.3"),
testctx.WithCurrentTag("v1.2.3"),
testctx.WithCommit("123"),
)
binary, err := tmpl.New(ctx).
Apply(`-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}"`)
require.NoError(t, err)
@@ -515,7 +494,7 @@ func TestBuild_hooksKnowGoosGoarch(t *testing.T) {
},
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
build,
},
@@ -547,7 +526,7 @@ func TestPipeOnBuild_hooksRunPerTarget(t *testing.T) {
},
},
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
build,
},
@@ -571,7 +550,7 @@ func TestPipeOnBuild_invalidBinaryTpl(t *testing.T) {
"linux_amd64",
},
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
build,
},
@@ -703,7 +682,7 @@ func TestBuildOptionsForTarget(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: tmpDir,
Builds: []config.Build{tc.build},
})
@@ -739,8 +718,7 @@ func TestRunHookFailWithLogs(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "2.4.5"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("2.4.5"))
require.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: failed to run 'sh -c echo foo; exit 1': exit status 1")
require.Empty(t, ctx.Artifacts.List())
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -20,66 +21,62 @@ func TestDescription(t *testing.T) {
}
func TestChangelogProvidedViaFlag(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotesFile = "testdata/changes.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "c0ff33 coffeee\n", ctx.ReleaseNotes)
}
func TestChangelogProvidedViaFlagIsAWhitespaceOnlyFile(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotesFile = "testdata/changes-empty.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "\n", ctx.ReleaseNotes)
}
func TestChangelogProvidedViaFlagIsReallyEmpty(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotesFile = "testdata/changes-really-empty.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "", ctx.ReleaseNotes)
}
func TestChangelogTmplProvidedViaFlagIsReallyEmpty(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotesTmpl = "testdata/changes-really-empty.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "", ctx.ReleaseNotes)
}
func TestTemplatedChangelogProvidedViaFlag(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseNotesFile = "testdata/changes.md"
ctx.ReleaseNotesTmpl = "testdata/changes-templated.md"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "c0ff33 coffeee v0.0.1\n", ctx.ReleaseNotes)
}
func TestTemplatedChangelogProvidedViaFlagResultIsEmpty(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseNotesTmpl = "testdata/changes-templated-empty.md"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "\n\n", ctx.ReleaseNotes)
}
func TestChangelogProvidedViaFlagDoesntExist(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotesFile = "testdata/changes.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/changes.nope: no such file or directory")
}
func TestReleaseHeaderProvidedViaFlagDoesntExist(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseHeaderFile = "testdata/header.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/header.nope: no such file or directory")
}
func TestReleaseFooterProvidedViaFlagDoesntExist(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseFooterFile = "testdata/footer.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/footer.nope: no such file or directory")
}
@@ -98,7 +95,7 @@ func TestChangelog(t *testing.T) {
testlib.GitCommit(t, "Merge pull request #999 from goreleaser/some-branch")
testlib.GitCommit(t, "this is not a Merge pull request")
testlib.GitTag(t, "v0.0.2")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Use: "git",
@@ -111,9 +108,7 @@ func TestChangelog(t *testing.T) {
},
},
},
})
ctx.Git.PreviousTag = "v0.0.1"
ctx.Git.CurrentTag = "v0.0.2"
}, testctx.WithCurrentTag("v0.0.2"), testctx.WithPreviousTag("v0.0.1"))
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.NotContains(t, ctx.ReleaseNotes, "first")
@@ -150,22 +145,24 @@ func TestChangelogForGitlab(t *testing.T) {
testlib.GitCommit(t, "Merge pull request #999 from goreleaser/some-branch")
testlib.GitCommit(t, "this is not a Merge pull request")
testlib.GitTag(t, "v0.0.2")
ctx := context.New(config.Project{
Dist: folder,
Changelog: config.Changelog{
Filters: config.Filters{
Exclude: []string{
"docs:",
"ignored:",
"(?i)cars",
"^Merge pull request",
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
Changelog: config.Changelog{
Filters: config.Filters{
Exclude: []string{
"docs:",
"ignored:",
"(?i)cars",
"^Merge pull request",
},
},
},
},
})
ctx.TokenType = context.TokenTypeGitLab
ctx.Git.PreviousTag = "v0.0.1"
ctx.Git.CurrentTag = "v0.0.2"
testctx.GitLabTokenType,
testctx.WithCurrentTag("v0.0.2"),
testctx.WithPreviousTag("v0.0.1"),
)
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.NotContains(t, ctx.ReleaseNotes, "first")
@@ -190,11 +187,11 @@ func TestChangelogSort(t *testing.T) {
testlib.GitCommit(t, "a: commit")
testlib.GitCommit(t, "b: commit")
testlib.GitTag(t, "v1.0.0")
ctx := context.New(config.Project{
Changelog: config.Changelog{},
})
ctx.Git.PreviousTag = "v0.9.9"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.New(
testctx.WithCurrentTag("v1.0.0"),
testctx.WithPreviousTag("v0.9.9"),
)
for _, cfg := range []struct {
Sort string
@@ -240,7 +237,7 @@ func TestChangelogSort(t *testing.T) {
}
func TestChangelogInvalidSort(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Sort: "dope",
},
@@ -261,9 +258,7 @@ func TestChangelogOfFirstRelease(t *testing.T) {
testlib.GitCommit(t, msg)
}
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
@@ -278,7 +273,7 @@ func TestChangelogFilterInvalidRegex(t *testing.T) {
testlib.GitTag(t, "v0.0.3")
testlib.GitCommit(t, "commitzzz")
testlib.GitTag(t, "v0.0.4")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Filters: config.Filters{
Exclude: []string{
@@ -286,9 +281,7 @@ func TestChangelogFilterInvalidRegex(t *testing.T) {
},
},
},
})
ctx.Git.PreviousTag = "v0.0.3"
ctx.Git.CurrentTag = "v0.0.4"
}, testctx.WithCurrentTag("v0.0.4"), testctx.WithPreviousTag("v0.0.3"))
require.EqualError(t, Pipe{}.Run(ctx), "error parsing regexp: invalid or unsupported Perl syntax: `(?ia`")
}
@@ -296,7 +289,7 @@ func TestChangelogNoTags(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitCommit(t, "first")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.Empty(t, ctx.ReleaseNotes)
}
@@ -315,9 +308,7 @@ func TestChangelogOnBranchWithSameNameAsTag(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
@@ -342,9 +333,7 @@ func TestChangeLogWithReleaseHeader(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseHeaderFile = "testdata/release-header.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -368,9 +357,7 @@ func TestChangeLogWithTemplatedReleaseHeader(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseHeaderTmpl = "testdata/release-header-templated.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -394,9 +381,7 @@ func TestChangeLogWithReleaseFooter(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseFooterFile = "testdata/release-footer.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -421,9 +406,7 @@ func TestChangeLogWithTemplatedReleaseFooter(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
ctx.ReleaseFooterTmpl = "testdata/release-footer-templated.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -448,20 +431,18 @@ func TestChangeLogWithoutReleaseFooter(t *testing.T) {
}
testlib.GitTag(t, "v0.0.1")
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx := testctx.New(testctx.WithCurrentTag("v0.0.1"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.Equal(t, rune(ctx.ReleaseNotes[len(ctx.ReleaseNotes)-1]), '\n')
}
func TestGetChangelogGitHub(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHub,
},
})
}, testctx.WithCurrentTag("v0.180.2"), testctx.WithPreviousTag("v0.180.1"))
expected := "c90f1085f255d0af0b055160bfff5ee40f47af79: fix: do not skip any defaults (#2521) (@caarlos0)"
mock := client.NewMock()
@@ -474,21 +455,17 @@ func TestGetChangelogGitHub(t *testing.T) {
},
}
ctx.Git = context.GitInfo{
CurrentTag: "v0.180.2",
PreviousTag: "v0.180.1",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
}
func TestGetChangelogGitHubNative(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHubNative,
},
})
}, testctx.WithCurrentTag("v0.180.2"), testctx.WithPreviousTag("v0.180.1"))
expected := `## What's changed
@@ -505,10 +482,6 @@ func TestGetChangelogGitHubNative(t *testing.T) {
Name: "goreleaser",
},
}
ctx.Git = context.GitInfo{
CurrentTag: "v0.180.2",
PreviousTag: "v0.180.1",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
@@ -516,11 +489,11 @@ func TestGetChangelogGitHubNative(t *testing.T) {
}
func TestGetChangelogGitHubNativeFirstRelease(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHubNative,
},
})
}, testctx.WithCurrentTag("v0.1.0"))
expected := `## What's changed
@@ -537,9 +510,6 @@ func TestGetChangelogGitHubNativeFirstRelease(t *testing.T) {
Name: "goreleaser",
},
}
ctx.Git = context.GitInfo{
CurrentTag: "v0.1.0",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
@@ -548,13 +518,13 @@ func TestGetChangelogGitHubNativeFirstRelease(t *testing.T) {
func TestGetChangeloger(t *testing.T) {
t.Run("default", func(t *testing.T) {
c, err := getChangeloger(context.New(config.Project{}))
c, err := getChangeloger(testctx.New())
require.NoError(t, err)
require.IsType(t, c, gitChangeloger{})
})
t.Run(useGit, func(t *testing.T) {
c, err := getChangeloger(context.New(config.Project{
c, err := getChangeloger(testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGit,
},
@@ -564,24 +534,22 @@ func TestGetChangeloger(t *testing.T) {
})
t.Run(useGitHub, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHub,
},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
c, err := getChangeloger(ctx)
require.NoError(t, err)
require.IsType(t, c, &scmChangeloger{})
})
t.Run(useGitHubNative, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHubNative,
},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
c, err := getChangeloger(ctx)
require.NoError(t, err)
require.IsType(t, c, &githubNativeChangeloger{})
@@ -591,24 +559,22 @@ func TestGetChangeloger(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gist.github.com/")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHubNative,
},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
c, err := getChangeloger(ctx)
require.EqualError(t, err, "unsupported repository URL: https://gist.github.com/")
require.Nil(t, c)
})
t.Run(useGitLab, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitLab,
},
})
ctx.TokenType = context.TokenTypeGitLab
}, testctx.GitHubTokenType)
c, err := getChangeloger(ctx)
require.NoError(t, err)
require.IsType(t, c, &scmChangeloger{})
@@ -618,19 +584,18 @@ func TestGetChangeloger(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gist.github.com/")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: useGitHub,
},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
c, err := getChangeloger(ctx)
require.EqualError(t, err, "unsupported repository URL: https://gist.github.com/")
require.Nil(t, c)
})
t.Run("invalid", func(t *testing.T) {
c, err := getChangeloger(context.New(config.Project{
c, err := getChangeloger(testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Use: "nope",
},
@@ -642,13 +607,12 @@ func TestGetChangeloger(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip on snapshot", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Changelog: config.Changelog{
Skip: true,
},
@@ -657,7 +621,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.False(t, Pipe{}.Skip(ctx))
})
}
@@ -678,7 +642,7 @@ func TestGroup(t *testing.T) {
testlib.GitCommit(t, "bug: Merge pull request #999 from goreleaser/some-branch")
testlib.GitCommit(t, "this is not a Merge pull request")
testlib.GitTag(t, "v0.0.2")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Groups: []config.ChangelogGroup{
@@ -708,9 +672,7 @@ func TestGroup(t *testing.T) {
},
},
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.Contains(t, ctx.ReleaseNotes, "### Bots")
@@ -726,7 +688,7 @@ func TestGroupBadRegex(t *testing.T) {
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
testlib.GitTag(t, "v0.0.2")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Groups: []config.ChangelogGroup{
@@ -736,9 +698,7 @@ func TestGroupBadRegex(t *testing.T) {
},
},
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.EqualError(t, Pipe{}.Run(ctx), "failed to group into \"Something\": error parsing regexp: missing closing ]: `[a-z`")
}
@@ -751,7 +711,7 @@ func TestChangelogFormat(t *testing.T) {
for _, use := range []string{useGit, useGitHub, useGitLab} {
t.Run(use, func(t *testing.T) {
out, err := formatChangelog(
context.New(makeConf(use)),
testctx.NewWithCfg(makeConf(use)),
[]string{
"aea123 foo",
"aef653 bar",
@@ -766,7 +726,7 @@ func TestChangelogFormat(t *testing.T) {
t.Run(useGitHubNative, func(t *testing.T) {
out, err := formatChangelog(
context.New(makeConf(useGitHubNative)),
testctx.NewWithCfg(makeConf(useGitHubNative)),
[]string{
"# What's changed",
"* aea123 foo",
@@ -794,7 +754,7 @@ func TestChangelogFormat(t *testing.T) {
t.Run(useGitHubNative, func(t *testing.T) {
out, err := formatChangelog(
context.New(makeConf(useGitHubNative)),
testctx.NewWithCfg(makeConf(useGitHubNative)),
[]string{
"# What's changed",
"* aea123 foo",
@@ -809,7 +769,7 @@ func TestChangelogFormat(t *testing.T) {
for _, use := range []string{useGit, useGitHub, useGitLab} {
t.Run(use, func(t *testing.T) {
out, err := formatChangelog(
context.New(makeConf(use)),
testctx.NewWithCfg(makeConf(use)),
[]string{
"aea123 foo",
"aef653 bar",
@@ -843,64 +803,54 @@ func TestAbbrev(t *testing.T) {
testlib.GitTag(t, "v0.0.2")
t.Run("no abbrev", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
t.Run("abbrev -1", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Abbrev: -1,
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
})
t.Run("abbrev 3", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Abbrev: 3,
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 3)
})
t.Run("abbrev 7", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Abbrev: 7,
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
t.Run("abbrev 40", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
Changelog: config.Changelog{
Abbrev: 40,
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
}, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t))
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
@@ -918,9 +868,11 @@ func ensureCommitHashLen(tb testing.TB, log string, l int) {
}
}
func firstCommit(tb testing.TB) string {
func withFirstCommit(tb testing.TB) testctx.Opt {
tb.Helper()
s, err := git.Clean(git.Run(context.New(config.Project{}), "rev-list", "--max-parents=0", "HEAD"))
require.NoError(tb, err)
return s
return func(ctx *context.Context) {
s, err := git.Clean(git.Run(testctx.New(), "rev-list", "--max-parents=0", "HEAD"))
require.NoError(tb, err)
ctx.Git.FirstCommit = s
}
}

View File

@@ -7,8 +7,8 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -50,7 +50,7 @@ func TestPipe(t *testing.T) {
folder := t.TempDir()
file := filepath.Join(folder, binary)
require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644))
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: binary,
@@ -59,10 +59,10 @@ func TestPipe(t *testing.T) {
Algorithm: "sha256",
IDs: tt.ids,
},
Env: []string{"FOO=bar"},
},
testctx.WithCurrentTag("1.2.3"),
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Env = map[string]string{"FOO": "bar"}
ctx.Artifacts.Add(&artifact.Artifact{
Name: binary,
Path: file,
@@ -106,18 +106,15 @@ func TestRefreshModifying(t *testing.T) {
folder := t.TempDir()
file := filepath.Join(folder, binary)
require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644))
ctx := context.New(
config.Project{
Dist: folder,
ProjectName: binary,
Checksum: config.Checksum{
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
Algorithm: "sha256",
},
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: binary,
Checksum: config.Checksum{
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
Algorithm: "sha256",
},
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Env = map[string]string{"FOO": "bar"}
Env: []string{"FOO=bar"},
}, testctx.WithCurrentTag("1.2.3"))
ctx.Artifacts.Add(&artifact.Artifact{
Name: binary,
Path: file,
@@ -137,15 +134,15 @@ func TestRefreshModifying(t *testing.T) {
func TestPipeFileNotExist(t *testing.T) {
folder := t.TempDir()
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
},
testctx.WithCurrentTag("1.2.3"),
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
Name: "nope",
Path: "/nope",
@@ -169,7 +166,7 @@ func TestPipeInvalidNameTemplate(t *testing.T) {
} {
t.Run(template, func(t *testing.T) {
folder := t.TempDir()
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "name",
@@ -178,8 +175,8 @@ func TestPipeInvalidNameTemplate(t *testing.T) {
Algorithm: "sha256",
},
},
testctx.WithCurrentTag("1.2.3"),
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
Name: "whatever",
Type: artifact.UploadableBinary,
@@ -202,7 +199,7 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
file := filepath.Join(folder, "checksums.txt")
require.NoError(t, os.WriteFile(file, []byte("some string"), 0o000))
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
Checksum: config.Checksum{
@@ -210,8 +207,8 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
Algorithm: "sha256",
},
},
testctx.WithCurrentTag("1.2.3"),
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
Name: "whatever",
Type: artifact.UploadableBinary,
@@ -223,19 +220,15 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
}
func TestPipeWhenNoArtifacts(t *testing.T) {
ctx := &context.Context{
Artifacts: artifact.New(),
}
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Len(t, ctx.Artifacts.List(), 0)
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Checksum: config.Checksum{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Checksum: config.Checksum{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(
t,
@@ -246,13 +239,11 @@ func TestDefault(t *testing.T) {
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
ctx := testctx.NewWithCfg(config.Project{
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate)
}
@@ -310,7 +301,7 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) {
folder := t.TempDir()
file := filepath.Join(folder, binary)
require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644))
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: binary,
@@ -363,7 +354,7 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) {
func TestExtraFilesNoMatch(t *testing.T) {
dir := t.TempDir()
ctx := context.New(
ctx := testctx.NewWithCfg(
config.Project{
Dist: dir,
ProjectName: "fake",
@@ -387,7 +378,7 @@ func TestExtraFilesNoMatch(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Checksum: config.Checksum{
Disable: true,
},
@@ -396,7 +387,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
require.False(t, Pipe{}.Skip(context.New(config.Project{})))
require.False(t, Pipe{}.Skip(testctx.New()))
})
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -20,22 +21,19 @@ func TestDescription(t *testing.T) {
}
func TestSkip(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.True(t, Pipe{}.Skip(ctx))
}
func TestDefault(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
Chocolateys: []config.Chocolatey{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
Chocolateys: []config.Chocolatey{
{},
},
}
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Chocolateys[0].Name)
@@ -106,17 +104,14 @@ func Test_doRun(t *testing.T) {
cmd = stdCmd{}
})
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "run-all",
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "app_1.0.1_windows_amd64.zip",
@@ -149,9 +144,7 @@ func Test_doRun(t *testing.T) {
}
func Test_buildNuspec(t *testing.T) {
ctx := &context.Context{
Version: "1.12.3",
}
ctx := testctx.New(testctx.WithVersion("1.12.3"))
choco := config.Chocolatey{
Name: "goreleaser",
IDs: []string{},
@@ -176,14 +169,7 @@ func Test_buildTemplate(t *testing.T) {
folder := t.TempDir()
file := filepath.Join(folder, "archive")
require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644))
ctx := &context.Context{
Version: "1.0.0",
Git: context.GitInfo{
CurrentTag: "v1.0.0",
},
}
ctx := testctx.New(testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
artifacts := []*artifact.Artifact{
{
Name: "app_1.0.0_windows_386.zip",
@@ -297,11 +283,7 @@ func TestPublish(t *testing.T) {
cmd = stdCmd{}
})
ctx := &context.Context{
SkipPublish: tt.skip,
Artifacts: artifact.New(),
}
ctx := testctx.New(func(ctx *context.Context) { ctx.SkipPublish = tt.skip })
for _, artifact := range tt.artifacts {
ctx.Artifacts.Add(&artifact)
}

View File

@@ -3,8 +3,8 @@ package custompublishers
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -14,21 +14,20 @@ func TestDescription(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip on skip-publish", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Publishers: []config.Publisher{
{},
},
})
ctx.SkipPublish = true
}, testctx.SkipPublish)
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Publishers: []config.Publisher{
{},
},
@@ -38,7 +37,7 @@ func TestSkip(t *testing.T) {
}
func TestPublish(t *testing.T) {
require.NoError(t, Pipe{}.Publish(context.New(config.Project{
require.NoError(t, Pipe{}.Publish(testctx.NewWithCfg(config.Project{
Publishers: []config.Publisher{
{
Cmd: "echo",

View File

@@ -3,9 +3,9 @@ package defaults
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -17,10 +17,7 @@ func TestFillBasicData(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx := testctx.New(testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
@@ -45,7 +42,7 @@ func TestFillPartial(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.company.com",
},
@@ -98,12 +95,11 @@ func TestFillPartial(t *testing.T) {
require.Equal(t, "disttt", ctx.Config.Dist)
require.NotEqual(t, "https://github.com", ctx.Config.GitHubURLs.Download)
ctx = context.New(config.Project{
ctx = testctx.NewWithCfg(config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1/",
},
})
ctx.TokenType = context.TokenTypeGitea
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "https://gitea.com", ctx.Config.GiteaURLs.Download)
@@ -136,16 +132,12 @@ func TestGiteaTemplateDownloadURL(t *testing.T) {
}
for _, tt := range tests {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"GORELEASER_TEST_GITEA_URLS_API=https://gitea.com/api/v1"},
GiteaURLs: config.GiteaURLs{
API: tt.apiURL,
},
})
ctx.TokenType = context.TokenTypeGitea
ctx.Env = context.Env{
"GORELEASER_TEST_GITEA_URLS_API": "https://gitea.com/api/v1",
}
}, testctx.GiteaTokenType)
err := Pipe{}.Run(ctx)
if tt.wantErr {

View File

@@ -3,8 +3,8 @@ package discord
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,13 +13,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Discord.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Discord: config.Discord{
MessageTemplate: "{{ .Foo }",
@@ -30,7 +30,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Discord: config.Discord{},
},
@@ -41,11 +41,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Discord: config.Discord{
Enabled: true,

View File

@@ -5,24 +5,15 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestDistDoesNotExist(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(
t,
Pipe{}.Run(
&context.Context{
Config: config.Project{
Dist: dist,
},
},
),
)
require.NoError(t, Pipe{}.Run(testctx.NewWithCfg(config.Project{Dist: dist})))
}
func TestPopulatedDistExists(t *testing.T) {
@@ -32,11 +23,7 @@ func TestPopulatedDistExists(t *testing.T) {
f, err := os.Create(filepath.Join(dist, "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := &context.Context{
Config: config.Project{
Dist: dist,
},
}
ctx := testctx.NewWithCfg(config.Project{Dist: dist})
require.Error(t, Pipe{}.Run(ctx))
ctx.Clean = true
require.NoError(t, Pipe{}.Run(ctx))
@@ -48,11 +35,7 @@ func TestEmptyDistExists(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := &context.Context{
Config: config.Project{
Dist: dist,
},
}
ctx := testctx.NewWithCfg(config.Project{Dist: dist})
require.NoError(t, Pipe{}.Run(ctx))
_, err := os.Stat(dist)
require.False(t, os.IsNotExist(err))

View File

@@ -10,6 +10,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -965,24 +966,19 @@ func TestRunPipe(t *testing.T) {
require.NoError(t, f.Close())
}
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
Dockers: docker.dockers,
DockerManifests: docker.manifests,
})
ctx.Parallelism = 1
ctx.Env = docker.env
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.0",
Commit: "a1b2c3d4",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
}
ctx := testctx.NewWithCfg(
config.Project{
ProjectName: "mybin",
Dist: dist,
Dockers: docker.dockers,
DockerManifests: docker.manifests,
},
testctx.WithEnv(docker.env),
testctx.WithVersion("1.0.0"),
testctx.WithCurrentTag("v1.0.0"),
testctx.WithCommit("a1b2c3d4"),
testctx.WithSemver(1, 0, 0, ""),
)
for _, os := range []string{"linux", "darwin"} {
for _, arch := range []string{"amd64", "386", "arm64"} {
for _, bin := range []string{"mybin", "anotherbin"} {
@@ -1116,7 +1112,7 @@ func TestDescription(t *testing.T) {
}
func TestNoDockerWithoutImageName(t *testing.T) {
testlib.AssertSkipped(t, Pipe{}.Run(context.New(config.Project{
testlib.AssertSkipped(t, Pipe{}.Run(testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{
{
Goos: "linux",
@@ -1126,24 +1122,22 @@ func TestNoDockerWithoutImageName(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{
{
IDs: []string{"aa"},
},
{
Use: useBuildx,
},
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{
{
IDs: []string{"aa"},
},
DockerManifests: []config.DockerManifest{
{},
{
Use: useDocker,
},
{
Use: useBuildx,
},
},
}
DockerManifests: []config.DockerManifest{
{},
{
Use: useDocker,
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Dockers, 2)
docker := ctx.Config.Dockers[0]
@@ -1162,41 +1156,37 @@ func TestDefault(t *testing.T) {
}
func TestDefaultDuplicateID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{
{ID: "foo"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
DockerManifests: []config.DockerManifest{
{ID: "bar"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{
{ID: "foo"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
}
DockerManifests: []config.DockerManifest{
{ID: "bar"},
{ /* empty */ },
{ID: "bar"},
{ID: "foo"},
},
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 dockers with the ID 'foo', please fix your config")
require.EqualError(t, ManifestPipe{}.Default(ctx), "found 2 docker_manifests with the ID 'bar', please fix your config")
}
func TestDefaultInvalidUse(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{
{
Use: "something",
},
},
DockerManifests: []config.DockerManifest{
{
Use: "something",
},
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{
{
Use: "something",
},
},
}
DockerManifests: []config.DockerManifest{
{
Use: "something",
},
},
})
err := Pipe{}.Default(ctx)
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), `docker: invalid use: something, valid options are`))
@@ -1207,17 +1197,15 @@ func TestDefaultInvalidUse(t *testing.T) {
}
func TestDefaultDockerfile(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Builds: []config.Build{
{},
},
Dockers: []config.Docker{
{},
{},
},
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{},
},
}
Dockers: []config.Docker{
{},
{},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Dockers, 2)
require.Equal(t, "Dockerfile", ctx.Config.Dockers[0].Dockerfile)
@@ -1225,68 +1213,58 @@ func TestDefaultDockerfile(t *testing.T) {
}
func TestDraftRelease(t *testing.T) {
ctx := context.New(
config.Project{
Release: config.Release{
Draft: true,
},
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Draft: true,
},
)
})
require.False(t, pipe.IsSkip(Pipe{}.Publish(ctx)))
}
func TestDefaultNoDockers(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Dockers)
}
func TestDefaultFilesDot(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dist: "/tmp/distt",
Dockers: []config.Docker{
{
Files: []string{"./lala", "./lolsob", "."},
},
ctx := testctx.NewWithCfg(config.Project{
Dist: "/tmp/distt",
Dockers: []config.Docker{
{
Files: []string{"./lala", "./lolsob", "."},
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), `invalid docker.files: can't be . or inside dist folder: .`)
}
func TestDefaultFilesDis(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dist: "/tmp/dist",
Dockers: []config.Docker{
{
Files: []string{"./fooo", "/tmp/dist/asdasd/asd", "./bar"},
},
ctx := testctx.NewWithCfg(config.Project{
Dist: "/tmp/dist",
Dockers: []config.Docker{
{
Files: []string{"./fooo", "/tmp/dist/asdasd/asd", "./bar"},
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), `invalid docker.files: can't be . or inside dist folder: /tmp/dist/asdasd/asd`)
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Dockers: []config.Docker{
{
IDs: []string{"foo"},
Goos: "windows",
Goarch: "i386",
Dockerfile: "Dockerfile.foo",
},
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{
{
IDs: []string{"foo"},
Goos: "windows",
Goarch: "i386",
Dockerfile: "Dockerfile.foo",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Dockers, 1)
docker := ctx.Config.Dockers[0]
@@ -1297,8 +1275,8 @@ func TestDefaultSet(t *testing.T) {
}
func Test_processImageTemplates(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Builds: []config.Build{
{
ID: "default",
@@ -1315,22 +1293,13 @@ func Test_processImageTemplates(t *testing.T) {
SkipPush: "true",
},
},
Env: []string{"FOO=123"},
},
}
ctx.Env = map[string]string{
"FOO": "123",
}
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.0",
Commit: "a1b2c3d4",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
}
testctx.WithVersion("1.0.0"),
testctx.WithCurrentTag("v1.0.0"),
testctx.WithCommit("a1b2c3d4"),
testctx.WithSemver(1, 0, 0, ""),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Dockers, 1)
@@ -1350,11 +1319,11 @@ func Test_processImageTemplates(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("image", func(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip docker", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{{}},
})
ctx.SkipDocker = true
@@ -1362,7 +1331,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dockers: []config.Docker{{}},
})
require.False(t, Pipe{}.Skip(ctx))
@@ -1371,19 +1340,18 @@ func TestSkip(t *testing.T) {
t.Run("manifest", func(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, ManifestPipe{}.Skip(context.New(config.Project{})))
require.True(t, ManifestPipe{}.Skip(testctx.New()))
})
t.Run("skip docker", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
DockerManifests: []config.DockerManifest{{}},
})
ctx.SkipDocker = true
}, testctx.SkipDocker)
require.True(t, ManifestPipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
DockerManifests: []config.DockerManifest{{}},
})
require.False(t, ManifestPipe{}.Skip(ctx))

View File

@@ -5,9 +5,9 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -19,11 +19,9 @@ func TestRun(t *testing.T) {
folder := testlib.Mktmp(t)
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(
config.Project{
Dist: dist,
},
)
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
})
require.NoError(t, Pipe{}.Run(ctx))
bts, err := os.ReadFile(filepath.Join(dist, "config.yaml"))
require.NoError(t, err)

View File

@@ -5,6 +5,7 @@ import (
"os"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -17,7 +18,7 @@ func TestDescription(t *testing.T) {
func TestSetDefaultTokenFiles(t *testing.T) {
t.Run("empty config", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
setDefaultTokenFiles(ctx)
require.Equal(t, "~/.config/goreleaser/github_token", ctx.Config.EnvFiles.GitHubToken)
require.Equal(t, "~/.config/goreleaser/gitlab_token", ctx.Config.EnvFiles.GitLabToken)
@@ -25,7 +26,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
})
t.Run("custom config config", func(t *testing.T) {
cfg := "what"
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GitHubToken: cfg,
},
@@ -34,7 +35,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
require.Equal(t, cfg, ctx.Config.EnvFiles.GitHubToken)
})
t.Run("templates", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foobar",
Env: []string{
"FOO=FOO_{{ .Env.BAR }}",
@@ -52,7 +53,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
})
t.Run("template error", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"FOO={{ .Asss }",
},
@@ -61,7 +62,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
})
t.Run("no token", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, ctx.TokenType, context.TokenTypeGitHub)
})
@@ -69,9 +70,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
func TestValidGithubEnv(t *testing.T) {
require.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "asdf", ctx.Token)
require.Equal(t, context.TokenTypeGitHub, ctx.TokenType)
@@ -81,9 +80,7 @@ func TestValidGithubEnv(t *testing.T) {
func TestValidGitlabEnv(t *testing.T) {
require.NoError(t, os.Setenv("GITLAB_TOKEN", "qwertz"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "qwertz", ctx.Token)
require.Equal(t, context.TokenTypeGitLab, ctx.TokenType)
@@ -93,9 +90,7 @@ func TestValidGitlabEnv(t *testing.T) {
func TestValidGiteaEnv(t *testing.T) {
require.NoError(t, os.Setenv("GITEA_TOKEN", "token"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "token", ctx.Token)
require.Equal(t, context.TokenTypeGitea, ctx.TokenType)
@@ -106,9 +101,7 @@ func TestValidGiteaEnv(t *testing.T) {
func TestInvalidEnv(t *testing.T) {
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.EqualError(t, Pipe{}.Run(ctx), ErrMissingToken.Error())
}
@@ -117,9 +110,7 @@ func TestMultipleEnvTokens(t *testing.T) {
require.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
require.NoError(t, os.Setenv("GITLAB_TOKEN", "qwertz"))
require.NoError(t, os.Setenv("GITEA_TOKEN", "token"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
require.EqualError(t, Pipe{}.Run(ctx), "multiple tokens found, but only one is allowed: GITHUB_TOKEN, GITLAB_TOKEN, GITEA_TOKEN\n\nLearn more at https://goreleaser.com/errors/multiple-tokens\n")
// so the tests do not depend on each other
@@ -130,25 +121,19 @@ func TestMultipleEnvTokens(t *testing.T) {
func TestEmptyGithubFileEnv(t *testing.T) {
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
}
func TestEmptyGitlabFileEnv(t *testing.T) {
require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
}
func TestEmptyGiteaFileEnv(t *testing.T) {
require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
ctx := &context.Context{
Config: config.Project{},
}
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
}
@@ -158,13 +143,11 @@ func TestEmptyGithubEnvFile(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := &context.Context{
Config: config.Project{
EnvFiles: config.EnvFiles{
GitHubToken: f.Name(),
},
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GitHubToken: f.Name(),
},
}
})
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load github token: open %s: permission denied", f.Name()))
}
@@ -174,13 +157,11 @@ func TestEmptyGitlabEnvFile(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := &context.Context{
Config: config.Project{
EnvFiles: config.EnvFiles{
GitLabToken: f.Name(),
},
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GitLabToken: f.Name(),
},
}
})
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load gitlab token: open %s: permission denied", f.Name()))
}
@@ -190,22 +171,17 @@ func TestEmptyGiteaEnvFile(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
require.NoError(t, os.Chmod(f.Name(), 0o377))
ctx := &context.Context{
Config: config.Project{
EnvFiles: config.EnvFiles{
GiteaToken: f.Name(),
},
ctx := testctx.NewWithCfg(config.Project{
EnvFiles: config.EnvFiles{
GiteaToken: f.Name(),
},
}
})
require.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load gitea token: open %s: permission denied", f.Name()))
}
func TestInvalidEnvChecksSkipped(t *testing.T) {
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
ctx := &context.Context{
Config: config.Project{},
SkipPublish: true,
}
ctx := testctx.New(testctx.SkipPublish)
require.NoError(t, Pipe{}.Run(ctx))
}
@@ -213,7 +189,7 @@ func TestInvalidEnvReleaseDisabled(t *testing.T) {
require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
t.Run("true", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{},
Release: config.Release{
Disable: "true",
@@ -223,7 +199,7 @@ func TestInvalidEnvReleaseDisabled(t *testing.T) {
})
t.Run("tmpl true", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
Disable: "{{ .Env.FOO }}",
@@ -233,7 +209,7 @@ func TestInvalidEnvReleaseDisabled(t *testing.T) {
})
t.Run("tmpl false", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
Disable: "{{ .Env.FOO }}-nope",
@@ -243,7 +219,7 @@ func TestInvalidEnvReleaseDisabled(t *testing.T) {
})
t.Run("tmpl error", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Disable: "{{ .Env.FOO }}",
},

View File

@@ -6,9 +6,9 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -18,7 +18,7 @@ func TestDescription(t *testing.T) {
func TestNotAGitFolder(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx := testctx.New()
require.EqualError(t, Pipe{}.Run(ctx), ErrNotRepository.Error())
}
@@ -28,7 +28,7 @@ func TestSingleCommit(t *testing.T) {
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Equal(t, "v0.0.1", ctx.Git.Summary)
@@ -43,7 +43,7 @@ func TestAnnotatedTags(t *testing.T) {
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitAnnotatedTag(t, "v0.0.1", "first version\n\nlalalla\nlalal\nlah")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Equal(t, "first version", ctx.Git.TagSubject)
@@ -59,7 +59,7 @@ func TestBranch(t *testing.T) {
testlib.GitCommit(t, "test-branch-commit")
testlib.GitTag(t, "test-branch-tag")
testlib.GitCheckoutBranch(t, "test-branch")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "test-branch", ctx.Git.Branch)
require.Equal(t, "test-branch-tag", ctx.Git.Summary)
@@ -70,14 +70,14 @@ func TestNoRemote(t *testing.T) {
testlib.GitInit(t)
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.EqualError(t, Pipe{}.Run(ctx), "couldn't get remote URL: fatal: No remote configured to list refs from.")
}
func TestNewRepository(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
ctx := context.New(config.Project{})
ctx := testctx.New()
// TODO: improve this error handling
require.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
}
@@ -90,7 +90,7 @@ func TestNoTagsNoSnapshot(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "first")
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.Snapshot = false
require.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
}
@@ -107,18 +107,16 @@ func TestDirty(t *testing.T) {
testlib.GitTag(t, "v0.0.1")
require.NoError(t, os.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0o644))
t.Run("all checks up", func(t *testing.T) {
err := Pipe{}.Run(context.New(config.Project{}))
err := Pipe{}.Run(testctx.New())
require.Error(t, err)
require.Contains(t, err.Error(), "git is in a dirty state")
})
t.Run("skip validate is set", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipValidate = true
ctx := testctx.New(testctx.SkipValidate)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
t.Run("snapshot", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
@@ -130,7 +128,7 @@ func TestRemoteURLContainsWithUsernameAndToken(t *testing.T) {
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
}
@@ -141,7 +139,7 @@ func TestRemoteURLContainsWithUsernameAndTokenWithInvalidURL(t *testing.T) {
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.Error(t, Pipe{}.Run(ctx))
}
@@ -159,16 +157,14 @@ func TestShallowClone(t *testing.T) {
)
t.Run("all checks up", func(t *testing.T) {
// its just a warning now
require.NoError(t, Pipe{}.Run(context.New(config.Project{})))
require.NoError(t, Pipe{}.Run(testctx.New()))
})
t.Run("skip validate is set", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipValidate = true
ctx := testctx.New(testctx.SkipValidate)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
t.Run("snapshot", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
@@ -182,7 +178,7 @@ func TestTagSortOrder(t *testing.T) {
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1-rc.2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Git: config.Git{
TagSort: "-version:creatordate",
},
@@ -198,7 +194,7 @@ func TestTagIsNotLastCommit(t *testing.T) {
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
ctx := context.New(config.Project{})
ctx := testctx.New()
err := Pipe{}.Run(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
@@ -214,7 +210,7 @@ func TestValidState(t *testing.T) {
testlib.GitTag(t, "v0.0.2")
testlib.GitCommit(t, "commit4")
testlib.GitTag(t, "v0.0.3")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.2", ctx.Git.PreviousTag)
require.Equal(t, "v0.0.3", ctx.Git.CurrentTag)
@@ -228,8 +224,7 @@ func TestSnapshotNoTags(t *testing.T) {
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitAdd(t)
testlib.GitCommit(t, "whatever")
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag)
@@ -240,16 +235,14 @@ func TestSnapshotNoCommits(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo, ctx.Git)
}
func TestSnapshotWithoutRepo(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo, ctx.Git)
}
@@ -262,8 +255,7 @@ func TestSnapshotDirty(t *testing.T) {
testlib.GitCommit(t, "whatever")
testlib.GitTag(t, "v0.0.1")
require.NoError(t, os.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0o644))
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.Summary)
}
@@ -274,7 +266,7 @@ func TestGitNotInPath(t *testing.T) {
require.NoError(t, os.Setenv("PATH", path))
}()
require.NoError(t, os.Setenv("PATH", ""))
require.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
require.EqualError(t, Pipe{}.Run(testctx.New()), ErrNoGit.Error())
}
func TestTagFromCI(t *testing.T) {
@@ -299,7 +291,7 @@ func TestTagFromCI(t *testing.T) {
require.NoError(t, os.Setenv(name, value))
}
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, tc.expected, ctx.Git.CurrentTag)
@@ -315,7 +307,7 @@ func TestNoPreviousTag(t *testing.T) {
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag, "should be empty")
@@ -346,7 +338,7 @@ func TestPreviousTagFromCI(t *testing.T) {
require.NoError(t, os.Setenv(name, value))
}
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, tc.expected, ctx.Git.PreviousTag)

View File

@@ -7,6 +7,7 @@ import (
"runtime"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -17,7 +18,7 @@ func TestGoModProxy(t *testing.T) {
t.Run("goreleaser", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
@@ -32,10 +33,7 @@ func TestGoModProxy(t *testing.T) {
Dir: ".",
},
},
})
ctx.Git.CurrentTag = "v0.161.1"
ctx.ModulePath = "github.com/goreleaser/goreleaser"
}, testctx.WithCurrentTag("v0.161.1"), withGoReleaserModulePath)
fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, ProxyPipe{}.Run(ctx))
@@ -51,7 +49,7 @@ func TestGoModProxy(t *testing.T) {
t.Run("nfpm", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
@@ -65,10 +63,7 @@ func TestGoModProxy(t *testing.T) {
Main: "./cmd/nfpm",
},
},
})
ctx.Git.CurrentTag = "v2.3.1"
ctx.ModulePath = "github.com/goreleaser/nfpm/v2"
}, testctx.WithCurrentTag("v2.3.1"), withNfpmModulePath)
fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, ProxyPipe{}.Run(ctx))
requireGoMod(t)
@@ -81,7 +76,7 @@ func TestGoModProxy(t *testing.T) {
t.Run("no go.sum", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
@@ -94,10 +89,7 @@ func TestGoModProxy(t *testing.T) {
Goarch: []string{runtime.GOARCH},
},
},
})
ctx.Git.CurrentTag = "v0.0.1"
ctx.ModulePath = "github.com/goreleaser/example-mod-proxy"
}, testctx.WithCurrentTag("v0.0.1"), withExampleModulePath)
fakeGoMod(t, ctx.ModulePath)
require.NoError(t, ProxyPipe{}.Run(ctx))
requireGoMod(t)
@@ -115,7 +107,7 @@ func TestGoModProxy(t *testing.T) {
t.Run(file, func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
@@ -128,10 +120,7 @@ func TestGoModProxy(t *testing.T) {
Goarch: []string{runtime.GOARCH},
},
},
})
ctx.Git.CurrentTag = "v0.161.1"
ctx.ModulePath = "github.com/goreleaser/goreleaser"
}, withGoReleaserModulePath, testctx.WithCurrentTag("v0.161.1"))
fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, ProxyPipe{}.Run(ctx)) // should succeed at first
@@ -146,7 +135,7 @@ func TestGoModProxy(t *testing.T) {
t.Run("goreleaser with main.go", func(t *testing.T) {
dir := testlib.Mktmp(t)
dist := filepath.Join(dir, "dist")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
GoMod: config.GoMod{
Proxy: true,
@@ -160,10 +149,7 @@ func TestGoModProxy(t *testing.T) {
Main: "main.go",
},
},
})
ctx.Git.CurrentTag = "v0.161.1"
ctx.ModulePath = "github.com/goreleaser/goreleaser"
}, withGoReleaserModulePath, testctx.WithCurrentTag("v0.161.1"))
fakeGoModAndSum(t, ctx.ModulePath)
require.NoError(t, ProxyPipe{}.Run(ctx))
@@ -180,37 +166,33 @@ func TestProxyDescription(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip false gomod.proxy", func(t *testing.T) {
require.True(t, ProxyPipe{}.Skip(context.New(config.Project{})))
require.True(t, ProxyPipe{}.Skip(testctx.New()))
})
t.Run("skip snapshot", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
Proxy: true,
},
})
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.Snapshot = true
}, withGoReleaserModulePath, testctx.Snapshot)
require.True(t, ProxyPipe{}.Skip(ctx))
})
t.Run("skip not a go module", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
Proxy: true,
},
})
ctx.ModulePath = ""
}, func(ctx *context.Context) { ctx.ModulePath = "" })
require.True(t, ProxyPipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
Proxy: true,
},
})
ctx.ModulePath = "github.com/goreleaser/goreleaser"
}, withGoReleaserModulePath)
require.False(t, ProxyPipe{}.Skip(ctx))
})
}
@@ -237,3 +219,15 @@ func fakeGoMod(tb testing.TB, module string) {
tb.Helper()
require.NoError(tb, os.WriteFile("go.mod", []byte(fmt.Sprintf("module %s\n", module)), 0o666))
}
func withGoReleaserModulePath(ctx *context.Context) {
ctx.ModulePath = "github.com/goreleaser/goreleaser"
}
func withNfpmModulePath(ctx *context.Context) {
ctx.ModulePath = "github.com/goreleaser/nfpm/v2"
}
func withExampleModulePath(ctx *context.Context) {
ctx.ModulePath = "github.com/goreleaser/example-mod-proxy"
}

View File

@@ -5,21 +5,21 @@ import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath)
}
func TestRunCustomMod(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
Mod: "readonly",
},
@@ -32,7 +32,7 @@ func TestRunCustomMod(t *testing.T) {
func TestCustomEnv(t *testing.T) {
bin := filepath.Join(t.TempDir(), "go.bin")
require.NoError(t, os.WriteFile(bin, []byte("#!/bin/sh\nenv | grep -qw FOO=bar"), 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
GoBinary: bin,
Env: []string{"FOO=bar"},
@@ -45,14 +45,14 @@ func TestCustomEnv(t *testing.T) {
func TestRunOutsideGoModule(t *testing.T) {
dir := testlib.Mktmp(t)
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\nfunc main() {println(0)}"), 0o666))
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Empty(t, ctx.ModulePath)
}
func TestRunCommandError(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
GoMod: config.GoMod{
GoBinary: "not-a-valid-binary",
},

View File

@@ -6,6 +6,7 @@ import (
_ "github.com/distribution/distribution/v3/registry/auth/htpasswd"
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -18,7 +19,7 @@ const (
)
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"KO_DOCKER_REPO=" + registry,
"COSIGN_REPOSITORY=" + registry,
@@ -59,7 +60,7 @@ func TestDefault(t *testing.T) {
}
func TestDefaultNoImage(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{
@@ -79,18 +80,18 @@ func TestDescription(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip ko set", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Kos: []config.Ko{{}},
})
ctx.SkipKo = true
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("skip no kos", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Kos: []config.Ko{{}},
})
require.False(t, Pipe{}.Skip(ctx))
@@ -98,7 +99,7 @@ func TestSkip(t *testing.T) {
}
func TestPublishPipeNoMatchingBuild(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "doesnt matter",
@@ -152,7 +153,7 @@ func TestPublishPipeSuccess(t *testing.T) {
for _, table := range table {
t.Run(table.Name, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -185,7 +186,7 @@ func TestPublishPipeSuccess(t *testing.T) {
func TestPublishPipeError(t *testing.T) {
makeCtx := func() *context.Context {
ctx := context.New(config.Project{
return testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
@@ -201,9 +202,7 @@ func TestPublishPipeError(t *testing.T) {
Tags: []string{"latest", "{{.Tag}}"},
},
},
})
ctx.Git.CurrentTag = "v1.0.0"
return ctx
}, testctx.WithCurrentTag("v1.0.0"))
}
t.Run("invalid base image", func(t *testing.T) {
@@ -266,14 +265,14 @@ func TestPublishPipeError(t *testing.T) {
func TestApplyTemplate(t *testing.T) {
t.Run("success", func(t *testing.T) {
foo, err := applyTemplate(context.New(config.Project{
foo, err := applyTemplate(testctx.NewWithCfg(config.Project{
Env: []string{"FOO=bar"},
}), []string{"{{ .Env.FOO }}"})
require.NoError(t, err)
require.Equal(t, []string{"bar"}, foo)
})
t.Run("error", func(t *testing.T) {
_, err := applyTemplate(context.New(config.Project{}), []string{"{{ .Nope}}"})
_, err := applyTemplate(testctx.New(), []string{"{{ .Nope}}"})
require.Error(t, err)
})
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -218,17 +219,9 @@ func TestFullPipe(t *testing.T) {
} {
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",
"BAR": "honk",
},
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
Krews: []config.Krew{
@@ -239,8 +232,11 @@ func TestFullPipe(t *testing.T) {
ShortDescription: "short desc {{.Env.BAR}}",
},
},
Env: []string{"FOO=foo_is_bar", "BAR=honk"},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
tt.prepare(ctx)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bar_bin.tar.gz",
@@ -316,13 +312,9 @@ func TestFullPipe(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "unibin",
Krews: []config.Krew{
@@ -340,7 +332,10 @@ func TestRunPipeUniversalBinary(t *testing.T) {
},
},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin.tar.gz",
@@ -374,13 +369,8 @@ func TestRunPipeUniversalBinary(t *testing.T) {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "unibin",
Krews: []config.Krew{
@@ -398,7 +388,9 @@ func TestRunPipeUniversalBinaryNotReplacing(t *testing.T) {
},
},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "unibin_amd64.tar.gz",
@@ -459,16 +451,8 @@ func TestRunPipeUniversalBinaryNotReplacing(t *testing.T) {
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": t.Name(),
},
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Krews: []config.Krew{
@@ -485,8 +469,11 @@ func TestRunPipeNameTemplate(t *testing.T) {
},
},
},
Env: []string{"FOO_BAR=" + t.Name()},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
@@ -521,16 +508,8 @@ func TestRunPipeNameTemplate(t *testing.T) {
func TestRunPipeMultipleKrewWithSkip(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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Krews: []config.Krew{
@@ -573,8 +552,11 @@ func TestRunPipeMultipleKrewWithSkip(t *testing.T) {
SkipUpload: "true",
},
},
Env: []string{"FOO_BAR=is_bar"},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bin.tar.gz",
@@ -621,17 +603,8 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
} {
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{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: name,
Krews: []config.Krew{
@@ -655,8 +628,12 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
Name: "test",
},
},
Env: []string{"FOO=foo_is_bar"},
},
}
testctx.GitHubTokenType,
testctx.WithVersion("1.0.1"),
testctx.WithCurrentTag("v1.0.1"),
)
fn(ctx)
for _, a := range []struct {
name string
@@ -731,22 +708,19 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
}
func TestRunPipeNoBuilds(t *testing.T) {
ctx := context.New(
config.Project{
Krews: []config.Krew{
{
Name: manifestName(t),
Description: "Some desc",
ShortDescription: "Short desc",
Index: config.RepoRef{
Owner: "test",
Name: "test",
},
ctx := testctx.NewWithCfg(config.Project{
Krews: []config.Krew{
{
Name: manifestName(t),
Description: "Some desc",
ShortDescription: "Short desc",
Index: config.RepoRef{
Owner: "test",
Name: "test",
},
},
},
)
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
@@ -754,7 +728,7 @@ func TestRunPipeNoBuilds(t *testing.T) {
func TestRunPipeNoUpload(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
@@ -769,9 +743,7 @@ func TestRunPipeNoUpload(t *testing.T) {
},
},
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
}, testctx.GitHubTokenType, testctx.WithCurrentTag("v1.0.1"))
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -812,7 +784,7 @@ func TestRunPipeNoUpload(t *testing.T) {
func TestRunEmptyTokenType(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
@@ -827,8 +799,7 @@ func TestRunEmptyTokenType(t *testing.T) {
},
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
}, testctx.WithCurrentTag("v1.0.1"))
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -853,7 +824,7 @@ func TestRunEmptyTokenType(t *testing.T) {
func TestRunMultipleBinaries(t *testing.T) {
folder := t.TempDir()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
@@ -868,8 +839,7 @@ func TestRunMultipleBinaries(t *testing.T) {
},
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
}, testctx.WithCurrentTag("v1.0.1"))
path := filepath.Join(folder, "whatever.tar.gz")
f, err := os.Create(path)
require.NoError(t, err)
@@ -895,15 +865,12 @@ func TestRunMultipleBinaries(t *testing.T) {
func TestDefault(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ProjectName: "myproject",
Krews: []config.Krew{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "myproject",
Krews: []config.Krew{
{},
},
}
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Krews[0].Name)
require.NotEmpty(t, ctx.Config.Krews[0].CommitAuthor.Name)
@@ -918,11 +885,11 @@ func TestGHFolder(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Krews: []config.Krew{
{},
},
@@ -932,7 +899,7 @@ func TestSkip(t *testing.T) {
}
func TestRunSkipNoName(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Krews: []config.Krew{{}},
})

View File

@@ -7,8 +7,7 @@ import (
"net/http/httptest"
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/stretchr/testify/require"
)
@@ -21,7 +20,7 @@ func TestCreateLinkedInClient(t *testing.T) {
{
"non-empty context and access token",
oauthClientConfig{
Context: context.New(config.Project{}),
Context: testctx.New(),
AccessToken: "foo",
},
nil,
@@ -37,7 +36,7 @@ func TestCreateLinkedInClient(t *testing.T) {
{
"empty access token",
oauthClientConfig{
Context: context.New(config.Project{}),
Context: testctx.New(),
AccessToken: "",
},
fmt.Errorf("empty access token"),
@@ -67,7 +66,7 @@ func TestClient_Share(t *testing.T) {
defer server.Close()
c, err := createLinkedInClient(oauthClientConfig{
Context: context.New(config.Project{}),
Context: testctx.New(),
AccessToken: "foo",
})
if err != nil {

View File

@@ -3,8 +3,8 @@ package linkedin
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,19 +13,19 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.LinkedIn.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceDisabled(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `linkedin: env: environment variable "LINKEDIN_ACCESS_TOKEN" should not be empty`)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
LinkedIn: config.LinkedIn{
Enabled: true,
@@ -37,7 +37,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
LinkedIn: config.LinkedIn{
Enabled: true,
@@ -50,11 +50,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
LinkedIn: config.LinkedIn{
Enabled: true,

View File

@@ -3,8 +3,8 @@ package mastodon
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,13 +13,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Mastodon.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{
MessageTemplate: "{{ .Foo }",
@@ -30,7 +30,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{},
},
@@ -41,11 +41,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip empty server", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{
require.True(t, Pipe{}.Skip(testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{
Enabled: true,
@@ -56,7 +56,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
require.False(t, Pipe{}.Skip(context.New(config.Project{
require.False(t, Pipe{}.Skip(testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{
Enabled: true,

View File

@@ -9,8 +9,8 @@ import (
"github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
func TestStringer(t *testing.T) {
@@ -18,13 +18,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Mattermost.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mattermost: config.Mattermost{
MessageTemplate: "{{ .Foo }",
@@ -35,7 +35,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mattermost: config.Mattermost{},
},
@@ -46,11 +46,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Mattermost: config.Mattermost{
Enabled: true,
@@ -78,7 +78,7 @@ func TestPostWebhook(t *testing.T) {
}))
defer ts.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "Honk",
Announce: config.Announce{
Mattermost: config.Mattermost{

View File

@@ -8,13 +8,13 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestRunWithError(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: "testadata/nope",
ProjectName: "foo",
})
@@ -23,21 +23,18 @@ func TestRunWithError(t *testing.T) {
func TestRun(t *testing.T) {
tmp := t.TempDir()
ctx := context.New(config.Project{
Dist: tmp,
ProjectName: "name",
})
ctx.Runtime = context.Runtime{
Goos: "fakeos",
Goarch: "fakearch",
}
ctx.Version = "1.2.3"
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
PreviousTag: "v1.2.2",
Commit: "aef34a",
}
ctx.Date = time.Date(2022, 0o1, 22, 10, 12, 13, 0, time.UTC)
ctx := testctx.NewWithCfg(
config.Project{
Dist: tmp,
ProjectName: "name",
},
testctx.WithPreviousTag("v1.2.2"),
testctx.WithCurrentTag("v1.2.3"),
testctx.WithCommit("aef34a"),
testctx.WithVersion("1.2.3"),
testctx.WithDate(time.Date(2022, 0o1, 22, 10, 12, 13, 0, time.UTC)),
testctx.WithFakeRuntime,
)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo",
Path: "foo.txt",

View File

@@ -4,9 +4,9 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -15,7 +15,7 @@ func TestDefaultWithRepoConfig(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{
{
Repo: config.Repo{
@@ -24,8 +24,7 @@ func TestDefaultWithRepoConfig(t *testing.T) {
},
},
},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "configrepo", ctx.Config.Milestones[0].Repo.Name)
require.Equal(t, "configowner", ctx.Config.Milestones[0].Repo.Owner)
@@ -36,10 +35,9 @@ func TestDefaultWithInvalidRemote(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:githubowner.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
require.Error(t, Pipe{}.Default(ctx))
}
@@ -48,17 +46,16 @@ func TestDefaultWithRepoRemote(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "githubrepo", ctx.Config.Milestones[0].Repo.Name)
require.Equal(t, "githubowner", ctx.Config.Milestones[0].Repo.Owner)
}
func TestDefaultWithNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{
{
NameTemplate: "confignametemplate",
@@ -71,20 +68,18 @@ func TestDefaultWithNameTemplate(t *testing.T) {
func TestDefaultWithoutGitRepo(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
require.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
}
func TestDefaultWithoutGitRepoOrigin(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
ctx.TokenType = context.TokenTypeGitHub
}, testctx.GitHubTokenType)
testlib.GitInit(t)
require.EqualError(t, Pipe{}.Default(ctx), "no remote configured to list refs from")
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
@@ -92,17 +87,15 @@ func TestDefaultWithoutGitRepoOrigin(t *testing.T) {
func TestDefaultWithoutGitRepoSnapshot(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Snapshot = true
}, testctx.GitHubTokenType, testctx.Snapshot)
require.NoError(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
}
func TestDefaultWithoutNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{{}},
})
require.NoError(t, Pipe{}.Default(ctx))
@@ -114,7 +107,7 @@ func TestString(t *testing.T) {
}
func TestPublishCloseDisabled(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{
{
Close: false,
@@ -127,7 +120,7 @@ func TestPublishCloseDisabled(t *testing.T) {
}
func TestPublishCloseEnabled(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{
{
Close: true,
@@ -138,8 +131,7 @@ func TestPublishCloseEnabled(t *testing.T) {
},
},
},
})
ctx.Git.CurrentTag = "v1.0.0"
}, testctx.WithCurrentTag("v1.0.0"))
client := client.NewMock()
require.NoError(t, doPublish(ctx, client))
require.Equal(t, "v1.0.0", client.ClosedMilestone)
@@ -158,8 +150,7 @@ func TestPublishCloseError(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
client := &client.Mock{
FailToCloseMilestone: true,
}
@@ -181,8 +172,7 @@ func TestPublishCloseFailOnError(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
client := &client.Mock{
FailToCloseMilestone: true,
}
@@ -192,11 +182,11 @@ func TestPublishCloseFailOnError(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Milestones: []config.Milestone{
{},
},

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -20,25 +21,22 @@ func TestDescription(t *testing.T) {
}
func TestRunPipeNoFormats(t *testing.T) {
ctx := &context.Context{
Version: "1.0.0",
Git: context.GitInfo{
CurrentTag: "v1.0.0",
},
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
NFPMs: []config.NFPM{
{},
},
},
Parallelism: runtime.NumCPU(),
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}
func TestDefaultsDeprecated(t *testing.T) {
t.Run("replacements", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
NFPMOverridables: config.NFPMOverridables{
@@ -54,7 +52,7 @@ func TestDefaultsDeprecated(t *testing.T) {
})
t.Run("replacements overrides", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
Overrides: map[string]config.NFPMOverridables{
@@ -73,7 +71,7 @@ func TestDefaultsDeprecated(t *testing.T) {
}
func TestRunPipeError(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
NFPMs: []config.NFPM{
{
@@ -99,7 +97,7 @@ func TestRunPipeError(t *testing.T) {
}
func TestRunPipeInvalidFormat(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "nope",
NFPMs: []config.NFPM{
{
@@ -112,11 +110,7 @@ func TestRunPipeInvalidFormat(t *testing.T) {
},
},
},
})
ctx.Version = "1.2.3"
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
}
}, testctx.WithVersion("1.2.3"), testctx.WithCurrentTag("v1.2.3"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -143,7 +137,7 @@ func TestRunPipe(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Env: []string{
@@ -219,9 +213,7 @@ func TestRunPipe(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin", "ios"} {
for _, goarch := range []string{"amd64", "386", "arm64", "arm", "mips"} {
if goos == "ios" && goarch != "arm64" {
@@ -356,7 +348,7 @@ func TestRunPipeConventionalNameTemplate(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -378,9 +370,7 @@ func TestRunPipeConventionalNameTemplate(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386", "arm64", "arm", "mips"} {
switch goarch {
@@ -495,11 +485,8 @@ func TestRunPipeConventionalNameTemplate(t *testing.T) {
func TestInvalidTemplate(t *testing.T) {
makeCtx := func() *context.Context {
ctx := &context.Context{
Version: "1.2.3",
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
ProjectName: "test",
NFPMs: []config.NFPM{
{
@@ -508,7 +495,8 @@ func TestInvalidTemplate(t *testing.T) {
},
},
},
}
testctx.WithVersion("1.2.3"),
)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Goos: "linux",
@@ -607,27 +595,23 @@ func TestInvalidTemplate(t *testing.T) {
}
func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {
ctx := &context.Context{
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
NFPMs: []config.NFPM{
{
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
Contents: []*files.Content{
{
Source: "{{.asdsd}",
Destination: "testfile",
},
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
Contents: []*files.Content{
{
Source: "{{.asdsd}",
Destination: "testfile",
},
},
Formats: []string{"deb"},
Builds: []string{"default"},
},
Formats: []string{"deb"},
Builds: []string{"default"},
},
},
}
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Goos: "linux",
@@ -641,18 +625,14 @@ func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {
}
func TestNoBuildsFound(t *testing.T) {
ctx := &context.Context{
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
NFPMs: []config.NFPM{
{
Formats: []string{"deb"},
Builds: []string{"nope"},
},
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
Formats: []string{"deb"},
Builds: []string{"nope"},
},
},
}
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Goos: "linux",
@@ -670,7 +650,7 @@ func TestCreateFileDoesntExist(t *testing.T) {
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
ProjectName: "asd",
NFPMs: []config.NFPM{
@@ -688,11 +668,7 @@ func TestCreateFileDoesntExist(t *testing.T) {
},
},
},
})
ctx.Version = "1.2.3"
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
}
}, testctx.WithVersion("1.2.3"), testctx.WithCurrentTag("v1.2.3"))
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: filepath.Join(dist, "mybin", "mybin"),
@@ -711,7 +687,7 @@ func TestInvalidConfig(t *testing.T) {
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: dist,
NFPMs: []config.NFPM{
{
@@ -719,9 +695,7 @@ func TestInvalidConfig(t *testing.T) {
Builds: []string{"default"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: filepath.Join(dist, "mybin", "mybin"),
@@ -736,14 +710,12 @@ func TestInvalidConfig(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "foobar",
NFPMs: []config.NFPM{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foobar",
NFPMs: []config.NFPM{
{},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "/usr/bin", ctx.Config.NFPMs[0].Bindir)
require.Empty(t, ctx.Config.NFPMs[0].Builds)
@@ -752,19 +724,17 @@ func TestDefault(t *testing.T) {
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
NFPMs: []config.NFPM{
{
Builds: []string{"foo"},
Bindir: "/bin",
NFPMOverridables: config.NFPMOverridables{
FileNameTemplate: "foo",
},
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
Builds: []string{"foo"},
Bindir: "/bin",
NFPMOverridables: config.NFPMOverridables{
FileNameTemplate: "foo",
},
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
@@ -773,23 +743,21 @@ func TestDefaultSet(t *testing.T) {
}
func TestOverrides(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
NFPMs: []config.NFPM{
{
Bindir: "/bin",
NFPMOverridables: config.NFPMOverridables{
FileNameTemplate: "foo",
},
Overrides: map[string]config.NFPMOverridables{
"deb": {
FileNameTemplate: "bar",
},
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
Bindir: "/bin",
NFPMOverridables: config.NFPMOverridables{
FileNameTemplate: "foo",
},
Overrides: map[string]config.NFPMOverridables{
"deb": {
FileNameTemplate: "bar",
},
},
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
merged, err := mergeOverrides(ctx.Config.NFPMs[0], "deb")
require.NoError(t, err)
@@ -810,7 +778,7 @@ func TestDebSpecificConfig(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -835,9 +803,7 @@ func TestDebSpecificConfig(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -907,7 +873,7 @@ func TestRPMSpecificConfig(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -931,9 +897,7 @@ func TestRPMSpecificConfig(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -981,7 +945,7 @@ func TestRPMSpecificScriptsConfig(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -1000,9 +964,7 @@ func TestRPMSpecificScriptsConfig(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1053,7 +1015,7 @@ func TestAPKSpecificConfig(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -1078,9 +1040,7 @@ func TestAPKSpecificConfig(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1132,7 +1092,7 @@ func TestAPKSpecificScriptsConfig(t *testing.T) {
PreUpgrade: "/does/not/exist_preupgrade.sh",
PostUpgrade: "/does/not/exist_postupgrade.sh",
}
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -1155,9 +1115,7 @@ func TestAPKSpecificScriptsConfig(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1204,18 +1162,16 @@ func TestAPKSpecificScriptsConfig(t *testing.T) {
}
func TestSeveralNFPMsWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
NFPMs: []config.NFPM{
{
ID: "a",
},
{
ID: "a",
},
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{
ID: "a",
},
{
ID: "a",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 nfpms with the ID 'a', please fix your config")
}
@@ -1228,7 +1184,7 @@ func TestMeta(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -1282,9 +1238,7 @@ func TestMeta(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1327,7 +1281,7 @@ func TestSkipSign(t *testing.T) {
binPath := filepath.Join(dist, "mybin", "mybin")
_, err = os.Create(binPath)
require.NoError(t, err)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@@ -1362,9 +1316,7 @@ func TestSkipSign(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1407,7 +1359,7 @@ func TestBinDirTemplating(t *testing.T) {
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Env: []string{
@@ -1434,9 +1386,7 @@ func TestBinDirTemplating(t *testing.T) {
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
}, testctx.WithVersion("1.0.0"), testctx.WithCurrentTag("v1.0.0"))
for _, goos := range []string{"linux"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
@@ -1466,11 +1416,11 @@ func TestBinDirTemplating(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
NFPMs: []config.NFPM{
{},
},

View File

@@ -3,8 +3,8 @@ package opencollective
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,14 +13,14 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.OpenCollective.TitleTemplate, defaultTitleTemplate)
require.Equal(t, ctx.Config.Announce.OpenCollective.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
OpenCollective: config.OpenCollective{
MessageTemplate: "{{ .Foo }",
@@ -31,7 +31,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
OpenCollective: config.OpenCollective{},
},
@@ -42,11 +42,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip empty slug", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{
require.True(t, Pipe{}.Skip(testctx.NewWithCfg(config.Project{
Announce: config.Announce{
OpenCollective: config.OpenCollective{
Enabled: true,
@@ -57,7 +57,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
require.False(t, Pipe{}.Skip(context.New(config.Project{
require.False(t, Pipe{}.Skip(testctx.NewWithCfg(config.Project{
Announce: config.Announce{
OpenCollective: config.OpenCollective{
Enabled: true,

View File

@@ -5,12 +5,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
func TestCustomProjectName(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Release: config.Release{
GitHub: config.Repo{
@@ -24,7 +24,7 @@ func TestCustomProjectName(t *testing.T) {
}
func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Owner: "bar",
@@ -37,7 +37,7 @@ func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
}
func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Owner: "bar",
@@ -50,7 +50,7 @@ func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
}
func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Owner: "bar",
@@ -63,7 +63,7 @@ func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
}
func TestEmptyProjectNameAndRelease(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{},
},

View File

@@ -3,8 +3,8 @@ package publish
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,23 +13,19 @@ func TestDescription(t *testing.T) {
}
func TestPublish(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.Release.Disable = "true"
ctx.TokenType = context.TokenTypeGitHub
for i := range ctx.Config.Dockers {
ctx.Config.Dockers[i].SkipPush = "true"
}
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{Disable: "true"},
}, testctx.GitHubTokenType)
require.NoError(t, Pipe{}.Run(ctx))
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipPublish = true
ctx := testctx.New(testctx.SkipPublish)
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
require.False(t, Pipe{}.Skip(context.New(config.Project{})))
require.False(t, Pipe{}.Skip(testctx.New()))
})
}

View File

@@ -3,8 +3,8 @@ package reddit
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,13 +13,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Reddit.TitleTemplate, defaultTitleTemplate)
}
func TestAnnounceInvalidURLTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Reddit: config.Reddit{
URLTemplate: "{{ .Foo }",
@@ -30,7 +30,7 @@ func TestAnnounceInvalidURLTemplate(t *testing.T) {
}
func TestAnnounceInvalidTitleTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Reddit: config.Reddit{
TitleTemplate: "{{ .Foo }",
@@ -41,7 +41,7 @@ func TestAnnounceInvalidTitleTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Reddit: config.Reddit{},
},
@@ -52,11 +52,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Reddit: config.Reddit{
Enabled: true,

View File

@@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -11,7 +12,7 @@ import (
func TestDescribeBody(t *testing.T) {
changelog := "feature1: description\nfeature2: other description"
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotes = changelog
out, err := describeBody(ctx)
require.NoError(t, err)
@@ -21,7 +22,7 @@ func TestDescribeBody(t *testing.T) {
func TestDontEscapeHTML(t *testing.T) {
changelog := "<h1>test</h1>"
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.ReleaseNotes = changelog
out, err := describeBody(ctx)
@@ -31,14 +32,16 @@ func TestDontEscapeHTML(t *testing.T) {
func TestDescribeBodyWithHeaderAndFooter(t *testing.T) {
changelog := "feature1: description\nfeature2: other description"
ctx := context.New(config.Project{
Release: config.Release{
Header: "## Yada yada yada\nsomething\n",
Footer: "\n---\n\nGet images at docker.io/foo/bar:{{.Tag}}\n\n---\n\nGet GoReleaser Pro at https://goreleaser.com/pro",
ctx := testctx.NewWithCfg(
config.Project{
Release: config.Release{
Header: "## Yada yada yada\nsomething\n",
Footer: "\n---\n\nGet images at docker.io/foo/bar:{{.Tag}}\n\n---\n\nGet GoReleaser Pro at https://goreleaser.com/pro",
},
},
})
ctx.ReleaseNotes = changelog
ctx.Git = context.GitInfo{CurrentTag: "v1.0"}
testctx.WithCurrentTag("v1.0"),
func(ctx *context.Context) { ctx.ReleaseNotes = changelog },
)
out, err := describeBody(ctx)
require.NoError(t, err)
@@ -46,7 +49,7 @@ func TestDescribeBodyWithHeaderAndFooter(t *testing.T) {
}
func TestDescribeBodyWithInvalidHeaderTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Header: "## {{ .Nop }\n",
},
@@ -56,7 +59,7 @@ func TestDescribeBodyWithInvalidHeaderTemplate(t *testing.T) {
}
func TestDescribeBodyWithInvalidFooterTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Footer: "{{ .Nops }",
},

View File

@@ -8,6 +8,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -46,8 +47,7 @@ func TestRunPipeWithoutIDsThenDoesNotFilter(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -155,8 +155,7 @@ func TestRunPipeWithIDsThenFilters(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -209,8 +208,7 @@ func TestRunPipeReleaseCreationFailed(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
client := &client.Mock{
FailToCreateRelease: true,
}
@@ -228,8 +226,7 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -253,8 +250,7 @@ func TestRunPipeUploadFailure(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -281,8 +277,7 @@ func TestRunPipeExtraFileNotFound(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
client := &client.Mock{}
require.EqualError(t, doPublish(ctx, client), "globbing failed for pattern ./nope: matching \"./nope\": file does not exist")
require.True(t, client.CreatedRelease)
@@ -302,8 +297,7 @@ func TestRunPipeExtraOverride(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
client := &client.Mock{}
require.NoError(t, doPublish(ctx, client))
require.True(t, client.CreatedRelease)
@@ -324,8 +318,7 @@ func TestRunPipeUploadRetry(t *testing.T) {
},
},
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx := testctx.NewWithCfg(config, testctx.WithCurrentTag("v1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -344,10 +337,15 @@ func TestDefault(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.GitHubURLs.Download = "https://github.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
@@ -358,11 +356,15 @@ func TestDefaultInvalidURL(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.GitHubURLs.Download = "https://github.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.Error(t, Pipe{}.Default(ctx))
}
@@ -371,10 +373,15 @@ func TestDefaultWithGitlab(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@gitlab.com:gitlabowner/gitlabrepo.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.GitLabURLs.Download = "https://gitlab.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GitLabURLs: config.GitLabURLs{
Download: "https://gitlab.com",
},
},
testctx.GitLabTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "gitlabrepo", ctx.Config.Release.GitLab.Name)
require.Equal(t, "gitlabowner", ctx.Config.Release.GitLab.Owner)
@@ -385,11 +392,15 @@ func TestDefaultWithGitlabInvalidURL(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@gitlab.com:gitlabrepo.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.GitLabURLs.Download = "https://gitlab.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GitLabURLs: config.GitLabURLs{
Download: "https://gitlab.com",
},
},
testctx.GiteaTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.Error(t, Pipe{}.Default(ctx))
}
@@ -398,10 +409,15 @@ func TestDefaultWithGitea(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@gitea.example.com:giteaowner/gitearepo.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitea
ctx.Config.GiteaURLs.Download = "https://git.honk.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GiteaURLs: config.GiteaURLs{
Download: "https://git.honk.com",
},
},
testctx.GiteaTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "gitearepo", ctx.Config.Release.Gitea.Name)
require.Equal(t, "giteaowner", ctx.Config.Release.Gitea.Owner)
@@ -412,11 +428,15 @@ func TestDefaultWithGiteaInvalidURL(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@gitea.example.com:gitearepo.git")
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitea
ctx.Config.GiteaURLs.Download = "https://git.honk.com"
ctx.Git.CurrentTag = "v1.0.0"
ctx := testctx.NewWithCfg(
config.Project{
GiteaURLs: config.GiteaURLs{
Download: "https://git.honk.com",
},
},
testctx.GiteaTokenType,
testctx.WithCurrentTag("v1.0.0"),
)
require.Error(t, Pipe{}.Default(ctx))
}
@@ -426,7 +446,7 @@ func TestDefaultPreRelease(t *testing.T) {
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
t.Run("prerelease", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Prerelease: "true",
},
@@ -442,10 +462,8 @@ func TestDefaultPreRelease(t *testing.T) {
})
t.Run("release", func(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
Prerelease: "false",
},
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Semver = context.Semver{
@@ -459,55 +477,47 @@ func TestDefaultPreRelease(t *testing.T) {
})
t.Run("auto-release", func(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
Prerelease: "auto",
ctx := testctx.NewWithCfg(
config.Project{
Release: config.Release{
Prerelease: "auto",
},
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
}
testctx.GitHubTokenType,
testctx.WithSemver(1, 0, 0, ""),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, false, ctx.PreRelease)
})
t.Run("auto-rc", func(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
Prerelease: "auto",
ctx := testctx.NewWithCfg(
config.Project{
Release: config.Release{
Prerelease: "auto",
},
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
Prerelease: "rc1",
}
testctx.GitHubTokenType,
testctx.WithSemver(1, 0, 0, "rc1"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, true, ctx.PreRelease)
})
t.Run("auto-rc-github-setup", func(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "foo",
Owner: "foo",
ctx := testctx.NewWithCfg(
config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "foo",
Owner: "foo",
},
Prerelease: "auto",
},
Prerelease: "auto",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
Prerelease: "rc1",
}
testctx.GitHubTokenType,
testctx.WithSemver(1, 0, 0, "rc1"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, true, ctx.PreRelease)
})
@@ -518,7 +528,7 @@ func TestDefaultPipeDisabled(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Disable: "true",
},
@@ -534,7 +544,7 @@ func TestDefaultFilled(t *testing.T) {
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "foo",
@@ -550,16 +560,14 @@ func TestDefaultFilled(t *testing.T) {
func TestDefaultNotAGitRepo(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx := testctx.New(testctx.GitHubTokenType)
require.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
require.Empty(t, ctx.Config.Release.GitHub.String())
}
func TestDefaultGitRepoWithoutOrigin(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx := testctx.New(testctx.GitHubTokenType)
testlib.GitInit(t)
require.EqualError(t, Pipe{}.Default(ctx), "no remote configured to list refs from")
require.Empty(t, ctx.Config.Release.GitHub.String())
@@ -567,23 +575,20 @@ func TestDefaultGitRepoWithoutOrigin(t *testing.T) {
func TestDefaultNotAGitRepoSnapshot(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx.Snapshot = true
ctx := testctx.New(testctx.GitHubTokenType, testctx.Snapshot)
require.NoError(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Release.GitHub.String())
}
func TestDefaultGitRepoWithoutRemote(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx := testctx.New(testctx.GitHubTokenType)
require.Error(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Release.GitHub.String())
}
func TestDefaultMultipleReleasesDefined(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Owner: "githubName",
@@ -604,7 +609,7 @@ func TestDefaultMultipleReleasesDefined(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Disable: "true",
},
@@ -615,7 +620,7 @@ func TestSkip(t *testing.T) {
})
t.Run("skip tmpl", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
Disable: "{{ .Env.FOO }}",
@@ -627,7 +632,7 @@ func TestSkip(t *testing.T) {
})
t.Run("tmpl err", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Disable: "{{ .Env.FOO }}",
},
@@ -637,7 +642,7 @@ func TestSkip(t *testing.T) {
})
t.Run("skip upload", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=true"},
Release: config.Release{
SkipUpload: "{{ .Env.FOO }}",
@@ -650,7 +655,7 @@ func TestSkip(t *testing.T) {
})
t.Run("skip upload tmpl", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
SkipUpload: "true",
},
@@ -662,7 +667,7 @@ func TestSkip(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
b, err := Pipe{}.Skip(context.New(config.Project{}))
b, err := Pipe{}.Skip(testctx.New())
require.NoError(t, err)
require.False(t, b)
})

View File

@@ -4,17 +4,15 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestSetupGitLab(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, setupGitLab(ctx))
repo, err := git.ExtractRepoFromConfig(ctx)
require.NoError(t, err)
require.Equal(t, repo.Owner, ctx.Config.Release.GitLab.Owner)
@@ -22,7 +20,7 @@ func TestSetupGitLab(t *testing.T) {
})
t.Run("with templates", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GitLabURLs: config.GitLabURLs{
Download: "https://{{ .Env.OWNER }}/download",
@@ -43,7 +41,7 @@ func TestSetupGitLab(t *testing.T) {
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Name: "foo",
@@ -56,7 +54,7 @@ func TestSetupGitLab(t *testing.T) {
})
t.Run("name", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Name: "{{.Env.NOPE}}",
@@ -71,15 +69,14 @@ func TestSetupGitLab(t *testing.T) {
func TestSetupGitea(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, setupGitea(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.Gitea.Owner)
require.Equal(t, "goreleaser", ctx.Config.Release.Gitea.Name)
})
t.Run("with templates", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GiteaURLs: config.GiteaURLs{
Download: "https://{{ .Env.OWNER }}/download",
@@ -100,7 +97,7 @@ func TestSetupGitea(t *testing.T) {
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Name: "foo",
@@ -113,7 +110,7 @@ func TestSetupGitea(t *testing.T) {
})
t.Run("name", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Name: "{{.Env.NOPE}}",
@@ -128,15 +125,14 @@ func TestSetupGitea(t *testing.T) {
func TestSetupGitHub(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, setupGitHub(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
})
t.Run("with templates", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GitHubURLs: config.GitHubURLs{
Download: "https://{{ .Env.OWNER }}/download",
@@ -157,7 +153,7 @@ func TestSetupGitHub(t *testing.T) {
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "foo",
@@ -170,7 +166,7 @@ func TestSetupGitHub(t *testing.T) {
})
t.Run("name", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "{{.Env.NOPE}}",

View File

@@ -9,6 +9,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -145,11 +146,9 @@ func TestSBOMCatalogDefault(t *testing.T) {
for _, test := range tests {
t.Run(fmt.Sprintf("artifact=%q", test.configs[0].Artifacts), func(t *testing.T) {
testlib.CheckPath(t, "syft")
ctx := &context.Context{
Config: config.Project{
SBOMs: test.configs,
},
}
ctx := testctx.NewWithCfg(config.Project{
SBOMs: test.configs,
})
err := Pipe{}.Default(ctx)
if test.err {
require.Error(t, err)
@@ -166,37 +165,34 @@ func TestSBOMCatalogDefault(t *testing.T) {
}
func TestSBOMCatalogInvalidArtifacts(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.SBOMs = []config.SBOM{
{Artifacts: "foo"},
}
ctx := testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{{Artifacts: "foo"}},
})
err := Pipe{}.Run(ctx)
require.EqualError(t, err, "invalid list of artifacts to catalog: foo")
}
func TestSeveralSBOMsWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
SBOMs: []config.SBOM{
{
ID: "a",
},
{
ID: "a",
},
ctx := testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
ID: "a",
},
{
ID: "a",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 sboms with the ID 'a', please fix your config")
}
func TestSkipCataloging(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip SBOM cataloging", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "all",
@@ -208,7 +204,7 @@ func TestSkipCataloging(t *testing.T) {
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "all",
@@ -230,81 +226,69 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
{
desc: "catalog errors",
expectedErrMsg: "cataloging artifacts: exit failed",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Cmd: "exit",
Args: []string{"1"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Cmd: "exit",
Args: []string{"1"},
},
},
),
}),
},
{
desc: "invalid args template",
expectedErrMsg: `cataloging artifacts failed: arg "${FOO}-{{ .foo }{{}}{": invalid template: template: tmpl:1: unexpected "}" in operand`,
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Cmd: "exit",
Args: []string{"${FOO}-{{ .foo }{{}}{"},
},
},
Env: []string{
"FOO=BAR",
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Cmd: "exit",
Args: []string{"${FOO}-{{ .foo }{{}}{"},
},
},
),
Env: []string{
"FOO=BAR",
},
}),
},
{
desc: "catalog source archives",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{Artifacts: "source"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{Artifacts: "source"},
},
),
}),
sbomPaths: []string{"artifact5.tar.gz.sbom"},
sbomNames: []string{"artifact5.tar.gz.sbom"},
},
{
desc: "catalog archives",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{Artifacts: "archive"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{Artifacts: "archive"},
},
),
}),
sbomPaths: []string{"artifact1.sbom", "artifact2.sbom"},
sbomNames: []string{"artifact1.sbom", "artifact2.sbom"},
},
{
desc: "catalog linux packages",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{Artifacts: "package"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{Artifacts: "package"},
},
),
}),
sbomPaths: []string{"package1.deb.sbom"},
sbomNames: []string{"package1.deb.sbom"},
},
{
desc: "catalog binaries",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{Artifacts: "binary"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{Artifacts: "binary"},
},
),
}),
sbomPaths: []string{
"artifact3-name_1.2.2_linux_amd64.sbom",
"artifact4-name_1.2.2_linux_amd64.sbom",
@@ -316,48 +300,44 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
},
{
desc: "manual cataloging",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "any",
Args: []string{
"--file",
"$document0",
"--output",
"spdx-json",
"artifact5.tar.gz",
},
Documents: []string{
"final.sbom",
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "any",
Args: []string{
"--file",
"$document0",
"--output",
"spdx-json",
"artifact5.tar.gz",
},
Documents: []string{
"final.sbom",
},
},
},
),
}),
sbomPaths: []string{"final.sbom"},
sbomNames: []string{"final.sbom"},
},
{
desc: "multiple SBOM configs",
ctx: context.New(
config.Project{
Env: []string{
"SBOM_SUFFIX=s2-ish",
ctx: testctx.NewWithCfg(config.Project{
Env: []string{
"SBOM_SUFFIX=s2-ish",
},
SBOMs: []config.SBOM{
{
ID: "s1",
Artifacts: "binary",
},
SBOMs: []config.SBOM{
{
ID: "s1",
Artifacts: "binary",
},
{
ID: "s2",
Artifacts: "archive",
Documents: []string{"{{ .ArtifactName }}.{{ .Env.SBOM_SUFFIX }}.sbom"},
},
{
ID: "s2",
Artifacts: "archive",
Documents: []string{"{{ .ArtifactName }}.{{ .Env.SBOM_SUFFIX }}.sbom"},
},
},
),
}),
sbomPaths: []string{
"artifact1.s2-ish.sbom",
"artifact2.s2-ish.sbom",
@@ -373,16 +353,14 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
},
{
desc: "catalog artifacts with filtered by ID",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
IDs: []string{"foo"},
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
IDs: []string{"foo"},
},
},
),
}),
sbomPaths: []string{
"artifact3-name_1.2.2_linux_amd64.sbom",
},
@@ -392,28 +370,26 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
},
{
desc: "catalog binary artifacts with env in arguments",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Args: []string{
"--file",
"$document",
"--output",
"spdx-json",
"$artifact",
},
Documents: []string{
"{{ .ArtifactName }}.{{ .Env.TEST_USER }}.sbom",
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "binary",
Args: []string{
"--file",
"$document",
"--output",
"spdx-json",
"$artifact",
},
Documents: []string{
"{{ .ArtifactName }}.{{ .Env.TEST_USER }}.sbom",
},
},
Env: []string{
"TEST_USER=test-user-name",
},
},
),
Env: []string{
"TEST_USER=test-user-name",
},
}),
sbomPaths: []string{
"artifact3-name.test-user-name.sbom",
"artifact4.test-user-name.sbom",
@@ -425,16 +401,14 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
},
{
desc: "cataloging 'any' artifacts fails",
ctx: context.New(
config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "any",
Cmd: "false",
},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
Artifacts: "any",
Cmd: "false",
},
},
),
}),
expectedErrMsg: "cataloging artifacts: false failed: exit status 1: ",
},
}
@@ -741,10 +715,9 @@ func Test_templateNames(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: tt.dist,
})
ctx.Version = tt.version
}, testctx.WithVersion(tt.version))
cfg := tt.cfg
require.NoError(t, setConfigDefaults(&cfg))

View File

@@ -1,7 +1,6 @@
package scoop
import (
ctx "context"
"os"
"path/filepath"
"testing"
@@ -9,6 +8,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -22,10 +22,10 @@ func TestDescription(t *testing.T) {
func TestDefault(t *testing.T) {
testlib.Mktmp(t)
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{ProjectName: "barr"},
}
ctx := testctx.NewWithCfg(
config.Project{ProjectName: "barr"},
testctx.GitHubTokenType,
)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Scoop.Name)
require.NotEmpty(t, ctx.Config.Scoop.CommitAuthor.Name)
@@ -72,24 +72,23 @@ func Test_doRun(t *testing.T) {
"valid public github",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -108,23 +107,22 @@ func Test_doRun(t *testing.T) {
"wrap in directory",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -157,24 +155,23 @@ func Test_doRun(t *testing.T) {
"valid enterprise github",
args{
func() *context.Context {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{Download: "https://api.custom.github.enterprise.com"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{Download: "https://api.custom.github.enterprise.com"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -193,23 +190,22 @@ func Test_doRun(t *testing.T) {
"valid public gitlab",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://gitlab.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://gitlab.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitLab
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -236,24 +232,23 @@ func Test_doRun(t *testing.T) {
"valid enterprise gitlab",
args{
func() *context.Context {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{Download: "https://api.custom.gitlab.enterprise.com"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{Download: "https://api.custom.gitlab.enterprise.com"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://gitlab.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://gitlab.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitLab
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -280,23 +275,22 @@ func Test_doRun(t *testing.T) {
"no windows build",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -309,26 +303,25 @@ func Test_doRun(t *testing.T) {
"is draft",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Release: config.Release{
Draft: true,
},
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Release: config.Release{
Draft: true,
},
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -344,30 +337,24 @@ func Test_doRun(t *testing.T) {
"is prerelease and skip upload set to auto",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
SkipUpload: "auto",
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
SkipUpload: "auto",
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1-pre.1",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 1,
Prerelease: "-pre.1",
}
ctx.Version = "1.0.1-pre.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1-pre.1"),
testctx.WithVersion("1.0.1-pre.1"),
testctx.WithSemver(1, 0, 0, "pre.1"),
)
},
client.NewMock(),
},
@@ -383,24 +370,23 @@ func Test_doRun(t *testing.T) {
"skip upload set to true",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
SkipUpload: "true",
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
SkipUpload: "true",
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -416,26 +402,25 @@ func Test_doRun(t *testing.T) {
"release is disabled",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Release: config.Release{
Disable: "true",
},
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Release: config.Release{
Disable: "true",
},
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -451,23 +436,22 @@ func Test_doRun(t *testing.T) {
"no archive",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -480,24 +464,23 @@ func Test_doRun(t *testing.T) {
"invalid ref tmpl",
args{
func() *context.Context {
ctx := context.New(config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "{{ .Env.aaaaaa }}",
Name: "test",
return testctx.NewWithCfg(
config.Project{
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "{{ .Env.aaaaaa }}",
Name: "test",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -512,26 +495,25 @@ func Test_doRun(t *testing.T) {
"ref templ",
args{
func() *context.Context {
ctx := context.New(config.Project{
Env: []string{"FOO=test", "BRANCH=main"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "{{ .Env.FOO }}",
Name: "{{ .Env.FOO }}",
Branch: "{{ .Env.BRANCH }}",
return testctx.NewWithCfg(
config.Project{
Env: []string{"FOO=test", "BRANCH=main"},
ProjectName: "run-pipe",
Scoop: config.Scoop{
Bucket: config.RepoRef{
Owner: "{{ .Env.FOO }}",
Name: "{{ .Env.FOO }}",
Branch: "{{ .Env.BRANCH }}",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
Folder: "scoops",
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
},
})
ctx.TokenType = context.TokenTypeGitHub
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.1",
}
ctx.Version = "1.0.1"
return ctx
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
},
client.NewMock(),
},
@@ -549,7 +531,6 @@ func Test_doRun(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.args.ctx()
ctx.Artifacts = artifact.New()
for _, a := range tt.artifacts {
a.Type = artifact.UploadableArchive
ctx.Artifacts.Add(&a)
@@ -574,15 +555,8 @@ func Test_buildManifest(t *testing.T) {
}{
{
"common",
&context.Context{
Context: ctx.Background(),
TokenType: context.TokenTypeGitHub,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
@@ -603,19 +577,15 @@ func Test_buildManifest(t *testing.T) {
Persist: []string{"data", "config", "test.ini"},
},
},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
),
},
{
"pre-post-install",
&context.Context{
Context: ctx.Background(),
TokenType: context.TokenTypeGitHub,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
@@ -638,19 +608,15 @@ func Test_buildManifest(t *testing.T) {
PostInstall: []string{"Write-Host 'Running postinstall command'"},
},
},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
),
},
{
"url template",
&context.Context{
Context: ctx.Background(),
TokenType: context.TokenTypeGitHub,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
testctx.NewWithCfg(
config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.com",
},
@@ -667,19 +633,15 @@ func Test_buildManifest(t *testing.T) {
Persist: []string{"data.cfg", "etc"},
},
},
},
testctx.WithCurrentTag("v1.0.1"),
testctx.GitHubTokenType,
testctx.WithVersion("1.0.1"),
),
},
{
"gitlab url template",
&context.Context{
Context: ctx.Background(),
TokenType: context.TokenTypeGitLab,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
testctx.NewWithCfg(
config.Project{
GitLabURLs: config.GitLabURLs{
Download: "https://gitlab.com",
},
@@ -696,7 +658,10 @@ func Test_buildManifest(t *testing.T) {
Persist: []string{"data.cfg", "etc"},
},
},
},
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
),
},
}
@@ -772,13 +737,8 @@ func Test_buildManifest(t *testing.T) {
}
func getScoopPipeSkipCtx(folder string) (*context.Context, string) {
ctx := &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "run-pipe",
Scoop: config.Scoop{
@@ -791,7 +751,9 @@ func getScoopPipeSkipCtx(folder string) (*context.Context, string) {
Name: "run-pipe",
},
},
}
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
path := filepath.Join(folder, "bin.tar.gz")
@@ -846,14 +808,9 @@ func TestWrapInDirectory(t *testing.T) {
folder := t.TempDir()
file := filepath.Join(folder, "archive")
require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644))
ctx := &context.Context{
TokenType: context.TokenTypeGitLab,
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
GitLabURLs: config.GitLabURLs{
Download: "https://gitlab.com",
},
@@ -870,7 +827,11 @@ func TestWrapInDirectory(t *testing.T) {
Persist: []string{"data.cfg", "etc"},
},
},
}
testctx.GitHubTokenType,
testctx.WithCurrentTag("v1.0.1"),
testctx.WithVersion("1.0.1"),
)
require.NoError(t, Pipe{}.Default(ctx))
cl, err := client.New(ctx)
require.NoError(t, err)
@@ -903,11 +864,11 @@ func TestWrapInDirectory(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Scoop: config.Scoop{
Bucket: config.RepoRef{
Name: "a",

View File

@@ -3,7 +3,7 @@ package semver
import (
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,8 +13,7 @@ func TestDescription(t *testing.T) {
}
func TestValidSemver(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.5.2-rc1"
ctx := testctx.New(testctx.WithCurrentTag("v1.5.2-rc1"))
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, context.Semver{
Major: 1,
@@ -25,8 +24,7 @@ func TestValidSemver(t *testing.T) {
}
func TestInvalidSemver(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "aaaav1.5.2-rc1"
ctx := testctx.New(testctx.WithCurrentTag("aaaav1.5.2-rc1"))
err := Pipe{}.Run(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to parse tag 'aaaav1.5.2-rc1' as semver")

View File

@@ -8,9 +8,9 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -19,11 +19,9 @@ func TestDockerSignDescription(t *testing.T) {
}
func TestDockerSignDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
DockerSigns: []config.Sign{{}},
},
}
ctx := testctx.NewWithCfg(config.Project{
DockerSigns: []config.Sign{{}},
})
err := DockerPipe{}.Default(ctx)
require.NoError(t, err)
require.Equal(t, "cosign", ctx.Config.DockerSigns[0].Cmd)
@@ -33,19 +31,21 @@ func TestDockerSignDefault(t *testing.T) {
}
func TestDockerSignDisabled(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.DockerSigns = []config.Sign{
{Artifacts: "none"},
}
ctx := testctx.NewWithCfg(config.Project{
DockerSigns: []config.Sign{
{Artifacts: "none"},
},
})
err := DockerPipe{}.Publish(ctx)
require.EqualError(t, err, "artifact signing is disabled")
}
func TestDockerSignInvalidArtifacts(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.DockerSigns = []config.Sign{
{Artifacts: "foo"},
}
ctx := testctx.NewWithCfg(config.Project{
DockerSigns: []config.Sign{
{Artifacts: "foo"},
},
})
err := DockerPipe{}.Publish(ctx)
require.EqualError(t, err, "invalid list of artifacts to sign: foo")
}
@@ -154,8 +154,9 @@ func TestDockerSignArtifacts(t *testing.T) {
// TODO: keyless test?
} {
t.Run(name, func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.DockerSigns = cfg.Signs
ctx := testctx.NewWithCfg(config.Project{
DockerSigns: cfg.Signs,
})
wd, err := os.Getwd()
require.NoError(t, err)
tmp := testlib.Mktmp(t)
@@ -210,17 +211,16 @@ func TestDockerSignArtifacts(t *testing.T) {
func TestDockerSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, DockerPipe{}.Skip(context.New(config.Project{})))
require.True(t, DockerPipe{}.Skip(testctx.New()))
})
t.Run("skip sign", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipSign = true
ctx := testctx.New(testctx.SkipSign)
require.True(t, DockerPipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
DockerSigns: []config.Sign{
{},
},

View File

@@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -50,11 +51,9 @@ func TestDescription(t *testing.T) {
}
func TestSignDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Signs: []config.Sign{{}},
},
}
ctx := testctx.NewWithCfg(config.Project{
Signs: []config.Sign{{}},
})
err := Pipe{}.Default(ctx)
require.NoError(t, err)
require.Equal(t, ctx.Config.Signs[0].Cmd, "gpg")
@@ -64,19 +63,13 @@ func TestSignDefault(t *testing.T) {
}
func TestSignDisabled(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.Signs = []config.Sign{
{Artifacts: "none"},
}
ctx := testctx.NewWithCfg(config.Project{Signs: []config.Sign{{Artifacts: "none"}}})
err := Pipe{}.Run(ctx)
require.EqualError(t, err, "artifact signing is disabled")
}
func TestSignInvalidArtifacts(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Config.Signs = []config.Sign{
{Artifacts: "foo"},
}
ctx := testctx.NewWithCfg(config.Project{Signs: []config.Sign{{Artifacts: "foo"}}})
err := Pipe{}.Run(ctx)
require.EqualError(t, err, "invalid list of artifacts to sign: foo")
}
@@ -96,171 +89,149 @@ func TestSignArtifacts(t *testing.T) {
{
desc: "sign cmd not found",
expectedErrMsg: `sign: not-a-valid-cmd failed: exec: "not-a-valid-cmd": executable file not found in $PATH: `,
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "not-a-valid-cmd",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "not-a-valid-cmd",
},
},
),
}),
},
{
desc: "sign errors",
expectedErrMsg: "sign: exit failed",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Args: []string{"1"},
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Args: []string{"1"},
},
},
),
}),
},
{
desc: "invalid certificate template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:3: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Certificate: "{{ .blah }}",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Certificate: "{{ .blah }}",
},
},
),
}),
},
{
desc: "invalid signature template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:3: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Signature: "{{ .blah }}",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Signature: "{{ .blah }}",
},
},
),
}),
},
{
desc: "invalid args template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1: unexpected "}" in operand`,
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Args: []string{"${FOO}-{{ .foo }{{}}{"},
},
},
Env: []string{
"FOO=BAR",
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Args: []string{"${FOO}-{{ .foo }{{}}{"},
},
},
),
Env: []string{
"FOO=BAR",
},
}),
},
{
desc: "invalid env template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:5: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Env: []string{"A={{ .blah }}"},
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Cmd: "exit",
Env: []string{"A={{ .blah }}"},
},
},
),
}),
},
{
desc: "sign all artifacts",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
},
{
desc: "sign archives",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "archive",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "archive",
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig"},
},
{
desc: "sign packages",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "package",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "package",
},
},
),
}),
signaturePaths: []string{"package1.deb.sig"},
signatureNames: []string{"package1.deb.sig"},
},
{
desc: "sign binaries",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "binary",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "binary",
},
},
),
}),
signaturePaths: []string{"artifact3.sig", "linux_amd64/artifact4.sig"},
signatureNames: []string{"artifact3_1.0.0_linux_amd64.sig", "artifact4_1.0.0_linux_amd64.sig"},
},
{
desc: "multiple sign configs",
ctx: context.New(
config.Project{
Env: []string{
"GPG_KEY_ID=" + fakeGPGKeyID,
ctx: testctx.NewWithCfg(config.Project{
Env: []string{
"GPG_KEY_ID=" + fakeGPGKeyID,
},
Signs: []config.Sign{
{
ID: "s1",
Artifacts: "checksum",
},
Signs: []config.Sign{
{
ID: "s1",
Artifacts: "checksum",
},
{
ID: "s2",
Artifacts: "archive",
Signature: "${artifact}.{{ .Env.GPG_KEY_ID }}.sig",
},
{
ID: "s2",
Artifacts: "archive",
Signature: "${artifact}.{{ .Env.GPG_KEY_ID }}.sig",
},
},
),
}),
signaturePaths: []string{
"artifact1." + fakeGPGKeyID + ".sig",
"artifact2." + fakeGPGKeyID + ".sig",
@@ -276,279 +247,251 @@ func TestSignArtifacts(t *testing.T) {
},
{
desc: "sign filtered artifacts",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
IDs: []string{"foo"},
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
IDs: []string{"foo"},
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "artifact5.tar.gz.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact5.tar.gz.sig", "package1.deb.sig"},
},
{
desc: "sign only checksums",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "checksum",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "checksum",
},
},
),
}),
signaturePaths: []string{"checksum.sig", "checksum2.sig"},
signatureNames: []string{"checksum.sig", "checksum2.sig"},
},
{
desc: "sign only filtered checksums",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "checksum",
IDs: []string{"foo"},
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "checksum",
IDs: []string{"foo"},
},
},
),
}),
signaturePaths: []string{"checksum.sig", "checksum2.sig"},
signatureNames: []string{"checksum.sig", "checksum2.sig"},
},
{
desc: "sign only source",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "source",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "source",
},
},
),
}),
signaturePaths: []string{"artifact5.tar.gz.sig"},
signatureNames: []string{"artifact5.tar.gz.sig"},
},
{
desc: "sign only source filter by id",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "source",
IDs: []string{"should-not-be-used"},
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "source",
IDs: []string{"should-not-be-used"},
},
},
),
}),
signaturePaths: []string{"artifact5.tar.gz.sig"},
signatureNames: []string{"artifact5.tar.gz.sig"},
},
{
desc: "sign only sbom",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "sbom",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "sbom",
},
},
),
}),
signaturePaths: []string{"artifact5.tar.gz.sbom.sig"},
signatureNames: []string{"artifact5.tar.gz.sbom.sig"},
},
{
desc: "sign all artifacts with env",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
"${TEST_USER}",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
"${TEST_USER}",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
},
Env: []string{
fmt.Sprintf("TEST_USER=%s", user),
},
},
),
Env: []string{
fmt.Sprintf("TEST_USER=%s", user),
},
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
},
{
desc: "sign all artifacts with template",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
"{{ .Env.SOME_TEST_USER }}",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
"{{ .Env.SOME_TEST_USER }}",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
},
Env: []string{
fmt.Sprintf("SOME_TEST_USER=%s", user),
},
},
),
Env: []string{
fmt.Sprintf("SOME_TEST_USER=%s", user),
},
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
},
{
desc: "sign single with password from stdin",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
Stdin: &stdin,
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
Stdin: &stdin,
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
user: passwordUser,
},
{
desc: "sign single with password from templated stdin",
ctx: context.New(
config.Project{
Env: []string{"GPG_PASSWORD=" + stdin},
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
Stdin: &tmplStdin,
ctx: testctx.NewWithCfg(config.Project{
Env: []string{"GPG_PASSWORD=" + stdin},
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
Stdin: &tmplStdin,
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
user: passwordUser,
},
{
desc: "sign single with password from stdin_file",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
StdinFile: filepath.Join(keyring, passwordUser),
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"-u",
passwordUser,
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
},
StdinFile: filepath.Join(keyring, passwordUser),
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
user: passwordUser,
},
{
desc: "missing stdin_file",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
},
StdinFile: "/tmp/non-existing-file",
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Artifacts: "all",
Args: []string{
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase-fd",
"0",
},
StdinFile: "/tmp/non-existing-file",
},
},
),
}),
expectedErrMsg: `sign failed: cannot open file /tmp/non-existing-file: open /tmp/non-existing-file: no such file or directory`,
},
{
desc: "sign creating certificate",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Certificate: "${artifact}.pem",
Artifacts: "checksum",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Certificate: "${artifact}.pem",
Artifacts: "checksum",
},
},
),
}),
signaturePaths: []string{"checksum.sig", "checksum2.sig"},
signatureNames: []string{"checksum.sig", "checksum2.sig"},
certificateNames: []string{"checksum.pem", "checksum2.pem"},
},
{
desc: "sign all artifacts with env and certificate",
ctx: context.New(
config.Project{
Signs: []config.Sign{
{
Env: []string{"NOT_HONK=honk", "HONK={{ .Env.NOT_HONK }}"},
Certificate: `{{ trimsuffix (trimsuffix .Env.artifact ".tar.gz") ".deb" }}_${HONK}.pem`,
Artifacts: "all",
},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Env: []string{"NOT_HONK=honk", "HONK={{ .Env.NOT_HONK }}"},
Certificate: `{{ trimsuffix (trimsuffix .Env.artifact ".tar.gz") ".deb" }}_${HONK}.pem`,
Artifacts: "all",
},
},
),
}),
signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig", "artifact5.tar.gz.sig", "artifact5.tar.gz.sbom.sig", "package1.deb.sig"},
certificateNames: []string{"artifact1_honk.pem", "artifact2_honk.pem", "artifact3_1.0.0_linux_amd64_honk.pem", "checksum_honk.pem", "checksum2_honk.pem", "artifact4_1.0.0_linux_amd64_honk.pem", "artifact5_honk.pem", "artifact5.tar.gz.sbom_honk.pem", "package1_honk.pem"},
@@ -748,34 +691,31 @@ func verifySignature(tb testing.TB, ctx *context.Context, sig string, user strin
}
func TestSeveralSignsWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Signs: []config.Sign{
{
ID: "a",
},
{
ID: "a",
},
ctx := testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
ID: "a",
},
{
ID: "a",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 signs with the ID 'a', please fix your config")
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("skip sign", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipSign = true
ctx := testctx.New(testctx.SkipSign)
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{},
},

View File

@@ -5,9 +5,9 @@ import (
"os"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/yaml"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/slack-go/slack"
"github.com/stretchr/testify/require"
)
@@ -17,13 +17,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Slack.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Slack: config.Slack{
MessageTemplate: "{{ .Foo }",
@@ -34,7 +34,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Slack: config.Slack{},
},
@@ -45,11 +45,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Slack: config.Slack{
Enabled: true,
@@ -67,29 +67,20 @@ func TestParseRichText(t *testing.T) {
t.Run("parse only - full slack config with blocks and attachments", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(goodRichSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
blocks, attachments, err := parseAdvancedFormatting(ctx)
require.NoError(t, err)
require.Len(t, blocks.BlockSet, 4)
require.Len(t, attachments, 2)
})
t.Run("parse only - slack config with bad blocks", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(badBlocksSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
_, _, err := parseAdvancedFormatting(ctx)
require.Error(t, err)
require.ErrorContains(t, err, "json")
@@ -97,13 +88,9 @@ func TestParseRichText(t *testing.T) {
t.Run("parse only - slack config with bad attachments", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(badAttachmentsSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
_, _, err := parseAdvancedFormatting(ctx)
require.Error(t, err)
require.ErrorContains(t, err, "json")
@@ -117,25 +104,17 @@ func TestRichText(t *testing.T) {
t.Run("e2e - full slack config with blocks and attachments", func(t *testing.T) {
t.SkipNow() // requires a valid webhook for integration testing
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(goodRichSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
require.NoError(t, Pipe{}.Announce(ctx))
})
t.Run("slack config with bad blocks", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(badBlocksSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
err := Pipe{}.Announce(ctx)
require.Error(t, err)
require.ErrorContains(t, err, "json")
@@ -147,35 +126,25 @@ func TestUnmarshall(t *testing.T) {
t.Run("happy unmarshal", func(t *testing.T) {
t.Parallel()
ctx := context.New(config.Project{})
ctx.Version = testVersion
ctx := testctx.New(testctx.WithVersion(testVersion))
var blocks slack.Blocks
require.NoError(t, unmarshal(ctx, []interface{}{map[string]interface{}{"type": "divider"}}, &blocks))
})
t.Run("unmarshal fails on MarshalJSON", func(t *testing.T) {
t.Parallel()
ctx := context.New(config.Project{})
ctx.Version = testVersion
ctx := testctx.New(testctx.WithVersion(testVersion))
var blocks slack.Blocks
require.Error(t, unmarshal(ctx, []interface{}{map[string]interface{}{"type": func() {}}}, &blocks))
})
t.Run("unmarshal happy to resolve template", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(goodTemplateSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
var blocks slack.Blocks
require.NoError(t, unmarshal(ctx, ctx.Config.Announce.Slack.Blocks, &blocks))
require.Len(t, blocks.BlockSet, 1)
header, ok := blocks.BlockSet[0].(*slack.HeaderBlock)
require.True(t, ok)
@@ -184,12 +153,9 @@ func TestUnmarshall(t *testing.T) {
t.Run("unmarshal fails on resolve template", func(t *testing.T) {
t.Parallel()
var project config.Project
require.NoError(t, yaml.Unmarshal(badTemplateSlackConf(), &project))
ctx := context.New(project)
ctx.Version = testVersion
ctx := testctx.NewWithCfg(project, testctx.WithVersion(testVersion))
var blocks slack.Blocks
require.Error(t, unmarshal(ctx, ctx.Config.Announce.Slack.Blocks, &blocks))
})

View File

@@ -4,8 +4,8 @@ import (
"strconv"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -15,11 +15,11 @@ func TestStringer(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
SMTP: config.SMTP{
Enabled: true,
@@ -31,7 +31,7 @@ func TestSkip(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
SMTP: config.SMTP{
Enabled: true,

View File

@@ -9,6 +9,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/yaml"
"github.com/goreleaser/goreleaser/pkg/config"
@@ -31,13 +32,11 @@ func TestRunPipeMissingInfo(t *testing.T) {
pipe.Skip("no summary nor description were provided"): {},
} {
t.Run(fmt.Sprintf("testing if %v happens", eerr), func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Snapcrafts: []config.Snapcraft{
snap,
},
ctx := testctx.NewWithCfg(config.Project{
Snapcrafts: []config.Snapcraft{
snap,
},
}
})
require.Equal(t, eerr, Pipe{}.Run(ctx))
})
}
@@ -48,7 +47,7 @@ func TestRunPipe(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -77,9 +76,7 @@ func TestRunPipe(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"))
addBinaries(t, ctx, "bar", filepath.Join(dist, "bar"))
require.NoError(t, Pipe{}.Run(ctx))
@@ -92,7 +89,7 @@ func TestBadTemolate(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -103,9 +100,7 @@ func TestBadTemolate(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"))
t.Run("description", func(t *testing.T) {
@@ -126,7 +121,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -138,9 +133,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
}
@@ -150,7 +143,7 @@ func TestRunPipeWithName(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -165,9 +158,7 @@ func TestRunPipeWithName(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
require.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
@@ -186,7 +177,7 @@ func TestRunPipeMetadata(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -259,9 +250,7 @@ func TestRunPipeMetadata(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
require.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
@@ -331,7 +320,7 @@ func TestNoSnapcraftInPath(t *testing.T) {
require.NoError(t, os.Setenv("PATH", path))
}()
require.NoError(t, os.Setenv("PATH", ""))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapcrafts: []config.Snapcraft{
{
Summary: "dummy",
@@ -347,7 +336,7 @@ func TestRunNoArguments(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -365,9 +354,7 @@ func TestRunNoArguments(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
require.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
@@ -383,7 +370,7 @@ func TestCompleter(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -402,9 +389,7 @@ func TestCompleter(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
addBinaries(t, ctx, "bar", dist)
require.NoError(t, Pipe{}.Run(ctx))
@@ -422,7 +407,7 @@ func TestCommand(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -441,9 +426,7 @@ func TestCommand(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
require.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
@@ -459,7 +442,7 @@ func TestExtraFile(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
@@ -481,9 +464,7 @@ func TestExtraFile(t *testing.T) {
ChannelTemplates: []string{"stable"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "1.2.3"
}, testctx.WithCurrentTag("v1.2.3"), testctx.WithVersion("1.2.3"))
addBinaries(t, ctx, "foo", dist)
require.NoError(t, Pipe{}.Run(ctx))
@@ -494,7 +475,7 @@ func TestExtraFile(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{{ID: "foo"}},
Snapcrafts: []config.Snapcraft{{}},
})
@@ -506,7 +487,7 @@ func TestDefault(t *testing.T) {
}
func TestDefaultGradeTmpl(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"Grade=devel"},
Builds: []config.Build{{ID: "foo"}},
Snapcrafts: []config.Snapcraft{{Grade: "{{.Env.Grade}}"}},
@@ -519,7 +500,7 @@ func TestDefaultGradeTmpl(t *testing.T) {
}
func TestDefaultGradeTmplError(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{{ID: "foo"}},
Snapcrafts: []config.Snapcraft{{Grade: "{{.Env.Grade}}"}},
})
@@ -527,7 +508,7 @@ func TestDefaultGradeTmplError(t *testing.T) {
}
func TestPublish(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: "nope.snap",
@@ -543,8 +524,7 @@ func TestPublish(t *testing.T) {
}
func TestPublishSkip(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipPublish = true
ctx := testctx.New(testctx.SkipPublish)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: "nope.snap",
@@ -559,7 +539,7 @@ func TestPublishSkip(t *testing.T) {
}
func TestDefaultSet(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapcrafts: []config.Snapcraft{
{
ID: "devel",
@@ -580,8 +560,8 @@ func TestDefaultSet(t *testing.T) {
}
func Test_processChannelsTemplates(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ctx := testctx.NewWithCfg(
config.Project{
Builds: []config.Build{
{
ID: "default",
@@ -596,26 +576,16 @@ func Test_processChannelsTemplates(t *testing.T) {
},
},
},
Env: []string{"FOO=123"},
},
}
ctx.SkipPublish = true
ctx.Env = map[string]string{
"FOO": "123",
}
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.0",
Commit: "a1b2c3d4",
}
ctx.Semver = context.Semver{
Major: 1,
Minor: 0,
Patch: 0,
}
testctx.SkipPublish,
testctx.WithCommit("a1b2c3d4"),
testctx.WithCurrentTag("v1.0.0"),
testctx.WithSemver(1, 0, 0, ""),
testctx.WithVersion("1.0.0"),
)
require.NoError(t, Pipe{}.Default(ctx))
snap := ctx.Config.Snapcrafts[0]
require.Equal(t, "mybin", snap.Name)
@@ -679,18 +649,16 @@ func addBinaries(t *testing.T, ctx *context.Context, name, dist string) {
}
func TestSeveralSnapssWithTheSameID(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Snapcrafts: []config.Snapcraft{
{
ID: "a",
},
{
ID: "a",
},
ctx := testctx.NewWithCfg(config.Project{
Snapcrafts: []config.Snapcraft{
{
ID: "a",
},
{
ID: "a",
},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), "found 2 snapcrafts with the ID 'a', please fix your config")
}
@@ -716,11 +684,11 @@ func Test_isValidArch(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapcrafts: []config.Snapcraft{
{},
},

View File

@@ -3,9 +3,9 @@ package snapshot
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -14,29 +14,25 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Snapshot: config.Snapshot{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Snapshot: config.Snapshot{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "{{ .Version }}-SNAPSHOT-{{ .ShortCommit }}", ctx.Config.Snapshot.NameTemplate)
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Snapshot: config.Snapshot{
NameTemplate: "snap",
},
ctx := testctx.NewWithCfg(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "snap",
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "snap", ctx.Config.Snapshot.NameTemplate)
}
func TestSnapshotInvalidNametemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{.ShortCommit}{{{sss}}}",
},
@@ -45,34 +41,31 @@ func TestSnapshotInvalidNametemplate(t *testing.T) {
}
func TestSnapshotEmptyFinalName(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{ .Commit }}",
},
})
ctx.Git.CurrentTag = "v1.2.3"
}, testctx.WithCurrentTag("v1.2.3"))
require.EqualError(t, Pipe{}.Run(ctx), "empty snapshot name")
}
func TestSnapshot(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{ incpatch .Tag }}",
},
})
ctx.Git.CurrentTag = "v1.2.3"
}, testctx.WithCurrentTag("v1.2.3"))
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v1.2.4", ctx.Version)
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
ctx := testctx.New(testctx.Snapshot)
require.False(t, Pipe{}.Skip(ctx))
})
}

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -30,21 +31,23 @@ func TestArchive(t *testing.T) {
require.NoError(t, os.MkdirAll("subfolder", 0o755))
require.NoError(t, os.WriteFile("subfolder/file.md", []byte("a file within a folder, added later"), 0o655))
ctx := context.New(config.Project{
ProjectName: "foo",
Dist: "dist",
Source: config.Source{
Format: format,
Enabled: true,
PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
Files: []config.File{
{Source: "*.txt"},
{Source: "subfolder/*"},
ctx := testctx.NewWithCfg(
config.Project{
ProjectName: "foo",
Dist: "dist",
Source: config.Source{
Format: format,
Enabled: true,
PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
Files: []config.File{
{Source: "*.txt"},
{Source: "subfolder/*"},
},
},
},
})
ctx.Git.FullCommit = "HEAD"
ctx.Version = "1.0.0"
testctx.WithGitInfo(context.GitInfo{FullCommit: "HEAD"}),
testctx.WithVersion("1.0.0"),
)
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Run(ctx))
@@ -80,7 +83,7 @@ func TestArchive(t *testing.T) {
}
func TestInvalidFormat(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
ProjectName: "foo",
Source: config.Source{
@@ -94,7 +97,7 @@ func TestInvalidFormat(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.Source{
NameTemplate: "{{ .ProjectName }}-{{ .Version }}",
@@ -103,7 +106,7 @@ func TestDefault(t *testing.T) {
}
func TestInvalidNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Source: config.Source{
Enabled: true,
NameTemplate: "{{ .foo }-asdda",
@@ -121,7 +124,7 @@ func TestInvalidInvalidFileTemplate(t *testing.T) {
testlib.GitAdd(t)
testlib.GitCommit(t, "feat: first")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Dist: "dist",
Source: config.Source{
@@ -139,7 +142,7 @@ func TestInvalidInvalidFileTemplate(t *testing.T) {
}
func TestInvalidPrefixTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
Source: config.Source{
Enabled: true,
@@ -151,16 +154,16 @@ func TestInvalidPrefixTemplate(t *testing.T) {
}
func TestDisabled(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Source: config.Source{
Enabled: true,
},

View File

@@ -3,8 +3,8 @@ package teams
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,13 +13,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Teams.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,
@@ -31,7 +31,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,
@@ -44,11 +44,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,

View File

@@ -3,9 +3,9 @@ package telegram
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -14,14 +14,14 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Telegram.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
t.Run("message", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Telegram: config.Telegram{
MessageTemplate: "{{ .Foo }",
@@ -31,7 +31,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
})
t.Run("chatid", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Telegram: config.Telegram{
MessageTemplate: "test",
@@ -42,7 +42,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
})
t.Run("chatid not int", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"CHAT_ID=test"},
Announce: config.Announce{
Telegram: config.Telegram{
@@ -56,7 +56,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"CHAT_ID=10"},
Announce: config.Announce{
Telegram: config.Telegram{
@@ -70,11 +70,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Telegram: config.Telegram{
Enabled: true,

View File

@@ -3,8 +3,8 @@ package twitter
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -13,13 +13,13 @@ func TestStringer(t *testing.T) {
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Twitter.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Twitter: config.Twitter{
MessageTemplate: "{{ .Foo }",
@@ -30,7 +30,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Twitter: config.Twitter{},
},
@@ -41,11 +41,11 @@ func TestAnnounceMissingEnv(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Twitter: config.Twitter{
Enabled: true,

View File

@@ -10,9 +10,9 @@ import (
"time"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -22,14 +22,12 @@ func TestDescription(t *testing.T) {
func TestDefault(t *testing.T) {
t.Run("empty", func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.UniversalBinary{
ID: "proj",
@@ -39,14 +37,12 @@ func TestDefault(t *testing.T) {
})
t.Run("given ids", func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{IDs: []string{"foo"}},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{IDs: []string{"foo"}},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.UniversalBinary{
ID: "proj",
@@ -56,14 +52,12 @@ func TestDefault(t *testing.T) {
})
t.Run("given id", func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{ID: "foo"},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{ID: "foo"},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.UniversalBinary{
ID: "foo",
@@ -73,14 +67,12 @@ func TestDefault(t *testing.T) {
})
t.Run("given name", func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{NameTemplate: "foo"},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{NameTemplate: "foo"},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.UniversalBinary{
ID: "proj",
@@ -90,26 +82,24 @@ func TestDefault(t *testing.T) {
})
t.Run("duplicated ids", func(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{ID: "foo"},
{ID: "foo"},
},
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
UniversalBinaries: []config.UniversalBinary{
{ID: "foo"},
{ID: "foo"},
},
}
})
require.EqualError(t, Pipe{}.Default(ctx), `found 2 universal_binaries with the ID 'foo', please fix your config`)
})
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
UniversalBinaries: []config.UniversalBinary{{}},
})
require.False(t, Pipe{}.Skip(ctx))
@@ -138,9 +128,9 @@ func TestRun(t *testing.T) {
},
},
}
ctx1 := context.New(cfg)
ctx1 := testctx.NewWithCfg(cfg)
ctx2 := context.New(config.Project{
ctx2 := testctx.NewWithCfg(config.Project{
Dist: dist,
UniversalBinaries: []config.UniversalBinary{
{
@@ -151,7 +141,7 @@ func TestRun(t *testing.T) {
},
})
ctx3 := context.New(config.Project{
ctx3 := testctx.NewWithCfg(config.Project{
Dist: dist,
UniversalBinaries: []config.UniversalBinary{
{
@@ -162,7 +152,7 @@ func TestRun(t *testing.T) {
},
})
ctx4 := context.New(config.Project{
ctx4 := testctx.NewWithCfg(config.Project{
Dist: dist,
UniversalBinaries: []config.UniversalBinary{
{
@@ -173,7 +163,7 @@ func TestRun(t *testing.T) {
},
})
ctx5 := context.New(config.Project{
ctx5 := testctx.NewWithCfg(config.Project{
Dist: dist,
UniversalBinaries: []config.UniversalBinary{
{
@@ -193,7 +183,7 @@ func TestRun(t *testing.T) {
},
})
ctx6 := context.New(config.Project{
ctx6 := testctx.NewWithCfg(config.Project{
Dist: dist,
UniversalBinaries: []config.UniversalBinary{
{
@@ -268,7 +258,7 @@ func TestRun(t *testing.T) {
})
t.Run("bad template", func(t *testing.T) {
testlib.RequireTemplateError(t, Pipe{}.Run(context.New(config.Project{
testlib.RequireTemplateError(t, Pipe{}.Run(testctx.NewWithCfg(config.Project{
UniversalBinaries: []config.UniversalBinary{
{
NameTemplate: "{{.Name}",

View File

@@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -100,7 +101,7 @@ func TestRunPipe_ModeBinary(t *testing.T) {
w.WriteHeader(http.StatusCreated)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -119,14 +120,12 @@ func TestRunPipe_ModeBinary(t *testing.T) {
Username: "productionuser",
},
},
Archives: []config.Archive{
{},
Archives: []config.Archive{{}},
Env: []string{
"UPLOAD_PRODUCTION-US_SECRET=deployuser-secret",
"UPLOAD_PRODUCTION-EU_SECRET=productionuser-apikey",
},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION-US_SECRET": "deployuser-secret",
"UPLOAD_PRODUCTION-EU_SECRET": "productionuser-apikey",
}
for _, goos := range []string{"linux", "darwin"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
@@ -152,7 +151,7 @@ func TestRunPipe_ModeArchive(t *testing.T) {
require.NoError(t, err)
require.NoError(t, debfile.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "goreleaser",
Dist: folder,
Uploads: []config.Upload{
@@ -164,14 +163,9 @@ func TestRunPipe_ModeArchive(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Version = "1.0.0"
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
}, testctx.WithVersion("1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -244,7 +238,7 @@ func TestRunPipe_ModeBinary_CustomArtifactName(t *testing.T) {
w.WriteHeader(http.StatusCreated)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -257,13 +251,9 @@ func TestRunPipe_ModeBinary_CustomArtifactName(t *testing.T) {
CustomArtifactName: true,
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION-US_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION-US_SECRET": "deployuser-secret",
}
for _, goos := range []string{"linux", "darwin"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
@@ -289,7 +279,7 @@ func TestRunPipe_ModeArchive_CustomArtifactName(t *testing.T) {
require.NoError(t, err)
require.NoError(t, debfile.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "goreleaser",
Dist: folder,
Uploads: []config.Upload{
@@ -302,14 +292,11 @@ func TestRunPipe_ModeArchive_CustomArtifactName(t *testing.T) {
CustomArtifactName: true,
},
},
Archives: []config.Archive{
{},
Archives: []config.Archive{{}},
Env: []string{
"UPLOAD_PRODUCTION_SECRET=deployuser-secret",
},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Version = "1.0.0"
}, testctx.WithVersion("1.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -356,7 +343,7 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
require.NoError(t, err)
require.NoError(t, tarfile.Close())
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "goreleaser",
Dist: folder,
Uploads: []config.Upload{
@@ -368,11 +355,8 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
Username: "deployuser",
},
},
})
ctx.Version = "2.0.0"
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
}, testctx.WithVersion("2.0.0"))
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
@@ -388,7 +372,7 @@ func TestRunPipe_TargetTemplateError(t *testing.T) {
dist := filepath.Join(folder, "dist")
binPath := filepath.Join(dist, "mybin", "mybin")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -401,13 +385,9 @@ func TestRunPipe_TargetTemplateError(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -440,7 +420,7 @@ func TestRunPipe_BadCredentials(t *testing.T) {
w.WriteHeader(http.StatusUnauthorized)
})
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -452,13 +432,9 @@ func TestRunPipe_BadCredentials(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
@@ -485,13 +461,9 @@ func TestRunPipe_FileNotFound(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: "archivetest/dist/mybin/mybin",
@@ -512,7 +484,7 @@ func TestRunPipe_UnparsableTarget(t *testing.T) {
d1 := []byte("hello\ngo\n")
require.NoError(t, os.WriteFile(binPath, d1, 0o666))
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -527,6 +499,7 @@ func TestRunPipe_UnparsableTarget(t *testing.T) {
Archives: []config.Archive{
{},
},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
@@ -549,7 +522,7 @@ func TestRunPipe_DirUpload(t *testing.T) {
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
binPath := filepath.Join(dist, "mybin")
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "mybin",
Dist: dist,
Uploads: []config.Upload{
@@ -561,13 +534,9 @@ func TestRunPipe_DirUpload(t *testing.T) {
Username: "deployuser",
},
},
Archives: []config.Archive{
{},
},
Archives: []config.Archive{{}},
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
ctx.Env = map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: filepath.Dir(binPath),
@@ -584,45 +553,37 @@ func TestDescription(t *testing.T) {
}
func TestPutsWithoutTarget(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Username: "deployuser",
},
},
}
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
require.True(t, pipe.IsSkip(Pipe{}.Publish(ctx)))
}
func TestPutsWithoutUsername(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
},
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
},
},
}
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
require.True(t, pipe.IsSkip(Pipe{}.Publish(ctx)))
}
func TestPutsWithoutName(t *testing.T) {
require.True(t, pipe.IsSkip(Pipe{}.Publish(context.New(config.Project{
require.True(t, pipe.IsSkip(Pipe{}.Publish(testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
@@ -634,7 +595,7 @@ func TestPutsWithoutName(t *testing.T) {
}
func TestPutsWithoutSecret(t *testing.T) {
require.True(t, pipe.IsSkip(Pipe{}.Publish(context.New(config.Project{
require.True(t, pipe.IsSkip(Pipe{}.Publish(testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
@@ -647,37 +608,31 @@ func TestPutsWithoutSecret(t *testing.T) {
}
func TestPutsWithInvalidMode(t *testing.T) {
ctx := &context.Context{
Env: map[string]string{
"UPLOAD_PRODUCTION_SECRET": "deployuser-secret",
},
Config: config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Mode: "does-not-exists",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPut,
Name: "production",
Mode: "does-not-exists",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
},
}
Env: []string{"UPLOAD_PRODUCTION_SECRET=deployuser-secret"},
})
require.Error(t, Pipe{}.Publish(ctx))
}
func TestDefault(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Uploads: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Name: "production",
Target: "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Username: "deployuser",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Uploads, 1)
upload := ctx.Config.Uploads[0]
@@ -686,26 +641,22 @@ func TestDefault(t *testing.T) {
}
func TestDefaultNoPuts(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Uploads: []config.Upload{},
},
}
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Empty(t, ctx.Config.Uploads)
}
func TestDefaultSet(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPost,
Mode: "custom",
},
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{
Method: h.MethodPost,
Mode: "custom",
},
},
}
})
require.NoError(t, Pipe{}.Default(ctx))
require.Len(t, ctx.Config.Uploads, 1)
upload := ctx.Config.Uploads[0]
@@ -715,11 +666,11 @@ func TestDefaultSet(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Uploads: []config.Upload{
{},
},

View File

@@ -10,8 +10,8 @@ import (
"testing"
"github.com/google/uuid"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
@@ -20,7 +20,7 @@ func TestStringer(t *testing.T) {
}
func TestNoEndpoint(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: config.Webhook{},
},
@@ -29,7 +29,7 @@ func TestNoEndpoint(t *testing.T) {
}
func TestMalformedEndpoint(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: config.Webhook{
EndpointURL: "httxxx://example.com",
@@ -40,7 +40,7 @@ func TestMalformedEndpoint(t *testing.T) {
}
func TestAnnounceInvalidMessageTemplate(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: config.Webhook{
EndpointURL: "https://example.com/webhook",
@@ -76,7 +76,7 @@ func TestAnnounceWebhook(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
@@ -106,7 +106,7 @@ func TestAnnounceTLSWebhook(t *testing.T) {
}))
defer srv.Close()
fmt.Println(srv.URL)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
@@ -134,7 +134,7 @@ func TestAnnounceTLSCheckCertWebhook(t *testing.T) {
}))
defer srv.Close()
fmt.Println(srv.URL)
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
@@ -170,7 +170,7 @@ func TestAnnounceBasicAuthWebhook(t *testing.T) {
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
@@ -206,7 +206,7 @@ func TestAnnounceAdditionalHeadersWebhook(t *testing.T) {
}))
defer srv.Close()
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "webhook-test",
Announce: config.Announce{
Webhook: config.Webhook{
@@ -223,11 +223,11 @@ func TestAnnounceAdditionalHeadersWebhook(t *testing.T) {
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
require.True(t, Pipe{}.Skip(testctx.New()))
})
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Announce: config.Announce{
Webhook: config.Webhook{
Enabled: true,

View File

@@ -5,20 +5,19 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/shell"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/stretchr/testify/require"
)
func TestRunCommand(t *testing.T) {
t.Run("simple", func(t *testing.T) {
require.NoError(t, shell.Run(context.New(config.Project{}), "", []string{"echo", "oi"}, []string{}, false))
require.NoError(t, shell.Run(testctx.New(), "", []string{"echo", "oi"}, []string{}, false))
})
t.Run("cmd failed", func(t *testing.T) {
require.EqualError(
t,
shell.Run(context.New(config.Project{}), "", []string{"sh", "-c", "exit 1"}, []string{}, false),
shell.Run(testctx.New(), "", []string{"sh", "-c", "exit 1"}, []string{}, false),
`failed to run 'sh -c exit 1': exit status 1`,
)
})
@@ -26,14 +25,14 @@ func TestRunCommand(t *testing.T) {
t.Run("cmd with output", func(t *testing.T) {
require.EqualError(
t,
shell.Run(context.New(config.Project{}), "", []string{"sh", "-c", `echo something; exit 1`}, []string{}, true),
shell.Run(testctx.New(), "", []string{"sh", "-c", `echo something; exit 1`}, []string{}, true),
`failed to run 'sh -c echo something; exit 1': exit status 1`,
)
})
t.Run("with env and dir", func(t *testing.T) {
dir := t.TempDir()
require.NoError(t, shell.Run(context.New(config.Project{}), dir, []string{"sh", "-c", "touch $FOO"}, []string{"FOO=bar"}, false))
require.NoError(t, shell.Run(testctx.New(), dir, []string{"sh", "-c", "touch $FOO"}, []string{"FOO=bar"}, false))
require.FileExists(t, filepath.Join(dir, "bar"))
})
}

141
internal/testctx/testctx.go Normal file
View File

@@ -0,0 +1,141 @@
// Package testctx provides a test context to be used in unit tests.
package testctx
import (
"time"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Opt is an option for a test context.
type Opt func(ctx *context.Context)
func GitHubTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitHub)(ctx)
WithToken("githubtoken")(ctx)
}
func GitLabTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitLab)(ctx)
WithToken("gitlabtoken")(ctx)
}
func GiteaTokenType(ctx *context.Context) {
WithTokenType(context.TokenTypeGitea)(ctx)
WithToken("giteatoken")(ctx)
}
func WithTokenType(t context.TokenType) Opt {
return func(ctx *context.Context) {
ctx.TokenType = t
}
}
func WithToken(t string) Opt {
return func(ctx *context.Context) {
ctx.Token = t
}
}
func WithVersion(v string) Opt {
return func(ctx *context.Context) {
ctx.Version = v
}
}
func WithSemver(major, minor, patch uint64, prerelease string) Opt {
return func(ctx *context.Context) {
ctx.Semver = context.Semver{
Major: major,
Minor: minor,
Patch: patch,
Prerelease: prerelease,
}
}
}
func WithGitInfo(git context.GitInfo) Opt {
return func(ctx *context.Context) {
ctx.Git = git
}
}
func WithCurrentTag(tag string) Opt {
return func(ctx *context.Context) {
ctx.Git.CurrentTag = tag
}
}
func WithCommit(commig string) Opt {
return func(ctx *context.Context) {
ctx.Git.Commit = commig
}
}
func WithCommitDate(d time.Time) Opt {
return func(ctx *context.Context) {
ctx.Git.CommitDate = d
}
}
func WithPreviousTag(tag string) Opt {
return func(ctx *context.Context) {
ctx.Git.PreviousTag = tag
}
}
func WithEnv(env map[string]string) Opt {
return func(ctx *context.Context) {
ctx.Env = env
}
}
func WithDate(t time.Time) Opt {
return func(ctx *context.Context) {
ctx.Date = t
}
}
func WithFakeRuntime(ctx *context.Context) {
ctx.Runtime = context.Runtime{
Goos: "fakeos",
Goarch: "fakearch",
}
}
func SkipPublish(ctx *context.Context) {
ctx.SkipPublish = true
}
func SkipAnnounce(ctx *context.Context) {
ctx.SkipAnnounce = true
}
func SkipDocker(ctx *context.Context) {
ctx.SkipDocker = true
}
func SkipValidate(ctx *context.Context) {
ctx.SkipValidate = true
}
func Snapshot(ctx *context.Context) {
ctx.Snapshot = true
}
func SkipSign(ctx *context.Context) {
ctx.SkipSign = true
}
func NewWithCfg(c config.Project, opts ...Opt) *context.Context {
ctx := context.New(c)
for _, opt := range opts {
opt(ctx)
}
return ctx
}
func New(opts ...Opt) *context.Context {
return NewWithCfg(config.Project{}, opts...)
}

View File

@@ -8,6 +8,7 @@ import (
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -15,30 +16,30 @@ import (
func TestWithArtifact(t *testing.T) {
t.Parallel()
ctx := context.New(config.Project{
ProjectName: "proj",
})
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.Env = map[string]string{
"FOO": "bar",
"MULTILINE": "something with\nmultiple lines\nremove this\nto test things",
}
ctx.Version = "1.2.3"
ctx.Git.PreviousTag = "v1.2.2"
ctx.Git.CurrentTag = "v1.2.3"
ctx.Semver = context.Semver{
Major: 1,
Minor: 2,
Patch: 3,
}
ctx.Git.Branch = "test-branch"
ctx.Git.Commit = "commit"
ctx.Git.FullCommit = "fullcommit"
ctx.Git.ShortCommit = "shortcommit"
ctx.Git.TagSubject = "awesome release"
ctx.Git.TagContents = "awesome release\n\nanother line"
ctx.Git.TagBody = "another line"
ctx.ReleaseNotes = "test release notes"
ctx := testctx.NewWithCfg(
config.Project{ProjectName: "proj"},
testctx.WithVersion("1.2.3"),
testctx.WithGitInfo(context.GitInfo{
PreviousTag: "v1.2.2",
CurrentTag: "v1.2.3",
Branch: "test-branch",
Commit: "commit",
FullCommit: "fullcommit",
ShortCommit: "shortcommit",
TagSubject: "awesome release",
TagContents: "awesome release\n\nanother line",
TagBody: "another line",
}),
testctx.WithEnv(map[string]string{
"FOO": "bar",
"MULTILINE": "something with\nmultiple lines\nremove this\nto test things",
}),
testctx.WithSemver(1, 2, 3, ""),
func(ctx *context.Context) {
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.ReleaseNotes = "test release notes"
},
)
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
@@ -140,11 +141,10 @@ func TestEnv(t *testing.T) {
out: "",
},
}
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
ctx := testctx.New(
testctx.WithEnv(map[string]string{"FOO": "BAR"}),
testctx.WithCurrentTag("v1.2.3"),
)
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
out, _ := New(ctx).Apply(tC.in)
@@ -154,11 +154,10 @@ func TestEnv(t *testing.T) {
}
func TestWithEnv(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
ctx := testctx.New(
testctx.WithEnv(map[string]string{"FOO": "BAR"}),
testctx.WithCurrentTag("v1.2.3"),
)
tpl := New(ctx).WithEnvS([]string{
"FOO=foo",
"BAR=bar",
@@ -177,7 +176,7 @@ func TestWithEnv(t *testing.T) {
}
func TestFuncMap(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "proj",
Env: []string{
"FOO=bar",
@@ -273,7 +272,7 @@ func TestFuncMap(t *testing.T) {
}
func TestApplySingleEnvOnly(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"FOO=value",
"BAR=another",
@@ -339,22 +338,20 @@ func TestApplySingleEnvOnly(t *testing.T) {
}
func TestInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.1.1"
ctx := testctx.New(testctx.WithCurrentTag("v1.1.1"))
_, err := New(ctx).Apply("{{{.Foo}")
require.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
}
func TestEnvNotFound(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.2.4"
ctx := testctx.New(testctx.WithCurrentTag("v1.2.4"))
result, err := New(ctx).Apply("{{.Env.FOO}}")
require.Empty(t, result)
require.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
}
func TestWithExtraFields(t *testing.T) {
ctx := context.New(config.Project{})
ctx := testctx.New()
out, _ := New(ctx).WithExtraFields(Fields{
"MyCustomField": "foo",
}).Apply("{{ .MyCustomField }}")
@@ -369,7 +366,7 @@ func TestBool(t *testing.T) {
"TRUE",
} {
t.Run(v, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=" + v},
})
b, err := New(ctx).Bool("{{.Env.FOO}}")
@@ -386,7 +383,7 @@ func TestBool(t *testing.T) {
"yada yada",
} {
t.Run(v, func(t *testing.T) {
ctx := context.New(config.Project{
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"FOO=" + v},
})
b, err := New(ctx).Bool("{{.Env.FOO}}")