Skip to content

Commit

Permalink
Allow skipping uv sync for lock runner via CLI flag
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Feb 4, 2025
1 parent ef1fa13 commit 7e6416e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies = [
"packaging>=24.2",
"tox>=4.24.1,<5",
"typing-extensions>=4.12.2; python_version<'3.10'",
"uv>=0.5.22,<1",
"uv>=0.5.27,<1",
]
urls.Changelog = "https://github.com/tox-dev/tox-uv/releases"
urls.Documentation = "https://github.com/tox-dev/tox-uv#tox-uv"
Expand All @@ -62,14 +62,14 @@ dev = [
test = [
"covdefaults>=2.3",
"devpi-process>=1.0.2",
"diff-cover>=9.2.1",
"diff-cover>=9.2.2",
"pytest>=8.3.4",
"pytest-cov>=6",
"pytest-mock>=3.14",
]
type = [ "mypy==1.14.1", { include-group = "test" } ]
lint = [ "pre-commit-uv>=4.1.4" ]
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.22" ]
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.27" ]

[tool.hatch]
build.hooks.vcs.version-file = "src/tox_uv/version.py"
Expand Down
49 changes: 25 additions & 24 deletions src/tox_uv/_run_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,32 @@ def register_config(self) -> None:

def _setup_env(self) -> None:
super()._setup_env()
cmd = [
"uv",
"sync",
"--frozen",
]
if self.conf["uv_python_preference"] != "none":
cmd.extend(("--python-preference", self.conf["uv_python_preference"]))
for extra in cast("set[str]", sorted(self.conf["extras"])):
cmd.extend(("--extra", extra))
groups = sorted(self.conf["dependency_groups"])
if self.conf["no_default_groups"] and not groups:
cmd.append("--no-default-groups")
install_pkg = getattr(self.options, "install_pkg", None)
if install_pkg is not None:
cmd.append("--no-install-project")
if self.options.verbosity > 3: # noqa: PLR2004
cmd.append("-v")
for group in groups:
cmd.extend(("--group", group))
cmd.extend(self.conf["uv_sync_flags"])
cmd.extend(("-p", self.env_version_spec()))

show = self.options.verbosity > 2 # noqa: PLR2004
outcome = self.execute(cmd, stdin=StdinSource.OFF, run_id="uv-sync", show=show)
outcome.assert_success()
if not getattr(self.options, "skip_uv_sync", False):
cmd = [
"uv",
"sync",
"--frozen",
]
if self.conf["uv_python_preference"] != "none":
cmd.extend(("--python-preference", self.conf["uv_python_preference"]))
for extra in cast("set[str]", sorted(self.conf["extras"])):
cmd.extend(("--extra", extra))
groups = sorted(self.conf["dependency_groups"])
if self.conf["no_default_groups"] and not groups:
cmd.append("--no-default-groups")
if install_pkg is not None:
cmd.append("--no-install-project")
if self.options.verbosity > 3: # noqa: PLR2004
cmd.append("-v")
for group in groups:
cmd.extend(("--group", group))
cmd.extend(self.conf["uv_sync_flags"])
cmd.extend(("-p", self.env_version_spec()))

show = self.options.verbosity > 2 # noqa: PLR2004
outcome = self.execute(cmd, stdin=StdinSource.OFF, run_id="uv-sync", show=show)
outcome.assert_success()
if install_pkg is not None:
path = Path(install_pkg)
pkg = (WheelPackage if path.suffix == ".whl" else SdistPackage)(path, deps=[])
Expand Down
12 changes: 12 additions & 0 deletions src/tox_uv/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ._run_lock import UvVenvLockRunner

if TYPE_CHECKING:
from tox.config.cli.parser import ToxParser
from tox.tox_env.register import ToxEnvRegister


Expand All @@ -24,6 +25,17 @@ def tox_register_tox_env(register: ToxEnvRegister) -> None:
register._default_run_env = UvVenvRunner.id() # noqa: SLF001


@impl
def tox_add_option(parser: ToxParser) -> None:
for key in ("run", "exec"):
parser.handlers[key][0].add_argument(
"--skip-uv-sync",
dest="skip_uv_sync",
help="skip uv sync (lock mode only)",
action="store_true",
)


def tox_append_version_info() -> str:
return f"with uv=={version('uv')}"

Expand Down
35 changes: 35 additions & 0 deletions tests/test_tox_uv_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,38 @@ def test_uv_sync_uv_python_preference(
("py", "commands[0]", ["python", "hello"]),
]
assert calls == expected


def test_skip_uv_sync(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.toml": """
[env_run_base]
runner = "uv-venv-lock-runner"
commands = [["python", "hello"]]
"""
})
execute_calls = project.patch_execute(lambda r: 0 if r.run_id != "venv" else None)
result = project.run("run", "--skip-uv-sync")
result.assert_success()

calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list]
uv = find_uv_bin()

expected = [
(
"py",
"venv",
[
uv,
"venv",
"-p",
sys.executable,
"--allow-existing",
"--python-preference",
"system",
str(project.path / ".tox" / "py"),
],
),
("py", "commands[0]", ["python", "hello"]),
]
assert calls == expected
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
requires =
tox>=4.24.1
tox-uv>=1.20
tox-uv>=1.21.1
env_list =
fix
3.13
Expand Down

0 comments on commit 7e6416e

Please sign in to comment.