1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-16 03:52:12 +02:00

fix: brew dependency: string or struct (#1559)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2020-05-25 06:40:27 -03:00 committed by GitHub
parent 843262e3cc
commit 48be332890
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 0 deletions

View File

@ -45,6 +45,28 @@ type HomebrewDependency struct {
Type string `yaml:",omitempty"`
}
// type alias to prevent stack overflowing in the custom unmarshaler
type homebrewDependency HomebrewDependency
// UnmarshalYAML is a custom unmarshaler that accept brew deps in both the old and new format.
func (a *HomebrewDependency) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err == nil {
a.Name = str
return nil
}
var dep homebrewDependency
if err := unmarshal(&dep); err != nil {
return err
}
a.Name = dep.Name
a.Type = dep.Type
return nil
}
// String of the repo, e.g. owner/name
func (r Repo) String() string {
if r.Owner == "" && r.Name == "" {

View File

@ -0,0 +1,72 @@
package config
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestUnmarshalHomebrewDependency(t *testing.T) {
t.Run("string arr", func(t *testing.T) {
var conf = `
brews:
- name: foo
dependencies:
- foo
- bar
`
buf := strings.NewReader(conf)
prop, err := LoadReader(buf)
require.NoError(t, err)
require.Equal(t, []HomebrewDependency{
{
Name: "foo",
}, {
Name: "bar",
},
}, prop.Brews[0].Dependencies)
})
t.Run("mixed", func(t *testing.T) {
var conf = `
brews:
- name: foo
dependencies:
- name: foo
- bar
- name: foobar
type: optional
`
buf := strings.NewReader(conf)
prop, err := LoadReader(buf)
require.NoError(t, err)
require.Equal(t, []HomebrewDependency{
{
Name: "foo",
}, {
Name: "bar",
}, {
Name: "foobar",
Type: "optional",
},
}, prop.Brews[0].Dependencies)
})
t.Run("mixed", func(t *testing.T) {
var conf = `
brews:
- name: foo
dependencies:
- name: foo
- namer: bar
- asdda
`
buf := strings.NewReader(conf)
_, err := LoadReader(buf)
require.EqualError(t, err, "yaml: unmarshal errors:\n line 6: field namer not found in type config.homebrewDependency")
})
}