Skip to content

Commit

Permalink
Make python -m pip work in cli runner even if we haven't sourced pyod…
Browse files Browse the repository at this point in the history
…ide venv
  • Loading branch information
hoodmane committed Feb 23, 2025
1 parent 6269bfa commit ff23142
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/templates/python
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ EOF
set -e
if [[ $1 == "-m" ]] && [[ $2 == "pip" ]]; then
# redirect python -m pip to execute in host environment
shift 1
exec $@
shift 2
which $(dirname $0)/pip || { \
>&2 echo "Cannot find pyodide pip. Make a pyodide venv first?" && exit 1; \
}
exec $(dirname $0)/pip $@
fi

which node > /dev/null || { \
Expand Down
36 changes: 26 additions & 10 deletions src/tests/test_cmdline_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,16 @@ def test_dash_m(selenium):

@only_node
def test_dash_m_pip(selenium, monkeypatch, tmp_path):
import os

monkeypatch.setenv("PATH", str(tmp_path), prepend=":")
pip_path = tmp_path / "pip"
pip_path.write_text("echo 'pip got' $@")
os.chmod(pip_path, 0o777)

result = subprocess.run(
[script_path, "-m", "pip", "install", "pytest"],
capture_output=True,
encoding="utf8",
check=False,
)
assert result.returncode == 0
assert result.stderr == ""
assert result.stdout.strip() == "pip got install pytest"
assert result.returncode == 1
assert (
result.stderr.strip() == "Cannot find pyodide pip. Make a pyodide venv first?"
)


@only_node
Expand Down Expand Up @@ -544,3 +538,25 @@ def test_cpp_exceptions(selenium, venv):
print(result.stderr)
assert result.returncode == 1
assert "ImportError: oops" in result.stderr


@only_node
def test_dash_m_pip_venv(selenium, venv):
result = subprocess.run(
[venv / "bin/python", "-m", "pip", "install", "regex"],
capture_output=True,
encoding="utf8",
check=False,
)
assert result.returncode == 0
assert (
clean_pkg_install_stdout(result.stdout)
== dedent(
"""
Looking in links: .../dist
Processing ./dist/regex-*-cpxxx-cpxxx-pyodide_*_wasm32.whl
Installing collected packages: regex
Successfully installed regex-*
"""
).strip()
)

0 comments on commit ff23142

Please sign in to comment.