Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/virtualenv/run/plugin/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def get_discover(parser, args):
help="interpreter discovery method",
)
options, _ = parser.parse_known_args(args)

# Check if the specified discovery method is valid
if options.discovery not in discover_types:
available = ", ".join(f"'{key}'" for key in sorted(discover_types.keys()))
msg = f"Invalid discovery method '{options.discovery}'. Available options: {available}"
raise ValueError(msg)

discover_class = discover_types[options.discovery]
discover_class.add_parser_arguments(discovery_parser)
options, _ = parser.parse_known_args(args, namespace=options)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ def test_logging_setup(caplog, on):
assert not caplog.records
else:
assert caplog.records


def test_invalid_discovery_method_via_env(monkeypatch):
"""Test that an invalid discovery method via env var raises a helpful error."""
monkeypatch.setenv("VIRTUALENV_DISCOVERY", "pyenv")
with pytest.raises(ValueError, match=r"Invalid discovery method 'pyenv'") as exc_info:
session_via_cli(["env"])

error_message = str(exc_info.value)
assert "Invalid discovery method 'pyenv'" in error_message
assert "Available options:" in error_message
assert "'builtin'" in error_message
Loading