Skip to content

Commit

Permalink
fix: pip run should look for executable in venv
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jan 26, 2024
1 parent 5e09e90 commit 6e86941
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/onepm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Any, Iterable, NoReturn


Expand Down Expand Up @@ -32,9 +33,9 @@ def __init__(self) -> None:
self.command = self.get_command()

@staticmethod
def find_executable(name: str) -> str:
def find_executable(name: str, path: str | Path | None = None) -> str:
# TODO: to keep it simple, only search in PATH(no alias/shell function)
executable = shutil.which(name)
executable = shutil.which(name, path=path)
if not executable:
raise Exception(f"{name} is not found in PATH, did you install it?")
return executable
Expand Down
11 changes: 10 additions & 1 deletion src/onepm/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ def uninstall(self, *args: str) -> NoReturn:
self.execute("uninstall", *args)

def run(self, *args: str) -> NoReturn:
self._execute_command(list(args))
if len(args) == 0:
raise Exception("Please specify a command to run.")
command, *rest = args
venv = self._ensure_virtualenv()
bin_dir = "Scripts" if sys.platform == "win32" else "bin"
path = os.getenv("PATH", "")
command = self.find_executable(
command, os.pathsep.join([os.path.join(venv, bin_dir), path])
)
self._execute_command([command, *rest])
7 changes: 7 additions & 0 deletions tests/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

@pytest.fixture
def venv(monkeypatch):
@staticmethod
def mock_find_executable(name: str, path: str | None = None) -> str:
return name

monkeypatch.setenv("VIRTUAL_ENV", "foo")
monkeypatch.setattr(
"onepm.base.PackageManager.find_executable", mock_find_executable
)


def test_pip_detect_activated_venv(venv, project):
Expand Down

0 comments on commit 6e86941

Please sign in to comment.