Skip to content

Commit

Permalink
LoadOptions was ignored with ini.Empty()
Browse files Browse the repository at this point in the history
This ensures we initialize all the relevant parsing settings when using
Empty().  Previously behavior differed between LoadSources and Empty()
  • Loading branch information
Ashley Penney committed Nov 5, 2024
1 parent b2f570e commit 0cd2eea
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
8 changes: 6 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ func Empty(opts ...LoadOptions) *File {
opt = opts[0]
}

// Ignore error here, we are sure our data is good.
f, _ := LoadSources(opt, []byte(""))
// Create file with empty data source
f := newFile(nil, opt)

// Force a parse of empty data to ensure options are properly set
_ = f.parse(bytes.NewBuffer(nil))

return f
}

Expand Down
79 changes: 79 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ package ini
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -535,3 +538,79 @@ v = 3
require.NoError(t, f.Reload())
assert.Equal(t, []string{"1", "2", "3"}, f.Section("slice").Key("v").ValueWithShadows())
}

// Ensures that LoadOptions are propagated with Empty()
func TestEmptyWithOptions(t *testing.T) {
tests := []struct {
name string
opts LoadOptions
value string
wantQuotes bool
}{
{
name: "URL with fragment and IgnoreInlineComment",
opts: LoadOptions{IgnoreInlineComment: true},
value: "https://example.com/start#/",
wantQuotes: false,
},
{
name: "URL with fragment without IgnoreInlineComment",
opts: LoadOptions{IgnoreInlineComment: false},
value: "https://example.com/start#/",
wantQuotes: true,
},
{
name: "Regular value with #",
opts: LoadOptions{IgnoreInlineComment: true},
value: "value#comment",
wantQuotes: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test with Empty()
cfg1 := Empty(tt.opts)
section1, _ := cfg1.NewSection("test")
section1.Key("url").SetValue(tt.value)

tempFile1 := filepath.Join(os.TempDir(), "ini-test-1.tmp")
if err := cfg1.SaveToIndent(tempFile1, ""); err != nil {
t.Fatalf("Failed to save config 1: %v", err)
}

content1, err := os.ReadFile(tempFile1)
if err != nil {
t.Fatalf("Failed to read config 1: %v", err)
}
os.Remove(tempFile1)

hasQuotes1 := strings.Contains(string(content1), "`"+tt.value+"`")
if hasQuotes1 != tt.wantQuotes {
t.Errorf("Empty() quotation = %v, want %v", hasQuotes1, tt.wantQuotes)
}

// Compare with LoadSources for same behavior
cfg2, _ := LoadSources(tt.opts, []byte(""))
section2, _ := cfg2.NewSection("test")
section2.Key("url").SetValue(tt.value)

tempFile2 := filepath.Join(os.TempDir(), "ini-test-2.tmp")
if err := cfg2.SaveToIndent(tempFile2, ""); err != nil {
t.Fatalf("Failed to save config 2: %v", err)
}

content2, err := os.ReadFile(tempFile2)
if err != nil {
t.Fatalf("Failed to read config 2: %v", err)
}
os.Remove(tempFile2)

hasQuotes2 := strings.Contains(string(content2), "`"+tt.value+"`")
if hasQuotes1 != hasQuotes2 {
t.Errorf("Empty() and LoadSources behave differently: Empty=%v, LoadSources=%v",
hasQuotes1, hasQuotes2)
}
})
}
}

0 comments on commit 0cd2eea

Please sign in to comment.