add new binutils 1259 grant temporary name
[utils.git] / src / budget_sync / test / test_config.py
1 import unittest
2 import io
3 from budget_sync.config import Config, ConfigParseError
4
5
6 class TestConfig(unittest.TestCase):
7 maxDiff = None
8
9 def test_config_parsing(self):
10 def check_error(text: str, expected_error_text: str):
11 with self.assertRaises(ConfigParseError) as e:
12 Config.from_str(text)
13 self.assertEqual(str(e.exception), expected_error_text)
14
15 def check(text: str, expected_repr_text: str):
16 self.assertEqual(repr(Config.from_str(text)), expected_repr_text)
17
18 check_error(
19 "bad-toml=",
20 "TOML parse error: Empty value is invalid "
21 "(line 1 column 1 char 0)")
22 check_error(
23 """
24 """,
25 "`bugzilla_url` field is missing")
26 check_error(
27 """
28 [bugzilla_url]
29 """,
30 "`bugzilla_url` must be a string")
31 check_error(
32 """
33 bugzilla_url = ""
34 """,
35 "`people` table is missing")
36 check_error(
37 """
38 blah = ""
39 """,
40 "unknown config entry: `blah`")
41 check_error(
42 """
43 bugzilla_url = ""
44 people = []
45 """,
46 "`people` field must be a table")
47 check_error(
48 """
49 bugzilla_url = ""
50 [people]
51 person1 = 1
52 """,
53 "person entry for 'person1' must be a table")
54 check_error(
55 """
56 bugzilla_url = ""
57 [people."person1"]
58 aliases = ""
59 """,
60 "`aliases` field in person entry for 'person1' must be a list "
61 "of strings")
62 check_error(
63 """
64 bugzilla_url = ""
65 [people."person1"]
66 aliases = [1]
67 """,
68 "`aliases` field in person entry for 'person1' must be a list "
69 "of strings")
70 check_error(
71 """
72 bugzilla_url = ""
73 [people."person1"]
74 aliases = ["a", "a"]
75 """,
76 "duplicate alias in person entry for 'person1': 'a'")
77 check_error(
78 """
79 bugzilla_url = ""
80 [people]
81 """,
82 "`milestones` table is missing")
83 check_error(
84 """
85 bugzilla_url = ""
86 [people."person1"]
87 """,
88 "`full_name` field is missing in person entry for "
89 "'person1'")
90 check_error(
91 """
92 bugzilla_url = ""
93 [people."person1"]
94 full_name = 1
95 """,
96 "`full_name` field in person entry for 'person1' must "
97 "be a string")
98 check(
99 """
100 bugzilla_url = ""
101 [milestones]
102 [people."person1"]
103 aliases = ["a"]
104 full_name = "Person One"
105 [people."person2"]
106 aliases = ["b"]
107 full_name = "Person Two"
108 """,
109 "Config(bugzilla_url='', people={"
110 "'person1': Person(config=..., identifier='person1', "
111 "full_name='Person One', "
112 "aliases=OrderedSet(['a']), email=None), "
113 "'person2': Person(config=..., identifier='person2', "
114 "full_name='Person Two', "
115 "aliases=OrderedSet(['b']), email=None)}, milestones={})")
116 check_error(
117 """
118 bugzilla_url = ""
119 [people."person1"]
120 email = 123
121 full_name = "Person One"
122 """,
123 "`email` field in person entry for 'person1' must be a string")
124 check(
125 """
126 bugzilla_url = ""
127 [people]
128 [milestones]
129 """,
130 "Config(bugzilla_url='', people={}, milestones={})")
131 check(
132 """
133 bugzilla_url = ""
134 [milestones]
135 [people."person1"]
136 email = "email@example.com"
137 full_name = "Person One"
138 """,
139 "Config(bugzilla_url='', people={"
140 "'person1': Person(config=..., identifier='person1', "
141 "full_name='Person One', "
142 "aliases=OrderedSet(), email='email@example.com')}, "
143 "milestones={})")
144 check_error(
145 """
146 bugzilla_url = ""
147 [people."person1"]
148 blah = 123
149 full_name = "Person One"
150 """,
151 "unknown field in person entry for 'person1': `blah`")
152 check_error(
153 """
154 bugzilla_url = ""
155 [milestones]
156 [people."person1"]
157 full_name = "Person One"
158 [people."person2"]
159 aliases = ["person1"]
160 full_name = "Person Two"
161 """,
162 "alias is not allowed to be the same as any person's identifier: "
163 "in person entry for 'person2': 'person1' is also the identifier "
164 "for person 'person1'")
165 check_error(
166 """
167 bugzilla_url = ""
168 [milestones]
169 [people."person1"]
170 full_name = "Person One"
171 aliases = ["a"]
172 [people."person2"]
173 aliases = ["a"]
174 full_name = "Person Two"
175 """,
176 "alias is not allowed to be the same as another person's alias, "
177 "email, or full_name: in person entry for 'person2': 'a' is also an alias, "
178 "email, or full_name for person 'person1'")
179 check_error(
180 """
181 bugzilla_url = ""
182 [milestones]
183 [people."person1"]
184 full_name = "Person One"
185 aliases = ["abc@example.com"]
186 [people."person2"]
187 email = "abc@example.com"
188 full_name = "Person Two"
189 """,
190 "email is not allowed to be the same as another person's alias, "
191 "email, or full_name: in person entry for 'person2': 'abc@example.com' is also "
192 "an alias, email, or full_name for person 'person1'")
193 check_error(
194 """
195 bugzilla_url = ""
196 [milestones]
197 [people."person1"]
198 full_name = "Person One"
199 aliases = ["Person Two"]
200 [people."person2"]
201 email = "abc@example.com"
202 full_name = "Person Two"
203 """,
204 "full_name is not allowed to be the same as another person's alias, "
205 "email, or full_name: in person entry for 'person2': 'Person Two' is also "
206 "an alias, email, or full_name for person 'person1'")
207 check_error(
208 """
209 bugzilla_url = ""
210 [milestones]
211 [people."person2"]
212 email = "abc@example.com"
213 full_name = "Person Two"
214 [people."person1"]
215 full_name = "Person One"
216 aliases = ["abc@example.com"]
217 """,
218 "alias is not allowed to be the same as another person's alias, "
219 "email, or full_name: in person entry for 'person1': 'abc@example.com' is also "
220 "an alias, email, or full_name for person 'person2'")
221 check_error(
222 """
223 bugzilla_url = ""
224 [milestones]
225 "abc" = 1
226 [people]
227 """,
228 "milestones entry for 'abc' must be a table")
229 check_error(
230 """
231 bugzilla_url = ""
232 [milestones]
233 "abc" = { canonical_bug_id = "abc" }
234 [people]
235 """,
236 "`canonical_bug_id` field in milestones entry for 'abc' must "
237 "be an integer")
238 check_error(
239 """
240 bugzilla_url = ""
241 [milestones]
242 "abc" = { blah = "def" }
243 [people]
244 """,
245 "unknown field in milestones entry for 'abc': `blah`")
246 check_error(
247 """
248 bugzilla_url = ""
249 [milestones]
250 "abc" = {}
251 [people]
252 """,
253 "`canonical_bug_id` field is missing in milestones entry for 'abc'")
254 check_error(
255 """
256 bugzilla_url = ""
257 milestones = 1
258 [people]
259 """,
260 "`milestones` field must be a table")
261 check_error(
262 """
263 bugzilla_url = ""
264 [milestones]
265 "abc" = { canonical_bug_id = 1 }
266 "def" = { canonical_bug_id = 1 }
267 [people]
268 """,
269 "canonical_bug_id is not allowed to be the same as another "
270 "milestone's canonical_bug_id: in milestone entry for 'def': "
271 "1 is also the canonical_bug_id for milestone 'abc'")
272
273 def test_all_names(self):
274 config = Config.from_str(
275 """
276 bugzilla_url = ""
277 [milestones]
278 [people."person1"]
279 aliases = ["person1_alias1", "alias1"]
280 full_name = "Person One"
281 [people."person2"]
282 aliases = ["person2_alias2", "alias2"]
283 full_name = "Person Two"
284 """)
285 person1 = config.people['person1']
286 person2 = config.people['person2']
287 self.assertEqual(config.all_names,
288 {
289 'person1': person1,
290 'Person One': person1,
291 'person1_alias1': person1,
292 'alias1': person1,
293 'person2': person2,
294 'Person Two': person2,
295 'person2_alias2': person2,
296 'alias2': person2,
297 })
298
299 def test_canonical_bug_ids(self):
300 config = Config.from_str(
301 """
302 bugzilla_url = ""
303 [people]
304 [milestones]
305 "Milestone 1" = { canonical_bug_id = 1 }
306 "Milestone 2" = { canonical_bug_id = 2 }
307 """)
308 milestone1 = config.milestones['Milestone 1']
309 milestone2 = config.milestones['Milestone 2']
310 self.assertEqual(config.canonical_bug_ids,
311 {
312 1: milestone1,
313 2: milestone2,
314 })
315
316 def test_bugzilla_url_stripped(self):
317 c = Config.from_str(
318 """
319 bugzilla_url = "https://bugzilla.example.com/prefix"
320 [people]
321 [milestones]
322 """
323 )
324 self.assertEqual(c.bugzilla_url_stripped,
325 "https://bugzilla.example.com/prefix")
326 c = Config.from_str(
327 """
328 bugzilla_url = "https://bugzilla.example.com/prefix/"
329 [people]
330 [milestones]
331 """
332 )
333 self.assertEqual(c.bugzilla_url_stripped,
334 "https://bugzilla.example.com/prefix")
335 c = Config.from_str(
336 """
337 bugzilla_url = "https://bugzilla.example.com/"
338 [people]
339 [milestones]
340 """
341 )
342 self.assertEqual(c.bugzilla_url_stripped,
343 "https://bugzilla.example.com")
344
345 def test_from_file(self):
346 def load(text):
347 with io.StringIO(text) as file:
348 return Config.from_file(file)
349
350 with self.assertRaisesRegex(TypeError,
351 "^list is not a valid file or path$"):
352 Config.from_file([])
353
354 with self.assertRaisesRegex(
355 ConfigParseError,
356 "^TOML parse error: Empty value is invalid"):
357 load("""bad-toml=""")
358
359 self.assertEqual(str(load(
360 """
361 bugzilla_url = "https://bugzilla.example.com/"
362 [people."person1"]
363 email = "person1@example.com"
364 aliases = ["alias1"]
365 full_name = "Person One"
366 [milestones]
367 "Milestone 1" = { canonical_bug_id = 123 }
368 """)),
369 "Config(bugzilla_url='https://bugzilla.example.com/', "
370 "people={'person1': Person(config=..., identifier='person1', "
371 "full_name='Person One', "
372 "aliases=OrderedSet(['alias1']), email='person1@example.com')}, "
373 "milestones={'Milestone 1': Milestone(config=..., "
374 "identifier='Milestone 1', canonical_bug_id=123)})")
375
376
377 if __name__ == "__main__":
378 unittest.main()