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: "
174 "in person entry for 'person2': 'a' is also an alias for person "
183 "milestones entry for 'abc' must be a table")
188 "abc" = { canonical_bug_id = "abc" }
191 "`canonical_bug_id` field in milestones entry for 'abc' must "
197 "abc" = { blah = "def" }
200 "unknown field in milestones entry for 'abc': `blah`")
208 "`canonical_bug_id` field is missing in milestones entry for 'abc'")
215 "`milestones` field must be a table")
220 "abc" = { canonical_bug_id = 1 }
221 "def" = { canonical_bug_id = 1 }
224 "canonical_bug_id is not allowed to be the same as another "
225 "milestone's canonical_bug_id: in milestone entry for 'def': "
226 "1 is also the canonical_bug_id for milestone 'abc'")
228 def test_all_names(self
):
229 config
= Config
.from_str(
234 aliases = ["person1_alias1", "alias1"]
235 output_markdown_file = "person1.mdwn"
237 aliases = ["person2_alias2", "alias2"]
238 output_markdown_file = "person2.mdwn"
240 person1
= config
.people
['person1']
241 person2
= config
.people
['person2']
242 self
.assertEqual(config
.all_names
,
245 'person1_alias1': person1
,
248 'person2_alias2': person2
,
252 def test_canonical_bug_ids(self
):
253 config
= Config
.from_str(
258 "Milestone 1" = { canonical_bug_id = 1 }
259 "Milestone 2" = { canonical_bug_id = 2 }
261 milestone1
= config
.milestones
['Milestone 1']
262 milestone2
= config
.milestones
['Milestone 2']
263 self
.assertEqual(config
.canonical_bug_ids
,
269 def test_bugzilla_url_stripped(self
):
272 bugzilla_url = "https://bugzilla.example.com/prefix"
277 self
.assertEqual(c
.bugzilla_url_stripped
,
278 "https://bugzilla.example.com/prefix")
281 bugzilla_url = "https://bugzilla.example.com/prefix/"
286 self
.assertEqual(c
.bugzilla_url_stripped
,
287 "https://bugzilla.example.com/prefix")
290 bugzilla_url = "https://bugzilla.example.com/"
295 self
.assertEqual(c
.bugzilla_url_stripped
,
296 "https://bugzilla.example.com")
298 def test_from_file(self
):
300 with io
.StringIO(text
) as file:
301 return Config
.from_file(file)
303 with self
.assertRaisesRegex(TypeError,
304 "^list is not a valid file or path$"):
307 with self
.assertRaisesRegex(
309 "^TOML parse error: Empty value is invalid"):
310 load("""bad-toml=""")
312 self
.assertEqual(str(load(
314 bugzilla_url = "https://bugzilla.example.com/"
316 email = "person1@example.com"
318 output_markdown_file = "person1.mdwn"
320 "Milestone 1" = { canonical_bug_id = 123 }
322 "Config(bugzilla_url='https://bugzilla.example.com/', "
323 "people={'person1': Person(config=..., identifier='person1', "
324 "output_markdown_file='person1.mdwn', "
325 "aliases={'alias1'}, email='person1@example.com')}, "
326 "milestones={'Milestone 1': Milestone(config=..., "
327 "identifier='Milestone 1', canonical_bug_id=123)})")
330 if __name__
== "__main__":