3 from budget_sync
.config
import Config
, ConfigParseError
6 class TestConfig(unittest
.TestCase
):
7 def test_config_parsing(self
):
8 def check_error(text
: str, expected_error_text
: str):
9 with self
.assertRaises(ConfigParseError
) as e
:
11 self
.assertEqual(str(e
.exception
), expected_error_text
)
13 def check(text
: str, expected_repr_text
: str):
14 self
.assertEqual(repr(Config
.from_str(text
)), expected_repr_text
)
18 "TOML parse error: Empty value is invalid "
19 "(line 1 column 1 char 0)")
23 "`bugzilla_url` field is missing")
28 "`bugzilla_url` must be a string")
33 "`people` table is missing")
38 "unknown config entry: `blah`")
44 "`people` field must be a table")
51 "person entry for 'person1' must be a table")
58 "`aliases` field in person entry for 'person1' must be a list "
66 "`aliases` field in person entry for 'person1' must be a list "
74 "duplicate alias in person entry for 'person1': 'a'")
80 "`milestones` table is missing")
86 "`output_markdown_file` field is missing in person entry for "
92 output_markdown_file = 1
94 "`output_markdown_file` field in person entry for 'person1' must "
102 output_markdown_file = "person1.mdwn"
105 output_markdown_file = "person2.mdwn"
107 "Config(bugzilla_url='', people={"
108 "'person1': Person(config=..., identifier='person1', "
109 "output_markdown_file='person1.mdwn', "
110 "aliases={'a'}, email=None), "
111 "'person2': Person(config=..., identifier='person2', "
112 "output_markdown_file='person2.mdwn', "
113 "aliases={'b'}, email=None)}, milestones={})")
119 output_markdown_file = "person1.mdwn"
121 "`email` field in person entry for 'person1' must be a string")
128 "Config(bugzilla_url='', people={}, milestones={})")
134 email = "email@example.com"
135 output_markdown_file = "person1.mdwn"
137 "Config(bugzilla_url='', people={"
138 "'person1': Person(config=..., identifier='person1', "
139 "output_markdown_file='person1.mdwn', "
140 "aliases=set(), email='email@example.com')}, milestones={})")
146 output_markdown_file = "person1.mdwn"
148 "unknown field in person entry for 'person1': `blah`")
154 output_markdown_file = "person1.mdwn"
156 aliases = ["person1"]
157 output_markdown_file = "person2.mdwn"
159 "alias is not allowed to be the same as any person's identifier: "
160 "in person entry for 'person2': 'person1' is also the identifier "
161 "for person 'person1'")
167 output_markdown_file = "person1.mdwn"
171 output_markdown_file = "person2.mdwn"
173 "alias is not allowed to be the same as another person's alias or "
174 "email: in person entry for 'person2': 'a' is also an alias or "
175 "email for person 'person1'")
181 output_markdown_file = "person1.mdwn"
182 aliases = ["abc@example.com"]
184 email = "abc@example.com"
185 output_markdown_file = "person2.mdwn"
187 "email is not allowed to be the same as another person's alias or "
188 "email: in person entry for 'person2': 'abc@example.com' is also "
189 "an alias or email for person 'person1'")
195 email = "abc@example.com"
196 output_markdown_file = "person2.mdwn"
198 output_markdown_file = "person1.mdwn"
199 aliases = ["abc@example.com"]
201 "alias is not allowed to be the same as another person's alias or "
202 "email: in person entry for 'person1': 'abc@example.com' is also "
203 "an alias or email for person 'person2'")
211 "milestones entry for 'abc' must be a table")
216 "abc" = { canonical_bug_id = "abc" }
219 "`canonical_bug_id` field in milestones entry for 'abc' must "
225 "abc" = { blah = "def" }
228 "unknown field in milestones entry for 'abc': `blah`")
236 "`canonical_bug_id` field is missing in milestones entry for 'abc'")
243 "`milestones` field must be a table")
248 "abc" = { canonical_bug_id = 1 }
249 "def" = { canonical_bug_id = 1 }
252 "canonical_bug_id is not allowed to be the same as another "
253 "milestone's canonical_bug_id: in milestone entry for 'def': "
254 "1 is also the canonical_bug_id for milestone 'abc'")
256 def test_all_names(self
):
257 config
= Config
.from_str(
262 aliases = ["person1_alias1", "alias1"]
263 output_markdown_file = "person1.mdwn"
265 aliases = ["person2_alias2", "alias2"]
266 output_markdown_file = "person2.mdwn"
268 person1
= config
.people
['person1']
269 person2
= config
.people
['person2']
270 self
.assertEqual(config
.all_names
,
273 'person1_alias1': person1
,
276 'person2_alias2': person2
,
280 def test_canonical_bug_ids(self
):
281 config
= Config
.from_str(
286 "Milestone 1" = { canonical_bug_id = 1 }
287 "Milestone 2" = { canonical_bug_id = 2 }
289 milestone1
= config
.milestones
['Milestone 1']
290 milestone2
= config
.milestones
['Milestone 2']
291 self
.assertEqual(config
.canonical_bug_ids
,
297 def test_bugzilla_url_stripped(self
):
300 bugzilla_url = "https://bugzilla.example.com/prefix"
305 self
.assertEqual(c
.bugzilla_url_stripped
,
306 "https://bugzilla.example.com/prefix")
309 bugzilla_url = "https://bugzilla.example.com/prefix/"
314 self
.assertEqual(c
.bugzilla_url_stripped
,
315 "https://bugzilla.example.com/prefix")
318 bugzilla_url = "https://bugzilla.example.com/"
323 self
.assertEqual(c
.bugzilla_url_stripped
,
324 "https://bugzilla.example.com")
326 def test_from_file(self
):
328 with io
.StringIO(text
) as file:
329 return Config
.from_file(file)
331 with self
.assertRaisesRegex(TypeError,
332 "^list is not a valid file or path$"):
335 with self
.assertRaisesRegex(
337 "^TOML parse error: Empty value is invalid"):
338 load("""bad-toml=""")
340 self
.assertEqual(str(load(
342 bugzilla_url = "https://bugzilla.example.com/"
344 email = "person1@example.com"
346 output_markdown_file = "person1.mdwn"
348 "Milestone 1" = { canonical_bug_id = 123 }
350 "Config(bugzilla_url='https://bugzilla.example.com/', "
351 "people={'person1': Person(config=..., identifier='person1', "
352 "output_markdown_file='person1.mdwn', "
353 "aliases={'alias1'}, email='person1@example.com')}, "
354 "milestones={'Milestone 1': Milestone(config=..., "
355 "identifier='Milestone 1', canonical_bug_id=123)})")
358 if __name__
== "__main__":