From c58d9e7da46e98258de9ca9a2f9f880045dbab09 Mon Sep 17 00:00:00 2001 From: "o.bilko" Date: Sat, 17 May 2025 09:44:45 +0300 Subject: [PATCH] fix tests --- pkg/yqlib/decoder_ini.go | 4 +-- pkg/yqlib/encoder_ini.go | 18 +++++------ pkg/yqlib/format.go | 2 +- pkg/yqlib/ini.go | 3 -- pkg/yqlib/ini_test.go | 69 ++++++++++++++++------------------------ project-words.txt | 3 +- 6 files changed, 41 insertions(+), 58 deletions(-) diff --git a/pkg/yqlib/decoder_ini.go b/pkg/yqlib/decoder_ini.go index f3761e78..eb64b53e 100644 --- a/pkg/yqlib/decoder_ini.go +++ b/pkg/yqlib/decoder_ini.go @@ -35,13 +35,13 @@ func (dec *iniDecoder) Decode() (*CandidateNode, error) { // Read all content from the stored reader content, err := io.ReadAll(dec.reader) if err != nil { - return nil, fmt.Errorf("failed to read INI content: %v", err) + return nil, fmt.Errorf("failed to read INI content: %w", err) } // Parse the INI content cfg, err := ini.Load(content) if err != nil { - return nil, fmt.Errorf("failed to parse INI content: %v", err) + return nil, fmt.Errorf("failed to parse INI content: %w", err) } // Create a root CandidateNode as a MappingNode (since INI is key-value based) diff --git a/pkg/yqlib/encoder_ini.go b/pkg/yqlib/encoder_ini.go index cf743490..b2b50550 100644 --- a/pkg/yqlib/encoder_ini.go +++ b/pkg/yqlib/encoder_ini.go @@ -14,13 +14,10 @@ type iniEncoder struct { indentString string } -// NewINIEncoder creates a new INI encoder with the specified indent level for formatting. -func NewINIEncoder(indent int) Encoder { - var indentString = "" - for index := 0; index < indent; index++ { - indentString = indentString + " " - } - return &iniEncoder{indentString} +// NewINIEncoder creates a new INI encoder +func NewINIEncoder() Encoder { + // Hardcoded indent value of 0, meaning no additional spacing. + return &iniEncoder{""} } // CanHandleAliases indicates whether the encoder supports aliases. INI does not support aliases. @@ -63,13 +60,14 @@ func (ie *iniEncoder) Encode(writer io.Writer, node *CandidateNode) error { valueNode := node.Content[i+1] key := keyNode.Value - if valueNode.Kind == ScalarNode { + switch valueNode.Kind { + case ScalarNode: // Add key-value pair to the default section. _, err := defaultSection.NewKey(key, valueNode.Value) if err != nil { return err } - } else if valueNode.Kind == MappingNode { + case MappingNode: // Create a new section for nested MappingNode. section, err := cfg.NewSection(key) if err != nil { @@ -88,7 +86,7 @@ func (ie *iniEncoder) Encode(writer io.Writer, node *CandidateNode) error { log.Debugf("Skipping nested non-scalar value for key %s: %v", nestedKeyNode.Value, nestedValueNode.Kind) } } - } else { + default: log.Debugf("Skipping non-scalar value for key %s: %v", key, valueNode.Kind) } } diff --git a/pkg/yqlib/format.go b/pkg/yqlib/format.go index 81f1b240..a74ce599 100644 --- a/pkg/yqlib/format.go +++ b/pkg/yqlib/format.go @@ -78,7 +78,7 @@ var LuaFormat = &Format{"lua", []string{"l"}, } var INIFormat = &Format{"ini", []string{"i"}, - func() Encoder { return NewINIEncoder(0) }, + func() Encoder { return NewINIEncoder() }, func() Decoder { return NewINIDecoder() }, } diff --git a/pkg/yqlib/ini.go b/pkg/yqlib/ini.go index 7c498d9d..ba8bbaf2 100644 --- a/pkg/yqlib/ini.go +++ b/pkg/yqlib/ini.go @@ -1,20 +1,17 @@ package yqlib type INIPreferences struct { - Indent int ColorsEnabled bool } func NewDefaultINIPreferences() INIPreferences { return INIPreferences{ - Indent: 2, ColorsEnabled: false, } } func (p *INIPreferences) Copy() INIPreferences { return INIPreferences{ - Indent: p.Indent, ColorsEnabled: p.ColorsEnabled, } } diff --git a/pkg/yqlib/ini_test.go b/pkg/yqlib/ini_test.go index f201a564..d73a46d1 100644 --- a/pkg/yqlib/ini_test.go +++ b/pkg/yqlib/ini_test.go @@ -32,7 +32,6 @@ var iniScenarios = []formatScenario{ { description: "Encode INI: simple", input: `section: {key: value}`, - indent: 0, expected: expectedSimpleINIOutput, scenarioType: "encode", }, @@ -41,7 +40,6 @@ var iniScenarios = []formatScenario{ input: simpleINIInput, expected: expectedSimpleINIOutput, scenarioType: "roundtrip", - indent: 0, }, { description: "bad ini", @@ -51,7 +49,7 @@ var iniScenarios = []formatScenario{ }, } -func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario, indent int) { +func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) if s.subdescription != "" { @@ -66,17 +64,13 @@ func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario, indent int) expression := s.expression if expression != "" { - writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini -I=%v '%v' sample.ini\n```\n", indent, expression)) + writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini '%v' sample.ini\n```\n", expression)) } else { - writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini -I=%v sample.ini\n```\n", indent)) + writeOrPanic(w, "```bash\nyq -p=ini -o=ini sample.ini\n```\n") } writeOrPanic(w, "will output\n") - prefs := ConfiguredINIPreferences.Copy() - prefs.Indent = indent - - // Pass prefs.Indent instead of prefs - writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent)))) + writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder()))) } func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) { @@ -100,25 +94,19 @@ func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) { } writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)))) } func testINIScenario(t *testing.T, s formatScenario) { - prefs := ConfiguredINIPreferences.Copy() - prefs.Indent = s.indent switch s.scenarioType { case "encode": - // Pass prefs.Indent instead of prefs - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder()), s.description) case "decode": test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "roundtrip": - // Pass prefs.Indent instead of prefs - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder()), s.description) case "decode-error": - // Pass prefs.Indent instead of prefs - result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent)) + result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder()) if err == nil { t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) } else { @@ -129,7 +117,7 @@ func testINIScenario(t *testing.T, s formatScenario) { } } -func documentINIScenario(t *testing.T, w *bufio.Writer, i interface{}) { +func documentINIScenario(_ *testing.T, w *bufio.Writer, i interface{}) { s := i.(formatScenario) if s.skipDoc { return @@ -140,18 +128,9 @@ func documentINIScenario(t *testing.T, w *bufio.Writer, i interface{}) { case "decode": documentDecodeINIScenario(w, s) case "roundtrip": - documentRoundtripINIScenario(w, s, s.indent) + documentRoundtripINIScenario(w, s) case "decode-error": - // Add handling for decode-error scenario type to prevent panic - writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) - if s.subdescription != "" { - writeOrPanic(w, s.subdescription) - writeOrPanic(w, "\n\n") - } - writeOrPanic(w, "Given a sample.ini file of:\n") - writeOrPanic(w, fmt.Sprintf("```ini\n%v\n```\n", s.input)) - writeOrPanic(w, "then an error is expected:\n") - writeOrPanic(w, fmt.Sprintf("```\n%v\n```\n\n", s.expectedError)) + documentDecodeErrorINIScenario(w, s) default: panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType)) } @@ -175,17 +154,25 @@ func documentINIEncodeScenario(w *bufio.Writer, s formatScenario) { expression = "." } - if s.indent == 2 { - writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini '%v' sample.yml\n```\n", expression)) - } else { - writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini -I=%v '%v' sample.yml\n```\n", s.indent, expression)) - } - writeOrPanic(w, "will output\n") - prefs := ConfiguredINIPreferences.Copy() - prefs.Indent = s.indent + writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini '%v' sample.yml\n```\n", expression)) - // Pass prefs.Indent instead of prefs - writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent)))) + writeOrPanic(w, "will output\n") + writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder()))) +} + +func documentDecodeErrorINIScenario(w *bufio.Writer, s formatScenario) { + writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) + + if s.subdescription != "" { + writeOrPanic(w, s.subdescription) + writeOrPanic(w, "\n\n") + } + + writeOrPanic(w, "Given a sample.ini file of:\n") + writeOrPanic(w, fmt.Sprintf("```ini\n%v\n```\n", s.input)) + + writeOrPanic(w, "then an error is expected:\n") + writeOrPanic(w, fmt.Sprintf("```\n%v\n```\n\n", s.expectedError)) } func TestINIScenarios(t *testing.T) { diff --git a/project-words.txt b/project-words.txt index f69c3417..a030b96c 100644 --- a/project-words.txt +++ b/project-words.txt @@ -256,4 +256,5 @@ tonumber noyaml nolint shortfile -Unmarshalling \ No newline at end of file +Unmarshalling +noini \ No newline at end of file