Recognize python test files like: test_*.py .

This commit is contained in:
Andrew Hamilton 2016-02-19 23:41:46 +00:00
parent 8283132eed
commit 1dfb04841c
6 changed files with 23 additions and 11 deletions

2
TODO
View file

@ -10,7 +10,6 @@ Todo (tool related)
- Report on python doctests. (also coverage of) - Report on python doctests. (also coverage of)
- Cache tools._python_version. - Cache tools._python_version.
- Add bandit tool for python. - Add bandit tool for python.
- Recognize python test files like: test_*.py
Done Done
@ -162,6 +161,7 @@ Done
<- All the existing tools now have basic tests. <- All the existing tools now have basic tests.
- Determine if perl files are perl5 or perl6. - Determine if perl files are perl5 or perl6.
<- At least done by perl_syntax. <- 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-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 A B C D E F G H

4
golden-files/input/test_foo.py Executable file
View file

@ -0,0 +1,4 @@
#!/usr/bin/env python3
print("tested foo")

View file

@ -0,0 +1,7 @@
hi
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK

View file

@ -0,0 +1,2 @@
tested foo

View file

@ -293,18 +293,18 @@ def _has_shebang_line(path):
return file_.read(2) == "#!" 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): def python_unittests(path):
if str(path).endswith("_test.py"): if _is_python_test_file(path):
command = ([path] if _has_shebang_line(path) command = ([path] if _has_shebang_line(path)
else [_python_version(path), path]) else [_python_version(path), path])
stdout, stderr, returncode = _do_command(command, timeout=TIMEOUT) stdout, stderr, returncode = _do_command(command, timeout=TIMEOUT)
status = Status.ok if returncode == 0 else Status.problem status = Status.ok if returncode == 0 else Status.problem
native_style = pygments.styles.get_style_by_name("native") return status, fill3.Text(stdout + "\n" + stderr)
return status, _syntax_highlight(stderr, _python_console_lexer,
native_style)
else: else:
return Status.not_applicable, fill3.Text("No tests.") return Status.not_applicable, fill3.Text("No tests.")
python_unittests.dependencies = {"python", "python3"} python_unittests.dependencies = {"python", "python3"}
@ -332,6 +332,7 @@ def _colorize_coverage_report(text):
def python_coverage(path): def python_coverage(path):
# FIX: Also use test_*.py files.
test_path = path[:-(len(".py"))] + "_test.py" test_path = path[:-(len(".py"))] + "_test.py"
if os.path.exists(test_path): if os.path.exists(test_path):
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
@ -753,10 +754,6 @@ def _get_python_traceback_lexer():
return pygments.lexers.PythonTracebackLexer() return pygments.lexers.PythonTracebackLexer()
def _get_python_console_lexer():
return pygments.lexers.PythonConsoleLexer()
def run_tool_no_error(path, tool): def run_tool_no_error(path, tool):
try: try:
status, result = tool(path) status, result = tool(path)

View file

@ -75,7 +75,9 @@ class ToolsTestCase(unittest.TestCase):
def test_python_unittests(self): def test_python_unittests(self):
self._test_tool(tools.python_unittests, self._test_tool(tools.python_unittests,
[("hi3.py", tools.Status.not_applicable), [("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_NORMAL = [("hi3.py", tools.Status.normal),
("hi.py", tools.Status.normal)] ("hi.py", tools.Status.normal)]