From f96a66efe1c5fc260ee21bc24ccbeba076477a09 Mon Sep 17 00:00:00 2001
From: yuangongji
Date: Mon, 10 Aug 2020 09:53:25 +0800
Subject: [PATCH] file:fix deleting the default section makes the next section
inaccessible
---
file.go | 2 +-
file_test.go | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/file.go b/file.go
index f95606f..733ab28 100644
--- a/file.go
+++ b/file.go
@@ -347,7 +347,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
}
- if i > 0 || DefaultHeader {
+ if i > 0 || DefaultHeader || (i == 0 && strings.ToUpper(sec.name) != DefaultSection) {
if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
return nil, err
}
diff --git a/file_test.go b/file_test.go
index ef18bc0..e956014 100644
--- a/file_test.go
+++ b/file_test.go
@@ -332,6 +332,25 @@ func TestFile_DeleteSection(t *testing.T) {
f.DeleteSection("")
So(f.SectionStrings(), ShouldResemble, []string{"author", "package"})
})
+
+ Convey("Delete default section", t, func() {
+ f := ini.Empty()
+ So(f, ShouldNotBeNil)
+
+ f.Section("").Key("foo").SetValue("bar")
+ f.Section("section1").Key("key1").SetValue("value1")
+ f.DeleteSection("")
+ So(f.SectionStrings(), ShouldResemble, []string{"section1"})
+
+ var buf bytes.Buffer
+ _, err := f.WriteTo(&buf)
+ So(err, ShouldBeNil)
+
+ So(buf.String(), ShouldEqual, `[section1]
+key1 = value1
+
+`)
+ })
}
func TestFile_Append(t *testing.T) {