@@ -17,8 +17,11 @@ package ini
17
17
import (
18
18
"bytes"
19
19
"io/ioutil"
20
+ "os"
21
+ "path/filepath"
20
22
"runtime"
21
23
"sort"
24
+ "strings"
22
25
"testing"
23
26
24
27
"github.com/stretchr/testify/assert"
@@ -535,3 +538,79 @@ v = 3
535
538
require .NoError (t , f .Reload ())
536
539
assert .Equal (t , []string {"1" , "2" , "3" }, f .Section ("slice" ).Key ("v" ).ValueWithShadows ())
537
540
}
541
+
542
+ // Ensures that LoadOptions are propagated with Empty()
543
+ func TestEmptyWithOptions (t * testing.T ) {
544
+ tests := []struct {
545
+ name string
546
+ opts LoadOptions
547
+ value string
548
+ wantQuotes bool
549
+ }{
550
+ {
551
+ name : "URL with fragment and IgnoreInlineComment" ,
552
+ opts : LoadOptions {IgnoreInlineComment : true },
553
+ value : "https://example.com/start#/" ,
554
+ wantQuotes : false ,
555
+ },
556
+ {
557
+ name : "URL with fragment without IgnoreInlineComment" ,
558
+ opts : LoadOptions {IgnoreInlineComment : false },
559
+ value : "https://example.com/start#/" ,
560
+ wantQuotes : true ,
561
+ },
562
+ {
563
+ name : "Regular value with #" ,
564
+ opts : LoadOptions {IgnoreInlineComment : true },
565
+ value : "value#comment" ,
566
+ wantQuotes : false ,
567
+ },
568
+ }
569
+
570
+ for _ , tt := range tests {
571
+ t .Run (tt .name , func (t * testing.T ) {
572
+ // Test with Empty()
573
+ cfg1 := Empty (tt .opts )
574
+ section1 , _ := cfg1 .NewSection ("test" )
575
+ section1 .Key ("url" ).SetValue (tt .value )
576
+
577
+ tempFile1 := filepath .Join (os .TempDir (), "ini-test-1.tmp" )
578
+ if err := cfg1 .SaveToIndent (tempFile1 , "" ); err != nil {
579
+ t .Fatalf ("Failed to save config 1: %v" , err )
580
+ }
581
+
582
+ content1 , err := os .ReadFile (tempFile1 )
583
+ if err != nil {
584
+ t .Fatalf ("Failed to read config 1: %v" , err )
585
+ }
586
+ os .Remove (tempFile1 )
587
+
588
+ hasQuotes1 := strings .Contains (string (content1 ), "`" + tt .value + "`" )
589
+ if hasQuotes1 != tt .wantQuotes {
590
+ t .Errorf ("Empty() quotation = %v, want %v" , hasQuotes1 , tt .wantQuotes )
591
+ }
592
+
593
+ // Compare with LoadSources for same behavior
594
+ cfg2 , _ := LoadSources (tt .opts , []byte ("" ))
595
+ section2 , _ := cfg2 .NewSection ("test" )
596
+ section2 .Key ("url" ).SetValue (tt .value )
597
+
598
+ tempFile2 := filepath .Join (os .TempDir (), "ini-test-2.tmp" )
599
+ if err := cfg2 .SaveToIndent (tempFile2 , "" ); err != nil {
600
+ t .Fatalf ("Failed to save config 2: %v" , err )
601
+ }
602
+
603
+ content2 , err := os .ReadFile (tempFile2 )
604
+ if err != nil {
605
+ t .Fatalf ("Failed to read config 2: %v" , err )
606
+ }
607
+ os .Remove (tempFile2 )
608
+
609
+ hasQuotes2 := strings .Contains (string (content2 ), "`" + tt .value + "`" )
610
+ if hasQuotes1 != hasQuotes2 {
611
+ t .Errorf ("Empty() and LoadSources behave differently: Empty=%v, LoadSources=%v" ,
612
+ hasQuotes1 , hasQuotes2 )
613
+ }
614
+ })
615
+ }
616
+ }
0 commit comments