From 1056b7b783ae5e0b440f273da02817d4ad1f503e Mon Sep 17 00:00:00 2001 From: Martin Stolle Date: Mon, 18 Nov 2024 08:36:45 +0100 Subject: [PATCH] Set password directly if keyring credentials are used --- src/hatch/publish/auth.py | 6 ++---- tests/utils/test_auth.py | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/hatch/publish/auth.py b/src/hatch/publish/auth.py index 4d3afbece..5600ff5cc 100644 --- a/src/hatch/publish/auth.py +++ b/src/hatch/publish/auth.py @@ -46,10 +46,6 @@ def __get_password(self) -> str: if password is not None: return password - password = keyring.get_password(self._repo, self.username) - if password is not None: - return password - if self._options['no_prompt']: self._app.abort('Missing required option: auth') @@ -77,6 +73,8 @@ def _read_keyring(self) -> str | None: creds = keyring.get_credential(self._repo, None) if not creds: return None + self.__password = creds.password + self.__password_was_read = True return creds.username def _read_previous_working_user_data(self) -> str | None: diff --git a/tests/utils/test_auth.py b/tests/utils/test_auth.py index 9a9a254f2..12a379b33 100644 --- a/tests/utils/test_auth.py +++ b/tests/utils/test_auth.py @@ -1,8 +1,24 @@ +import pytest + import hatch.publish.auth from hatch.publish.auth import AuthenticationCredentials from hatch.utils.fs import Path +@pytest.fixture(autouse=True) +def mock_keyring(monkeypatch): + class MockKeyring: + @staticmethod + def get_credential(*_): + class Credential: + username = 'gat' + password = 'guido' + + return Credential() + + monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring) + + def test_pypirc(fs): fs.create_file( Path.home() / '.pypirc', @@ -45,21 +61,7 @@ def test_pypirc(fs): assert credentials.password == 'gat' -def test_keyring_credentials(monkeypatch): - class MockKeyring: - @staticmethod - def get_credential(*_): - class Credential: - username = 'gat' - - return Credential() - - @staticmethod - def get_password(*_): - return 'guido' - - monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring) - +def test_keyring_credentials(): credentials = AuthenticationCredentials( app=None, cache_dir=Path('/none'),