summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.txt4
-rw-r--r--coverage/cmdline.py8
-rw-r--r--tests/coveragetest.py18
-rw-r--r--tests/test_process.py39
-rw-r--r--tests/test_summary.py16
5 files changed, 56 insertions, 29 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 44f350a0..eb110d6b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -14,6 +14,9 @@ Change history for Coverage.py
- The XML report now contains a <source> element, fixing `issue 94`_. Thanks
Stan Hu.
+- The ``fail-under`` value is now rounded the same as reported results,
+ preventing paradoxical results, fixing `issue 284`_.
+
- The XML report will now create the output directory if need be, fixing
`issue 285`_. Thanks Chris Rose.
@@ -25,6 +28,7 @@ Change history for Coverage.py
.. _issue 57: https://bitbucket.org/ned/coveragepy/issue/57/annotate-command-fails-to-annotate-many
.. _issue 94: https://bitbucket.org/ned/coveragepy/issue/94/coverage-xml-doesnt-produce-sources
+.. _issue 284: https://bitbucket.org/ned/coveragepy/issue/284/fail-under-should-show-more-precision
.. _issue 285: https://bitbucket.org/ned/coveragepy/issue/285/xml-report-fails-if-output-file-directory
.. _issue 305: https://bitbucket.org/ned/coveragepy/issue/305/pendingdeprecationwarning-the-imp-module
diff --git a/coverage/cmdline.py b/coverage/cmdline.py
index 9a67b436..bd10d5a8 100644
--- a/coverage/cmdline.py
+++ b/coverage/cmdline.py
@@ -470,6 +470,14 @@ class CoverageScript(object):
total = self.coverage.xml_report(outfile=outfile, **report_args)
if options.fail_under is not None:
+ # Total needs to be rounded, but be careful of 0 and 100.
+ if 0 < total < 1:
+ total = 1
+ elif 99 < total < 100:
+ total = 99
+ else:
+ total = round(total)
+
if total >= options.fail_under:
return OK
else:
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index aa851954..fa55dec3 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -1,6 +1,6 @@
"""Base test case class for coverage testing."""
-import glob, os, random, shlex, shutil, sys, tempfile, textwrap
+import glob, os, random, re, shlex, shutil, sys, tempfile, textwrap
import atexit, collections
import coverage
@@ -486,6 +486,22 @@ class CoverageTest(TestCase):
print(output)
return status, output
+ def report_from_command(self, cmd):
+ """Return the report from the `cmd`, with some convenience added."""
+ report = self.run_command(cmd).replace('\\', '/')
+ self.assertNotIn("error", report.lower())
+ return report
+
+ def line_count(self, report):
+ """How many lines are in `report`?"""
+ self.assertEqual(report.split('\n')[-1], "")
+ return len(report.split('\n')) - 1
+
+ def last_line_squeezed(self, report):
+ """Return the last line of `report` with the spaces squeezed down."""
+ last_line = report.split('\n')[-2]
+ return re.sub(r"\s+", " ", last_line)
+
# We run some tests in temporary directories, because they may need to make
# files for the tests. But this is expensive, so we can change per-class
# whether a temp dir is used or not. It's easy to forget to set that
diff --git a/tests/test_process.py b/tests/test_process.py
index 987e961d..b9203e3e 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -568,32 +568,47 @@ class FailUnderTest(CoverageTest):
def setUp(self):
super(FailUnderTest, self).setUp()
- self.make_file("fifty.py", """\
- # I have 50% coverage!
+ self.make_file("forty_two_plus.py", """\
+ # I have 42.857% (3/7) coverage!
a = 1
- if a > 2:
- b = 3
- c = 4
+ b = 2
+ if a > 3:
+ b = 4
+ c = 5
+ d = 6
+ e = 7
""")
- st, _ = self.run_command_status("coverage run fifty.py", 0)
+ st, _ = self.run_command_status("coverage run forty_two_plus.py", 0)
+ self.assertEqual(st, 0)
+ st, out = self.run_command_status("coverage report")
self.assertEqual(st, 0)
+ self.assertEqual(
+ self.last_line_squeezed(out),
+ "forty_two_plus 7 4 43%"
+ )
def test_report(self):
- st, _ = self.run_command_status("coverage report --fail-under=50", 0)
+ st, _ = self.run_command_status("coverage report --fail-under=42", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage report --fail-under=43", 0)
self.assertEqual(st, 0)
- st, _ = self.run_command_status("coverage report --fail-under=51", 2)
+ st, _ = self.run_command_status("coverage report --fail-under=44", 2)
self.assertEqual(st, 2)
def test_html_report(self):
- st, _ = self.run_command_status("coverage html --fail-under=50", 0)
+ st, _ = self.run_command_status("coverage html --fail-under=42", 0)
self.assertEqual(st, 0)
- st, _ = self.run_command_status("coverage html --fail-under=51", 2)
+ st, _ = self.run_command_status("coverage html --fail-under=43", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage html --fail-under=44", 2)
self.assertEqual(st, 2)
def test_xml_report(self):
- st, _ = self.run_command_status("coverage xml --fail-under=50", 0)
+ st, _ = self.run_command_status("coverage xml --fail-under=42", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage xml --fail-under=43", 0)
self.assertEqual(st, 0)
- st, _ = self.run_command_status("coverage xml --fail-under=51", 2)
+ st, _ = self.run_command_status("coverage xml --fail-under=44", 2)
self.assertEqual(st, 2)
diff --git a/tests/test_summary.py b/tests/test_summary.py
index 2612fe36..336a2af3 100644
--- a/tests/test_summary.py
+++ b/tests/test_summary.py
@@ -21,22 +21,6 @@ class SummaryTest(CoverageTest):
# Parent class saves and restores sys.path, we can just modify it.
sys.path.append(self.nice_file(os.path.dirname(__file__), 'modules'))
- def report_from_command(self, cmd):
- """Return the report from the `cmd`, with some convenience added."""
- report = self.run_command(cmd).replace('\\', '/')
- self.assertNotIn("error", report.lower())
- return report
-
- def line_count(self, report):
- """How many lines are in `report`?"""
- self.assertEqual(report.split('\n')[-1], "")
- return len(report.split('\n')) - 1
-
- def last_line_squeezed(self, report):
- """Return the last line of `report` with the spaces squeezed down."""
- last_line = report.split('\n')[-2]
- return re.sub(r"\s+", " ", last_line)
-
def test_report(self):
out = self.run_command("coverage run mycode.py")
self.assertEqual(out, 'done\n')