mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Python: Fail tests when errors/warnings are logged
This is primarily useful for ensuring that errors where a node does not have an appropriate context set in `python.tsg` actually have an effect on the pass/fail status of the parser tests. Previously, these would just be logged to stdout, but test could still succeed when there were errors present. Also fixes one of the logging lines in `tsg_parser.py` to be more consistent with the others.
This commit is contained in:
@@ -97,9 +97,27 @@ class AstDumper(object):
|
|||||||
|
|
||||||
|
|
||||||
class StdoutLogger(logging.Logger):
|
class StdoutLogger(logging.Logger):
|
||||||
|
error_count = 0
|
||||||
def log(self, level, fmt, *args):
|
def log(self, level, fmt, *args):
|
||||||
sys.stdout.write(fmt % args + "\n")
|
sys.stdout.write(fmt % args + "\n")
|
||||||
|
|
||||||
|
def info(self, fmt, *args):
|
||||||
|
self.log(logging.INFO, fmt, *args)
|
||||||
|
|
||||||
|
def warn(self, fmt, *args):
|
||||||
|
self.log(logging.WARN, fmt, *args)
|
||||||
|
self.error_count += 1
|
||||||
|
|
||||||
|
def error(self, fmt, *args):
|
||||||
|
self.log(logging.ERROR, fmt, *args)
|
||||||
|
self.error_count += 1
|
||||||
|
|
||||||
|
def had_errors(self):
|
||||||
|
return self.error_count > 0
|
||||||
|
|
||||||
|
def reset_error_count(self):
|
||||||
|
self.error_count = 0
|
||||||
|
|
||||||
def old_parser(inputfile, logger):
|
def old_parser(inputfile, logger):
|
||||||
mod = PythonSourceModule(None, inputfile, logger)
|
mod = PythonSourceModule(None, inputfile, logger)
|
||||||
logger.close()
|
logger.close()
|
||||||
|
|||||||
@@ -440,7 +440,7 @@ def concatenate_stringparts(stringparts, logger):
|
|||||||
try:
|
try:
|
||||||
return "".join(decode_str(stringpart.s) for stringpart in stringparts)
|
return "".join(decode_str(stringpart.s) for stringpart in stringparts)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.error("Unable to concatenate string %s getting error %s", stringparts, ex)
|
logger.error("Unable to concatenate string {} getting error {}".format(stringparts, ex))
|
||||||
return stringparts[0].s
|
return stringparts[0].s
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ class ParserTest(unittest.TestCase):
|
|||||||
diff = e.output
|
diff = e.output
|
||||||
if diff:
|
if diff:
|
||||||
pytest.fail(diff.decode("utf-8"))
|
pytest.fail(diff.decode("utf-8"))
|
||||||
|
self.check_for_stdout_errors(logger)
|
||||||
|
|
||||||
self.assertEqual(self.capsys.readouterr().err, "")
|
self.assertEqual(self.capsys.readouterr().err, "")
|
||||||
os.remove(oldfile)
|
os.remove(oldfile)
|
||||||
os.remove(newfile)
|
os.remove(newfile)
|
||||||
@@ -84,9 +86,15 @@ class ParserTest(unittest.TestCase):
|
|||||||
diff = e.output
|
diff = e.output
|
||||||
if diff:
|
if diff:
|
||||||
pytest.fail(diff.decode("utf-8"))
|
pytest.fail(diff.decode("utf-8"))
|
||||||
|
|
||||||
|
self.check_for_stdout_errors(logger)
|
||||||
self.assertEqual(self.capsys.readouterr().err, "")
|
self.assertEqual(self.capsys.readouterr().err, "")
|
||||||
os.remove(actual)
|
os.remove(actual)
|
||||||
|
|
||||||
|
def check_for_stdout_errors(self, logger):
|
||||||
|
if logger.had_errors():
|
||||||
|
logger.reset_error_count()
|
||||||
|
pytest.fail("Errors/warnings were logged to stdout during testing.")
|
||||||
|
|
||||||
def setup_tests():
|
def setup_tests():
|
||||||
test_folder = os.path.join(os.path.dirname(__file__), "parser")
|
test_folder = os.path.join(os.path.dirname(__file__), "parser")
|
||||||
|
|||||||
Reference in New Issue
Block a user