Skip to content

Commit 43eb594

Browse files
committed
bug: fix json logging config file syntax exception/fix test for windows
If the json logging config file has mimatched {} or [], it raises an IndexError. Handle that case and test it. Also handle embedded filenames in tests when testsare run on windows:(/ vs \ directory sep).
1 parent 949af33 commit 43eb594

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

roundup/configuration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,11 @@ def load_config_dict_from_json_file(self, filename):
26042604
error_at_doc_line = e.lineno
26052605
# subtract 1 - zero index on config_list
26062606
# remove '\n' for display
2607-
line = config_list[error_at_doc_line - 1][:-1]
2607+
try:
2608+
line = config_list[error_at_doc_line - 1][:-1]
2609+
except IndexError:
2610+
line = _("Error found at end of file. Maybe missing a "
2611+
"block closing '}'.")
26082612

26092613
hint = ""
26102614
if line.find('//') != -1:

test/test_config.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,14 +1169,15 @@ def testCopyConfig(self):
11691169
self.assertEqual(config['STATIC_FILES'], None)
11701170

11711171
# load config
1172+
STATIC_FILES = os.path.join(self.dirname, "html2")
11721173
config.load(self.dirname)
1173-
self.assertEqual(config['STATIC_FILES'], ['_test_instance/html2'])
1174+
self.assertEqual(config['STATIC_FILES'], [ STATIC_FILES ])
11741175

11751176
# copy config
11761177
config_copy = config.copy()
11771178

11781179
# this should work
1179-
self.assertEqual(config_copy['STATIC_FILES'], ['_test_instance/html2'])
1180+
self.assertEqual(config_copy['STATIC_FILES'], [STATIC_FILES])
11801181

11811182
@skip_py2
11821183
def testConfigValueInterpolateError(self):
@@ -1272,13 +1273,14 @@ def testLoggerFormat(self):
12721273
configuration.ParsingOptionError) as cm:
12731274
config.load(self.dirname)
12741275

1275-
self.assertEqual(cm.exception.args[0],
1276-
"Error in _test_instance/config.ini with section "
1276+
ini_path = os.path.join(self.dirname, "config.ini")
1277+
self.assertEqual(cm.exception.args[0],(
1278+
f"Error in {ini_path} with section "
12771279
"[logging] at option format: Bad value substitution: "
12781280
"option 'format' in section 'logging' contains an "
12791281
"interpolation key 'asctime' which is not a valid "
12801282
"option name. Raw value: '%(asctime)s %%(trace_id)s "
1281-
"%%(levelname) %%(message)s'")
1283+
"%%(levelname) %%(message)s'"))
12821284

12831285
def testDictLoggerConfigViaJson(self):
12841286

@@ -1538,26 +1540,41 @@ def testDictLoggerConfigViaJson(self):
15381540
"%s'\n" % (log_config_filename, access_filename))
15391541
self.assertEqual(output, target)
15401542

1541-
def test_missing_logging_config_file(self):
1542-
saved_config = self.db.config['LOGGING_CONFIG']
1543-
1544-
self.db.config['LOGGING_CONFIG'] = 'logging.json'
1545-
1546-
with self.assertRaises(configuration.OptionValueError) as cm:
1547-
self.db.config.init_logging()
1543+
# mess up '}' so json file block isn't properly closed.
1544+
test_config = config1.replace(
1545+
' }',
1546+
' ')
15481547

1549-
self.assertEqual(cm.exception.args[1], "_test_instance/logging.json")
1550-
self.assertEqual(cm.exception.args[2],
1551-
"Unable to find logging config file.")
1548+
with open(log_config_filename, "w") as log_config_file:
1549+
log_config_file.write(test_config)
15521550

1553-
self.db.config['LOGGING_CONFIG'] = 'logging.ini'
1551+
# file is made relative to tracker dir.
1552+
self.db.config["LOGGING_CONFIG"] = '_test_log_config.json'
1553+
with self.assertRaises(configuration.LoggingConfigError) as cm:
1554+
config = self.db.config.init_logging()
15541555

1555-
with self.assertRaises(configuration.OptionValueError) as cm:
1556-
self.db.config.init_logging()
1556+
output = cm.exception.args[0].replace(r'\\','\\')
1557+
target = ("Error parsing json logging dict "
1558+
"(%s) near \n\n"
1559+
" Error found at end of file. Maybe missing a "
1560+
"block closing '}'.\n\n"
1561+
"Expecting ',' delimiter: line 86 column 1." %
1562+
(log_config_filename,))
1563+
self.assertEqual(output, target)
15571564

1558-
self.assertEqual(cm.exception.args[1], "_test_instance/logging.ini")
1559-
self.assertEqual(cm.exception.args[2],
1560-
"Unable to find logging config file.")
1565+
def test_missing_logging_config_file(self):
1566+
saved_config = self.db.config['LOGGING_CONFIG']
1567+
1568+
for logging_file in ["logging.json", "logging.ini", "logging.foobar"]:
1569+
self.db.config['LOGGING_CONFIG'] = logging_file
1570+
1571+
with self.assertRaises(configuration.OptionValueError) as cm:
1572+
self.db.config.init_logging()
1573+
1574+
logging_configfile = os.path.join(self.dirname, logging_file)
1575+
self.assertEqual(cm.exception.args[1], logging_configfile)
1576+
self.assertEqual(cm.exception.args[2],
1577+
"Unable to find logging config file.")
15611578

15621579
self.db.config['LOGGING_CONFIG'] = saved_config
15631580

@@ -1566,11 +1583,12 @@ def test_unknown_logging_config_file_type(self):
15661583

15671584
self.db.config['LOGGING_CONFIG'] = 'schema.py'
15681585

1569-
1586+
15701587
with self.assertRaises(configuration.OptionValueError) as cm:
15711588
self.db.config.init_logging()
15721589

1573-
self.assertEqual(cm.exception.args[1], "_test_instance/schema.py")
1590+
logging_configfile = os.path.join(self.dirname, "schema.py")
1591+
self.assertEqual(cm.exception.args[1], logging_configfile)
15741592
self.assertEqual(cm.exception.args[2],
15751593
"Unable to load logging config file. "
15761594
"File extension must be '.ini' or '.json'.\n")

0 commit comments

Comments
 (0)