|
1 | 1 | """Tests the presentation style of exceptions."""
|
2 | 2 |
|
3 | 3 | import io
|
| 4 | +import locale |
| 5 | +import logging |
| 6 | +import pathlib |
| 7 | +import sys |
4 | 8 | import textwrap
|
| 9 | +from typing import Optional, Tuple |
5 | 10 |
|
6 | 11 | import pytest
|
7 | 12 | from pip._vendor import rich
|
8 | 13 |
|
9 |
| -from pip._internal.exceptions import DiagnosticPipError |
| 14 | +from pip._internal.exceptions import DiagnosticPipError, ExternallyManagedEnvironment |
10 | 15 |
|
11 | 16 |
|
12 | 17 | class TestDiagnosticPipErrorCreation:
|
@@ -472,3 +477,120 @@ def test_no_hint_no_note_no_context(self) -> None:
|
472 | 477 | It broke. :(
|
473 | 478 | """
|
474 | 479 | )
|
| 480 | + |
| 481 | + |
| 482 | +class TestExternallyManagedEnvironment: |
| 483 | + default_text = ( |
| 484 | + f"The Python environment under {sys.prefix} is managed externally, " |
| 485 | + f"and may not be\nmanipulated by the user. Please use specific " |
| 486 | + f"tooling from the distributor of\nthe Python installation to " |
| 487 | + f"interact with this environment instead.\n" |
| 488 | + ) |
| 489 | + |
| 490 | + @pytest.fixture(autouse=True) |
| 491 | + def patch_locale(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 492 | + orig_getlocal = locale.getlocale |
| 493 | + |
| 494 | + def fake_getlocale(category: int) -> Tuple[Optional[str], Optional[str]]: |
| 495 | + """Fake getlocale() that always report zh_Hant.""" |
| 496 | + result = orig_getlocal(category) |
| 497 | + if category == locale.LC_MESSAGES: |
| 498 | + return "zh_Hant", result[1] |
| 499 | + return result |
| 500 | + |
| 501 | + monkeypatch.setattr(locale, "getlocale", fake_getlocale) |
| 502 | + |
| 503 | + @pytest.fixture() |
| 504 | + def marker(self, tmp_path: pathlib.Path) -> pathlib.Path: |
| 505 | + marker = tmp_path.joinpath("EXTERNALLY-MANAGED") |
| 506 | + marker.touch() |
| 507 | + return marker |
| 508 | + |
| 509 | + def test_invalid_config_format( |
| 510 | + self, |
| 511 | + caplog: pytest.LogCaptureFixture, |
| 512 | + marker: pathlib.Path, |
| 513 | + ) -> None: |
| 514 | + marker.write_text("invalid", encoding="utf8") |
| 515 | + |
| 516 | + with caplog.at_level(logging.WARNING, "pip._internal.exceptions"): |
| 517 | + exc = ExternallyManagedEnvironment.from_config(marker) |
| 518 | + assert len(caplog.records) == 1 |
| 519 | + assert caplog.records[-1].getMessage() == f"Failed to read {marker}" |
| 520 | + |
| 521 | + assert str(exc.context) == self.default_text |
| 522 | + |
| 523 | + @pytest.mark.parametrize( |
| 524 | + "config", |
| 525 | + [ |
| 526 | + pytest.param("", id="empty"), |
| 527 | + pytest.param("[foo]\nblah = blah", id="no-section"), |
| 528 | + pytest.param("[externally-managed]\nblah = blah", id="no-key"), |
| 529 | + ], |
| 530 | + ) |
| 531 | + def test_config_without_key( |
| 532 | + self, |
| 533 | + caplog: pytest.LogCaptureFixture, |
| 534 | + marker: pathlib.Path, |
| 535 | + config: str, |
| 536 | + ) -> None: |
| 537 | + marker.write_text(config, encoding="utf8") |
| 538 | + |
| 539 | + with caplog.at_level(logging.WARNING, "pip._internal.exceptions"): |
| 540 | + exc = ExternallyManagedEnvironment.from_config(marker) |
| 541 | + assert not caplog.records |
| 542 | + assert str(exc.context) == self.default_text |
| 543 | + |
| 544 | + @pytest.mark.parametrize( |
| 545 | + "config, expected", |
| 546 | + [ |
| 547 | + pytest.param( |
| 548 | + """\ |
| 549 | + [externally-managed] |
| 550 | + Error = 最後 |
| 551 | + Error-en = English |
| 552 | + Error-zh = 中文 |
| 553 | + Error-zh_Hant = 繁體 |
| 554 | + Error-zh_Hans = 简体 |
| 555 | + """, |
| 556 | + "繁體", |
| 557 | + id="full", |
| 558 | + ), |
| 559 | + pytest.param( |
| 560 | + """\ |
| 561 | + [externally-managed] |
| 562 | + Error = 最後 |
| 563 | + Error-en = English |
| 564 | + Error-zh = 中文 |
| 565 | + Error-zh_Hans = 简体 |
| 566 | + """, |
| 567 | + "中文", |
| 568 | + id="no-variant", |
| 569 | + ), |
| 570 | + pytest.param( |
| 571 | + """\ |
| 572 | + [externally-managed] |
| 573 | + Error = 最後 |
| 574 | + Error-en = English |
| 575 | + """, |
| 576 | + "最後", |
| 577 | + id="fallback", |
| 578 | + ), |
| 579 | + ], |
| 580 | + ) |
| 581 | + def test_config_canonical( |
| 582 | + self, |
| 583 | + caplog: pytest.LogCaptureFixture, |
| 584 | + marker: pathlib.Path, |
| 585 | + config: str, |
| 586 | + expected: str, |
| 587 | + ) -> None: |
| 588 | + marker.write_text( |
| 589 | + textwrap.dedent(config), |
| 590 | + encoding="utf8", |
| 591 | + ) |
| 592 | + |
| 593 | + with caplog.at_level(logging.WARNING, "pip._internal.exceptions"): |
| 594 | + exc = ExternallyManagedEnvironment.from_config(marker) |
| 595 | + assert not caplog.records |
| 596 | + assert str(exc.context) == expected |
0 commit comments