From fceff55ff94d88bd314dbe03ec6727c9e1e40de9 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Wed, 3 Jun 2020 21:38:28 +0000 Subject: Enable coverage enforcement --- setup.cfg | 3 --- setup.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/setup.cfg b/setup.cfg index b0efa3b..27ce9a2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,2 @@ [aliases] release = test flake8 - -[coverage:run] -branch = True diff --git a/setup.py b/setup.py index 2e76532..a4f43c3 100755 --- a/setup.py +++ b/setup.py @@ -1,11 +1,13 @@ #!/usr/bin/env python import os +import math import shutil import subprocess from distutils import log from distutils.core import Command from setuptools.command.test import test +from distutils.errors import DistutilsError from setuptools import setup, find_packages @@ -13,18 +15,63 @@ class TestsWithCoverage(test): description = "run unit tests with coverage" + coverage_goal = 100 + missed_branches_goal = 0 + partial_branches_goal = 0 + + def initialize_options(self): + super().initialize_options() + self.missed_coverage_goals = False + + def enforce_coverage_goals(self, rel_path, analysis): + # There is no coverage goal for the player package, just the API + if os.path.split(rel_path)[0] == "pydora": + return + + coverage_percent = math.ceil(analysis.numbers.pc_covered) + if coverage_percent != self.coverage_goal: + self.missed_coverage_goals = True + self.announce( + "Coverage: {!r} coverage is {}%, goal is {}%".format( + rel_path, coverage_percent, self.coverage_goal), log.ERROR) + + missed_branches = analysis.numbers.n_missing_branches + if missed_branches != self.missed_branches_goal: + self.missed_coverage_goals = True + self.announce( + "Coverage: {!r} missed branch count is {}, goal is {}".format( + rel_path, missed_branches, self.missed_branches_goal), + log.ERROR) + + partially_covered_branches = analysis.numbers.n_partial_branches + if partially_covered_branches != self.partial_branches_goal: + self.missed_coverage_goals = True + self.announce( + "Coverage: {!r} partial branch count is {}, goal is {}".format( + rel_path, partially_covered_branches, + self.partial_branches_goal), log.ERROR) + def run(self): from coverage import Coverage - cov = Coverage(source=self.distribution.packages) - cov.start() + cov = Coverage(source=self.distribution.packages, branch=True) + cov.start() super().run() - cov.stop() - cov.xml_report() + + # Save HTML report for debugging missed coverage cov.html_report() + # Print coverage report to console for CI log + cov.report() + + for rep in cov._get_file_reporters(): + self.enforce_coverage_goals(rep.relname, cov._analyze(rep)) + + if self.missed_coverage_goals: + raise DistutilsError("Project missed coverage goals") + class PyPiReleaseCommand(Command): -- cgit v1.2.3