1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00
goreleaser/internal/pipe/defaults/defaults_test.go
Steve Azzopardi 68ff8e996f
feat: support templates for scm urls (#2465)
Background
---
When a git repository is hosted in multiple GitLab instances the
`.goreleaser.yml` needs to take in consideration both APIs endpoints. At
the moment it defaults to GitLab.com and you can override it with
`gitlab_urls` however this forces you to only support 1 GitLab instance.

We need this for
https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/14122
where we have a tool that is developed on GitLab.com but then mirrored
to an internal GitLab instance since we need it to operate GitLab.com
even when it's down.

Solution
---
Support templates like `{{ .Env.CI_SERVER_URL }}` for the
`gitlab_urls`, `github_urls`  and `gitea_urls` so it can use environment
variables and the same `.goreleaser` file can be used in multiple SCM
instances.

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2021-09-09 01:42:13 +00:00

167 lines
4.1 KiB
Go

package defaults
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}
func TestFillBasicData(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{},
}
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
require.NotEmpty(t, ctx.Config.Builds)
require.Equal(t, "goreleaser", ctx.Config.Builds[0].Binary)
require.Equal(t, ".", ctx.Config.Builds[0].Main)
require.Contains(t, ctx.Config.Builds[0].Goos, "darwin")
require.Contains(t, ctx.Config.Builds[0].Goos, "linux")
require.Contains(t, ctx.Config.Builds[0].Goarch, "386")
require.Contains(t, ctx.Config.Builds[0].Goarch, "amd64")
require.Equal(t, "tar.gz", ctx.Config.Archives[0].Format)
require.Empty(t, ctx.Config.Dockers)
require.Equal(t, "https://github.com", ctx.Config.GitHubURLs.Download)
require.NotEmpty(t, ctx.Config.Archives[0].NameTemplate)
require.NotEmpty(t, ctx.Config.Builds[0].Ldflags)
require.NotEmpty(t, ctx.Config.Archives[0].Files)
require.NotEmpty(t, ctx.Config.Dist)
}
func TestFillPartial(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
ctx := &context.Context{
Config: config.Project{
GitHubURLs: config.GitHubURLs{
Download: "https://github.company.com",
},
Dist: "disttt",
Release: config.Release{
GitHub: config.Repo{
Owner: "goreleaser",
Name: "test",
},
},
Archives: []config.Archive{
{
Files: []config.File{
{Source: "glob/*"},
},
},
},
Builds: []config.Build{
{
ID: "build1",
Binary: "testreleaser",
},
{Goos: []string{"linux"}},
{
ID: "build3",
Binary: "another",
Ignore: []config.IgnoredBuild{
{Goos: "darwin", Goarch: "amd64"},
},
},
},
Dockers: []config.Docker{
{
ImageTemplates: []string{"a/b"},
},
},
Brews: []config.Homebrew{
{
Description: "foo",
},
},
},
}
require.NoError(t, Pipe{}.Run(ctx))
require.Len(t, ctx.Config.Archives[0].Files, 1)
require.Equal(t, `bin.install "test"`, ctx.Config.Brews[0].Install)
require.NotEmpty(t, ctx.Config.Dockers[0].Goos)
require.NotEmpty(t, ctx.Config.Dockers[0].Goarch)
require.NotEmpty(t, ctx.Config.Dockers[0].Dockerfile)
require.Empty(t, ctx.Config.Dockers[0].Goarm)
require.Equal(t, "disttt", ctx.Config.Dist)
require.NotEqual(t, "https://github.com", ctx.Config.GitHubURLs.Download)
ctx = &context.Context{
TokenType: context.TokenTypeGitea,
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
},
}
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "https://gitea.com", ctx.Config.GiteaURLs.Download)
}
func TestGiteaTemplateDownloadURL(t *testing.T) {
tests := []struct {
name string
apiURL string
wantErr bool
}{
{
name: "string_url",
apiURL: "https://gitea.com/api/v1",
},
{
name: "download_url_template",
apiURL: "{{ .Env.GORELEASER_TEST_GITEA_URLS_API }}",
},
{
name: "download_url_template_invalid_value",
apiURL: "{{ .Env.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
apiURL: "{{.dddddddddd",
wantErr: true,
},
}
for _, tt := range tests {
ctx := &context.Context{
TokenType: context.TokenTypeGitea,
Env: context.Env{
"GORELEASER_TEST_GITEA_URLS_API": "https://gitea.com/api/v1",
},
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: tt.apiURL,
},
},
}
err := Pipe{}.Run(ctx)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, "https://gitea.com", ctx.Config.GiteaURLs.Download)
}
}