Repository: goreleaser/goreleaser
Pull request #1756 (2020-08-15 17:17:29)

fix: brew name template
closes https://github.com/goreleaser/goreleaser/issues/1755

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/g2as654e4](https://vercel.com/goreleaser/goreleaser/g2as654e4)  
✅ Preview: *In Progress*



Patch:
diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go
index 99d75bbf..f9f8dc42 100644
--- a/internal/pipe/brew/brew.go
+++ b/internal/pipe/brew/brew.go
@@ -175,6 +175,12 @@ func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
 		return ErrNoArchivesFound
 	}
 
+	name, err := tmpl.New(ctx).Apply(brew.Name)
+	if err != nil {
+		return err
+	}
+	brew.Name = name
+
 	content, err := buildFormula(ctx, brew, cl, archives)
 	if err != nil {
 		return err
diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go
index 2eb817c3..064ee8e6 100644
--- a/internal/pipe/build/build.go
+++ b/internal/pipe/build/build.go
@@ -3,7 +3,9 @@
 package build
 
 import (
+	"bytes"
 	"fmt"
+	"io"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -11,6 +13,7 @@ import (
 
 	"github.com/apex/log"
 	"github.com/goreleaser/goreleaser/internal/ids"
+	"github.com/goreleaser/goreleaser/internal/logext"
 	"github.com/goreleaser/goreleaser/internal/semerrgroup"
 	"github.com/goreleaser/goreleaser/internal/tmpl"
 	builders "github.com/goreleaser/goreleaser/pkg/build"
@@ -215,15 +218,18 @@ func extFor(target string, flags config.FlagArray) string {
 func run(ctx *context.Context, dir string, command, env []string) error {
 	/* #nosec */
 	var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
-	var log = log.WithField("env", env).WithField("cmd", command)
+	var entry = log.WithField("cmd", command)
 	cmd.Env = env
+	var b bytes.Buffer
+	cmd.Stderr = io.MultiWriter(logext.NewErrWriter(entry), &b)
+	cmd.Stdout = io.MultiWriter(logext.NewWriter(entry), &b)
 	if dir != "" {
 		cmd.Dir = dir
 	}
-	log.Debug("running")
-	if out, err := cmd.CombinedOutput(); err != nil {
-		log.WithError(err).Debug("failed")
-		return errors.Wrapf(err, "%q", string(out))
+	entry.WithField("env", env).Debug("running")
+	if err := cmd.Run(); err != nil {
+		entry.WithError(err).Debug("failed")
+		return errors.Wrapf(err, "%q", b.String())
 	}
 	return nil
 }


Test patch:
diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go
index ceff19fc..9911f84c 100644
--- a/internal/pipe/brew/brew_test.go
+++ b/internal/pipe/brew/brew_test.go
@@ -232,6 +232,70 @@ func TestRunPipe(t *testing.T) {
 		})
 	}
 }
+
+func TestRunPipeNameTemplate(t *testing.T) {
+	folder, err := ioutil.TempDir("", "goreleasertest")
+	assert.NoError(t, err)
+	var ctx = &context.Context{
+		Git: context.GitInfo{
+			CurrentTag: "v1.0.1",
+		},
+		Version:   "1.0.1",
+		Artifacts: artifact.New(),
+		Env: map[string]string{
+			"FOO_BAR": "is_bar",
+		},
+		Config: config.Project{
+			Dist:        folder,
+			ProjectName: "foo",
+			Brews: []config.Homebrew{
+				{
+					Name: "foo_{{ .Env.FOO_BAR }}",
+					Tap: config.RepoRef{
+						Owner: "foo",
+						Name:  "bar",
+					},
+					IDs: []string{
+						"foo",
+					},
+				},
+			},
+		},
+	}
+	var path = filepath.Join(folder, "bin.tar.gz")
+	ctx.Artifacts.Add(&artifact.Artifact{
+		Name:   "bin.tar.gz",
+		Path:   path,
+		Goos:   "darwin",
+		Goarch: "amd64",
+		Type:   artifact.UploadableArchive,
+		Extra: map[string]interface{}{
+			"ID":                 "foo",
+			"Format":             "tar.gz",
+			"ArtifactUploadHash": "820ead5d9d2266c728dce6d4d55b6460",
+		},
+	})
+
+	_, err = os.Create(path)
+	assert.NoError(t, err)
+	client := &DummyClient{}
+	var distFile = filepath.Join(folder, "foo_is_bar.rb")
+
+	assert.NoError(t, doRun(ctx, ctx.Config.Brews[0], client))
+	assert.True(t, client.CreatedFile)
+	var golden = "testdata/foo_is_bar.rb.golden"
+	if *update {
+		assert.NoError(t, ioutil.WriteFile(golden, []byte(client.Content), 0655))
+	}
+	bts, err := ioutil.ReadFile(golden)
+	assert.NoError(t, err)
+	assert.Equal(t, string(bts), client.Content)
+
+	distBts, err := ioutil.ReadFile(distFile)
+	assert.NoError(t, err)
+	assert.Equal(t, string(bts), string(distBts))
+}
+
 func TestRunPipeForMultipleArmVersions(t *testing.T) {
 	for name, fn := range map[string]func(ctx *context.Context){
 		"multiple_armv5": func(ctx *context.Context) {
diff --git a/internal/pipe/brew/testdata/foo_is_bar.rb.golden b/internal/pipe/brew/testdata/foo_is_bar.rb.golden
new file mode 100644
index 00000000..c15e1ed5
--- /dev/null
+++ b/internal/pipe/brew/testdata/foo_is_bar.rb.golden
@@ -0,0 +1,16 @@
+# This file was generated by GoReleaser. DO NOT EDIT.
+class FooIsBar < Formula
+  desc ""
+  homepage ""
+  version "1.0.1"
+  bottle :unneeded
+
+  if OS.mac?
+    url "https://dummyhost/download/v1.0.1/bin.tar.gz"
+    sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+  elsif OS.linux?
+  end
+
+  def install
+  end
+end
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index bae79714..14359ee4 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -731,3 +731,28 @@ func TestHookInvalidShelCommand(t *testing.T) {
 		},
 	}))
 }
+
+func TestRunHookFailWithLogs(t *testing.T) {
+	folder, back := testlib.Mktmp(t)
+	defer back()
+	var config = config.Project{
+		Dist: folder,
+		Builds: []config.Build{
+			{
+				Lang:   "fakeFail",
+				Binary: "testing",
+				Flags:  []string{"-v"},
+				Hooks: config.HookConfig{
+					Pre: []config.BuildHook{
+						{Cmd: "sh -c 'echo foo; exit 1'"},
+					},
+				},
+				Targets: []string{"whatever"},
+			},
+		},
+	}
+	var ctx = context.New(config)
+	ctx.Git.CurrentTag = "2.4.5"
+	assert.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: \"foo\\n\": exit status 1")
+	assert.Empty(t, ctx.Artifacts.List())
+}