From 339b9729b190347191beda1ef443b77ddaffc6c7 Mon Sep 17 00:00:00 2001 From: Thomas Philippeau Date: Fri, 15 Jan 2021 14:58:06 -0500 Subject: [PATCH 1/2] fix case where the user requests the empty section after removing the DEFAULT section also add go.mod to comply with go modules Change-Id: I38e9131b2c995bf28bff8475bf7e096d04edcc4b Signed-off-by: Thomas Philippeau --- bench_test.go | 2 +- file.go | 5 +++-- file_test.go | 30 +++++++++++++++++++++++++++++- go.mod | 5 +++++ go.sum | 13 +++++++++++++ ini_python_multiline_test.go | 2 +- ini_test.go | 2 +- key_test.go | 2 +- parser_test.go | 2 +- section_test.go | 2 +- struct_test.go | 3 +-- 11 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/bench_test.go b/bench_test.go index a1244a7..2ed34bf 100644 --- a/bench_test.go +++ b/bench_test.go @@ -17,7 +17,7 @@ package ini_test import ( "testing" - "gopkg.in/ini.v1" + "github.com/go-ini/ini" ) func newTestFile(block bool) *ini.File { diff --git a/file.go b/file.go index b96d172..1345918 100644 --- a/file.go +++ b/file.go @@ -168,8 +168,9 @@ func (f *File) SectionsByName(name string) ([]*Section, error) { func (f *File) Section(name string) *Section { sec, err := f.GetSection(name) if err != nil { - // Note: It's OK here because the only possible error is empty section name, - // but if it's empty, this piece of code won't be executed. + if name == "" { + name = DefaultSection + } sec, _ = f.NewSection(name) return sec } diff --git a/file_test.go b/file_test.go index 9a70d5e..e0b21a8 100644 --- a/file_test.go +++ b/file_test.go @@ -18,10 +18,11 @@ import ( "bytes" "io/ioutil" "runtime" + "sort" "testing" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) func TestEmpty(t *testing.T) { @@ -274,6 +275,33 @@ VERSION = v1`)) So(f.Section("").Key("name").String(), ShouldEqual, "ini") So(f.Section("").Key("version").String(), ShouldEqual, "v1") }) + + Convey("Get section after deletion", t, func() { + f, err := ini.Load([]byte(` +[RANDOM] +`)) + So(f, ShouldNotBeNil) + So(err, ShouldBeNil) + sectionNames := f.SectionStrings() + sort.Strings(sectionNames) + So(sectionNames, ShouldResemble, []string{ini.DefaultSection, "RANDOM"}) + + for _, currentSection := range sectionNames { + f.DeleteSection(currentSection) + } + Convey("Section recreated", func() { + for sectionParam, expectedSectionName := range map[string]string{ + "": ini.DefaultSection, + "RANDOM": "RANDOM", + } { + sec := f.Section(sectionParam) + So(sec, ShouldNotBeNil) + So(sec.Name(), ShouldEqual, expectedSectionName) + } + }) + + }) + } func TestFile_Sections(t *testing.T) { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6e75f15 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/go-ini/ini + +go 1.15 + +require github.com/smartystreets/goconvey v1.6.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a46a039 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= diff --git a/ini_python_multiline_test.go b/ini_python_multiline_test.go index 9070c29..d7dd068 100644 --- a/ini_python_multiline_test.go +++ b/ini_python_multiline_test.go @@ -5,8 +5,8 @@ import ( "runtime" "testing" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) type testData struct { diff --git a/ini_test.go b/ini_test.go index fb9a681..4680588 100644 --- a/ini_test.go +++ b/ini_test.go @@ -20,8 +20,8 @@ import ( "io/ioutil" "testing" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) const ( diff --git a/key_test.go b/key_test.go index 238f938..9ffe094 100644 --- a/key_test.go +++ b/key_test.go @@ -22,8 +22,8 @@ import ( "testing" "time" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) func TestKey_AddShadow(t *testing.T) { diff --git a/parser_test.go b/parser_test.go index bd3c6ac..8616c6b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -17,8 +17,8 @@ package ini_test import ( "testing" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) func TestBOM(t *testing.T) { diff --git a/section_test.go b/section_test.go index 37da867..7afd707 100644 --- a/section_test.go +++ b/section_test.go @@ -17,8 +17,8 @@ package ini_test import ( "testing" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/ini.v1" ) func TestSection_SetBody(t *testing.T) { diff --git a/struct_test.go b/struct_test.go index 65e6925..3f5c7d8 100644 --- a/struct_test.go +++ b/struct_test.go @@ -21,9 +21,8 @@ import ( "testing" "time" + "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" - - "gopkg.in/ini.v1" ) type testNested struct { From d2d403aa4dad5a24f302078817ae905f2c320a02 Mon Sep 17 00:00:00 2001 From: Thomas Philippeau Date: Fri, 15 Jan 2021 16:51:16 -0500 Subject: [PATCH 2/2] revert changes for go modules Change-Id: I6211ca388560e21c172ee870ee8066bdded61b95 --- bench_test.go | 2 +- file_test.go | 2 +- go.mod | 5 ----- go.sum | 13 ------------- ini_python_multiline_test.go | 2 +- ini_test.go | 2 +- key_test.go | 2 +- parser_test.go | 2 +- section_test.go | 2 +- struct_test.go | 3 ++- 10 files changed, 9 insertions(+), 26 deletions(-) delete mode 100644 go.mod delete mode 100644 go.sum diff --git a/bench_test.go b/bench_test.go index 2ed34bf..a1244a7 100644 --- a/bench_test.go +++ b/bench_test.go @@ -17,7 +17,7 @@ package ini_test import ( "testing" - "github.com/go-ini/ini" + "gopkg.in/ini.v1" ) func newTestFile(block bool) *ini.File { diff --git a/file_test.go b/file_test.go index e0b21a8..096b02c 100644 --- a/file_test.go +++ b/file_test.go @@ -21,8 +21,8 @@ import ( "sort" "testing" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestEmpty(t *testing.T) { diff --git a/go.mod b/go.mod deleted file mode 100644 index 6e75f15..0000000 --- a/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/go-ini/ini - -go 1.15 - -require github.com/smartystreets/goconvey v1.6.4 diff --git a/go.sum b/go.sum deleted file mode 100644 index a46a039..0000000 --- a/go.sum +++ /dev/null @@ -1,13 +0,0 @@ -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= -github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= diff --git a/ini_python_multiline_test.go b/ini_python_multiline_test.go index d7dd068..9070c29 100644 --- a/ini_python_multiline_test.go +++ b/ini_python_multiline_test.go @@ -5,8 +5,8 @@ import ( "runtime" "testing" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) type testData struct { diff --git a/ini_test.go b/ini_test.go index 4680588..fb9a681 100644 --- a/ini_test.go +++ b/ini_test.go @@ -20,8 +20,8 @@ import ( "io/ioutil" "testing" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) const ( diff --git a/key_test.go b/key_test.go index 9ffe094..238f938 100644 --- a/key_test.go +++ b/key_test.go @@ -22,8 +22,8 @@ import ( "testing" "time" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestKey_AddShadow(t *testing.T) { diff --git a/parser_test.go b/parser_test.go index 8616c6b..bd3c6ac 100644 --- a/parser_test.go +++ b/parser_test.go @@ -17,8 +17,8 @@ package ini_test import ( "testing" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestBOM(t *testing.T) { diff --git a/section_test.go b/section_test.go index 7afd707..37da867 100644 --- a/section_test.go +++ b/section_test.go @@ -17,8 +17,8 @@ package ini_test import ( "testing" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestSection_SetBody(t *testing.T) { diff --git a/struct_test.go b/struct_test.go index 3f5c7d8..65e6925 100644 --- a/struct_test.go +++ b/struct_test.go @@ -21,8 +21,9 @@ import ( "testing" "time" - "github.com/go-ini/ini" . "github.com/smartystreets/goconvey/convey" + + "gopkg.in/ini.v1" ) type testNested struct {