mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-06 10:04:43 +08:00
feat: add --ini-preserve-quotes flag for INI round-trip quote preservation (#2728)
Add INIPreferences.PreserveSurroundedQuote option that wires through to go-ini/ini's LoadOptions.PreserveSurroundedQuote. When enabled, existing surrounding quotes on INI values are preserved during decode/encode round-trips. Fixes #2456 Co-authored-by: toller892 <toller892@gmail.com>
This commit is contained in:
+47
-5
@@ -22,6 +22,16 @@ const expectedSimpleINIYaml = `section:
|
||||
key: value
|
||||
`
|
||||
|
||||
const quotedINIInput = `[section]
|
||||
color_theme = "Default"
|
||||
theme_background = "False"
|
||||
`
|
||||
|
||||
const expectedQuotedINIOutput = `[section]
|
||||
color_theme = "Default"
|
||||
theme_background = "False"
|
||||
`
|
||||
|
||||
var iniScenarios = []formatScenario{
|
||||
{
|
||||
description: "Parse INI: simple",
|
||||
@@ -49,6 +59,22 @@ var iniScenarios = []formatScenario{
|
||||
},
|
||||
}
|
||||
|
||||
// iniPreserveQuotesPrefs returns INIPreferences with PreserveSurroundedQuote enabled.
|
||||
func iniPreserveQuotesPrefs() INIPreferences {
|
||||
prefs := NewDefaultINIPreferences()
|
||||
prefs.PreserveSurroundedQuote = true
|
||||
return prefs
|
||||
}
|
||||
|
||||
var iniPreserveQuotesScenarios = []formatScenario{
|
||||
{
|
||||
description: "Roundtrip INI: preserve quotes",
|
||||
input: quotedINIInput,
|
||||
expected: expectedQuotedINIOutput,
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
}
|
||||
|
||||
func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
|
||||
@@ -70,7 +96,7 @@ func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
}
|
||||
|
||||
writeOrPanic(w, "will output\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder())))
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(NewDefaultINIPreferences()), NewINIEncoder())))
|
||||
}
|
||||
|
||||
func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
@@ -94,7 +120,7 @@ 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))))
|
||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(NewDefaultINIPreferences()), NewYamlEncoder(ConfiguredYamlPreferences))))
|
||||
}
|
||||
|
||||
func testINIScenario(t *testing.T, s formatScenario) {
|
||||
@@ -102,11 +128,11 @@ func testINIScenario(t *testing.T, s formatScenario) {
|
||||
case "encode":
|
||||
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)
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(NewDefaultINIPreferences()), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
|
||||
case "roundtrip":
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder()), s.description)
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(NewDefaultINIPreferences()), NewINIEncoder()), s.description)
|
||||
case "decode-error":
|
||||
result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder())
|
||||
result, err := processFormatScenario(s, NewINIDecoder(NewDefaultINIPreferences()), NewINIEncoder())
|
||||
if err == nil {
|
||||
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
|
||||
} else {
|
||||
@@ -185,3 +211,19 @@ func TestINIScenarios(t *testing.T) {
|
||||
}
|
||||
documentScenarios(t, "usage", "convert", genericScenarios, documentINIScenario)
|
||||
}
|
||||
|
||||
func testINIPreserveQuotesScenario(t *testing.T, s formatScenario) {
|
||||
prefs := iniPreserveQuotesPrefs()
|
||||
switch s.scenarioType {
|
||||
case "roundtrip":
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(prefs), NewINIEncoder()), s.description)
|
||||
default:
|
||||
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
||||
}
|
||||
}
|
||||
|
||||
func TestINIPreserveQuotesScenarios(t *testing.T) {
|
||||
for _, tt := range iniPreserveQuotesScenarios {
|
||||
testINIPreserveQuotesScenario(t, tt)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user