Skip to content

Commit

Permalink
Fix python multiline EOF error
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Dec 1, 2021
1 parent e641746 commit d551d5d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
23 changes: 23 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,3 +1562,26 @@ func TestPythonMultiline(t *testing.T) {
testData.Value3,
)
}

func TestPythonMultiline_EOF(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping testing on Windows")
}

path := filepath.Join("testdata", "multiline_eof.ini")
f, err := LoadSources(LoadOptions{
AllowPythonMultilineValues: true,
ReaderBufferSize: 64 * 1024,
}, path)
require.NoError(t, err)
require.NotNil(t, f)
assert.Len(t, f.Sections(), 1)

defaultSection := f.Section("")
assert.NotNil(t, f.Section(""))

var testData testData
err = defaultSection.MapTo(&testData)
require.NoError(t, err)
assert.Equal(t, "some text here\n\tsome more text here 2", testData.Value1)
}
9 changes: 1 addition & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func hasSurroundedQuote(in string, quote byte) bool {
}

func (p *parser) readValue(in []byte, bufferSize int) (string, error) {

line := strings.TrimLeftFunc(string(in), unicode.IsSpace)
if len(line) == 0 {
if p.options.AllowPythonMultilineValues && len(in) > 0 && in[len(in)-1] == '\n' {
Expand Down Expand Up @@ -304,12 +303,7 @@ func (p *parser) readPythonMultilines(line string, bufferSize int) (string, erro

for {
peekData, peekErr := peekBuffer.ReadBytes('\n')
if peekErr != nil {
if peekErr == io.EOF {
p.debug("readPythonMultilines: io.EOF, peekData: %q, line: %q", string(peekData), line)
return line, nil
}

if peekErr != nil && peekErr != io.EOF {
p.debug("readPythonMultilines: failed to peek with error: %v", peekErr)
return "", peekErr
}
Expand Down Expand Up @@ -481,7 +475,6 @@ func (f *File) parse(reader io.Reader) (err error) {
key.Comment = strings.TrimSpace(p.comment.String())
p.comment.Reset()
continue

case f.options.SkipUnrecognizableLines:
continue
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/multiline.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ value3 = .

Id ornare arcu odio ut sem. Arcu dictum varius duis at consectetur lorem donec massa sapien. Proin libero nunc consequat interdum varius sit. Ut eu sem integer vitae justo. Vitae elementum curabitur vitae nunc. Diam quam nulla porttitor massa. Lectus mauris ultrices eros in cursus turpis massa tincidunt dui. Natoque penatibus et magnis dis parturient montes. Pellentesque habitant morbi tristique senectus et netus et malesuada fames. Libero nunc consequat interdum varius sit. Rhoncus dolor purus non enim praesent. Pellentesque sit amet porttitor eget. Nibh tortor id aliquet lectus proin nibh. Fermentum iaculis eu non diam phasellus vestibulum lorem sed.

Eu feugiat pretium nibh ipsum consequat nisl vel pretium lectus. Habitant morbi tristique senectus et netus et malesuada fames ac. Urna condimentum mattis pellentesque id. Lorem sed risus ultricies tristique nulla aliquet enim tortor at. Ipsum dolor sit amet consectetur adipiscing elit. Convallis a cras semper auctor neque vitae tempus quam. A diam sollicitudin tempor id eu nisl nunc mi ipsum. Maecenas sed enim ut sem viverra aliquet eget. Massa enim nec dui nunc mattis enim. Nam aliquam sem et tortor consequat. Adipiscing commodo elit at imperdiet dui accumsan sit amet nulla. Nullam eget felis eget nunc lobortis. Mauris a diam maecenas sed enim ut sem viverra. Ornare massa eget egestas purus. In hac habitasse platea dictumst. Ut tortor pretium viverra suspendisse potenti nullam ac tortor. Nisl nunc mi ipsum faucibus. At varius vel pharetra vel. Mauris ultrices eros in cursus turpis massa tincidunt.
Eu feugiat pretium nibh ipsum consequat nisl vel pretium lectus. Habitant morbi tristique senectus et netus et malesuada fames ac. Urna condimentum mattis pellentesque id. Lorem sed risus ultricies tristique nulla aliquet enim tortor at. Ipsum dolor sit amet consectetur adipiscing elit. Convallis a cras semper auctor neque vitae tempus quam. A diam sollicitudin tempor id eu nisl nunc mi ipsum. Maecenas sed enim ut sem viverra aliquet eget. Massa enim nec dui nunc mattis enim. Nam aliquam sem et tortor consequat. Adipiscing commodo elit at imperdiet dui accumsan sit amet nulla. Nullam eget felis eget nunc lobortis. Mauris a diam maecenas sed enim ut sem viverra. Ornare massa eget egestas purus. In hac habitasse platea dictumst. Ut tortor pretium viverra suspendisse potenti nullam ac tortor. Nisl nunc mi ipsum faucibus. At varius vel pharetra vel. Mauris ultrices eros in cursus turpis massa tincidunt.

0 comments on commit d551d5d

Please sign in to comment.