Skip to content

Commit fcec8e0

Browse files
committed
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.
1 parent 16fe7a2 commit fcec8e0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

python/extractor/semmle/python/parser/dump_ast.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,27 @@ def visit(self, node, level=0, visited=None):
9797

9898

9999
class StdoutLogger(logging.Logger):
100+
error_count = 0
100101
def log(self, level, fmt, *args):
101102
sys.stdout.write(fmt % args + "\n")
102103

104+
def info(self, fmt, *args):
105+
self.log(logging.INFO, fmt, *args)
106+
107+
def warn(self, fmt, *args):
108+
self.log(logging.WARN, fmt, *args)
109+
self.error_count += 1
110+
111+
def error(self, fmt, *args):
112+
self.log(logging.ERROR, fmt, *args)
113+
self.error_count += 1
114+
115+
def had_errors(self):
116+
return self.error_count > 0
117+
118+
def reset_error_count(self):
119+
self.error_count = 0
120+
103121
def old_parser(inputfile, logger):
104122
mod = PythonSourceModule(None, inputfile, logger)
105123
logger.close()

python/extractor/semmle/python/parser/tsg_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def concatenate_stringparts(stringparts, logger):
440440
try:
441441
return "".join(decode_str(stringpart.s) for stringpart in stringparts)
442442
except Exception as ex:
443-
logger.error("Unable to concatenate string %s getting error %s", stringparts, ex)
443+
logger.error("Unable to concatenate string {} getting error {}".format(stringparts, ex))
444444
return stringparts[0].s
445445

446446

python/extractor/tests/test_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def compare_parses(self, filename, logger):
4949
diff = e.output
5050
if diff:
5151
pytest.fail(diff.decode("utf-8"))
52+
self.check_for_stdout_errors(logger)
53+
5254
self.assertEqual(self.capsys.readouterr().err, "")
5355
os.remove(oldfile)
5456
os.remove(newfile)
@@ -84,9 +86,15 @@ def compare_expected(self, filename, logger, new=True ):
8486
diff = e.output
8587
if diff:
8688
pytest.fail(diff.decode("utf-8"))
89+
90+
self.check_for_stdout_errors(logger)
8791
self.assertEqual(self.capsys.readouterr().err, "")
8892
os.remove(actual)
8993

94+
def check_for_stdout_errors(self, logger):
95+
if logger.had_errors():
96+
logger.reset_error_count()
97+
pytest.fail("Errors/warnings were logged to stdout during testing.")
9098

9199
def setup_tests():
92100
test_folder = os.path.join(os.path.dirname(__file__), "parser")

0 commit comments

Comments
 (0)