From 92e2c92ae78286e6afda6ee264fc25416805e9db Mon Sep 17 00:00:00 2001 From: FFspiky Date: Tue, 17 Nov 2026 10:15:09 +0800 Subject: [PATCH 1/2] Normalize default section names in case-insensitive files --- file.go | 4 +-- file_test.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/file.go b/file.go index b58f26f..c4774f7 100644 --- a/file.go +++ b/file.go @@ -103,7 +103,7 @@ func (f *File) addSection(name string) (*Section, error) { return nil, errors.New("empty section name") } - if (f.options.Insensitive || f.options.InsensitiveSections) && name != f.options.DefaultSection { + if f.options.Insensitive || f.options.InsensitiveSections { name = strings.ToLower(name) } @@ -222,7 +222,7 @@ func (f *File) SectionWithIndex(name string, index int) *Section { func (f *File) missingSection(name string, index int) *Section { if name == "" { name = f.defaultSectionName() - } else if (f.options.Insensitive || f.options.InsensitiveSections) && name != f.options.DefaultSection { + } else if f.options.Insensitive || f.options.InsensitiveSections { name = strings.ToLower(name) } sec := newSection(f, name) diff --git a/file_test.go b/file_test.go index 45e3aa7..ab77940 100644 --- a/file_test.go +++ b/file_test.go @@ -56,6 +56,90 @@ func TestFile_NewSection(t *testing.T) { }) } +func TestFile_CaseInsensitiveDefaultSection(t *testing.T) { + for _, name := range []string{DefaultSection, "Profile"} { + for _, tc := range []struct { + name string + opt LoadOption + }{ + {"all", CaseInsensitive(true)}, + {"sections", CaseInsensitiveSections(true)}, + } { + t.Run(name+"/"+tc.name, func(t *testing.T) { + opts := []LoadOption{DefaultSectionName(name), tc.opt} + wantName := strings.ToLower(name) + + t.Run("parse", func(t *testing.T) { + f, err := Load([]byte("before=1\n["+name+"]\nafter=2\n"), opts...) + require.NoError(t, err) + assert.Equal(t, []string{wantName}, f.SectionNames()) + assert.Equal(t, map[string]string{"before": "1", "after": "2"}, f.Section("").KeyValues()) + var buf bytes.Buffer + _, err = f.WriteTo(&buf) + require.NoError(t, err) + again, err := Load(buf.Bytes(), opts...) + require.NoError(t, err) + assert.Equal(t, f.SectionNames(), again.SectionNames()) + assert.Equal(t, f.Section("").KeyValues(), again.Section("").KeyValues()) + }) + + t.Run("new", func(t *testing.T) { + f := Empty(opts...) + original := f.Section("") + sec, err := f.NewSection(name) + require.NoError(t, err) + assert.Same(t, original, sec) + sec.Key("port").SetValue("8080") + assert.Equal(t, "8080", f.Section("").Key("port").Value()) + assert.Equal(t, []string{wantName}, f.SectionNames()) + }) + + t.Run("missing", func(t *testing.T) { + f := Empty(opts...) + f.DeleteSection("") + byName, byEmpty := f.Section(name), f.Section("") + assert.Empty(t, f.Sections(), "reading a missing section must not add it") + byName.Key("host").SetValue("localhost") + assert.Equal(t, "localhost", byEmpty.Key("host").Value()) + assert.Same(t, byName, f.Section("")) + assert.Equal(t, []string{wantName}, f.SectionNames()) + f.DeleteSection(name) + assert.Empty(t, f.Sections()) + }) + + t.Run("nonunique", func(t *testing.T) { + f := Empty(append(opts, AllowNonUniqueSections(true))...) + sec, err := f.NewSection(name) + require.NoError(t, err) + sec.Key("value").SetValue("second") + f.SectionWithIndex(name, 2).Key("value").SetValue("third") + sections, err := f.SectionsByName("") + require.NoError(t, err) + require.Len(t, sections, 3) + assert.Equal(t, "second", sections[1].Key("value").Value()) + assert.Equal(t, "third", sections[2].Key("value").Value()) + }) + }) + } + } +} + +func TestFile_DefaultSectionKeepsCaseWithoutInsensitiveSections(t *testing.T) { + for _, opt := range []LoadOption{CaseInsensitive(false), CaseInsensitiveKeys(true)} { + f, err := Load([]byte("before=1\n[Profile]\nafter=2\n[profile]\nother=3\n"), DefaultSectionName("Profile"), opt) + require.NoError(t, err) + assert.Equal(t, []string{"Profile", "profile"}, f.SectionNames()) + assert.Equal(t, map[string]string{"before": "1", "after": "2"}, f.Section("").KeyValues()) + sec, err := f.NewSection("Profile") + require.NoError(t, err) + assert.Same(t, f.Section(""), sec) + f.DeleteSection("") + f.Section("Profile").Key("value").SetValue("new") + assert.Equal(t, "new", f.Section("").Key("value").Value()) + assert.Equal(t, "3", f.Section("profile").Key("other").Value()) + } +} + func TestFile_NonUniqueSection(t *testing.T) { t.Run("read and write non-unique sections", func(t *testing.T) { f, err := Load([]byte(`[Interface] From c7317f648979f3de60bbda3445abf3a3894ba2e8 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Tue, 17 Nov 2026 03:47:03 +0000 Subject: [PATCH 2/2] Drop default section case special-casing made redundant by addSection addSection now lowercases the default section name in case-insensitive files, so the lexer no longer needs to, and no section is stored under the configured name in a different case. --- file.go | 2 +- lexer.go | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/file.go b/file.go index c4774f7..df7777b 100644 --- a/file.go +++ b/file.go @@ -50,7 +50,7 @@ func Empty(opts ...LoadOption) *File { // section's name. Loading with CaseInsensitive or CaseInsensitiveSections // stores it lower-cased. func (f *File) isDefaultSection(name string) bool { - return name == f.options.DefaultSection || name == f.defaultSectionName() + return name == f.defaultSectionName() } // defaultSectionName returns the name the default section is stored under. diff --git a/lexer.go b/lexer.go index cc14a00..ab4af40 100644 --- a/lexer.go +++ b/lexer.go @@ -399,11 +399,7 @@ func (f *File) parse(reader io.Reader) error { l := &lexer{src: skipBOM(b.String()), opts: &f.options} // Ignore error because default section name is never empty string. - name := f.options.DefaultSection - if f.options.Insensitive || f.options.InsensitiveSections { - name = strings.ToLower(name) - } - section, _ := f.addSection(name) + section, _ := f.addSection(f.options.DefaultSection) var lastRegularKey *Key count := 1 // Counter for auto-increment key names ("-"), per section.