diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-12-27 16:56:32 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-12-27 17:52:41 -0500 |
commit | 5dbbe1430c16fe15b553e6909be8be5f2b9b71b9 (patch) | |
tree | 71c3e52b48ec793c48743e8b4c178326250b0f41 | |
parent | 8240c58c90a0157178e9c5f6fedd9f003aab892d (diff) | |
download | python-coveragepy-git-5dbbe1430c16fe15b553e6909be8be5f2b9b71b9.tar.gz |
Warnings can be marked to only display once.
-rw-r--r-- | coverage/control.py | 15 | ||||
-rw-r--r-- | tests/coveragetest.py | 3 | ||||
-rw-r--r-- | tests/test_api.py | 16 |
3 files changed, 28 insertions, 6 deletions
diff --git a/coverage/control.py b/coverage/control.py index 6e59078c..4358a541 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -203,6 +203,7 @@ class Coverage(object): self._warn_no_data = True self._warn_unimported_source = True self._warn_preimported_source = check_preimported + self._no_warn_slugs = None # A record of all the warnings that have been issued. self._warnings = [] @@ -333,12 +334,19 @@ class Coverage(object): return not reason - def _warn(self, msg, slug=None): + def _warn(self, msg, slug=None, once=False): """Use `msg` as a warning. For warning suppression, use `slug` as the shorthand. + + If `once` is true, only show this warning once (determined by the + slug.) + """ - if slug in self.config.disable_warnings: + if self._no_warn_slugs is None: + self._no_warn_slugs = list(self.config.disable_warnings) + + if slug in self._no_warn_slugs: # Don't issue the warning return @@ -349,6 +357,9 @@ class Coverage(object): msg = "[%d] %s" % (os.getpid(), msg) sys.stderr.write("Coverage.py warning: %s\n" % msg) + if once: + self._no_warn_slugs.append(slug) + def get_option(self, option_name): """Get an option from the configuration. diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 17b3f15d..1917c5b3 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -264,8 +264,9 @@ class CoverageTest( """ saved_warnings = [] - def capture_warning(msg, slug=None): + def capture_warning(msg, slug=None, once=False): # pylint: disable=unused-argument """A fake implementation of Coverage._warn, to capture warnings.""" + # NOTE: we don't implement `once`. if slug: msg = "%s (%s)" % (msg, slug) saved_warnings.append(msg) diff --git a/tests/test_api.py b/tests/test_api.py index 369324f7..63a0c7b1 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -532,12 +532,22 @@ class ApiTest(CoverageTest): self.assertIn("Hello\n", out) err = self.stderr() - self.assertIn(textwrap.dedent("""\ - Coverage.py warning: Module sys has no Python source. (module-not-python) - """), err) + self.assertIn( + "Coverage.py warning: Module sys has no Python source. (module-not-python)", + err + ) self.assertNotIn("module-not-imported", err) self.assertNotIn("no-data-collected", err) + def test_warn_once(self): + cov = coverage.Coverage() + cov.load() + cov._warn("Warning, warning 1!", slug="bot", once=True) + cov._warn("Warning, warning 2!", slug="bot", once=True) + err = self.stderr() + self.assertIn("Warning, warning 1!", err) + self.assertNotIn("Warning, warning 2!", err) + def test_source_and_include_dont_conflict(self): # A bad fix made this case fail: https://bitbucket.org/ned/coveragepy/issues/541 self.make_file("a.py", "import b\na = 1") |