+
Skip to content
Open
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
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)
}
})
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载