Skip to content

Commit

Permalink
fix: improve gpg.program detection, add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Mar 29, 2023
1 parent 9a97aaa commit f4fad65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions internal/pipe/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions internal/pipe/sign/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
31 changes: 28 additions & 3 deletions internal/pipe/sign/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

1 comment on commit f4fad65

@caarlos0
Copy link
Member Author

@caarlos0 caarlos0 commented on f4fad65 Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refs #3891

Please sign in to comment.