Skip to content

Commit

Permalink
file: fix whitespaces are filtered in the SaveTo method (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Aug 23, 2020
1 parent 29f972a commit 31bf7ce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
val = `"""` + val + `"""`
} else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
val = "`" + val + "`"
} else if len(strings.TrimSpace(val)) != len(val) {
val = `"` + val + `"`
}
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
return nil, err
Expand Down
19 changes: 19 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,25 @@ test =
`)

})

Convey("Keep leading and trailing spaces in value", t, func() {
f, _ := ini.Load([]byte(`[foo]
bar1 = ' val ue1 '
bar2 = """ val ue2 """
bar3 = " val ue3 "
`))
So(f, ShouldNotBeNil)

var buf bytes.Buffer
_, err := f.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(),ShouldEqual,`[foo]
bar1 = " val ue1 "
bar2 = " val ue2 "
bar3 = " val ue3 "
`)
})
}

func TestFile_SaveTo(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestFile_WriteTo.golden
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ true = 2+3=5
ADDRESS = """404 road,
NotFound, State, 50000"""
two_lines = how about continuation lines?
lots_of_lines = 1 2 3 4
lots_of_lines = "1 2 3 4 "

0 comments on commit 31bf7ce

Please sign in to comment.