From 98c10c3c315d52ebb95aa81f6b03f24de9e6671c Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sat, 1 Jan 2022 17:02:57 +0100 Subject: [PATCH] test(encoding): fix failing tests due to the yaml library update Signed-off-by: Mark Sagi-Kazar --- internal/encoding/yaml/codec_test.go | 87 ------------------------ internal/encoding/yaml/yaml2_test.go | 91 ++++++++++++++++++++++++++ internal/encoding/yaml/yaml3_test.go | 91 ++++++++++++++++++++++++++ viper_test.go | 98 ++++++++++++++-------------- viper_yaml2_test.go | 56 ++++++++++++++++ viper_yaml3_test.go | 56 ++++++++++++++++ 6 files changed, 343 insertions(+), 136 deletions(-) create mode 100644 internal/encoding/yaml/yaml2_test.go create mode 100644 internal/encoding/yaml/yaml3_test.go create mode 100644 viper_yaml2_test.go create mode 100644 viper_yaml3_test.go diff --git a/internal/encoding/yaml/codec_test.go b/internal/encoding/yaml/codec_test.go index d76a6c6..d24a013 100644 --- a/internal/encoding/yaml/codec_test.go +++ b/internal/encoding/yaml/codec_test.go @@ -5,93 +5,6 @@ import ( "testing" ) -// original form of the data -const original = `# key-value pair -key: value -list: -- item1 -- item2 -- item3 -map: - key: value - -# nested -# map -nested_map: - map: - key: value - list: - - item1 - - item2 - - item3 -` - -// encoded form of the data -const encoded = `key: value -list: -- item1 -- item2 -- item3 -map: - key: value -nested_map: - map: - key: value - list: - - item1 - - item2 - - item3 -` - -// decoded form of the data -// -// in case of YAML it's slightly different from Viper's internal representation -// (eg. map is decoded into a map with interface key) -var decoded = map[string]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - "map": map[interface{}]interface{}{ - "key": "value", - }, - "nested_map": map[interface{}]interface{}{ - "map": map[interface{}]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - }, - }, -} - -// Viper's internal representation -var data = map[string]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - "map": map[string]interface{}{ - "key": "value", - }, - "nested_map": map[string]interface{}{ - "map": map[string]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - }, - }, -} - func TestCodec_Encode(t *testing.T) { codec := Codec{} diff --git a/internal/encoding/yaml/yaml2_test.go b/internal/encoding/yaml/yaml2_test.go new file mode 100644 index 0000000..e39d429 --- /dev/null +++ b/internal/encoding/yaml/yaml2_test.go @@ -0,0 +1,91 @@ +//go:build !viper_yaml3 +// +build !viper_yaml3 + +package yaml + +// original form of the data +const original = `# key-value pair +key: value +list: +- item1 +- item2 +- item3 +map: + key: value + +# nested +# map +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// encoded form of the data +const encoded = `key: value +list: +- item1 +- item2 +- item3 +map: + key: value +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// decoded form of the data +// +// in case of YAML it's slightly different from Viper's internal representation +// (eg. map is decoded into a map with interface key) +var decoded = map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + "map": map[interface{}]interface{}{ + "key": "value", + }, + "nested_map": map[interface{}]interface{}{ + "map": map[interface{}]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +// Viper's internal representation +var data = map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + "map": map[string]interface{}{ + "key": "value", + }, + "nested_map": map[string]interface{}{ + "map": map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + }, + }, +} diff --git a/internal/encoding/yaml/yaml3_test.go b/internal/encoding/yaml/yaml3_test.go new file mode 100644 index 0000000..6beb263 --- /dev/null +++ b/internal/encoding/yaml/yaml3_test.go @@ -0,0 +1,91 @@ +//go:build viper_yaml3 +// +build viper_yaml3 + +package yaml + +// original form of the data +const original = `# key-value pair +key: value +list: + - item1 + - item2 + - item3 +map: + key: value + +# nested +# map +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// encoded form of the data +const encoded = `key: value +list: + - item1 + - item2 + - item3 +map: + key: value +nested_map: + map: + key: value + list: + - item1 + - item2 + - item3 +` + +// decoded form of the data +// +// in case of YAML it's slightly different from Viper's internal representation +// (eg. map is decoded into a map with interface key) +var decoded = map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + "map": map[string]interface{}{ + "key": "value", + }, + "nested_map": map[string]interface{}{ + "map": map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + }, + }, +} + +// Viper's internal representation +var data = map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + "map": map[string]interface{}{ + "key": "value", + }, + "nested_map": map[string]interface{}{ + "map": map[string]interface{}{ + "key": "value", + "list": []interface{}{ + "item1", + "item2", + "item3", + }, + }, + }, +} diff --git a/viper_test.go b/viper_test.go index 8c864f1..d0724c7 100644 --- a/viper_test.go +++ b/viper_test.go @@ -33,21 +33,21 @@ import ( "github.com/spf13/viper/internal/testutil" ) -var yamlExample = []byte(`Hacker: true -name: steve -hobbies: -- skateboarding -- snowboarding -- go -clothing: - jacket: leather - trousers: denim - pants: - size: large -age: 35 -eyes : brown -beard: true -`) +// var yamlExample = []byte(`Hacker: true +// name: steve +// hobbies: +// - skateboarding +// - snowboarding +// - go +// clothing: +// jacket: leather +// trousers: denim +// pants: +// size: large +// age: 35 +// eyes : brown +// beard: true +// `) var yamlExampleWithExtras = []byte(`Existing: true Bogus: true @@ -1558,21 +1558,21 @@ p_ppu = 0.55 p_batters.batter.type = Regular `) -var yamlWriteExpected = []byte(`age: 35 -beard: true -clothing: - jacket: leather - pants: - size: large - trousers: denim -eyes: brown -hacker: true -hobbies: -- skateboarding -- snowboarding -- go -name: steve -`) +// var yamlWriteExpected = []byte(`age: 35 +// beard: true +// clothing: +// jacket: leather +// pants: +// size: large +// trousers: denim +// eyes: brown +// hacker: true +// hobbies: +// - skateboarding +// - snowboarding +// - go +// name: steve +// `) func TestWriteConfig(t *testing.T) { fs := afero.NewMemMapFs() @@ -2410,25 +2410,25 @@ func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { assert.Equal(t, "cobra_flag", config.Foo.Bar) } -var yamlExampleWithDot = []byte(`Hacker: true -name: steve -hobbies: - - skateboarding - - snowboarding - - go -clothing: - jacket: leather - trousers: denim - pants: - size: large -age: 35 -eyes : brown -beard: true -emails: - steve@hacker.com: - created: 01/02/03 - active: true -`) +// var yamlExampleWithDot = []byte(`Hacker: true +// name: steve +// hobbies: +// - skateboarding +// - snowboarding +// - go +// clothing: +// jacket: leather +// trousers: denim +// pants: +// size: large +// age: 35 +// eyes : brown +// beard: true +// emails: +// steve@hacker.com: +// created: 01/02/03 +// active: true +// `) func TestKeyDelimiter(t *testing.T) { v := NewWithOptions(KeyDelimiter("::")) diff --git a/viper_yaml2_test.go b/viper_yaml2_test.go new file mode 100644 index 0000000..5a8a51e --- /dev/null +++ b/viper_yaml2_test.go @@ -0,0 +1,56 @@ +//go:build !viper_yaml3 +// +build !viper_yaml3 + +package viper + +var yamlExample = []byte(`Hacker: true +name: steve +hobbies: +- skateboarding +- snowboarding +- go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +`) + +var yamlWriteExpected = []byte(`age: 35 +beard: true +clothing: + jacket: leather + pants: + size: large + trousers: denim +eyes: brown +hacker: true +hobbies: +- skateboarding +- snowboarding +- go +name: steve +`) + +var yamlExampleWithDot = []byte(`Hacker: true +name: steve +hobbies: +- skateboarding +- snowboarding +- go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +emails: + steve@hacker.com: + created: 01/02/03 + active: true +`) diff --git a/viper_yaml3_test.go b/viper_yaml3_test.go new file mode 100644 index 0000000..c397865 --- /dev/null +++ b/viper_yaml3_test.go @@ -0,0 +1,56 @@ +//go:build viper_yaml3 +// +build viper_yaml3 + +package viper + +var yamlExample = []byte(`Hacker: true +name: steve +hobbies: + - skateboarding + - snowboarding + - go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +`) + +var yamlWriteExpected = []byte(`age: 35 +beard: true +clothing: + jacket: leather + pants: + size: large + trousers: denim +eyes: brown +hacker: true +hobbies: + - skateboarding + - snowboarding + - go +name: steve +`) + +var yamlExampleWithDot = []byte(`Hacker: true +name: steve +hobbies: + - skateboarding + - snowboarding + - go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +emails: + steve@hacker.com: + created: 01/02/03 + active: true +`)