forked from bigkevmcd/go-configparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser_test.go
155 lines (125 loc) · 4.05 KB
/
configparser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package configparser_test
import (
"io"
"os"
"path"
"strings"
"testing"
. "gopkg.in/check.v1"
"github.com/EloyTolosaDev/go-configparser"
)
func Test(t *testing.T) { TestingT(t) }
type ConfigParserSuite struct {
p *configparser.ConfigParser
}
var _ = Suite(&ConfigParserSuite{})
func (s *ConfigParserSuite) SetUpTest(c *C) {
s.p, _ = configparser.NewConfigParserFromFile("testdata/example.cfg")
}
// NewWithDefaults should add defaults to the configuration
func (s *ConfigParserSuite) TestNewWithDefaults(c *C) {
n := make(configparser.Dict)
n["testing"] = "value"
p, err := configparser.NewWithDefaults(n)
c.Assert(err, IsNil)
d := p.Defaults()
c.Assert(d["testing"], Equals, "value")
}
// NewWithDefaults should copy the items from the provided map
func (s *ConfigParserSuite) TestNewWithDefaultsCopied(c *C) {
n := make(configparser.Dict)
n["testing"] = "value"
p, err := configparser.NewWithDefaults(n)
c.Assert(err, IsNil)
n["testing2"] = "myvalue"
d := p.Defaults()
c.Assert(d["testing2"], Equals, "")
}
// SaveWithDelimiter(filename) should write the current state of the
// configuration to the provided filename with the specified delimiter
func (s *ConfigParserSuite) TestSaveWithDelimiter(c *C) {
p := configparser.New()
err := p.AddSection("testing")
c.Assert(err, IsNil)
err = p.Set("testing", "myoption", "value")
c.Assert(err, IsNil)
err = p.AddSection("othersection")
c.Assert(err, IsNil)
err = p.Set("othersection", "newoption", "novalue")
c.Assert(err, IsNil)
err = p.Set("othersection", "myoption", "myvalue")
c.Assert(err, IsNil)
tempfile := path.Join(c.MkDir(), "config.cfg")
err = p.SaveWithDelimiter(tempfile, "=")
c.Assert(err, IsNil)
f, err := os.Open(tempfile)
c.Assert(err, IsNil)
data, err := io.ReadAll(f)
c.Assert(err, IsNil)
f.Close()
c.Assert(string(data), Equals, "[othersection]\nmyoption = myvalue\nnewoption = novalue\n\n[testing]\nmyoption = value\n\n")
}
// Save(filename) should correctly write out the defaults section with the
// defaults
func (s *ConfigParserSuite) TestSaveWithDelimiterAndDefaults(c *C) {
n := make(configparser.Dict)
n["testing"] = "value"
p, err := configparser.NewWithDefaults(n)
c.Assert(err, IsNil)
err = p.AddSection("testing")
c.Assert(err, IsNil)
err = p.Set("testing", "myoption", "value")
c.Assert(err, IsNil)
err = p.AddSection("othersection")
c.Assert(err, IsNil)
err = p.Set("othersection", "newoption", "novalue")
c.Assert(err, IsNil)
err = p.Set("othersection", "myoption", "myvalue")
c.Assert(err, IsNil)
tempfile := path.Join(c.MkDir(), "config.cfg")
err = p.SaveWithDelimiter(tempfile, "=")
c.Assert(err, IsNil)
f, err := os.Open(tempfile)
c.Assert(err, IsNil)
data, err := io.ReadAll(f)
c.Assert(err, IsNil)
f.Close()
c.Assert(string(data), Equals, "[DEFAULT]\ntesting = value\n\n[othersection]\nmyoption = myvalue\nnewoption = novalue\n\n[testing]\nmyoption = value\n\n")
}
// ParseFromReader() parses the Config data from an io.Reader.
func (s *ConfigParserSuite) TestParseFromReader(c *C) {
parsed, err := configparser.ParseReader(strings.NewReader("[DEFAULT]\ntesting = value\n\n[othersection]\nmyoption = myvalue\nnewoption = novalue\nfinal = foo[bar]\n\n[testing]\nmyoption = value\nemptyoption\n\n"))
c.Assert(err, IsNil)
result, err := parsed.Items("othersection")
c.Assert(err, IsNil)
c.Assert(result, DeepEquals, configparser.Dict{
"myoption": "myvalue",
"newoption": "novalue",
"final": "foo[bar]",
})
}
// TestMultilineValue tests multiline value parsing.
func (s *ConfigParserSuite) TestMultilineValue(c *C) {
parsed, err := configparser.ParseReader(
strings.NewReader(`[DEFAULT]
testing = multiline
value
myoption = another
multiline
value
broken_option = this value will miss
its multiline
`),
)
c.Assert(err, IsNil)
result, err := parsed.Items("DEFAULT")
c.Assert(err, IsNil)
c.Assert(result, DeepEquals, configparser.Dict{
"testing": "multiline\nvalue",
"myoption": "another\nmultiline\nvalue",
"broken_option": "this value will miss",
})
}
func assertSuccessful(c *C, err error) {
c.Assert(err, IsNil)
}