1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/internal/pipe/release/scm_test.go
Carlos Alexandro Becker f544c5ce69
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>
2023-03-02 00:01:11 -03:00

181 lines
4.4 KiB
Go

package release
import (
"testing"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
func TestSetupGitLab(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
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)
require.Equal(t, repo.Name, ctx.Config.Release.GitLab.Name)
})
t.Run("with templates", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GitLabURLs: config.GitLabURLs{
Download: "https://{{ .Env.OWNER }}/download",
},
Release: config.Release{
GitLab: config.Repo{
Owner: "{{.Env.OWNER}}",
Name: "{{.Env.NAME}}",
},
},
})
require.NoError(t, setupGitLab(ctx))
require.Equal(t, "bar", ctx.Config.Release.GitLab.Owner)
require.Equal(t, "foo", ctx.Config.Release.GitLab.Name)
require.Equal(t, "https://bar/download/bar/foo/-/releases/", ctx.ReleaseURL)
})
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Name: "foo",
Owner: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitLab(ctx))
})
t.Run("name", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Name: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitLab(ctx))
})
})
}
func TestSetupGitea(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
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 := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GiteaURLs: config.GiteaURLs{
Download: "https://{{ .Env.OWNER }}/download",
},
Release: config.Release{
Gitea: config.Repo{
Owner: "{{.Env.OWNER}}",
Name: "{{.Env.NAME}}",
},
},
})
require.NoError(t, setupGitea(ctx))
require.Equal(t, "bar", ctx.Config.Release.Gitea.Owner)
require.Equal(t, "foo", ctx.Config.Release.Gitea.Name)
require.Equal(t, "https://bar/download/bar/foo/releases/tag/", ctx.ReleaseURL)
})
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Name: "foo",
Owner: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitea(ctx))
})
t.Run("name", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Name: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitea(ctx))
})
})
}
func TestSetupGitHub(t *testing.T) {
t.Run("no repo", func(t *testing.T) {
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 := testctx.NewWithCfg(config.Project{
Env: []string{"NAME=foo", "OWNER=bar"},
GitHubURLs: config.GitHubURLs{
Download: "https://{{ .Env.OWNER }}/download",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "{{.Env.OWNER}}",
Name: "{{.Env.NAME}}",
},
},
})
require.NoError(t, setupGitHub(ctx))
require.Equal(t, "bar", ctx.Config.Release.GitHub.Owner)
require.Equal(t, "foo", ctx.Config.Release.GitHub.Name)
require.Equal(t, "https://bar/download/bar/foo/releases/tag/", ctx.ReleaseURL)
})
t.Run("with invalid templates", func(t *testing.T) {
t.Run("owner", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "foo",
Owner: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitHub(ctx))
})
t.Run("name", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Name: "{{.Env.NOPE}}",
},
},
})
require.Error(t, setupGitHub(ctx))
})
})
}