Repository: goreleaser/goreleaser
Pull request #1876 (2020-10-28 10:51:27)

fix: ensure uniqueness of brew's bin.install
This patch ensures that there are no duplicate `bin.install` statements added to the homebrew formula. It resolves several upstream issues including https://github.com/ory/homebrew-kratos/issues/1 and others.

Closes https://github.com/ory/homebrew-kratos/issues/1


vercel[bot]: @aeneasr is attempting to deploy a commit to the **Goreleaser** Team on [Vercel](https://vercel.com).

A member of the Team first needs to [authorize it](https://vercel.com/git/authorize?team=Goreleaser&type=github&job=%7B%22headInfo%22%3A%7B%22sha%22%3A%2202d3948c124cb849fffb8848306bc5f71555b716%22%7D%2C%22id%22%3A%22QmY4BqZP4VF2r5Um6AZ7fVY6Xts8EVxsmXSSM2EUACeyBg%22%2C%22org%22%3A%22goreleaser%22%2C%22prId%22%3A1876%2C%22repo%22%3A%22goreleaser%22%7D).


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=h1) Report
> Merging [#1876](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/85f064bcfe9ab6892acee4a60a4628726cd7f5f3?el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1876/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN)](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1876      +/-   ##
==========================================
+ Coverage   81.42%   81.44%   +0.01%     
==========================================
  Files          73       73              
  Lines        3726     3729       +3     
==========================================
+ Hits         3034     3037       +3     
  Misses        570      570              
  Partials      122      122              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/pipe/brew/brew.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1876/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9icmV3L2JyZXcuZ28=) | `78.45% <100.00%> (+0.36%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=footer). Last update [85f064b...02d3948](https://codecov.io/gh/goreleaser/goreleaser/pull/1876?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go
index 191cc753..51b65028 100644
--- a/internal/pipe/brew/brew.go
+++ b/internal/pipe/brew/brew.go
@@ -83,17 +83,25 @@ func (Pipe) Default(ctx *context.Context) error {
 		var brew = &ctx.Config.Brews[i]
 
 		if brew.Install == "" {
-			// TODO: maybe replace this with a simplear also optimistic
+			// TODO: maybe replace this with a simpler also optimistic
 			// approach of just doing `bin.install "project_name"`?
 			var installs []string
 			for _, build := range ctx.Config.Builds {
 				if !isBrewBuild(build) {
 					continue
 				}
-				installs = append(
-					installs,
-					fmt.Sprintf(`bin.install "%s"`, build.Binary),
-				)
+				install := fmt.Sprintf(`bin.install "%s"`, build.Binary)
+				// Do not add duplicate "bin.install" statements when binary names overlap.
+				var found bool
+				for _, instruction := range installs {
+					if instruction == install {
+						found = true
+						break
+					}
+				}
+				if !found {
+					installs = append(installs, install)
+				}
 			}
 			brew.Install = strings.Join(installs, "\n")
 			log.Warnf("optimistically guessing `brew[%d].install`, double check", i)


Test patch:
diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go
index b33e76d6..e3f054dd 100644
--- a/internal/pipe/brew/brew_test.go
+++ b/internal/pipe/brew/brew_test.go
@@ -825,6 +825,37 @@ func TestRunTokenTypeNotImplementedForBrew(t *testing.T) {
 	require.Equal(t, ErrTokenTypeNotImplementedForBrew{TokenType: "gitea"}, doRun(ctx, ctx.Config.Brews[0], client))
 }
 
+func TestDefaultBinInstallUniqueness(t *testing.T) {
+	_, back := testlib.Mktmp(t)
+	defer back()
+
+	var ctx = &context.Context{
+		TokenType: context.TokenTypeGitHub,
+		Config: config.Project{
+			ProjectName: "myproject",
+			Brews: []config.Homebrew{
+				{},
+			},
+			Builds: []config.Build{
+				{
+					ID:     "macos",
+					Binary: "unique",
+					Goos:   []string{"darwin"},
+					Goarch: []string{"amd64"},
+				},
+				{
+					ID:     "macos-cgo",
+					Binary: "unique",
+					Goos:   []string{"darwin"},
+					Goarch: []string{"amd64"},
+				},
+			},
+		},
+	}
+	require.NoError(t, Pipe{}.Default(ctx))
+	require.Equal(t, `bin.install "unique"`, ctx.Config.Brews[0].Install)
+}
+
 func TestDefault(t *testing.T) {
 	_, back := testlib.Mktmp(t)
 	defer back()