Skip to content

Commit

Permalink
Update option_manager_mixin.py - issue 3839 (#4812)
Browse files Browse the repository at this point in the history
* Update option_manager_mixin.py

I  added user 'JoshMayberry' 's solution for issue 3839.
#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.
  • Loading branch information
PaaEl committed Aug 11, 2021
1 parent 0df828e commit d53a01f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -531,3 +531,5 @@ contributors:
* Michal Vasilek: contributor

* Kai Mueller (kasium): contributor

* Sam Vermeiren (PaaEl): contributor
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -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?
===========================
Expand Down
2 changes: 1 addition & 1 deletion pylint/config/option_manager_mixin.py
Expand Up @@ -258,7 +258,7 @@ def read_config_file(self, config_file=None, verbose=None):
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!")

Expand Down
32 changes: 32 additions & 0 deletions 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):
Expand Down Expand Up @@ -91,3 +95,31 @@ def test_can_read_toml_rich_types(tmp_path):
"""
)
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"

0 comments on commit d53a01f

Please sign in to comment.