Mercurial > p > roundup > code
comparison test/test_config.py @ 6557:8687c096a945
Handle configparser.InterpolationSyntaxError
Under Python 3, an option value with a single % (e.g. this % is a
test) throws
configparser.InterpolationSyntaxError: '%' must be followed by
'%' or '(', found: '%s))'
Added code to capture this, raise a different exception. roundup-admin
handles the error and exits cleanly. Other code shows the traceback.
The new error message reports the file, section and option causing the
problem to allow easier repair.
Also updated roundup translations and added tests.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 13 Dec 2021 12:48:57 -0500 |
| parents | 5c1db5d4baed |
| children | fdfe3b7387ea |
comparison
equal
deleted
inserted
replaced
| 6556:d92e0d059c3a | 6557:8687c096a945 |
|---|---|
| 17 | 17 |
| 18 import unittest | 18 import unittest |
| 19 import logging | 19 import logging |
| 20 import fileinput | 20 import fileinput |
| 21 | 21 |
| 22 import os, shutil, errno | 22 import os, shutil, errno, sys |
| 23 | 23 |
| 24 import pytest | 24 import pytest |
| 25 from roundup import configuration | 25 from roundup import configuration |
| 26 | 26 |
| 27 try: | 27 try: |
| 35 # https://github.com/pytest-dev/pytest/issues/568 | 35 # https://github.com/pytest-dev/pytest/issues/568 |
| 36 from .pytest_patcher import mark_class | 36 from .pytest_patcher import mark_class |
| 37 skip_xapian = mark_class(pytest.mark.skip( | 37 skip_xapian = mark_class(pytest.mark.skip( |
| 38 "Skipping Xapian indexer tests: 'xapian' not installed")) | 38 "Skipping Xapian indexer tests: 'xapian' not installed")) |
| 39 include_no_xapian = lambda func, *args, **kwargs: func | 39 include_no_xapian = lambda func, *args, **kwargs: func |
| 40 | 40 |
| 41 _py3 = sys.version_info[0] > 2 | |
| 42 if _py3: | |
| 43 skip_py2 = lambda func, *args, **kwargs: func | |
| 44 else: | |
| 45 # FIX: workaround for a bug in pytest.mark.skip(): | |
| 46 # https://github.com/pytest-dev/pytest/issues/568 | |
| 47 from .pytest_patcher import mark_class | |
| 48 skip_py2 = mark_class(pytest.mark.skip( | |
| 49 reason='Skipping test under python2.')) | |
| 50 | |
| 41 | 51 |
| 42 config = configuration.CoreConfig() | 52 config = configuration.CoreConfig() |
| 43 config.DATABASE = "db" | 53 config.DATABASE = "db" |
| 44 config.RDBMS_NAME = "rounduptest" | 54 config.RDBMS_NAME = "rounduptest" |
| 45 config.RDBMS_HOST = "localhost" | 55 config.RDBMS_HOST = "localhost" |
| 551 config_copy = config.copy() | 561 config_copy = config.copy() |
| 552 | 562 |
| 553 # this should work | 563 # this should work |
| 554 self.assertEqual(config_copy['HTML_VERSION'], 'xhtml') | 564 self.assertEqual(config_copy['HTML_VERSION'], 'xhtml') |
| 555 | 565 |
| 566 @skip_py2 | |
| 567 def testConfigValueInterpolateError(self): | |
| 568 ''' error is not raised using ConfigParser under Python 2. | |
| 569 Unknown cause, so skip it if running python 2. | |
| 570 ''' | |
| 571 | |
| 572 self.munge_configini(mods=[ ("admin_email = ", "a bare % is invalid") ]) | |
| 573 | |
| 574 config = configuration.CoreConfig() | |
| 575 | |
| 576 # load config generates: | |
| 577 ''' | |
| 578 E roundup.configuration.ParsingOptionError: Error in _test_instance/config.ini with section [main] at option admin_email: '%' must be followed by '%' or '(', found: '% is invalid' | |
| 579 ''' | |
| 580 | |
| 581 with self.assertRaises(configuration.ParsingOptionError) as cm: | |
| 582 config.load(self.dirname) | |
| 583 | |
| 584 print(cm.exception) | |
| 585 self.assertIn("'%' must be followed by '%' or '(', found: '% is invalid'", cm.exception.args[0]) | |
| 586 self.assertIn("_test_instance/config.ini with section [main] at option admin_email", cm.exception.args[0]) | |
| 587 | |
| 588 | |
| 556 def testInvalidIndexerValue(self): | 589 def testInvalidIndexerValue(self): |
| 557 """ Mistype native indexer. Verify exception is | 590 """ Mistype native indexer. Verify exception is |
| 558 generated. | 591 generated. |
| 559 """ | 592 """ |
| 560 | 593 |
