Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested values can span sections #306

Merged
merged 2 commits into from Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions ini_test.go
Expand Up @@ -1562,3 +1562,75 @@ Eu feugiat pretium nibh ipsum consequat nisl vel pretium lectus. Habitant morbi
testData.Value3,
)
}

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only test that fails prior to my fix, but I added the other subtests just to have some more comprehensive coverage, since there are currently no nested value tests at all.

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 changes: 2 additions & 0 deletions parser.go
Expand Up @@ -461,6 +461,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 {
Expand Down