Skip to content

Commit 1c1c7ac

Browse files
Fix creation of default section (#301)
1 parent 6a280a6 commit 1c1c7ac

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ func (f *File) SectionsByName(name string) ([]*Section, error) {
168168
func (f *File) Section(name string) *Section {
169169
sec, err := f.GetSection(name)
170170
if err != nil {
171-
// Note: It's OK here because the only possible error is empty section name,
172-
// but if it's empty, this piece of code won't be executed.
171+
if name == "" {
172+
name = DefaultSection
173+
}
173174
sec, _ = f.NewSection(name)
174175
return sec
175176
}

file_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"io/ioutil"
2020
"runtime"
21+
"sort"
2122
"testing"
2223

2324
"github.com/stretchr/testify/assert"
@@ -264,6 +265,33 @@ VERSION = v1`))
264265
assert.Equal(t, "ini", f.Section("").Key("name").String())
265266
assert.Equal(t, "v1", f.Section("").Key("version").String())
266267
})
268+
269+
Convey("Get section after deletion", t, func() {
270+
f, err := ini.Load([]byte(`
271+
[RANDOM]
272+
`))
273+
So(f, ShouldNotBeNil)
274+
So(err, ShouldBeNil)
275+
sectionNames := f.SectionStrings()
276+
sort.Strings(sectionNames)
277+
So(sectionNames, ShouldResemble, []string{ini.DefaultSection, "RANDOM"})
278+
279+
for _, currentSection := range sectionNames {
280+
f.DeleteSection(currentSection)
281+
}
282+
Convey("Section recreated", func() {
283+
for sectionParam, expectedSectionName := range map[string]string{
284+
"": ini.DefaultSection,
285+
"RANDOM": "RANDOM",
286+
} {
287+
sec := f.Section(sectionParam)
288+
So(sec, ShouldNotBeNil)
289+
So(sec.Name(), ShouldEqual, expectedSectionName)
290+
}
291+
})
292+
293+
})
294+
267295
}
268296

269297
func TestFile_Sections(t *testing.T) {

0 commit comments

Comments
 (0)