Skip to content

Commit

Permalink
Fix creation of default section (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
tphilippeau-tower committed Nov 12, 2021
1 parent 6a280a6 commit 1c1c7ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions file.go
Expand Up @@ -168,8 +168,9 @@ func (f *File) SectionsByName(name string) ([]*Section, error) {
func (f *File) Section(name string) *Section {
sec, err := f.GetSection(name)
if err != nil {
// Note: It's OK here because the only possible error is empty section name,
// but if it's empty, this piece of code won't be executed.
if name == "" {
name = DefaultSection
}
sec, _ = f.NewSection(name)
return sec
}
Expand Down
28 changes: 28 additions & 0 deletions file_test.go
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"io/ioutil"
"runtime"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -264,6 +265,33 @@ VERSION = v1`))
assert.Equal(t, "ini", f.Section("").Key("name").String())
assert.Equal(t, "v1", f.Section("").Key("version").String())
})

Convey("Get section after deletion", t, func() {
f, err := ini.Load([]byte(`
[RANDOM]
`))
So(f, ShouldNotBeNil)
So(err, ShouldBeNil)
sectionNames := f.SectionStrings()
sort.Strings(sectionNames)
So(sectionNames, ShouldResemble, []string{ini.DefaultSection, "RANDOM"})

for _, currentSection := range sectionNames {
f.DeleteSection(currentSection)
}
Convey("Section recreated", func() {
for sectionParam, expectedSectionName := range map[string]string{
"": ini.DefaultSection,
"RANDOM": "RANDOM",
} {
sec := f.Section(sectionParam)
So(sec, ShouldNotBeNil)
So(sec.Name(), ShouldEqual, expectedSectionName)
}
})

})

}

func TestFile_Sections(t *testing.T) {
Expand Down

0 comments on commit 1c1c7ac

Please sign in to comment.