From 1dfb04841c6a547d98657644533454810423f5d1 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Fri, 19 Feb 2016 23:41:46 +0000 Subject: [PATCH] Recognize python test files like: test_*.py . --- TODO | 2 +- golden-files/input/test_foo.py | 4 ++++ golden-files/results/python_unittests-hi3_test_py | 7 +++++++ golden-files/results/python_unittests-test_foo_py | 2 ++ tools.py | 15 ++++++--------- tools_test.py | 4 +++- 6 files changed, 23 insertions(+), 11 deletions(-) create mode 100755 golden-files/input/test_foo.py create mode 100644 golden-files/results/python_unittests-hi3_test_py create mode 100644 golden-files/results/python_unittests-test_foo_py diff --git a/TODO b/TODO index 4020c87..6b44e13 100644 --- a/TODO +++ b/TODO @@ -10,7 +10,6 @@ Todo (tool related) - Report on python doctests. (also coverage of) - Cache tools._python_version. - Add bandit tool for python. -- Recognize python test files like: test_*.py Done @@ -162,6 +161,7 @@ Done <- All the existing tools now have basic tests. - Determine if perl files are perl5 or perl6. <- At least done by perl_syntax. +- Recognize python test files like: test_*.py A-syntax, B-tests, C-auto docs, D-lint, E-coverage, F-profile, G-tidy, H-import deps A B C D E F G H diff --git a/golden-files/input/test_foo.py b/golden-files/input/test_foo.py new file mode 100755 index 0000000..b6c1be1 --- /dev/null +++ b/golden-files/input/test_foo.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 + + +print("tested foo") diff --git a/golden-files/results/python_unittests-hi3_test_py b/golden-files/results/python_unittests-hi3_test_py new file mode 100644 index 0000000..a9ce771 --- /dev/null +++ b/golden-files/results/python_unittests-hi3_test_py @@ -0,0 +1,7 @@ +hi + +. +---------------------------------------------------------------------- +Ran 1 test in 0.001s + +OK \ No newline at end of file diff --git a/golden-files/results/python_unittests-test_foo_py b/golden-files/results/python_unittests-test_foo_py new file mode 100644 index 0000000..1001d50 --- /dev/null +++ b/golden-files/results/python_unittests-test_foo_py @@ -0,0 +1,2 @@ +tested foo + \ No newline at end of file diff --git a/tools.py b/tools.py index fc867bf..f92719a 100644 --- a/tools.py +++ b/tools.py @@ -293,18 +293,18 @@ def _has_shebang_line(path): return file_.read(2) == "#!" -_python_console_lexer = pygments.lexers.PythonConsoleLexer() +def _is_python_test_file(path): + path = str(os.path.basename(path)) + return path.endswith("_test.py") or path.startswith("test_") def python_unittests(path): - if str(path).endswith("_test.py"): + if _is_python_test_file(path): command = ([path] if _has_shebang_line(path) else [_python_version(path), path]) stdout, stderr, returncode = _do_command(command, timeout=TIMEOUT) status = Status.ok if returncode == 0 else Status.problem - native_style = pygments.styles.get_style_by_name("native") - return status, _syntax_highlight(stderr, _python_console_lexer, - native_style) + return status, fill3.Text(stdout + "\n" + stderr) else: return Status.not_applicable, fill3.Text("No tests.") python_unittests.dependencies = {"python", "python3"} @@ -332,6 +332,7 @@ def _colorize_coverage_report(text): def python_coverage(path): + # FIX: Also use test_*.py files. test_path = path[:-(len(".py"))] + "_test.py" if os.path.exists(test_path): with tempfile.TemporaryDirectory() as temp_dir: @@ -753,10 +754,6 @@ def _get_python_traceback_lexer(): return pygments.lexers.PythonTracebackLexer() -def _get_python_console_lexer(): - return pygments.lexers.PythonConsoleLexer() - - def run_tool_no_error(path, tool): try: status, result = tool(path) diff --git a/tools_test.py b/tools_test.py index 1d1d9f7..76aeb3f 100755 --- a/tools_test.py +++ b/tools_test.py @@ -75,7 +75,9 @@ class ToolsTestCase(unittest.TestCase): def test_python_unittests(self): self._test_tool(tools.python_unittests, [("hi3.py", tools.Status.not_applicable), - ("hi.py", tools.Status.not_applicable)]) + ("hi.py", tools.Status.not_applicable), + ("hi3_test.py", tools.Status.ok), + ("test_foo.py", tools.Status.ok)]) HI_NORMAL = [("hi3.py", tools.Status.normal), ("hi.py", tools.Status.normal)]