diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-27 07:38:40 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-27 07:38:40 -0500 |
commit | c44bbcd1d86e6c4192988f20319d9e3c94e263b6 (patch) | |
tree | 9218d553861beac5f7030dfc645c708e29c8d301 | |
parent | 0429009cf349d69b9b63bf2dc30755590067b510 (diff) | |
download | python-coveragepy-git-c44bbcd1d86e6c4192988f20319d9e3c94e263b6.tar.gz |
Give error messages if an explicitly provided rcfile can't be read.
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | coverage/config.py | 16 | ||||
-rw-r--r-- | coverage/control.py | 24 | ||||
-rw-r--r-- | tests/test_config.py | 31 |
4 files changed, 53 insertions, 22 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 7a9036b0..206fbf35 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -27,6 +27,9 @@ Latest column is a "partial branches" column so that both reports show the same numbers. This closes `issue 342`_. +- If you specify a ``--rcfile`` that cannot be read, you will get an error + message. Fixes `issue 343`_. + - The ``--debug`` switch can now be used on any command. - You can now programmatically adjust the configuration of coverage by setting @@ -40,6 +43,7 @@ Latest .. _issue 328: https://bitbucket.org/ned/coveragepy/issue/328/misbehavior-in-run-source .. _issue 342: https://bitbucket.org/ned/coveragepy/issue/342/console-and-html-coverage-reports-differ +.. _issue 343: https://bitbucket.org/ned/coveragepy/issue/343/an-explicitly-named-non-existent-config Version 4.0a1 --- 27 September 2014 diff --git a/coverage/config.py b/coverage/config.py index 9598f74d..65f4222a 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -197,14 +197,24 @@ class CoverageConfig(object): self.attempted_config_files.append(filename) cp = HandyConfigParser(section_prefix) - files_read = cp.read(filename) + try: + files_read = cp.read(filename) + except configparser.Error as err: + raise CoverageException( + "Couldn't read config file %s: %s" % (filename, err) + ) if not files_read: return False self.config_files.extend(files_read) - for option_spec in self.CONFIG_FILE_OPTIONS: - self._set_attr_from_config_option(cp, *option_spec) + try: + for option_spec in self.CONFIG_FILE_OPTIONS: + self._set_attr_from_config_option(cp, *option_spec) + except ValueError as err: + raise CoverageException( + "Couldn't read config file %s: %s" % (filename, err) + ) # [paths] is special if cp.has_section('paths'): diff --git a/coverage/control.py b/coverage/control.py index 815c16b9..6aa06da8 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -94,20 +94,20 @@ class Coverage(object): # 1: defaults: self.config = CoverageConfig() - # 2: from the .coveragerc or setup.cfg file: + # 2: from the rcfile, .coveragerc or setup.cfg file: if config_file: - did_read_rc = should_read_setupcfg = False - if config_file is True: + did_read_rc = False + specified_file = (config_file is not True) + if not specified_file: config_file = ".coveragerc" - should_read_setupcfg = True - try: - did_read_rc = self.config.from_file(config_file) - except ValueError as err: - raise CoverageException( - "Couldn't read config file %s: %s" % (config_file, err) - ) - - if not did_read_rc and should_read_setupcfg: + + did_read_rc = self.config.from_file(config_file) + + if not did_read_rc: + if specified_file: + raise CoverageException( + "Couldn't read %r as a config file" % config_file + ) self.config.from_file("setup.cfg", section_prefix="coverage:") # 3: from environment variables: diff --git a/tests/test_config.py b/tests/test_config.py index 26a22222..34727ae6 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -91,13 +91,18 @@ class ConfigTest(CoverageTest): self.assertEqual(cov.config.data_file, "fromarg.dat") def test_parse_errors(self): - # Im-parseable values raise CoverageException - self.make_file(".coveragerc", """\ - [run] - timid = maybe? - """) - with self.assertRaises(CoverageException): - coverage.coverage() + # Im-parseable values raise CoverageException, with details. + bad_configs_and_msgs = [ + ("[run]\ntimid = maybe?\n", r"maybe[?]"), + ("timid = 1\n", r"timid = 1"), + ("[run\n", r"\[run"), + ] + + for bad_config, msg in bad_configs_and_msgs: + print("Trying %r" % bad_config) + self.make_file(".coveragerc", bad_config) + with self.assertRaisesRegex(CoverageException, msg): + coverage.coverage() def test_environment_vars_in_config(self): # Config files can have $envvars in them. @@ -339,3 +344,15 @@ class ConfigFileTest(CoverageTest): self.assertEqual(cov.config.html_title, "tabblo & «ταБЬℓσ» # numbers" ) + + def test_unreadable_config(self): + # If a config file is explicitly specified, then it is an error for it + # to not be readable. + bad_files = [ + "nosuchfile.txt", + ".", + ] + for bad_file in bad_files: + msg = "Couldn't read %r as a config file" % bad_file + with self.assertRaisesRegex(CoverageException, msg): + coverage.coverage(config_file=bad_file) |