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:
parent
9a97aaae99
commit
f4fad65471
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user