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

file: fix whitespaces are filtered in the SaveTo method #260

Merged
merged 1 commit into from
Aug 23, 2020
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
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 "