diff options
author | PaaEl <88253337+PaaEl@users.noreply.github.com> | 2021-08-11 20:25:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-11 22:25:24 +0200 |
commit | d53a01f1fe51678da8e2cf7a09cf3aa6607ebc2f (patch) | |
tree | c50a96475bad67d0434c1866eb6131aa51c31af3 | |
parent | 0df828e32e6bacbb4d76ab4f19d90ab3847557e6 (diff) | |
download | pylint-git-d53a01f1fe51678da8e2cf7a09cf3aa6607ebc2f.tar.gz |
Update option_manager_mixin.py - issue 3839 (#4812)
* Update option_manager_mixin.py
I added user 'JoshMayberry' 's solution for issue 3839.
https://github.com/PyCQA/pylint/issues/3839
I added a test to see if pylint handles environment variables in config file locations.
Added it to test_config because it also deals with config files.
To test I added an environment variable containing tmp_path and then unpacked it with os.path.expandvars.
Tmp_path gets converted to an environment variable which is fed to the read_config_file function to be tested.
Read_config_file succesfully reads the environment variable, if the variable is changed, the function raises an OSError.
-rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/config/option_manager_mixin.py | 2 | ||||
-rw-r--r-- | tests/test_config.py | 32 |
4 files changed, 39 insertions, 1 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 91d92a77c..030bb0efc 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -531,3 +531,5 @@ contributors: * Michal Vasilek: contributor * Kai Mueller (kasium): contributor + +* Sam Vermeiren (PaaEl): contributor @@ -101,6 +101,10 @@ Release date: TBA Closes #4711 +* Config files can now contain environment variables + + Closes #3839 + What's New in Pylint 2.9.6? =========================== diff --git a/pylint/config/option_manager_mixin.py b/pylint/config/option_manager_mixin.py index 5aa2323f0..cf21a1d60 100644 --- a/pylint/config/option_manager_mixin.py +++ b/pylint/config/option_manager_mixin.py @@ -258,7 +258,7 @@ class OptionsManagerMixIn: if config_file is None: config_file = self.config_file if config_file is not None: - config_file = os.path.expanduser(config_file) + config_file = os.path.expandvars(os.path.expanduser(config_file)) if not os.path.exists(config_file): raise OSError(f"The config file {config_file} doesn't exist!") diff --git a/tests/test_config.py b/tests/test_config.py index ecfac3328..1c33ef039 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,7 +1,11 @@ # pylint: disable=missing-module-docstring, missing-function-docstring, protected-access +import os import unittest.mock +import pytest + import pylint.lint +from pylint.config import OptionsManagerMixIn def check_configuration_file_reader(config_file): @@ -91,3 +95,31 @@ reports = true """ ) check_configuration_file_reader(config_file) + + +def test_can_read_env_variable(tmp_path): + # Check that we can read the "regular" INI .pylintrc file + # if it has an environment variable. + config_file = tmp_path / "pyproject.toml" + config_file.write_text( + """ +[tool.pylint."messages control"] +disable = "logging-not-lazy,logging-format-interpolation" +jobs = "10" +reports = "yes" +""" + ) + os.environ["tmp_path_env"] = str(tmp_path / "pyproject.toml") + options_manager_mix_in = OptionsManagerMixIn("", "${tmp_path_env}") + options_manager_mix_in.read_config_file("${tmp_path_env}") + + def test_read_config_file(): + with pytest.raises(OSError): + options_manager_mix_in.read_config_file("${tmp_path_en}") + + test_read_config_file() + options_manager_mix_in.load_config_file() + section = options_manager_mix_in.cfgfile_parser.sections()[0] + jobs, jobs_nr = options_manager_mix_in.cfgfile_parser.items(section)[1] + assert jobs == "jobs" + assert jobs_nr == "10" |