Repository: goreleaser/goreleaser
Pull request #1559 (2020-05-24 16:12:26)

fix: brew dependency: string or struct
refs #1554

not sure if there's a better way to do this... if there is, I'm all ears :) 

vercel-for-github[bot]: 
This pull request is being automatically deployed with Vercel ([learn more](https://zeit.ink/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/km3fiestz
  ✅ Preview: *In Progress*
		
_([94c5f37](https://github.com/goreleaser/goreleaser/commit/94c5f371e863b9a1b9209c192ff2398408497cd9), update in progress)_
codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1559?src=pr&el=h1) Report
> Merging [#1559](https://codecov.io/gh/goreleaser/goreleaser/pull/1559?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/843262e3ccb2597897e3efa6545cb245c43f471e&el=desc) will **increase** coverage by `0.04%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master    #1559      +/-   ##
==========================================
+ Coverage   85.53%   85.58%   +0.04%     
==========================================
  Files          69       69              
  Lines        3824     3835      +11     
==========================================
+ Hits         3271     3282      +11     
  Misses        458      458              
  Partials       95       95              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1559?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1559/diff?src=pr&el=tree#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `94.44% <100.00%> (+1.00%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1559?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/1559?src=pr&el=footer). Last update [843262e...94c5f37](https://codecov.io/gh/goreleaser/goreleaser/pull/1559?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/config/config.go b/pkg/config/config.go
index b861044e..a821ff4e 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -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 == "" {


Test patch:
diff --git a/pkg/config/config_homebrew_dependency_test.go b/pkg/config/config_homebrew_dependency_test.go
new file mode 100644
index 00000000..24379f88
--- /dev/null
+++ b/pkg/config/config_homebrew_dependency_test.go
@@ -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")
+	})
+}