Skip to content

Commit 168cea7

Browse files
authored
Merge pull request #11 from edx/bmedx/coverage_reporting
Add Django coverage reporting and threshold enforcement
2 parents 6784d15 + 564f191 commit 168cea7

28 files changed

Lines changed: 820 additions & 372 deletions

.annotations_sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source_path: ../
22
report_path: reports
33
safelist_path: .annotation_safe_list.yml
4+
coverage_target: 50.0
45
annotations:
56
".. no_pii::":
67
".. ignored::":

code_annotations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
from __future__ import absolute_import, unicode_literals
66

7-
__version__ = '0.1.0'
7+
__version__ = '0.2.0'

code_annotations/base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ def __init__(self, config_file_path, report_path_override, verbosity, source_pat
4848
self.safelist_path = raw_config['safelist_path']
4949
self.extensions = raw_config['extensions']
5050

51+
try:
52+
self.coverage_target = float(raw_config['coverage_target'])
53+
except (TypeError, ValueError):
54+
raise ConfigurationException(
55+
'Coverage target must be a number between 0 and 100 not "{}".'.format(raw_config['coverage_target'])
56+
)
57+
58+
if self.coverage_target < 0.0 or self.coverage_target > 100.0:
59+
raise ConfigurationException(
60+
'Invalid coverage target. {} is not between 0 and 100.'.format(self.coverage_target)
61+
)
62+
5163
self.verbosity = verbosity
5264
self.echo.set_verbosity(verbosity)
5365

@@ -60,9 +72,6 @@ def __init__(self, config_file_path, report_path_override, verbosity, source_pat
6072
self._configure_annotations(raw_config)
6173
self._configure_extensions()
6274

63-
self.echo.echo_v("Configuration:")
64-
self.echo.echo_v(str(self))
65-
6675
def _check_raw_config_keys(self, raw_config):
6776
"""
6877
Validate that all required keys exist in the configuration file.
@@ -74,7 +83,7 @@ def _check_raw_config_keys(self, raw_config):
7483
ConfigurationException on any missing keys
7584
"""
7685
errors = []
77-
for k in ('report_path', 'source_path', 'safelist_path', 'annotations', 'extensions'):
86+
for k in ('report_path', 'source_path', 'safelist_path', 'annotations', 'extensions', 'coverage_target'):
7887
if k not in raw_config:
7988
errors.append(k)
8089

@@ -365,6 +374,9 @@ def check_results(self, all_results):
365374
366375
Args:
367376
all_results: Dict of annotations found in search()
377+
378+
Returns:
379+
Boolean indicating whether or not any errors were found
368380
"""
369381
if self.config.verbosity >= 2:
370382
pprint.pprint(all_results, indent=3)
@@ -437,6 +449,8 @@ def check_results(self, all_results):
437449
if current_group:
438450
self.errors.append('File finished with an incomplete group {}!'.format(current_group))
439451

452+
return False if self.errors else True
453+
440454
def _add_annotation_error(self, annotation, message):
441455
"""
442456
Add an error message to self.errors, formatted nicely.

code_annotations/cli.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Command line interface for code annotation tools.
33
"""
44
import datetime
5+
import traceback
56

67
import click
78

@@ -42,7 +43,17 @@ def entry_point():
4243
@click.option('-v', '--verbosity', count=True, help='Verbosity level (-v through -vvv)')
4344
@click.option('--lint/--no_lint', help='Enable or disable linting checks', default=False, show_default=True)
4445
@click.option('--report/--no_report', help='Enable or disable writing the report', default=False, show_default=True)
45-
def django_find_annotations(config_file, seed_safelist, list_local_models, report_path, verbosity, lint, report):
46+
@click.option('--coverage/--no_coverage', help='Enable or disable coverage checks', default=False, show_default=True)
47+
def django_find_annotations(
48+
config_file,
49+
seed_safelist,
50+
list_local_models,
51+
report_path,
52+
verbosity,
53+
lint,
54+
report,
55+
coverage
56+
):
4657
"""
4758
Subcommand for dealing with annotations in Django models.
4859
"""
@@ -57,24 +68,29 @@ def django_find_annotations(config_file, seed_safelist, list_local_models, repor
5768
if list_local_models:
5869
searcher.list_local_models()
5970

60-
if lint or report:
71+
if lint or report or coverage:
6172
annotated_models = searcher.search()
6273

6374
if lint:
6475
click.echo("Performing linting checks...")
6576

6677
# Check grouping and choices
67-
searcher.check_results(annotated_models)
68-
69-
# If there are any errors, do not generate the report
70-
if searcher.errors:
78+
if not searcher.check_results(annotated_models):
7179
click.secho("\nSearch failed due to linting errors!", fg="red")
7280
click.secho("{} errors:".format(len(searcher.errors)), fg="red")
7381
click.secho("---------------------------------", fg="red")
7482
click.echo("\n".join(searcher.errors))
83+
# If there are any errors, do not continue
7584
exit(-1)
7685
click.echo("Linting passed without errors.")
7786

87+
if coverage:
88+
if not searcher.check_coverage():
89+
# If there are any errors, do not continue
90+
exit(-1)
91+
92+
click.echo("Coverage passed without errors.")
93+
7894
if report:
7995
searcher.report(annotated_models)
8096

@@ -85,7 +101,9 @@ def django_find_annotations(config_file, seed_safelist, list_local_models, repor
85101

86102
elapsed = datetime.datetime.now() - start_time
87103
click.echo("Search found {} annotations in {}.".format(annotation_count, elapsed))
104+
88105
except Exception as exc: # pylint: disable=broad-except
106+
click.echo(traceback.print_exc())
89107
fail(str(exc))
90108

91109

@@ -151,4 +169,5 @@ def static_find_annotations(config_file, source_path, report_path, verbosity, li
151169
click.echo("Search found {} annotations in {}.".format(annotation_count, elapsed))
152170

153171
except Exception as exc: # pylint: disable=broad-except
172+
click.echo(traceback.print_exc())
154173
fail(str(exc))

code_annotations/django_reporting_helpers.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)