diff --git a/config/config_test.go b/config/config_test.go index bad7f480ec..49da49cd04 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -14,7 +14,6 @@ import ( "github.com/zalando/skipper/filters/openpolicyagent" "github.com/zalando/skipper/net" "github.com/zalando/skipper/proxy" - "gopkg.in/yaml.v2" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -482,11 +481,3 @@ func TestDeprecatedFlags(t *testing.T) { } } } - -func TestMultiFlagYamlErr(t *testing.T) { - m := &multiFlag{} - err := yaml.Unmarshal([]byte(`foo=bar`), m) - if err == nil { - t.Error("Failed to get error on wrong yaml input") - } -} diff --git a/config/mapflags_test.go b/config/mapflags_test.go index 3909f2fd49..b6c0b08600 100644 --- a/config/mapflags_test.go +++ b/config/mapflags_test.go @@ -3,7 +3,8 @@ package config import ( "testing" - "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" ) @@ -83,7 +84,7 @@ func Test_mapFlags_Set_nil(t *testing.T) { } func Test_mapFlags_UnmarshalYAML(t *testing.T) { - tests := []struct { + for _, tt := range []struct { name string yml string wantErr bool @@ -102,30 +103,32 @@ key3: k3=10s`, "key3": "k3=10s", }, }, + } { + t.Run(tt.name+"_valid", func(t *testing.T) { + mf := &mapFlags{} + + err := yaml.Unmarshal([]byte(tt.yml), mf) + require.NoError(t, err) + assert.Equal(t, tt.want, mf.values) + }) + } + + for _, tt := range []struct { + name string + yml string + }{ { name: "test mapFlags with yaml error", yml: `--- foo=bar`, - wantErr: true, - want: nil, }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + } { + t.Run(tt.name+"_invalid", func(t *testing.T) { mf := &mapFlags{} - if err := yaml.Unmarshal([]byte(tt.yml), mf); (err != nil) != tt.wantErr { - t.Errorf("mapFlags.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr) - } - if !tt.wantErr { - if len(mf.values) != len(tt.want) { - t.Errorf("Failed to have mapFlags created: %d != %d", len(mf.values), len(tt.want)) - } - - if cmp.Diff(mf.values, tt.want) != "" { - t.Errorf("mapFlags.UnmarshalYAML() got %v, want %v", mf.values, tt.want) - } - } + err := yaml.Unmarshal([]byte(tt.yml), mf) + assert.Error(t, err) + assert.Nil(t, mf.values) }) } } diff --git a/config/multiflag_test.go b/config/multiflag_test.go new file mode 100644 index 0000000000..f6cdfb2a35 --- /dev/null +++ b/config/multiflag_test.go @@ -0,0 +1,56 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" +) + +func TestMultiFlagSet(t *testing.T) { + for _, tc := range []struct { + name string + args string + values string + wantErr bool + }{ + { + name: "single value", + args: "foo=bar", + values: "foo=bar", + wantErr: false, + }, + { + name: "multiple values", + args: "foo=bar foo=baz foo=qux bar=baz", + values: "foo=bar foo=baz foo=qux bar=baz", + wantErr: false, + }, + } { + t.Run(tc.name, func(t *testing.T) { + multiFlag := &multiFlag{} + err := multiFlag.Set(tc.args) + require.Condition(t, func() bool { + if (err != nil) && tc.wantErr == false { + t.Logf("set error: %v", err) + return false + } else if (err == nil) && tc.wantErr == true { + t.Logf("expected error, got nil") + return false + } else if err != nil { + return false + } + return true + }, "error: %v, wantErr: %v", err, tc.wantErr) + + assert.Equal(t, tc.values, multiFlag.String()) + }) + } +} + +func TestMultiFlagYamlErr(t *testing.T) { + m := &multiFlag{} + err := yaml.Unmarshal([]byte(`-foo=bar`), m) + require.Error(t, err, "Failed to get error on wrong yaml input") +}