Skip to content

Commit

Permalink
feat: ensurepip with sync by uv
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Oct 29, 2024
1 parent 47f5dfb commit fdf2ca7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
22 changes: 13 additions & 9 deletions fast_dev_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,18 +741,22 @@ def gen(self) -> str:
raise EnvError("There project is not managed by uv/pdm/poetry!")
return f"python -m pip install -r {self.filename}"
prefix = "" if is_venv() else f"{tool} run "
ensurepip = ""
match tool:
case "uv":
export_cmd = "uv export --no-hashes --all-extras --frozen"
case "pdm":
export_cmd = "pdm export --without-hashes --with=dev"
case "poetry":
export_cmd = "poetry export --without-hashes"
if UpgradeDependencies.should_with_dev():
export_cmd += " --with=dev"
if extras and isinstance(extras, str | list):
export_cmd += f" --{extras=}".replace("'", '"')
install_cmd = "{2} -o {0} && {1}pip install -r {0}"
if not check_call(prefix + "python -m pip --version"):
ensurepip = (
" {1}python -m ensurepip && {1}python -m pip install -U pip &&"
)
case "poetry" | "pdm":
export_cmd = f"{tool} export --without-hashes --with=dev"
if tool == "poetry":
if not UpgradeDependencies.should_with_dev():
export_cmd = export_cmd.replace(" --with=dev", "")
if extras and isinstance(extras, str | list):
export_cmd += f" --{extras=}".replace("'", '"')
install_cmd = "{2} -o {0} &&%s {1}python -m pip install -r {0}" % ensurepip
if should_remove and not save:
install_cmd += " && rm -f {0}"
return install_cmd.format(self.filename, prefix, export_cmd)
Expand Down
17 changes: 11 additions & 6 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from fast_dev_cli.cli import TOML_FILE, EnvError, Sync, sync
from fast_dev_cli.cli import TOML_FILE, EnvError, Sync, run_and_echo, sync

from .utils import chdir, temp_file

Expand Down Expand Up @@ -35,7 +35,7 @@ def test_sync_not_in_venv(mocker, capsys):
cmd = Sync("req.txt", "all", save=False, dry=True).gen()
assert (
cmd
== 'poetry export --without-hashes --extras="all" -o req.txt && poetry run pip install -r req.txt && rm -f req.txt'
== 'poetry export --without-hashes --extras="all" -o req.txt && poetry run python -m pip install -r req.txt && rm -f req.txt'
)
sync(extras="all", save=False, dry=True)
assert "pip install -r" in capsys.readouterr().out
Expand All @@ -44,7 +44,7 @@ def test_sync_not_in_venv(mocker, capsys):
)
assert (
Sync("req.txt", "", True, dry=True).gen()
== "pdm export --without-hashes --with=dev -o req.txt && pdm run pip install -r req.txt"
== "pdm export --without-hashes --with=dev -o req.txt && pdm run python -m pip install -r req.txt"
)


Expand All @@ -55,14 +55,14 @@ def test_sync(mocker):
cmd = Sync("req.txt", "all", save=False, dry=True).gen()
assert (
cmd
== 'poetry export --without-hashes --extras="all" -o req.txt && pip install -r req.txt && rm -f req.txt'
== 'poetry export --without-hashes --extras="all" -o req.txt && python -m pip install -r req.txt && rm -f req.txt'
)
mocker.patch(
"fast_dev_cli.cli.UpgradeDependencies.should_with_dev", return_value=True
)
assert (
Sync("req.txt", "", True, dry=True).gen()
== "pdm export --without-hashes --with=dev -o req.txt && pip install -r req.txt"
== "pdm export --without-hashes --with=dev -o req.txt && python -m pip install -r req.txt"
)


Expand Down Expand Up @@ -190,7 +190,12 @@ def test_sync_uv(mocker, tmp_path):
toml.with_name("uv.lock").write_text(UV_LOCK_EXAMPLE)
assert (
Sync("req.txt", "", True, dry=True).gen()
== "uv export --no-hashes --all-extras --frozen -o req.txt && uv run pip install -r req.txt"
== "uv export --no-hashes --all-extras --frozen -o req.txt && uv run python -m ensurepip && uv run python -m pip install -U pip && uv run python -m pip install -r req.txt"
)
run_and_echo("uv run python -m ensurepip")
assert (
Sync("req.txt", "", True, dry=True).gen()
== "uv export --no-hashes --all-extras --frozen -o req.txt && uv run python -m pip install -r req.txt"
)


Expand Down

0 comments on commit fdf2ca7

Please sign in to comment.