From 6bb3b30c36e9206c72fe1d834e08137f7b845d38 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Fri, 27 May 2016 05:09:16 -0700 Subject: [PATCH] do not crash when pygments crashes Summary: When syntax highlighting the source excerpts that Infer prints on stdout, we would crash if `pygments.lexers` did not find a suitable class given the name of the source file. Instead, do not colorize when that's the case. Reviewed By: martinoluca Differential Revision: D3358115 fbshipit-source-id: ccb9b41 --- infer/lib/python/inferlib/colorize.py | 6 +++++- .../build_systems/build_integration_tests.py | 19 ++++++++++++++++++- .../codetoanalyze/hello.unknown_ext | 1 + .../expected_outputs/unknown_ext_report.json | 7 +++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 120000 infer/tests/build_systems/codetoanalyze/hello.unknown_ext create mode 100644 infer/tests/build_systems/expected_outputs/unknown_ext_report.json diff --git a/infer/lib/python/inferlib/colorize.py b/infer/lib/python/inferlib/colorize.py index 5687cf14b..b1683c7d4 100644 --- a/infer/lib/python/inferlib/colorize.py +++ b/infer/lib/python/inferlib/colorize.py @@ -55,7 +55,11 @@ def syntax_highlighting(source_name, mode, s): if pygments is None or mode == PLAIN_FORMATTER: return s - lexer = pygments.lexers.get_lexer_for_filename(source_name) + try: + lexer = pygments.lexers.get_lexer_for_filename(source_name) + except pygments.lexers.ClassNotFound: + return s + formatter = None if mode == TERMINAL_FORMATTER: if not sys.stdout.isatty(): diff --git a/infer/tests/build_systems/build_integration_tests.py b/infer/tests/build_systems/build_integration_tests.py index c0cdea941..addcf7797 100755 --- a/infer/tests/build_systems/build_integration_tests.py +++ b/infer/tests/build_systems/build_integration_tests.py @@ -63,6 +63,7 @@ ALL_TESTS = [ 'javac', 'locale', 'make', + 'unknown_ext', 'utf8_in_pwd', 'waf', ] @@ -375,7 +376,7 @@ class BuildIntegrationTest(unittest.TestCase): return print('\nRunning utf8_in_pwd integration test') - utf8_in_pwd_path = os.path.join(CODETOANALYZE_DIR, 'utf8_ιn_pwd') + utf8_in_pwd_path = os.path.join(CODETOANALYZE_DIR, u'utf8_\u03B9n_pwd') # copy non-unicode dir to one with unicode in it shutil.rmtree(utf8_in_pwd_path, True) # remove just in case @@ -400,6 +401,22 @@ class BuildIntegrationTest(unittest.TestCase): report_name='utf8_in_pwd_make_report.json') shutil.rmtree(utf8_in_pwd_path, True) # remove copied dir + def test_unknown_extension(self): + if 'unknown_ext' not in to_test: + print('\nSkipping unknown extension integration test') + return + + print('\nRunning unknown extension integration test') + root = CODETOANALYZE_DIR + errors = run_analysis( + root, + [], + [['clang', '-x', 'c', '-c', 'hello.unknown_ext']], + INFER_EXECUTABLE) + original = os.path.join(EXPECTED_OUTPUTS_DIR, + 'unknown_ext_report.json') + do_test(errors, original) + if __name__ == '__main__': # hackish capturing of the arguments after '--' diff --git a/infer/tests/build_systems/codetoanalyze/hello.unknown_ext b/infer/tests/build_systems/codetoanalyze/hello.unknown_ext new file mode 120000 index 000000000..2227f671f --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/hello.unknown_ext @@ -0,0 +1 @@ +../../../../examples/hello.c \ No newline at end of file diff --git a/infer/tests/build_systems/expected_outputs/unknown_ext_report.json b/infer/tests/build_systems/expected_outputs/unknown_ext_report.json new file mode 100644 index 000000000..ffc159855 --- /dev/null +++ b/infer/tests/build_systems/expected_outputs/unknown_ext_report.json @@ -0,0 +1,7 @@ +[ + { + "bug_type": "NULL_DEREFERENCE", + "file": "hello.unknown_ext", + "procedure": "test" + } +] \ No newline at end of file