Skip to content

Commit

Permalink
config: fix handling of venv opts env vars
Browse files Browse the repository at this point in the history
This change correctly handles virtual environment creation options
provided via environment variables. For example, previously when
`POETRY_VIRTUALENVS_OPTIONS_SYSTEM_SITE_PACKAGES` was specified via an
environment variable, this was incorrectly ignored when a virtual
environment was created.
  • Loading branch information
abn committed Feb 25, 2024
1 parent 468f658 commit 8ca6c6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/poetry/config/config.py
Expand Up @@ -275,6 +275,11 @@ def get(self, setting_name: str, default: Any = None) -> Any:

value = value[key]

if self._use_environment and isinstance(value, dict):
# this is a configuration table, it is likely that we missed env vars
# in order to capture them recurse, eg: virtualenvs.options
return {k: self.get(f"{setting_name}.{k}") for k in value}

return self.process(value)

def process(self, value: Any) -> Any:
Expand Down
17 changes: 17 additions & 0 deletions tests/config/test_config.py
Expand Up @@ -75,6 +75,23 @@ def test_config_get_from_environment_variable(
assert config.get(name) is value


def test_config_get_from_environment_variable_nested(
config: Config,
environ: Iterator[None],
) -> None:
options = config.default_config["virtualenvs"]["options"]
expected = {}

for k, v in options.items():
if isinstance(v, bool):
expected[k] = not v
os.environ[f"POETRY_VIRTUALENVS_OPTIONS_{k.upper().replace('-', '_')}"] = (
"true" if expected[k] else "false"
)

assert config.get("virtualenvs.options") == expected


@pytest.mark.parametrize(
("path_config", "expected"),
[("~/.venvs", Path.home() / ".venvs"), ("venv", Path("venv"))],
Expand Down

0 comments on commit 8ca6c6d

Please sign in to comment.