diff options
| author | Ned Batchelder <ned@nedbatchelder.com> | 2021-05-31 19:37:53 -0400 |
|---|---|---|
| committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-05-31 19:37:53 -0400 |
| commit | 1157999dc5c6c3aa3129a7cd4bf249fe73598501 (patch) | |
| tree | cba407bacb180fb8fcb46a1b5eaf7da2e0fd88fb /coverage | |
| parent | 65d9e0edbca0dc893ca70fbf64969e2ddc5a3680 (diff) | |
| download | python-coveragepy-git-1157999dc5c6c3aa3129a7cd4bf249fe73598501.tar.gz | |
fix: --fail-under=100 could report 100 is less than 100.
Use the same rounding rules for the fail-under message that are used for totals
everywhere else, so that it won't say:
total of 100 is less than fail-under=100
Diffstat (limited to 'coverage')
| -rw-r--r-- | coverage/cmdline.py | 6 | ||||
| -rw-r--r-- | coverage/results.py | 24 |
2 files changed, 20 insertions, 10 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index d1e8f283..697d2960 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -20,7 +20,7 @@ from coverage.data import line_counts from coverage.debug import info_formatter, info_header, short_stack from coverage.exceptions import BaseCoverageException, ExceptionDuringRun, NoSource from coverage.execfile import PyRunner -from coverage.results import should_fail_under +from coverage.results import Numbers, should_fail_under class Opts: @@ -655,8 +655,8 @@ class CoverageScript: fail_under = self.coverage.get_option("report:fail_under") precision = self.coverage.get_option("report:precision") if should_fail_under(total, fail_under, precision): - msg = "total of {total:.{p}f} is less than fail-under={fail_under:.{p}f}".format( - total=total, fail_under=fail_under, p=precision, + msg = "total of {total} is less than fail-under={fail_under:.{p}f}".format( + total=Numbers.display_covered(total), fail_under=fail_under, p=precision, ) print("Coverage failure:", msg) return FAIL_UNDER diff --git a/coverage/results.py b/coverage/results.py index c60ccac2..f7331b41 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -219,14 +219,24 @@ class Numbers(SimpleReprMixin): result in either "0" or "100". """ - pc = self.pc_covered - if 0 < pc < self._near0: - pc = self._near0 - elif self._near100 < pc < 100: - pc = self._near100 + return self.display_covered(self.pc_covered) + + @classmethod + def display_covered(cls, pc): + """Return a displayable total percentage, as a string. + + Note that "0" is only returned when the value is truly zero, and "100" + is only returned when the value is truly 100. Rounding can never + result in either "0" or "100". + + """ + if 0 < pc < cls._near0: + pc = cls._near0 + elif cls._near100 < pc < 100: + pc = cls._near100 else: - pc = round(pc, self._precision) - return "%.*f" % (self._precision, pc) + pc = round(pc, cls._precision) + return "%.*f" % (cls._precision, pc) @classmethod def pc_str_width(cls): |
