1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-20 03:59:26 +02:00
goreleaser/internal/pipe/project/project_test.go
Carlos Alexandro Becker 3cfefcc4ce
fix: decouple project_name guessing from the release pipe (#4335)
the release's defaults run before the project's does, so, usually the
github/gitlab/gitea names are set.

however, in some cases, the release's defaults might be skipped, in
which case they'll be empty.

this breaks things like `goreleaser changelog`, especially on non-go
repositories.

this pr tries to extract the project name from the git remote url in the
project's defaulter.

it might be possible now to move it to run before the release defaulter,
even.

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2023-10-14 18:59:07 -03:00

105 lines
2.6 KiB
Go

package project
import (
"os/exec"
"testing"
"github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
)
func TestCustomProjectName(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "foo",
Release: config.Release{
GitHub: config.Repo{
Owner: "bar",
Name: "bar",
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "foo", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{
Owner: "bar",
Name: "bar",
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "bar", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitLab: config.Repo{
Owner: "bar",
Name: "bar",
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "bar", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
Gitea: config.Repo{
Owner: "bar",
Name: "bar",
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "bar", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToGoModPath(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.New()
require.NoError(t, exec.Command("go", "mod", "init", "github.com/foo/bar").Run())
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "bar", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToGitURL(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.New()
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "bar", ctx.Config.ProjectName)
}
func TestEmptyProjectName_DefaultsToNonSCMGitURL(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.New()
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@myhost.local:bar.git")
require.EqualError(t, Pipe{}.Default(ctx), "couldn't guess project_name, please add it to your config")
}
func TestEmptyProjectNameAndRelease(t *testing.T) {
_ = testlib.Mktmp(t)
ctx := testctx.NewWithCfg(config.Project{
Release: config.Release{
GitHub: config.Repo{},
},
})
require.EqualError(t, Pipe{}.Default(ctx), "couldn't guess project_name, please add it to your config")
}