From 57e6c29797176c59a4cbe31b6e146e90af8424c8 Mon Sep 17 00:00:00 2001 From: TAHRI Ahmed R Date: Mon, 22 Apr 2024 07:40:24 +0200 Subject: [PATCH] :heavy_check_mark: Add tests for the broken environment case (#111) --- HISTORY.md | 2 +- noxfile.py | 9 +++++++-- src/niquests/_compat.py | 7 ++++++- src/niquests/help.py | 6 +++++- tests/test_requests.py | 7 ++++++- tests/test_structures.py | 7 ++++++- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 560129fc5a..cf85925efc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,7 @@ Release History =============== -3.6.1 (2024-04-21) +3.6.1 (2024-04-22) ------------------ **Fixed** diff --git a/noxfile.py b/noxfile.py index 73259ba0a1..8ce90143b8 100644 --- a/noxfile.py +++ b/noxfile.py @@ -9,7 +9,7 @@ def tests_impl( session: nox.Session, extras: str = "socks", - cohabitation: bool = False, + cohabitation: bool | None = False, ) -> None: # Install deps and the package itself. session.install("-r", "requirements-dev.txt") @@ -19,9 +19,13 @@ def tests_impl( session.run("pip", "--version") session.run("python", "--version") - if cohabitation: + if cohabitation is True: session.run("pip", "install", "urllib3") session.run("python", "-m", "niquests.help") + elif cohabitation is None: + session.run("pip", "install", "urllib3") + session.run("pip", "uninstall", "-y", "urllib3") + session.run("python", "-m", "niquests.help") session.run( "python", @@ -55,6 +59,7 @@ def test(session: nox.Session) -> None: ) def test_cohabitation(session: nox.Session) -> None: tests_impl(session, cohabitation=True) + tests_impl(session, cohabitation=None) @nox.session diff --git a/src/niquests/_compat.py b/src/niquests/_compat.py index a8ada66037..c2917902e1 100644 --- a/src/niquests/_compat.py +++ b/src/niquests/_compat.py @@ -19,7 +19,12 @@ try: import urllib3 -except ImportError: + + # force detect broken or dummy urllib3 package + urllib3.Timeout # noqa + urllib3.Retry # noqa + urllib3.__version__ # noqa +except (ImportError, AttributeError): urllib3 = None # type: ignore[assignment] diff --git a/src/niquests/help.py b/src/niquests/help.py index 5b6552677b..748144278f 100644 --- a/src/niquests/help.py +++ b/src/niquests/help.py @@ -22,7 +22,11 @@ if HAS_LEGACY_URLLIB3 is True: import urllib3_future as urllib3 - from urllib3 import __version__ as __legacy_urllib3_version__ + + try: + from urllib3 import __version__ as __legacy_urllib3_version__ + except (ImportError, AttributeError): + __legacy_urllib3_version__ = None # type: ignore[assignment] else: import urllib3 # type: ignore[no-redef] diff --git a/tests/test_requests.py b/tests/test_requests.py index af81f41d50..ec58550fe2 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2632,7 +2632,12 @@ def test_prepared_copy(kwargs): def test_urllib3_retries(httpbin): - from urllib3.util import Retry + from niquests._compat import HAS_LEGACY_URLLIB3 + + if not HAS_LEGACY_URLLIB3: + from urllib3.util import Retry + else: + from urllib3_future.util import Retry s = niquests.Session() s.mount("http://", HTTPAdapter(max_retries=Retry(total=2, status_forcelist=[500]))) diff --git a/tests/test_structures.py b/tests/test_structures.py index 2ecf0d4102..2ccd48d9a6 100644 --- a/tests/test_structures.py +++ b/tests/test_structures.py @@ -3,7 +3,12 @@ import pytest from niquests.structures import CaseInsensitiveDict, LookupDict -from urllib3 import HTTPHeaderDict +from niquests._compat import HAS_LEGACY_URLLIB3 + +if not HAS_LEGACY_URLLIB3: + from urllib3 import HTTPHeaderDict +else: + from urllib3_future import HTTPHeaderDict class TestCaseInsensitiveDict: