Skip to content

Commit

Permalink
Issue 8: Add a plugin system for quality reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
barrywhart authored and Bachmann1234 committed Dec 12, 2019
1 parent 7e9f14d commit 6c88a41
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
5 changes: 5 additions & 0 deletions diff_cover/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from __future__ import unicode_literals

import pluggy

VERSION = '2.4.1'
DESCRIPTION = 'Automatically find diff lines that need test coverage.'
QUALITY_DESCRIPTION = 'Automatically find diff lines with quality violations.'

# Other packages that implement diff_cover plugins use this.
hookimpl = pluggy.HookimplMarker('diff_cover')
37 changes: 26 additions & 11 deletions diff_cover/diff_quality_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import os
import sys

import pluggy
import six

import diff_cover
from diff_cover import hookspecs
from diff_cover.diff_cover_tool import COMPARE_BRANCH_HELP, DIFF_RANGE_NOTATION_HELP, FAIL_UNDER_HELP, \
IGNORE_STAGED_HELP, IGNORE_UNSTAGED_HELP, EXCLUDE_HELP, HTML_REPORT_HELP, CSS_FILE_HELP
from diff_cover.diff_reporter import GitDiffReporter
Expand Down Expand Up @@ -222,20 +224,33 @@ def main(argv=None, directory=None):
last_char = user_options[-1]
if first_char == last_char and first_char in ('"', "'"):
user_options = user_options[1:-1]
reporter = None
driver = QUALITY_DRIVERS.get(tool)

if driver is not None:
# If we've been given pre-generated reports,
# try to open the files
if driver is None:
# The requested tool is not built into diff_cover. See if another Python
# package provides it.
pm = pluggy.PluginManager('diff_cover')
pm.add_hookspecs(hookspecs)
pm.load_setuptools_entrypoints('diff_cover')
for name, reporter_fn in pm.hook.diff_cover_report_quality():
if name == tool:
reporter = reporter_fn()
break

if reporter is not None or driver is not None:
input_reports = []

for path in arg_dict['input_reports']:
try:
input_reports.append(open(path, 'rb'))
except IOError:
LOGGER.warning("Could not load '{}'".format(path))
try:
reporter = QualityReporter(driver, input_reports, user_options)
if driver is not None:
# If we've been given pre-generated reports,
# try to open the files

for path in arg_dict['input_reports']:
try:
input_reports.append(open(path, 'rb'))
except IOError:
LOGGER.warning("Could not load '{}'".format(path))
reporter = QualityReporter(driver, input_reports, user_options)

percent_passing = generate_quality_report(
reporter,
arg_dict['compare_branch'],
Expand Down
12 changes: 12 additions & 0 deletions diff_cover/hookspecs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pluggy

hookspec = pluggy.HookspecMarker("diff_cover")


@hookspec
def diff_cover_report_quality():
"""
Return a 2-part tuple:
- Quality plugin name
- Object that implements the BaseViolationReporter protocol
"""
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
Jinja2>=2.7.1
six>=1.6.1
jinja2_pluralize
pluggy
pygments

0 comments on commit 6c88a41

Please sign in to comment.