-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathtest_load.py
More file actions
293 lines (245 loc) · 8.94 KB
/
test_load.py
File metadata and controls
293 lines (245 loc) · 8.94 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import configparser
import itertools
import os
from pathlib import Path
import sys
import textwrap
from unittest import TestCase
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
from bugwarrior.config import load
from ..base import ConfigTest
class LoadTest(ConfigTest):
def create(self, path):
"""
Create an empty file in the temporary directory, return the full path.
"""
fpath = os.path.join(self.tempdir, path)
if not os.path.exists(os.path.dirname(fpath)):
os.makedirs(os.path.dirname(fpath))
open(fpath, 'a').close()
return fpath
class ExampleTest(ConfigTest):
def setUp(self):
self.basedir = Path(__file__).parent
super().setUp()
def test_example(self):
for rcfile in ('example-bugwarriorrc', 'example-bugwarrior.toml'):
with self.subTest(rcfile=rcfile):
os.environ['BUGWARRIORRC'] = str(self.basedir / rcfile)
load.load_config('general', False, False)
class TestGetConfigPath(LoadTest):
def test_path_precedence(self):
# We're going to manually setup and teardown each subTest.
self.tearDown()
config_paths = [ # ordered by precedence
'.config/bugwarrior/bugwarriorrc',
'.config/bugwarrior/bugwarrior.toml',
'.bugwarriorrc',
'.bugwarrior.toml',
]
# https://docs.python.org/3/library/itertools.html#itertools.combinations
# > The combination tuples are emitted in lexicographic ordering
# > according to the order of the input iterable. So, if the input
# > iterable is sorted, the output tuples will be produced in sorted
# > order.
# So as long as the path list is in the correct order, path1 should have
# precedence.
for path1, path2 in itertools.combinations(config_paths, 2):
with self.subTest(path1=path1, path2=path2):
self.setUp()
try:
config1 = self.create(path1)
self.create(path2)
self.assertEqual(load.get_config_path(), config1)
finally:
self.tearDown()
def test_legacy(self):
"""
Falls back on .bugwarriorrc if it exists
"""
rc = self.create('.bugwarriorrc')
self.assertEqual(load.get_config_path(), rc)
def test_no_file(self):
"""
If no bugwarriorrc exist anywhere, the path to the prefered one is
returned.
"""
self.assertEqual(
load.get_config_path(),
os.path.join(self.tempdir, '.config/bugwarrior/bugwarriorrc'),
)
def test_BUGWARRIORRC(self):
"""
If $BUGWARRIORRC is set, it takes precedence over everything else (even
if the file doesn't exist).
"""
rc = os.path.join(self.tempdir, 'my-bugwarriorc')
os.environ['BUGWARRIORRC'] = rc
self.create('.bugwarriorrc')
self.create('.config/bugwarrior/bugwarriorrc')
self.assertEqual(load.get_config_path(), rc)
def test_BUGWARRIORRC_empty(self):
"""
If $BUGWARRIORRC is set but empty, it is not used and the default file
is used instead.
"""
os.environ['BUGWARRIORRC'] = ''
rc = self.create('.config/bugwarrior/bugwarriorrc')
self.assertEqual(load.get_config_path(), rc)
class TestBugwarriorConfigParser(TestCase):
def setUp(self):
self.config = load.BugwarriorConfigParser()
self.config['general'] = {
'someint': '4',
'somenone': '',
'somechar': 'somestring',
}
def test_getint(self):
self.assertEqual(self.config.getint('general', 'someint'), 4)
def test_getint_none(self):
self.assertEqual(self.config.getint('general', 'somenone'), None)
def test_getint_valueerror(self):
with self.assertRaises(ValueError):
self.config.getint('general', 'somechar')
class TestParseFile(LoadTest):
def test_toml(self):
config_path = self.create('.bugwarrior.toml')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general]
foo = "bar"
""")
)
load.parse_file(config_path)
def test_ini(self):
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general]
foo = bar
""")
)
config = load.parse_file(config_path)
self.assertEqual(
config, {'flavor': {'general': {'foo': 'bar'}}, 'services': []}
)
def test_toml_invalid(self):
config_path = self.create('.bugwarrior.toml')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general
foo = "bar"
""")
)
with self.assertRaises(tomllib.TOMLDecodeError):
load.parse_file(config_path)
def test_ini_invalid(self):
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general
foo = bar
""")
)
with self.assertRaises(configparser.MissingSectionHeaderError):
load.parse_file(config_path)
def test_toml_flavors(self):
config_path = self.create('.bugwarrior.toml')
with open(config_path, 'w') as fout:
fout.write('[flavor.myflavor]\ntargets = ["my_gitlab"]')
config = load.parse_file(config_path)
self.assertEqual(
config, {'flavor': {'myflavor': {'targets': ['my_gitlab']}}, 'services': []}
)
def test_ini_flavors(self):
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[flavor.myflavor]
targets = my_gitlab
""")
)
config = load.parse_file(config_path)
self.assertEqual(
config, {'flavor': {'myflavor': {'targets': 'my_gitlab'}}, 'services': []}
)
def test_ini_options_renamed(self):
"""
Prefixes are removed and log.* are renamed log_* in main section.
"""
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general]
foo = bar
log.level = DEBUG
[baz]
service = qux
qux.optionname
""")
)
config = load.parse_file(config_path)
baz_service = next(svc for svc in config['services'] if svc['target'] == 'baz')
self.assertIn('optionname', baz_service)
self.assertNotIn('prefix.optionname', baz_service)
self.assertIn('log_level', config['flavor']['general'])
self.assertNotIn('log.level', config['flavor']['general'])
def test_ini_missing_prefix(self):
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general]
foo = bar
[baz]
service = qux
optionname
""")
)
with self.assertRaises(SystemExit):
load.parse_file(config_path)
def test_ini_wrong_prefix(self):
config_path = self.create('.bugwarriorrc')
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[general]
foo = bar
[baz]
service = qux
wrong.optionname
""")
)
with self.assertRaises(SystemExit):
load.parse_file(config_path)
class TestLoadConfig(LoadTest):
def setUp(self):
self.basedir = Path(__file__).parent
super().setUp()
def test_main_section_does_not_exist(self):
config_path = self.create(".bugwarriorrc")
with open(config_path, 'w') as fout:
fout.write(
textwrap.dedent("""
[redmine]
service = redmine
redmine.url = example.com
""")
)
with self.assertRaises(SystemExit):
load.load_config("general", False, False)
self.assertEqual(len(self.caplog.records), 1)
self.assertIn("No section: 'general'", self.caplog.records[0].message)
def test_interactive_flag_propagated(self):
os.environ['BUGWARRIORRC'] = str(self.basedir / 'example-bugwarriorrc')
config = load.load_config('general', interactive=True, quiet=False)
self.assertTrue(config.main.interactive)