tools: Change coverage tool to work with '.coverage' file.

- Was generating coverage by running a corresponding test file
  for the current file.  Wasn't obvious where test file was.
- This shows coverage if a .coverage file exists which is newer
  than the current file.
- The .coverage file needs to be created independantly. And then
  the reports can be refreshed.
This commit is contained in:
Andrew Hamilton 2019-09-09 12:20:56 +10:00
parent 6fdbfc16c7
commit 3dfb81563c
3 changed files with 18 additions and 31 deletions

View file

@ -309,36 +309,31 @@ def mypy(path):
return status, stdout
def _colorize_coverage_report(text):
def _colorize_coverage_report(lines):
line_color = {"> ": termstr.Color.green, "! ": termstr.Color.red,
" ": None}
return fill3.join("", [termstr.TermStr(line).fg_color(line_color[line[:2]])
for line in text.splitlines(keepends=True)])
for line in lines])
@deps(deps={"pip/coverage"}, url="https://coverage.readthedocs.io/")
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:
coverage_cmd = [PYTHON_EXECUTABLE, "-m", "coverage"]
coverage_path = os.path.join(temp_dir, "coverage")
env = os.environ.copy()
env["COVERAGE_FILE"] = coverage_path
stdout, *rest = _do_command(
coverage_cmd + ["run", test_path], env=env, timeout=TIMEOUT)
coverage_path = ".coverage"
if not os.path.exists(coverage_path):
return Status.not_applicable, f'No "{coverage_path}" file.'
if os.stat(path).st_mtime > os.stat(coverage_path).st_mtime:
return (Status.not_applicable,
f'File has been modified since "{coverage_path}" file was generated.')
path = os.path.normpath(path)
stdout, *rest = _do_command(
coverage_cmd + ["annotate", "--directory", temp_dir, path],
env=env)
flat_path = path.replace("/", "_")
with open(os.path.join(temp_dir, flat_path + ",cover"), "r") as f:
stdout = f.read()
return Status.normal, _colorize_coverage_report(stdout)
else:
return Status.not_applicable, ("No corresponding test file: "
+ os.path.normpath(test_path))
with tempfile.TemporaryDirectory() as temp_dir:
_do_command([PYTHON_EXECUTABLE, "-m", "coverage",
"annotate", "--directory", temp_dir, path])
cover_filename = path.replace("/", "_") + ",cover"
with open(os.path.join(temp_dir, cover_filename), "r") as f:
lines = f.read().splitlines(keepends=True)
failed_lines = [line for line in lines if line.startswith("! ")]
status = Status.ok if not failed_lines else Status.normal
return status, _colorize_coverage_report(lines)
@deps(url="https://github.com/ahamilton/eris/blob/master/gut.py")

View file

@ -1,5 +0,0 @@

> def hi():
> print("hi")


View file

@ -106,9 +106,6 @@ class ToolsTestCase(unittest.TestCase):
def test_mypy(self):
self._test_tool(tools.mypy, self.HI_OK)
def test_python_coverage(self):
self._test_tool(tools.python_coverage, self.HI_NORMAL)
def test_pycodestyle(self):
self._test_tool(tools.pycodestyle, self.HI_OK)