diff --git a/src/tox_uv/_run_lock.py b/src/tox_uv/_run_lock.py index 374737b..251b480 100644 --- a/src/tox_uv/_run_lock.py +++ b/src/tox_uv/_run_lock.py @@ -60,7 +60,7 @@ def register_config(self) -> None: def _setup_env(self) -> None: super()._setup_env() - cmd = ["uv", "sync", "--frozen"] + cmd = ["uv", "sync", "--frozen", "--python-preference", self.conf["uv_python_preference"]] for extra in cast("set[str]", sorted(self.conf["extras"])): cmd.extend(("--extra", extra)) if not self.conf["with_dev"]: diff --git a/src/tox_uv/_venv.py b/src/tox_uv/_venv.py index 29093fb..2b3c4c0 100644 --- a/src/tox_uv/_venv.py +++ b/src/tox_uv/_venv.py @@ -60,13 +60,16 @@ def register_config(self) -> None: self.conf.add_config( keys=["uv_python_preference"], of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006 - default=None, + default="system", desc=( "Whether to prefer using Python installations that are already" " present on the system, or those that are downloaded and" " installed by uv [possible values: only-managed, installed," " managed, system, only-system]. Use none to use uv's" - " default." + " default. Our default value is 'system', while uv's default" + " value is 'managed' because we prefer using same python" + " interpreters with all tox environments and avoid accidental" + " downloading of other interpreters." ), ) diff --git a/tests/test_tox_uv_lock.py b/tests/test_tox_uv_lock.py index 1539d88..c15aca1 100644 --- a/tests/test_tox_uv_lock.py +++ b/tests/test_tox_uv_lock.py @@ -31,13 +31,41 @@ def test_uv_lock_list_dependencies_command(tox_project: ToxProjectCreator) -> No ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", "-v", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "-v", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], + ), + ( + "py", + "uv-sync", + [ + "uv", + "sync", + "--frozen", + "--python-preference", + "system", + "--extra", + "dev", + "--extra", + "type", + "--no-dev", + "-v", + ], ), - ("py", "uv-sync", ["uv", "sync", "--frozen", "--extra", "dev", "--extra", "type", "--no-dev", "-v"]), ("py", "freeze", [uv, "--color", "never", "pip", "freeze"]), ("py", "commands[0]", ["python", "hello"]), ] - assert calls == expected + assert len(calls) == len(expected) + for i in range(len(calls)): + assert calls[i] == expected[i] @pytest.mark.parametrize("verbose", ["", "-v", "-vv", "-vvv"]) @@ -63,9 +91,35 @@ def test_uv_lock_command(tox_project: ToxProjectCreator, verbose: str) -> None: ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", *v_args, str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + *v_args, + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], + ), + ( + "py", + "uv-sync", + [ + "uv", + "sync", + "--frozen", + "--python-preference", + "system", + "--extra", + "dev", + "--extra", + "type", + "--no-dev", + *v_args, + ], ), - ("py", "uv-sync", ["uv", "sync", "--frozen", "--extra", "dev", "--extra", "type", "--no-dev", *v_args]), ("py", "commands[0]", ["python", "hello"]), ] assert calls == expected @@ -91,9 +145,19 @@ def test_uv_lock_with_dev(tox_project: ToxProjectCreator) -> None: ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", "-v", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "-v", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], ), - ("py", "uv-sync", ["uv", "sync", "--frozen", "-v"]), + ("py", "uv-sync", ["uv", "sync", "--frozen", "--python-preference", "system", "-v"]), ] assert calls == expected @@ -124,9 +188,23 @@ def test_uv_lock_with_install_pkg(tox_project: ToxProjectCreator, name: str) -> ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", "-v", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "-v", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], + ), + ( + "py", + "uv-sync", + ["uv", "sync", "--frozen", "--python-preference", "system", "--no-dev", "--no-install-project", "-v"], ), - ("py", "uv-sync", ["uv", "sync", "--frozen", "--no-dev", "--no-install-project", "-v"]), ( "py", "install_external", @@ -157,12 +235,21 @@ def test_uv_sync_extra_flags(tox_project: ToxProjectCreator) -> None: ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], ), ( "py", "uv-sync", - ["uv", "sync", "--frozen", "--no-editable", "--inexact"], + ["uv", "sync", "--frozen", "--python-preference", "system", "--no-editable", "--inexact"], ), ("py", "commands[0]", ["python", "hello"]), ] @@ -190,12 +277,21 @@ def test_uv_sync_extra_flags_toml(tox_project: ToxProjectCreator) -> None: ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], ), ( "py", "uv-sync", - ["uv", "sync", "--frozen", "--no-editable", "--inexact"], + ["uv", "sync", "--frozen", "--python-preference", "system", "--no-editable", "--inexact"], ), ("py", "commands[0]", ["python", "hello"]), ] @@ -223,12 +319,21 @@ def test_uv_sync_dependency_groups(tox_project: ToxProjectCreator) -> None: ( "py", "venv", - [uv, "venv", "-p", sys.executable, "--allow-existing", str(project.path / ".tox" / "py")], + [ + uv, + "venv", + "-p", + sys.executable, + "--allow-existing", + "--python-preference", + "system", + str(project.path / ".tox" / "py"), + ], ), ( "py", "uv-sync", - ["uv", "sync", "--frozen", "--group", "test", "--group", "type"], + ["uv", "sync", "--frozen", "--python-preference", "system", "--group", "test", "--group", "type"], ), ("py", "commands[0]", ["python", "hello"]), ]