2018-05-15 02:22:55 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-05-26 20:02:57 +02:00
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/yaml"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-15 02:22:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Unmarshaled struct {
|
2022-01-27 03:12:10 +02:00
|
|
|
Strings StringArray `yaml:"strings,omitempty"`
|
|
|
|
Flags FlagArray `yaml:"flags,omitempty"`
|
2018-05-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type yamlUnmarshalTestCase struct {
|
|
|
|
yaml string
|
|
|
|
expected Unmarshaled
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringArrayTests = []yamlUnmarshalTestCase{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
Unmarshaled{},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"strings: []",
|
|
|
|
Unmarshaled{
|
|
|
|
Strings: StringArray{},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"strings: [one two, three]",
|
|
|
|
Unmarshaled{
|
|
|
|
Strings: StringArray{"one two", "three"},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"strings: one two",
|
|
|
|
Unmarshaled{
|
|
|
|
Strings: StringArray{"one two"},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"strings: {key: val}",
|
|
|
|
Unmarshaled{},
|
|
|
|
"yaml: unmarshal errors:\n line 1: cannot unmarshal !!map into string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var flagArrayTests = []yamlUnmarshalTestCase{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
Unmarshaled{},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"flags: []",
|
|
|
|
Unmarshaled{
|
|
|
|
Flags: FlagArray{},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"flags: [one two, three]",
|
|
|
|
Unmarshaled{
|
|
|
|
Flags: FlagArray{"one two", "three"},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"flags: one two",
|
|
|
|
Unmarshaled{
|
|
|
|
Flags: FlagArray{"one", "two"},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"flags: {key: val}",
|
|
|
|
Unmarshaled{},
|
|
|
|
"yaml: unmarshal errors:\n line 1: cannot unmarshal !!map into string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringArray(t *testing.T) {
|
|
|
|
for _, testCase := range stringArrayTests {
|
|
|
|
var actual Unmarshaled
|
|
|
|
|
|
|
|
err := yaml.UnmarshalStrict([]byte(testCase.yaml), &actual)
|
|
|
|
if testCase.err == "" {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, testCase.expected, actual)
|
2018-05-15 02:22:55 +02:00
|
|
|
} else {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.EqualError(t, err, testCase.err)
|
2018-05-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagArray(t *testing.T) {
|
|
|
|
for _, testCase := range flagArrayTests {
|
|
|
|
var actual Unmarshaled
|
|
|
|
|
|
|
|
err := yaml.UnmarshalStrict([]byte(testCase.yaml), &actual)
|
|
|
|
if testCase.err == "" {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2018-05-15 02:22:55 +02:00
|
|
|
} else {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.EqualError(t, err, testCase.err)
|
2018-05-15 02:22:55 +02:00
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, testCase.expected, actual)
|
2018-05-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
}
|