fix tests

This commit is contained in:
o.bilko 2025-05-17 09:44:45 +03:00 committed by Mike Farah
parent 3ac203ebb8
commit c58d9e7da4
6 changed files with 41 additions and 58 deletions

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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() },
}

View File

@ -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,
}
}

View File

@ -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
// 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) {

View File

@ -257,3 +257,4 @@ noyaml
nolint
shortfile
Unmarshalling
noini