Skip to content

Commit

Permalink
fix: ensure uniqueness of brew's bin.install (#1876)
Browse files Browse the repository at this point in the history
This patch ensures that there are no duplicate `bin.install` statements added to the homebrew formula. It resolves several upstream issues including ory/homebrew-kratos#1 and others.

Closes ory/homebrew-kratos#1
  • Loading branch information
aeneasr committed Oct 28, 2020
1 parent 85f064b commit f629ac7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/pipe/brew/brew.go
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions internal/pipe/brew/brew_test.go
Expand Up @@ -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()
Expand Down

1 comment on commit f629ac7

@vercel
Copy link

@vercel vercel bot commented on f629ac7 Oct 28, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.