diff --git a/file.go b/file.go index 9d91c31..f8b2240 100644 --- a/file.go +++ b/file.go @@ -342,6 +342,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) { // Use buffer to make sure target is safe until finish encoding. buf := bytes.NewBuffer(nil) + lastSectionIdx := len(f.sectionList) - 1 for i, sname := range f.sectionList { sec := f.SectionWithIndex(sname, f.sectionIndexes[i]) if len(sec.Comment) > 0 { @@ -371,12 +372,13 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) { } } + isLastSection := i == lastSectionIdx if sec.isRawSection { if _, err := buf.WriteString(sec.rawBody); err != nil { return nil, err } - if PrettySection { + if PrettySection && !isLastSection { // Put a line between sections if _, err := buf.WriteString(LineBreak); err != nil { return nil, err @@ -448,9 +450,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) { } if key.isBooleanType { - if kname != sec.keyList[len(sec.keyList)-1] { - buf.WriteString(LineBreak) - } + buf.WriteString(LineBreak) return true, nil } @@ -496,7 +496,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) { } } - if PrettySection { + if PrettySection && !isLastSection { // Put a line between sections if _, err := buf.WriteString(LineBreak); err != nil { return nil, err diff --git a/file_test.go b/file_test.go index 6ab6739..c9914b5 100644 --- a/file_test.go +++ b/file_test.go @@ -108,7 +108,6 @@ AllowedIPs = 192.168.2.3/32 [Peer] PublicKey = AllowedIPs = 192.168.2.4/32 - `, str) }) @@ -155,7 +154,6 @@ AllowedIPs = 192.168.2.2/32 [Peer] PublicKey = AllowedIPs = 192.168.2.4/32 - `, str) }) @@ -373,7 +371,6 @@ func TestFile_DeleteSection(t *testing.T) { assert.Equal(t, `[section1] key1 = value1 - `, buf.String()) }) @@ -456,7 +453,6 @@ domain = mydomain.com ; Multiline ; Comment test = - `, buf.String()) }) @@ -476,7 +472,6 @@ bar3 = " val ue3 " bar1 = " val ue1 " bar2 = " val ue2 " bar3 = " val ue3 " - `, buf.String()) }) } @@ -517,7 +512,6 @@ Populations -> 12345678,98765432 Coordinates -> 192.168,10.11 Flags -> true,false Note -> Hello world! - `) _, err = f.WriteTo(&actual) require.NoError(t, err) diff --git a/ini_test.go b/ini_test.go index ade2a2c..12672e2 100644 --- a/ini_test.go +++ b/ini_test.go @@ -250,7 +250,6 @@ func TestInsensitiveLoad(t *testing.T) { require.NoError(t, err) assert.Equal(t, `[author] e-mail = u@gogs.io - `, buf.String(), ) @@ -305,7 +304,6 @@ func TestLoadSources(t *testing.T) { require.NoError(t, err) assert.Equal(t, `[author] e-mail = u@gogs.io - `, buf.String(), ) @@ -333,7 +331,6 @@ e-mail = u@gogs.io require.NoError(t, err) assert.Equal(t, `[author] E-MAIL = u@gogs.io - `, buf.String(), ) @@ -361,7 +358,6 @@ E-MAIL = u@gogs.io require.NoError(t, err) assert.Equal(t, `[author] e-mail = u@gogs.io - `, buf.String(), ) @@ -517,7 +513,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`)) url = https://github.com/Antergone/test1.git url = https://github.com/Antergone/test2.git fetch = +refs/heads/*:refs/remotes/origin/* - `, buf.String(), ) @@ -822,7 +817,8 @@ my lesson state data – 1111111111111111111000000000000000001110000 111111111111111111100000000000111000000000 – end my lesson state data [COMMENTS] -<1> This slide has the fuel listed in the wrong units `)) +<1> This slide has the fuel listed in the wrong units +`)) require.NoError(t, err) require.NotNil(t, f) @@ -935,7 +931,6 @@ GITHUB = U;n;k;n;w;o;n require.NoError(t, err) assert.Equal(t, `[author] e-mail = u@gogs.io - `, buf.String(), ) @@ -1070,7 +1065,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`)) url = https://github.com/Antergone/test1.git url = https://github.com/Antergone/test2.git fetch = +refs/heads/*:refs/remotes/origin/* - `, buf.String(), ) @@ -1261,7 +1255,8 @@ my lesson state data – 1111111111111111111000000000000000001110000 111111111111111111100000000000111000000000 – end my lesson state data [COMMENTS] -<1> This slide has the fuel listed in the wrong units `)) +<1> This slide has the fuel listed in the wrong units +`)) require.NoError(t, err) require.NotNil(t, f) @@ -1413,7 +1408,6 @@ GITHUB = U;n;k;n;w;o;n require.NoError(t, err) assert.Equal(t, `[author] E-MAIL = u@gogs.io - `, buf.String(), ) @@ -1434,7 +1428,6 @@ E-MAIL = u@gogs.io require.NoError(t, err) assert.Equal(t, `[author] E-MAIL = u@gogs.io - `, buf.String(), ) @@ -1451,7 +1444,6 @@ E-MAIL = u@gogs.io [author] E-MAIL = u@gogs.io - `, buf.String(), ) diff --git a/key_test.go b/key_test.go index 83095d7..cbd9bed 100644 --- a/key_test.go +++ b/key_test.go @@ -604,7 +604,6 @@ region = us-west-2 s3 = max_concurrent_requests=10 max_queue_size=1000 - `, buf.String(), ) diff --git a/struct_test.go b/struct_test.go index 6bacaea..b51243a 100644 --- a/struct_test.go +++ b/struct_test.go @@ -169,7 +169,6 @@ AllowedIPs = 10.2.0.2/32,fd00:2::2/128 PublicKey = pubClientKey2 PresharedKey = psKey2 AllowedIPs = 10.2.0.3/32,fd00:2::3/128 - ` type unsupport struct { @@ -550,7 +549,6 @@ Populations = 12345678,98765432 Coordinates = 192.168,10.11 Flags = true,false None = - `, buf.String(), ) @@ -588,7 +586,6 @@ None = assert.Equal(t, `first_name = John last_name = Doe omitempty = 9 - `, buf.String(), ) @@ -623,7 +620,6 @@ enable = true type = type addr = address name = name - `, buf.String(), ) @@ -697,7 +693,6 @@ AllowedIPs = 10.2.0.4/32,fd00:2::4/128 PublicKey = pubClientKey4 PresharedKey = psKey4 AllowedIPs = 10.2.0.5/32,fd00:2::5/128 - `, buf.String(), ) @@ -723,7 +718,6 @@ PrivateKey = privServerKey PublicKey = pubClientKey5 PresharedKey = psKey5 AllowedIPs = 10.2.0.6/32,fd00:2::6/128 - `, buf.String(), ) @@ -759,7 +753,6 @@ path = /tmp/gpm-profiles/test1.profile`)) assert.Equal(t, `[include] path = /tmp/gpm-profiles/test5.profile path = /tmp/gpm-profiles/test1.profile - `, buf.String(), ) @@ -823,7 +816,6 @@ Coordinates = 10.11 Flags = true Flags = false None = - `, buf.String(), ) @@ -913,7 +905,6 @@ Title = Staff II Engineer [Employer "EMC"] Title = Consultant Engineer - `, buf.String(), ) diff --git a/testdata/TestFile_WriteTo.golden b/testdata/TestFile_WriteTo.golden index 885924f..ea13d2a 100644 --- a/testdata/TestFile_WriteTo.golden +++ b/testdata/TestFile_WriteTo.golden @@ -85,4 +85,3 @@ ADDRESS = """404 road, NotFound, State, 50000""" two_lines = how about continuation lines? lots_of_lines = "1 2 3 4 " -