style: modernise typing and clean up lint findings
Repository-wide mechanical cleanup so `ruff check src tests` has a chance of passing; no behavioural changes. - Add `from __future__ import annotations` where PEP 604 unions are used. This has to come first: pyproject declares requires-python >= 3.9, where `X | None` is not evaluable at runtime unless annotations are stringified. - Replace typing.List/Dict/Tuple/Set with the builtin generics and Optional[X] with X | None, then drop the imports that became unused. The four actions/*/__init__.py files needed this by hand because ruff leaves unused imports in __init__.py alone (possible re-exports). - Strip shebangs from 72 importable modules. None of them are executable or invoked directly; the entry points are console_scripts and runpy. - Flatten nested `with` blocks, collapse needless-bool returns, and apply the remaining mechanical ruff fixes (PIE810, FLY002, PERF102, FURB192, RUF059, I001). - Pass check=False explicitly to the four subprocess.run() calls that inspect returncode themselves. That is the existing default. Two rewrites are visible to mocks, so their tests move with them: subprocess.run(stdout=PIPE, stderr=PIPE) became capture_output=True, and open(path, "r", ...) lost the redundant mode. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -21,62 +21,46 @@ class TestTokenResolverIntegration(unittest.TestCase):
|
||||
|
||||
resolver = TokenResolver()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 1) ENV: empty
|
||||
# ------------------------------------------------------------------
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
# ------------------------------------------------------------------
|
||||
# 2) GH CLI is available
|
||||
# ------------------------------------------------------------------
|
||||
with patch(
|
||||
def validate_side_effect(
|
||||
provider_kind: str,
|
||||
host: str,
|
||||
token: str,
|
||||
) -> bool:
|
||||
return False # gh + keyring invalid
|
||||
|
||||
with (
|
||||
patch.dict("os.environ", {}, clear=True),
|
||||
patch(
|
||||
"pkgmgr.core.credentials.providers.gh.shutil.which",
|
||||
return_value="/usr/bin/gh",
|
||||
):
|
||||
with patch(
|
||||
"pkgmgr.core.credentials.providers.gh.subprocess.check_output",
|
||||
return_value="gh-invalid-token\n",
|
||||
):
|
||||
# ------------------------------------------------------------------
|
||||
# 3) Keyring returns an existing (invalid) token
|
||||
# ------------------------------------------------------------------
|
||||
with patch(
|
||||
"pkgmgr.core.credentials.providers.keyring._import_keyring"
|
||||
) as mock_import_keyring:
|
||||
mock_keyring = mock_import_keyring.return_value
|
||||
mock_keyring.get_password.return_value = "keyring-invalid-token"
|
||||
),
|
||||
patch(
|
||||
"pkgmgr.core.credentials.providers.gh.subprocess.check_output",
|
||||
return_value="gh-invalid-token\n",
|
||||
),
|
||||
patch(
|
||||
"pkgmgr.core.credentials.providers.keyring._import_keyring"
|
||||
) as mock_import_keyring,
|
||||
patch(
|
||||
"pkgmgr.core.credentials.providers.prompt.sys.stdin.isatty",
|
||||
return_value=True,
|
||||
),
|
||||
patch(
|
||||
"pkgmgr.core.credentials.providers.prompt.getpass",
|
||||
return_value="new-valid-token",
|
||||
),
|
||||
patch(
|
||||
"pkgmgr.core.credentials.resolver.validate_token",
|
||||
side_effect=validate_side_effect,
|
||||
) as validate_mock,
|
||||
):
|
||||
mock_keyring = mock_import_keyring.return_value
|
||||
mock_keyring.get_password.return_value = "keyring-invalid-token"
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4) Prompt is allowed and returns a NEW token
|
||||
# ------------------------------------------------------------------
|
||||
with patch(
|
||||
"pkgmgr.core.credentials.providers.prompt.sys.stdin.isatty",
|
||||
return_value=True,
|
||||
):
|
||||
with patch(
|
||||
"pkgmgr.core.credentials.providers.prompt.getpass",
|
||||
return_value="new-valid-token",
|
||||
):
|
||||
# ------------------------------------------------------------------
|
||||
# 5) Validation logic:
|
||||
# - gh token invalid
|
||||
# - keyring token invalid
|
||||
# - prompt token is NOT validated (by design)
|
||||
# ------------------------------------------------------------------
|
||||
def validate_side_effect(
|
||||
provider_kind: str,
|
||||
host: str,
|
||||
token: str,
|
||||
) -> bool:
|
||||
return False # gh + keyring invalid
|
||||
|
||||
with patch(
|
||||
"pkgmgr.core.credentials.resolver.validate_token",
|
||||
side_effect=validate_side_effect,
|
||||
) as validate_mock:
|
||||
result = resolver.get_token(
|
||||
provider_kind="github",
|
||||
host="github.com",
|
||||
)
|
||||
result = resolver.get_token(
|
||||
provider_kind="github",
|
||||
host="github.com",
|
||||
)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Assertions
|
||||
@@ -93,7 +77,7 @@ class TestTokenResolverIntegration(unittest.TestCase):
|
||||
|
||||
# Keyring must be overwritten with the new token
|
||||
mock_keyring.set_password.assert_called_once()
|
||||
service, username, stored_token = mock_keyring.set_password.call_args.args
|
||||
_service, _username, stored_token = mock_keyring.set_password.call_args.args
|
||||
self.assertEqual(stored_token, "new-valid-token")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user