Skip to content

Commit 3a15e01

Browse files
committed
Get the tests passing again
1 parent 4f8a613 commit 3a15e01

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/pip/_internal/network/auth.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
logger = getLogger(__name__)
3030

31+
KEYRING_DISABLED = False
32+
3133

3234
class Credentials(NamedTuple):
3335
url: str
@@ -174,11 +176,20 @@ def get_keyring_auth(url: Optional[str], username: Optional[str]) -> Optional[Au
174176
return None
175177

176178
keyring = get_keyring_provider()
177-
# Do nothin if keyring is not available
178-
if keyring is None:
179+
# Do nothing if keyring is not available
180+
global KEYRING_DISABLED
181+
if keyring is None or KEYRING_DISABLED:
179182
return None
180183

181-
return keyring.get_auth_info(url, username)
184+
try:
185+
return keyring.get_auth_info(url, username)
186+
except Exception as exc:
187+
logger.warning(
188+
"Keyring is skipped due to an exception: %s",
189+
str(exc),
190+
)
191+
KEYRING_DISABLED = True
192+
return None
182193

183194

184195
class MultiDomainBasicAuth(AuthBase):

tests/unit/test_network_auth.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
2-
from typing import Any, List, Optional, Tuple
2+
import sys
3+
from typing import Any, Iterable, List, Optional, Tuple
34

45
import pytest
56

@@ -8,6 +9,14 @@
89
from tests.lib.requests_mocks import MockConnection, MockRequest, MockResponse
910

1011

12+
@pytest.fixture(scope="function", autouse=True)
13+
def reset_keyring() -> Iterable[None]:
14+
yield None
15+
# Reset the state of the module between tests
16+
pip._internal.network.auth.KEYRING_DISABLED = False
17+
pip._internal.network.auth.get_keyring_provider.cache_clear()
18+
19+
1120
@pytest.mark.parametrize(
1221
["input_url", "url", "username", "password"],
1322
[
@@ -138,7 +147,7 @@ def test_keyring_get_password(
138147
expect: Tuple[Optional[str], Optional[str]],
139148
) -> None:
140149
keyring = KeyringModuleV1()
141-
monkeypatch.setattr("pip._internal.network.auth.keyring", keyring)
150+
monkeypatch.setitem(sys.modules, "keyring", keyring) # type: ignore[misc]
142151
auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"])
143152

144153
actual = auth._get_new_credentials(url, allow_netrc=False, allow_keyring=True)
@@ -147,7 +156,7 @@ def test_keyring_get_password(
147156

148157
def test_keyring_get_password_after_prompt(monkeypatch: pytest.MonkeyPatch) -> None:
149158
keyring = KeyringModuleV1()
150-
monkeypatch.setattr("pip._internal.network.auth.keyring", keyring)
159+
monkeypatch.setitem(sys.modules, "keyring", keyring) # type: ignore[misc]
151160
auth = MultiDomainBasicAuth()
152161

153162
def ask_input(prompt: str) -> str:
@@ -163,7 +172,7 @@ def test_keyring_get_password_after_prompt_when_none(
163172
monkeypatch: pytest.MonkeyPatch,
164173
) -> None:
165174
keyring = KeyringModuleV1()
166-
monkeypatch.setattr("pip._internal.network.auth.keyring", keyring)
175+
monkeypatch.setitem(sys.modules, "keyring", keyring) # type: ignore[misc]
167176
auth = MultiDomainBasicAuth()
168177

169178
def ask_input(prompt: str) -> str:
@@ -184,7 +193,7 @@ def test_keyring_get_password_username_in_index(
184193
monkeypatch: pytest.MonkeyPatch,
185194
) -> None:
186195
keyring = KeyringModuleV1()
187-
monkeypatch.setattr("pip._internal.network.auth.keyring", keyring)
196+
monkeypatch.setitem(sys.modules, "keyring", keyring) # type: ignore[misc]
188197
auth = MultiDomainBasicAuth(index_urls=["http://[email protected]/path2"])
189198
get = functools.partial(
190199
auth._get_new_credentials, allow_netrc=False, allow_keyring=True
@@ -217,7 +226,7 @@ def test_keyring_set_password(
217226
expect_save: bool,
218227
) -> None:
219228
keyring = KeyringModuleV1()
220-
monkeypatch.setattr("pip._internal.network.auth.keyring", keyring)
229+
monkeypatch.setitem(sys.modules, "keyring", keyring) # type: ignore[misc]
221230
auth = MultiDomainBasicAuth(prompting=True)
222231
monkeypatch.setattr(auth, "_get_url_and_credentials", lambda u: (u, None, None))
223232
monkeypatch.setattr(auth, "_prompt_for_password", lambda *a: creds)
@@ -293,7 +302,7 @@ def get_credential(self, system: str, username: str) -> Optional[Credential]:
293302
def test_keyring_get_credential(
294303
monkeypatch: pytest.MonkeyPatch, url: str, expect: str
295304
) -> None:
296-
monkeypatch.setattr(pip._internal.network.auth, "keyring", KeyringModuleV2())
305+
monkeypatch.setitem(sys.modules, "keyring", KeyringModuleV2()) # type: ignore[misc]
297306
auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"])
298307

299308
assert (
@@ -314,7 +323,7 @@ def get_credential(self, system: str, username: str) -> None:
314323

315324
def test_broken_keyring_disables_keyring(monkeypatch: pytest.MonkeyPatch) -> None:
316325
keyring_broken = KeyringModuleBroken()
317-
monkeypatch.setattr(pip._internal.network.auth, "keyring", keyring_broken)
326+
monkeypatch.setitem(sys.modules, "keyring", keyring_broken) # type: ignore[misc]
318327

319328
auth = MultiDomainBasicAuth(index_urls=["http://example.com/"])
320329

0 commit comments

Comments
 (0)