Fix nested values can span sections (#306)

This commit is contained in:
Riley Avron
2021-12-02 22:07:20 +08:00
committed by GitHub
parent 3ca1e6ad32
commit c71eccd557
2 changed files with 74 additions and 0 deletions
+72
View File
@@ -1585,3 +1585,75 @@ func TestPythonMultiline_EOF(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "some text here\n\tsome more text here 2", testData.Value1)
}
func Test_NestedValuesSpanningSections(t *testing.T) {
t.Run("basic nested value", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
AllowNestedValues: true,
}, []byte(`
[section]
key1 = value1
key2 =
nested1 = nestedvalue1
`))
require.NoError(t, err)
require.NotNil(t, f)
assert.Equal(t, "value1", f.Section("section").Key("key1").String())
assert.Equal(t, "", f.Section("section").Key("key2").String())
assert.Equal(t, []string{"nested1 = nestedvalue1"}, f.Section("section").Key("key2").NestedValues())
})
t.Run("no nested values", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
AllowNestedValues: true,
}, []byte(`
[section]
key1 = value1
key2 =
`))
require.NoError(t, err)
require.NotNil(t, f)
assert.Equal(t, "value1", f.Section("section").Key("key1").String())
assert.Equal(t, "", f.Section("section").Key("key2").String())
})
t.Run("no nested values and following sections", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
AllowNestedValues: true,
}, []byte(`
[section]
key1 = value1
key2 =
[section2]
key3 = value3
`))
require.NoError(t, err)
require.NotNil(t, f)
assert.Equal(t, "value1", f.Section("section").Key("key1").String())
assert.Equal(t, "", f.Section("section").Key("key2").String())
assert.Equal(t, "value3", f.Section("section2").Key("key3").String())
})
t.Run("no nested values and following sections with indentation", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
AllowNestedValues: true,
}, []byte(`
[section]
key1 = value1
key2 =
[section2]
key3 = value3
`))
require.NoError(t, err)
require.NotNil(t, f)
assert.Equal(t, "value1", f.Section("section").Key("key1").String())
assert.Equal(t, "", f.Section("section").Key("key2").String())
assert.Equal(t, "value3", f.Section("section2").Key("key3").String())
})
}
+2
View File
@@ -441,6 +441,8 @@ func (f *File) parse(reader io.Reader) (err error) {
// Reset auto-counter and comments
p.comment.Reset()
p.count = 1
// Nested values can't span sections
isLastValueEmpty = false
inUnparseableSection = false
for i := range f.options.UnparseableSections {