mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 03:45:41 +00:00
fix tests
This commit is contained in:
parent
3ac203ebb8
commit
c58d9e7da4
@ -35,13 +35,13 @@ func (dec *iniDecoder) Decode() (*CandidateNode, error) {
|
|||||||
// Read all content from the stored reader
|
// Read all content from the stored reader
|
||||||
content, err := io.ReadAll(dec.reader)
|
content, err := io.ReadAll(dec.reader)
|
||||||
if err != nil {
|
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
|
// Parse the INI content
|
||||||
cfg, err := ini.Load(content)
|
cfg, err := ini.Load(content)
|
||||||
if err != nil {
|
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)
|
// Create a root CandidateNode as a MappingNode (since INI is key-value based)
|
||||||
|
|||||||
@ -14,13 +14,10 @@ type iniEncoder struct {
|
|||||||
indentString string
|
indentString string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewINIEncoder creates a new INI encoder with the specified indent level for formatting.
|
// NewINIEncoder creates a new INI encoder
|
||||||
func NewINIEncoder(indent int) Encoder {
|
func NewINIEncoder() Encoder {
|
||||||
var indentString = ""
|
// Hardcoded indent value of 0, meaning no additional spacing.
|
||||||
for index := 0; index < indent; index++ {
|
return &iniEncoder{""}
|
||||||
indentString = indentString + " "
|
|
||||||
}
|
|
||||||
return &iniEncoder{indentString}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanHandleAliases indicates whether the encoder supports aliases. INI does not support aliases.
|
// 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]
|
valueNode := node.Content[i+1]
|
||||||
key := keyNode.Value
|
key := keyNode.Value
|
||||||
|
|
||||||
if valueNode.Kind == ScalarNode {
|
switch valueNode.Kind {
|
||||||
|
case ScalarNode:
|
||||||
// Add key-value pair to the default section.
|
// Add key-value pair to the default section.
|
||||||
_, err := defaultSection.NewKey(key, valueNode.Value)
|
_, err := defaultSection.NewKey(key, valueNode.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if valueNode.Kind == MappingNode {
|
case MappingNode:
|
||||||
// Create a new section for nested MappingNode.
|
// Create a new section for nested MappingNode.
|
||||||
section, err := cfg.NewSection(key)
|
section, err := cfg.NewSection(key)
|
||||||
if err != nil {
|
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)
|
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)
|
log.Debugf("Skipping non-scalar value for key %s: %v", key, valueNode.Kind)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,7 +78,7 @@ var LuaFormat = &Format{"lua", []string{"l"},
|
|||||||
}
|
}
|
||||||
|
|
||||||
var INIFormat = &Format{"ini", []string{"i"},
|
var INIFormat = &Format{"ini", []string{"i"},
|
||||||
func() Encoder { return NewINIEncoder(0) },
|
func() Encoder { return NewINIEncoder() },
|
||||||
func() Decoder { return NewINIDecoder() },
|
func() Decoder { return NewINIDecoder() },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,20 +1,17 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
type INIPreferences struct {
|
type INIPreferences struct {
|
||||||
Indent int
|
|
||||||
ColorsEnabled bool
|
ColorsEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultINIPreferences() INIPreferences {
|
func NewDefaultINIPreferences() INIPreferences {
|
||||||
return INIPreferences{
|
return INIPreferences{
|
||||||
Indent: 2,
|
|
||||||
ColorsEnabled: false,
|
ColorsEnabled: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *INIPreferences) Copy() INIPreferences {
|
func (p *INIPreferences) Copy() INIPreferences {
|
||||||
return INIPreferences{
|
return INIPreferences{
|
||||||
Indent: p.Indent,
|
|
||||||
ColorsEnabled: p.ColorsEnabled,
|
ColorsEnabled: p.ColorsEnabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,6 @@ var iniScenarios = []formatScenario{
|
|||||||
{
|
{
|
||||||
description: "Encode INI: simple",
|
description: "Encode INI: simple",
|
||||||
input: `section: {key: value}`,
|
input: `section: {key: value}`,
|
||||||
indent: 0,
|
|
||||||
expected: expectedSimpleINIOutput,
|
expected: expectedSimpleINIOutput,
|
||||||
scenarioType: "encode",
|
scenarioType: "encode",
|
||||||
},
|
},
|
||||||
@ -41,7 +40,6 @@ var iniScenarios = []formatScenario{
|
|||||||
input: simpleINIInput,
|
input: simpleINIInput,
|
||||||
expected: expectedSimpleINIOutput,
|
expected: expectedSimpleINIOutput,
|
||||||
scenarioType: "roundtrip",
|
scenarioType: "roundtrip",
|
||||||
indent: 0,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "bad ini",
|
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))
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||||
|
|
||||||
if s.subdescription != "" {
|
if s.subdescription != "" {
|
||||||
@ -66,17 +64,13 @@ func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario, indent int)
|
|||||||
|
|
||||||
expression := s.expression
|
expression := s.expression
|
||||||
if 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 {
|
} 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")
|
writeOrPanic(w, "will output\n")
|
||||||
prefs := ConfiguredINIPreferences.Copy()
|
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder())))
|
||||||
prefs.Indent = indent
|
|
||||||
|
|
||||||
// Pass prefs.Indent instead of prefs
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) {
|
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, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences))))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testINIScenario(t *testing.T, s formatScenario) {
|
func testINIScenario(t *testing.T, s formatScenario) {
|
||||||
prefs := ConfiguredINIPreferences.Copy()
|
|
||||||
prefs.Indent = s.indent
|
|
||||||
switch s.scenarioType {
|
switch s.scenarioType {
|
||||||
case "encode":
|
case "encode":
|
||||||
// Pass prefs.Indent instead of prefs
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder()), s.description)
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent)), s.description)
|
|
||||||
case "decode":
|
case "decode":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
|
||||||
case "roundtrip":
|
case "roundtrip":
|
||||||
// Pass prefs.Indent instead of prefs
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder()), s.description)
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent)), s.description)
|
|
||||||
case "decode-error":
|
case "decode-error":
|
||||||
// Pass prefs.Indent instead of prefs
|
result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder())
|
||||||
result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent))
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
|
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
|
||||||
} else {
|
} 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)
|
s := i.(formatScenario)
|
||||||
if s.skipDoc {
|
if s.skipDoc {
|
||||||
return
|
return
|
||||||
@ -140,18 +128,9 @@ func documentINIScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
|||||||
case "decode":
|
case "decode":
|
||||||
documentDecodeINIScenario(w, s)
|
documentDecodeINIScenario(w, s)
|
||||||
case "roundtrip":
|
case "roundtrip":
|
||||||
documentRoundtripINIScenario(w, s, s.indent)
|
documentRoundtripINIScenario(w, s)
|
||||||
case "decode-error":
|
case "decode-error":
|
||||||
// Add handling for decode-error scenario type to prevent panic
|
documentDecodeErrorINIScenario(w, s)
|
||||||
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))
|
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
||||||
}
|
}
|
||||||
@ -175,17 +154,25 @@ func documentINIEncodeScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
expression = "."
|
expression = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.indent == 2 {
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini '%v' sample.yml\n```\n", expression))
|
||||||
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, "will output\n")
|
||||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent))))
|
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) {
|
func TestINIScenarios(t *testing.T) {
|
||||||
|
|||||||
@ -256,4 +256,5 @@ tonumber
|
|||||||
noyaml
|
noyaml
|
||||||
nolint
|
nolint
|
||||||
shortfile
|
shortfile
|
||||||
Unmarshalling
|
Unmarshalling
|
||||||
|
noini
|
||||||
Loading…
Reference in New Issue
Block a user