From 59e5eb9a1c8cc37e757e9ccc0d05ccd3b9cf16c9 Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:42:25 +0100 Subject: [PATCH] Always use uv from virtual environment at runtime (#128371) --- homeassistant/util/package.py | 2 ++ tests/util/test_package.py | 34 ++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/homeassistant/util/package.py b/homeassistant/util/package.py index 3796bf35cd7..da0666290a1 100644 --- a/homeassistant/util/package.py +++ b/homeassistant/util/package.py @@ -104,6 +104,8 @@ def install_package( _LOGGER.info("Attempting install of %s", package) env = os.environ.copy() args = [ + sys.executable, + "-m", "uv", "pip", "install", diff --git a/tests/util/test_package.py b/tests/util/test_package.py index 10152254914..b7497d620cd 100644 --- a/tests/util/test_package.py +++ b/tests/util/test_package.py @@ -84,14 +84,18 @@ def mock_async_subprocess() -> Generator[MagicMock]: return async_popen -@pytest.mark.usefixtures("mock_sys", "mock_venv") -def test_install(mock_popen: MagicMock, mock_env_copy: MagicMock) -> None: +@pytest.mark.usefixtures("mock_venv") +def test_install( + mock_popen: MagicMock, mock_env_copy: MagicMock, mock_sys: MagicMock +) -> None: """Test an install attempt on a package that doesn't exist.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ, False) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -109,8 +113,10 @@ def test_install(mock_popen: MagicMock, mock_env_copy: MagicMock) -> None: assert mock_popen.return_value.communicate.call_count == 1 -@pytest.mark.usefixtures("mock_sys", "mock_venv") -def test_install_with_timeout(mock_popen: MagicMock, mock_env_copy: MagicMock) -> None: +@pytest.mark.usefixtures("mock_venv") +def test_install_with_timeout( + mock_popen: MagicMock, mock_env_copy: MagicMock, mock_sys: MagicMock +) -> None: """Test an install attempt on a package that doesn't exist with a timeout set.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ, False, timeout=10) @@ -118,6 +124,8 @@ def test_install_with_timeout(mock_popen: MagicMock, mock_env_copy: MagicMock) - env["HTTP_TIMEOUT"] = "10" assert mock_popen.mock_calls[0] == call( [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -135,14 +143,16 @@ def test_install_with_timeout(mock_popen: MagicMock, mock_env_copy: MagicMock) - assert mock_popen.return_value.communicate.call_count == 1 -@pytest.mark.usefixtures("mock_sys", "mock_venv") -def test_install_upgrade(mock_popen, mock_env_copy) -> None: +@pytest.mark.usefixtures("mock_venv") +def test_install_upgrade(mock_popen, mock_env_copy, mock_sys) -> None: """Test an upgrade attempt on a package.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -183,6 +193,8 @@ def test_install_target( mock_venv.return_value = is_venv mock_sys.platform = "linux" args = [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -226,6 +238,8 @@ def test_install_pip_compatibility_no_workaround( mock_venv.return_value = in_venv mock_sys.platform = "linux" args = [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -257,6 +271,8 @@ def test_install_pip_compatibility_use_workaround( mock_sys.executable = python site_dir = "/site_dir" args = [ + mock_sys.executable, + "-m", "uv", "pip", "install", @@ -292,8 +308,8 @@ def test_install_error(caplog: pytest.LogCaptureFixture, mock_popen) -> None: assert record.levelname == "ERROR" -@pytest.mark.usefixtures("mock_sys", "mock_venv") -def test_install_constraint(mock_popen, mock_env_copy) -> None: +@pytest.mark.usefixtures("mock_venv") +def test_install_constraint(mock_popen, mock_env_copy, mock_sys) -> None: """Test install with constraint file on not installed package.""" env = mock_env_copy() constraints = "constraints_file.txt" @@ -301,6 +317,8 @@ def test_install_constraint(mock_popen, mock_env_copy) -> None: assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [ + mock_sys.executable, + "-m", "uv", "pip", "install",