未验证 提交 a2cc7d8e 编写于 作者: F Frost Ming 提交者: GitHub

fix: don't save python path for venv commands (#1231)

* fix: don't save python path for venv commands

* improve the error message

* add test to cover changed code

* fix test

* reuse the saved python if possible
上级 1233be88
Don't save python path for venv commands.
......@@ -33,7 +33,15 @@ class ActivateCommand(BaseCommand):
raise SystemExit(1)
else:
# Use what is saved in .pdm.toml
interpreter = project.python_executable
interpreter = project.project_config.get("python.path")
if not interpreter:
project.core.ui.echo(
"The project doesn't have a saved python.path. "
"Run [green]pdm use[/] to pick one.",
style="yellow",
err=True,
)
raise SystemExit(1)
venv = get_venv_like_prefix(interpreter)
if venv is None:
project.core.ui.echo(
......
......@@ -26,11 +26,14 @@ class Backend(abc.ABC):
@cached_property
def _resolved_interpreter(self) -> PythonInfo:
if not self.python:
return self.project.python
saved_python = self.project.project_config.get("python.path")
if saved_python:
return PythonInfo.from_path(saved_python)
try: # pragma: no cover
return next(iter(self.project.find_interpreters(self.python)))
except StopIteration: # pragma: no cover
raise VirtualenvCreateError(f"Can't find python interpreter {self.python}")
python = f" {self.python}" if self.python else ""
raise VirtualenvCreateError(f"Can't resolve python interpreter{python}")
@property
def ident(self) -> str:
......
......@@ -22,6 +22,7 @@ def fake_create(monkeypatch):
@pytest.mark.usefixtures("fake_create")
def test_venv_create(invoke, project):
project.project_config.pop("python.path", None)
project.project_config["venv.in_project"] = False
result = invoke(["venv", "create"], obj=project)
assert result.exit_code == 0, result.stderr
......@@ -29,6 +30,7 @@ def test_venv_create(invoke, project):
r"Virtualenv (.+) is created successfully", result.output
).group(1)
assert os.path.exists(venv_path)
assert "python.path" not in project.project_config
@pytest.mark.usefixtures("fake_create")
......@@ -109,6 +111,13 @@ def test_venv_activate(invoke, mocker, project):
assert result.output.startswith("source")
def test_venv_activate_project_without_python(invoke, project):
project.project_config.pop("python.path", None)
result = invoke(["venv", "activate"], obj=project)
assert result.exit_code != 0
assert "The project doesn't have a saved python.path" in result.stderr
@pytest.mark.usefixtures("fake_create")
def test_venv_activate_error(invoke, project):
project.project_config["venv.in_project"] = False
......@@ -187,7 +196,6 @@ def test_venv_purge_interactive(invoke, user_choices, is_path_exists, project):
def test_virtualenv_backend_create(project, mocker):
interpreter = project.python_executable
backend = backends.VirtualenvBackend(project, None)
assert backend.ident
mock_call = mocker.patch("subprocess.check_call")
......@@ -202,20 +210,26 @@ def test_virtualenv_backend_create(project, mocker):
"--no-wheel",
str(location),
"-p",
interpreter,
str(backend._resolved_interpreter.executable),
],
stdout=ANY,
)
def test_venv_backend_create(project, mocker):
interpreter = project.python_executable
backend = backends.VenvBackend(project, None)
assert backend.ident
mock_call = mocker.patch("subprocess.check_call")
location = backend.create()
mock_call.assert_called_once_with(
[interpreter, "-m", "venv", "--without-pip", str(location)], stdout=ANY
[
str(backend._resolved_interpreter.executable),
"-m",
"venv",
"--without-pip",
str(location),
],
stdout=ANY,
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册