summaryrefslogtreecommitdiff
path: root/coverage/report.py
diff options
context:
space:
mode:
authorMatt Bachmann <bachmann.matt@gmail.com>2019-07-08 23:09:38 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-08-31 07:24:08 -0400
commit9a78a80aaf8f8161b857ebf3cf02dd511181dd07 (patch)
treed73603d17fc5269a3b8199085bf67c436d99aedf /coverage/report.py
parent790f0b30010a3a1f68f4fa7c172ce3b31c7c4b24 (diff)
downloadpython-coveragepy-git-9a78a80aaf8f8161b857ebf3cf02dd511181dd07.tar.gz
Create a JSON report
Diffstat (limited to 'coverage/report.py')
-rw-r--r--coverage/report.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/coverage/report.py b/coverage/report.py
index 9a740290..d24dddc8 100644
--- a/coverage/report.py
+++ b/coverage/report.py
@@ -2,9 +2,45 @@
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Reporter foundation for coverage.py."""
+import sys
+from coverage import env
from coverage.files import prep_patterns, FnmatchMatcher
-from coverage.misc import CoverageException, NoSource, NotPython
+from coverage.misc import CoverageException, NoSource, NotPython, ensure_dir_for_file, file_be_gone
+
+
+def render_report(output_path, reporter, morfs):
+ """Run the provided reporter ensuring any required setup and cleanup is done
+
+ At a high level this method ensures the output file is ready to be written to. Then writes the
+ report to it. Then closes the file and deletes any garbage created if necessary.
+ """
+ file_to_close = None
+ delete_file = False
+ if output_path:
+ if output_path == '-':
+ outfile = sys.stdout
+ else:
+ # Ensure that the output directory is created; done here
+ # because this report pre-opens the output file.
+ # HTMLReport does this using the Report plumbing because
+ # its task is more complex, being multiple files.
+ ensure_dir_for_file(output_path)
+ open_kwargs = {}
+ if env.PY3:
+ open_kwargs['encoding'] = 'utf8'
+ outfile = open(output_path, "w", **open_kwargs)
+ file_to_close = outfile
+ try:
+ return reporter.report(morfs, outfile=outfile)
+ except CoverageException:
+ delete_file = True
+ raise
+ finally:
+ if file_to_close:
+ file_to_close.close()
+ if delete_file:
+ file_be_gone(output_path)
def get_analysis_to_report(coverage, morfs):