Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func (f *File) GetSection(name string) (*Section, error) {
return secs[0], err
}

// HasSection returns true if the file contains a section with given name.
func (f *File) HasSection(name string) bool {
section, _ := f.GetSection(name)
return section != nil
}

// SectionsByName returns all sections with given name.
func (f *File) SectionsByName(name string) ([]*Section, error) {
if len(name) == 0 {
Expand Down
14 changes: 14 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ func TestFile_GetSection(t *testing.T) {
})
}

func TestFile_HasSection(t *testing.T) {
f, err := Load(fullConf)
require.NoError(t, err)
require.NotNil(t, f)

sec := f.HasSection("author")
assert.True(t, sec)

t.Run("section not exists", func(t *testing.T) {
nonexistent := f.HasSection("404")
assert.False(t, nonexistent)
})
}

func TestFile_Section(t *testing.T) {
t.Run("get a section", func(t *testing.T) {
f, err := Load(fullConf)
Expand Down