|
| 1 | +import textwrap |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tests.lib import PipTestEnvironment, create_basic_wheel_for_package |
| 7 | +from tests.lib.venv import VirtualEnvironment |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture() |
| 11 | +def patch_check_externally_managed(virtualenv: VirtualEnvironment) -> None: |
| 12 | + # Since the tests are run from a virtual environment, and we can't |
| 13 | + # guarantee access to the actual stdlib location (where EXTERNALLY-MANAGED |
| 14 | + # needs to go into), we patch the check to always raise a simple message. |
| 15 | + virtualenv.sitecustomize = textwrap.dedent( |
| 16 | + """\ |
| 17 | + from pip._internal.exceptions import ExternallyManagedEnvironment |
| 18 | + from pip._internal.utils import misc |
| 19 | +
|
| 20 | + def check_externally_managed(): |
| 21 | + raise ExternallyManagedEnvironment("I am externally managed") |
| 22 | +
|
| 23 | + misc.check_externally_managed = check_externally_managed |
| 24 | + """ |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "arguments", |
| 30 | + [ |
| 31 | + pytest.param(["install"], id="install"), |
| 32 | + pytest.param(["install", "--user"], id="install-user"), |
| 33 | + pytest.param(["uninstall", "-y"], id="uninstall"), |
| 34 | + ], |
| 35 | +) |
| 36 | +@pytest.mark.usefixtures("patch_check_externally_managed") |
| 37 | +def test_fails(script: PipTestEnvironment, arguments: List[str]) -> None: |
| 38 | + result = script.pip(*arguments, "pip", expect_error=True) |
| 39 | + assert "I am externally managed" in result.stderr |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "arguments", |
| 44 | + [ |
| 45 | + pytest.param(["install", "--root"], id="install-root"), |
| 46 | + pytest.param(["install", "--prefix"], id="install-prefix"), |
| 47 | + pytest.param(["install", "--target"], id="install-target"), |
| 48 | + ], |
| 49 | +) |
| 50 | +@pytest.mark.usefixtures("patch_check_externally_managed") |
| 51 | +def test_allows_if_out_of_environment( |
| 52 | + script: PipTestEnvironment, |
| 53 | + arguments: List[str], |
| 54 | +) -> None: |
| 55 | + wheel = create_basic_wheel_for_package(script, "foo", "1.0") |
| 56 | + result = script.pip(*arguments, script.scratch_path, wheel.as_uri()) |
| 57 | + assert "Successfully installed foo-1.0" in result.stdout |
| 58 | + assert "I am externally managed" not in result.stderr |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.usefixtures("patch_check_externally_managed") |
| 62 | +def test_allows_install_dry_run(script: PipTestEnvironment) -> None: |
| 63 | + wheel = create_basic_wheel_for_package(script, "foo", "1.0") |
| 64 | + result = script.pip("install", "--dry-run", wheel.as_uri()) |
| 65 | + assert "Would install foo-1.0" in result.stdout |
| 66 | + assert "I am externally managed" not in result.stderr |
0 commit comments