Repository: goreleaser/goreleaser
Pull request #1430 (2020-04-09 15:54:37)

feat(pipe/build): Report hook error in addition to stdout
This helps in situations where hook couldn't run at all, e.g. because of insufficient permissions.

Previously such failure would only be reported as empty stdout/stderr output - this allows exposure of the real root cause.

This PR is currently part of https://github.com/goreleaser/goreleaser/pull/1414 but I think the change is small enough in scope it could be merged separately and the PR can then be rebased.


Patch:
diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go
index 0d2e8aeb..d95646e8 100644
--- a/internal/pipe/brew/brew.go
+++ b/internal/pipe/brew/brew.go
@@ -259,13 +259,6 @@ func dataFor(ctx *context.Context, cfg config.Homebrew, tokenType context.TokenT
 
 		if cfg.URLTemplate == "" {
 			switch tokenType {
-			case context.TokenTypeGitHub:
-				cfg.URLTemplate = fmt.Sprintf(
-					"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
-					ctx.Config.GitHubURLs.Download,
-					ctx.Config.Release.GitHub.Owner,
-					ctx.Config.Release.GitHub.Name,
-				)
 			case context.TokenTypeGitLab:
 				cfg.URLTemplate = fmt.Sprintf(
 					"%s/%s/%s/uploads/{{ .ArtifactUploadHash }}/{{ .ArtifactName }}",
@@ -274,8 +267,13 @@ func dataFor(ctx *context.Context, cfg config.Homebrew, tokenType context.TokenT
 					ctx.Config.Release.GitLab.Name,
 				)
 			default:
-				log.WithField("type", tokenType).Info("here")
-				return result, ErrTokenTypeNotImplementedForBrew
+				log.Warn("no url_template set and not github/gitlab/gitea token found, defaulting to github url template")
+				cfg.URLTemplate = fmt.Sprintf(
+					"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
+					ctx.Config.GitHubURLs.Download,
+					ctx.Config.Release.GitHub.Owner,
+					ctx.Config.Release.GitHub.Name,
+				)
 			}
 		}
 		url, err := tmpl.New(ctx).WithArtifact(artifact, map[string]string{}).Apply(cfg.URLTemplate)
diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go
index 1c2cc0fc..d1193fd6 100644
--- a/internal/pipe/build/build.go
+++ b/internal/pipe/build/build.go
@@ -162,7 +162,7 @@ func run(ctx *context.Context, command, env []string) error {
 	log.Debug("running")
 	if out, err := cmd.CombinedOutput(); err != nil {
 		log.WithError(err).Debug("failed")
-		return errors.New(string(out))
+		return errors.Wrapf(err, "%q", string(out))
 	}
 	return nil
 }


Test patch:
diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go
index ce466a34..73b2a479 100644
--- a/internal/pipe/brew/brew_test.go
+++ b/internal/pipe/brew/brew_test.go
@@ -660,42 +660,6 @@ func TestRunPipeNoUpload(t *testing.T) {
 	})
 }
 
-func TestRunTokenTypeNotImplementedForBrew(t *testing.T) {
-	folder, err := ioutil.TempDir("", "goreleasertest")
-	assert.NoError(t, err)
-	var ctx = context.New(config.Project{
-		Dist:        folder,
-		ProjectName: "foo",
-		Release:     config.Release{},
-		Brews: []config.Homebrew{
-			{
-				GitHub: config.Repo{
-					Owner: "test",
-					Name:  "test",
-				},
-			},
-		},
-	})
-	ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
-	var path = filepath.Join(folder, "whatever.tar.gz")
-	_, err = os.Create(path)
-	assert.NoError(t, err)
-	ctx.Artifacts.Add(&artifact.Artifact{
-		Name:   "bin",
-		Path:   path,
-		Goos:   "darwin",
-		Goarch: "amd64",
-		Type:   artifact.UploadableArchive,
-		Extra: map[string]interface{}{
-			"ID":     "foo",
-			"Format": "tar.gz",
-		},
-	})
-	client := &DummyClient{}
-	require.Equal(t, ErrTokenTypeNotImplementedForBrew, Pipe{}.Run(ctx))
-	testlib.AssertSkipped(t, doPublish(ctx, client))
-}
-
 func TestDefault(t *testing.T) {
 	_, back := testlib.Mktmp(t)
 	defer back()
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index a29c586f..d471c30e 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -182,14 +182,14 @@ func TestRunPipeFailingHooks(t *testing.T) {
 		ctx.Git.CurrentTag = "2.3.4"
 		ctx.Config.Builds[0].Hooks.Pre = "exit 1"
 		ctx.Config.Builds[0].Hooks.Post = "echo post"
-		assert.EqualError(t, Pipe{}.Run(ctx), `pre hook failed: `)
+		assert.EqualError(t, Pipe{}.Run(ctx), `pre hook failed: "": exec: "exit": executable file not found in $PATH`)
 	})
 	t.Run("post-hook", func(t *testing.T) {
 		var ctx = context.New(config)
 		ctx.Git.CurrentTag = "2.3.4"
 		ctx.Config.Builds[0].Hooks.Pre = "echo pre"
 		ctx.Config.Builds[0].Hooks.Post = "exit 1"
-		assert.EqualError(t, Pipe{}.Run(ctx), `post hook failed: `)
+		assert.EqualError(t, Pipe{}.Run(ctx), `post hook failed: "": exec: "exit": executable file not found in $PATH`)
 	})
 }