1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-07 13:31:37 +02:00

fix: improve gpg.program detection, add more tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos A Becker 2023-03-29 13:23:23 -03:00
parent 9a97aaae99
commit f4fad65471
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
3 changed files with 37 additions and 10 deletions

View File

@ -339,13 +339,13 @@ func getFromEnv(s string) func() ([]string, error) {
}
// GetGPGProgram returns the user set GPG path or "gpg" if nothing is set
func GetGPGProgram(ctx *context.Context) (string, error) {
path, err := git.Clean(git.Run(ctx, "config", "gpg.program"))
func GetGPGProgram(ctx *context.Context) string {
path, _ := git.Clean(git.Run(ctx, "config", "gpg.program"))
// if config not set assume default
if len(path) == 0 {
path = "gpg"
}
return path, err
return path
}

View File

@ -12,10 +12,10 @@ import (
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/pipe/git"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
@ -36,11 +36,13 @@ func (Pipe) Dependencies(ctx *context.Context) []string {
return cmds
}
const defaultGpg = "gpg"
// Default sets the Pipes defaults.
func (Pipe) Default(ctx *context.Context) error {
gpgPath, err := git.GetGPGProgram(ctx)
if err != nil {
return err
gpgPath, _ := git.Clean(git.Run(ctx, "config", "gpg.program"))
if gpgPath == "" {
gpgPath = defaultGpg
}
ids := ids.New("signs")

View File

@ -15,7 +15,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/goreleaser/goreleaser/internal/artifact"
"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"
"github.com/stretchr/testify/require"
@ -51,17 +53,34 @@ func TestDescription(t *testing.T) {
}
func TestSignDefault(t *testing.T) {
_ = testlib.Mktmp(t)
testlib.GitInit(t)
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") // assumes git config gpg.program is not set
setGpg(t, ctx, "") // force empty gpg.program
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Signs[0].Cmd, "gpg")
require.Equal(t, ctx.Config.Signs[0].Signature, "${artifact}.sig")
require.Equal(t, ctx.Config.Signs[0].Args, []string{"--output", "$signature", "--detach-sig", "$artifact"})
require.Equal(t, ctx.Config.Signs[0].Artifacts, "none")
}
func TestDefaultGpgFromGitConfig(t *testing.T) {
_ = testlib.Mktmp(t)
testlib.GitInit(t)
ctx := testctx.NewWithCfg(config.Project{
Signs: []config.Sign{{}},
})
setGpg(t, ctx, "not-really-gpg")
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Signs[0].Cmd, "not-really-gpg")
}
func TestSignDisabled(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{Signs: []config.Sign{{Artifacts: "none"}}})
err := Pipe{}.Run(ctx)
@ -740,3 +759,9 @@ func TestDependencies(t *testing.T) {
})
require.Equal(t, []string{"cosign", "gpg2"}, Pipe{}.Dependencies(ctx))
}
func setGpg(tb testing.TB, ctx *context.Context, p string) {
tb.Helper()
_, err := git.Run(ctx, "config", "--local", "--add", "gpg.program", p)
require.NoError(tb, err)
}