diff 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
line wrap: on
line diff
--- a/test/test_config.py	Sun Dec 12 23:42:44 2021 -0500
+++ b/test/test_config.py	Mon Dec 13 12:48:57 2021 -0500
@@ -19,7 +19,7 @@
 import logging
 import fileinput
 
-import os, shutil, errno
+import os, shutil, errno, sys
 
 import pytest
 from roundup import configuration
@@ -37,7 +37,17 @@
     skip_xapian = mark_class(pytest.mark.skip(
         "Skipping Xapian indexer tests: 'xapian' not installed"))
     include_no_xapian = lambda func, *args, **kwargs: func
-    
+
+_py3 = sys.version_info[0] > 2
+if _py3:
+    skip_py2 = lambda func, *args, **kwargs: func
+else:
+    # FIX: workaround for a bug in pytest.mark.skip():
+    #   https://github.com/pytest-dev/pytest/issues/568
+    from .pytest_patcher import mark_class
+    skip_py2 = mark_class(pytest.mark.skip(
+        reason='Skipping test under python2.'))
+
 
 config = configuration.CoreConfig()
 config.DATABASE = "db"
@@ -553,6 +563,29 @@
         # this should work
         self.assertEqual(config_copy['HTML_VERSION'], 'xhtml')
 
+    @skip_py2
+    def testConfigValueInterpolateError(self):
+        ''' error is not raised using ConfigParser under Python 2.
+            Unknown cause, so skip it if running python 2.
+        '''
+
+        self.munge_configini(mods=[ ("admin_email = ", "a bare % is invalid") ])
+
+        config = configuration.CoreConfig()
+
+        # load config generates:
+        '''
+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'
+        '''
+
+        with self.assertRaises(configuration.ParsingOptionError) as cm:
+            config.load(self.dirname)
+
+        print(cm.exception)
+        self.assertIn("'%' must be followed by '%' or '(', found: '% is invalid'", cm.exception.args[0])
+        self.assertIn("_test_instance/config.ini with section [main] at option admin_email", cm.exception.args[0])
+
+
     def testInvalidIndexerValue(self):
         """ Mistype native indexer. Verify exception is
             generated.

Roundup Issue Tracker: http://roundup-tracker.org/