def read(self, filenames): # RawConfigParser takes a filename or list of filenames, but we only # ever call this with a single filename. assert isinstance(filenames, path_types) filename = filenames if env.PYVERSION >= (3, 6): filename = os.fspath(filename) try: with io.open(filename, encoding='utf-8') as fp: toml_text = fp.read() except IOError: return [] if toml: toml_text = substitute_variables(toml_text, os.environ) try: self.data = toml.loads(toml_text) except toml.TomlDecodeError as err: raise TomlDecodeError(*err.args) return [filename] else: has_toml = re.search(r"^\[tool\.coverage\.", toml_text, flags=re.MULTILINE) if self.our_file or has_toml: # Looks like they meant to read TOML, but we can't read it. msg = "Can't read {!r} without TOML support. Install with [toml] extra" raise CoverageException(msg.format(filename)) return []
def read(self, filenames): # RawConfigParser takes a filename or list of filenames, but we only # ever call this with a single filename. assert isinstance(filenames, (bytes, str, os.PathLike)) filename = os.fspath(filenames) try: with open(filename, encoding='utf-8') as fp: toml_text = fp.read() except OSError: return [] if tomli is not None: toml_text = substitute_variables(toml_text, os.environ) try: self.data = tomli.loads(toml_text) except tomli.TOMLDecodeError as err: raise TomlDecodeError(str(err)) from err return [filename] else: has_toml = re.search(r"^\[tool\.coverage\.", toml_text, flags=re.MULTILINE) if self.our_file or has_toml: # Looks like they meant to read TOML, but we can't read it. msg = "Can't read {!r} without TOML support. Install with [toml] extra" raise CoverageException(msg.format(filename)) return []
def get(self, section, option, *args, **kwargs): # pylint: disable=arguments-differ """Get a value, replacing environment variables also. The arguments are the same as `RawConfigParser.get`, but in the found value, ``$WORD`` or ``${WORD}`` are replaced by the value of the environment variable ``WORD``. Returns the finished value. """ for section_prefix in self.section_prefixes: real_section = section_prefix + section if configparser.RawConfigParser.has_option(self, real_section, option): break else: raise configparser.NoOptionError v = configparser.RawConfigParser.get(self, real_section, option, *args, **kwargs) v = substitute_variables(v, os.environ) return v
def test_substitute_variables_errors(text): with pytest.raises(CoverageException) as exc_info: substitute_variables(text, VARS) assert text in str(exc_info.value) assert "Variable NOTHING is undefined" in str(exc_info.value)
def test_substitute_variables(before, after): assert substitute_variables(before, VARS) == after