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