fix: resolve pylint/pyright issues in new test files
- test_contrib_gitea_client: remove unused Any import, fix _mock_response to use return_value instead of lambda (unknown lambda type), narrow HTTPError hdrs type, add type annotations to fake_urlopen helpers, suppress protected-access for _request tests - test_bootstrap: annotate **kw as **kw: object, use dict literal, unpack server_address via index to avoid tuple type mismatch - test_main: remove unused MagicMock import - test_watchdog: guard store.get() result before accessing .status Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,12 +17,13 @@ def _client() -> GiteaClient:
|
||||
def _mock_response(body: bytes) -> MagicMock:
|
||||
resp = MagicMock()
|
||||
resp.read.return_value = body
|
||||
resp.__enter__ = lambda s: s
|
||||
resp.__exit__ = MagicMock(return_value=False)
|
||||
resp.__enter__.return_value = resp
|
||||
resp.__exit__.return_value = False
|
||||
return resp
|
||||
|
||||
|
||||
class GiteaClientTest(unittest.TestCase):
|
||||
# pylint: disable=protected-access
|
||||
def setUp(self):
|
||||
self.client = _client()
|
||||
|
||||
@@ -46,10 +47,8 @@ class GiteaClientTest(unittest.TestCase):
|
||||
self.assertTrue(self.client.is_org_member("myorg", "alice"))
|
||||
|
||||
def test_is_org_member_false_on_http_error(self):
|
||||
with patch(
|
||||
"urllib.request.urlopen",
|
||||
side_effect=urllib.error.HTTPError("url", 404, "Not Found", {}, None),
|
||||
):
|
||||
err = urllib.error.HTTPError("url", 404, "Not Found", None, None) # type: ignore[arg-type]
|
||||
with patch("urllib.request.urlopen", side_effect=err):
|
||||
self.assertFalse(self.client.is_org_member("myorg", "nobody"))
|
||||
|
||||
def test_get_issue(self):
|
||||
@@ -84,34 +83,30 @@ class GiteaClientTest(unittest.TestCase):
|
||||
mock_open.assert_called_once()
|
||||
|
||||
def test_request_builds_correct_url(self):
|
||||
captured: list[object] = []
|
||||
import urllib.request as ureq
|
||||
captured: list[ureq.Request] = []
|
||||
|
||||
def fake_urlopen(req, timeout):
|
||||
def fake_urlopen(req: ureq.Request, timeout: float) -> MagicMock: # pylint: disable=unused-argument
|
||||
captured.append(req)
|
||||
return _mock_response(b"{}")
|
||||
|
||||
with patch("urllib.request.urlopen", side_effect=fake_urlopen):
|
||||
self.client.get_issue(5)
|
||||
|
||||
import urllib.request as ureq
|
||||
req = captured[0]
|
||||
assert isinstance(req, ureq.Request)
|
||||
self.assertIn("/issues/5", req.full_url)
|
||||
self.assertIn("/issues/5", captured[0].full_url)
|
||||
|
||||
def test_request_sends_auth_header(self):
|
||||
captured: list[object] = []
|
||||
import urllib.request as ureq
|
||||
captured: list[ureq.Request] = []
|
||||
|
||||
def fake_urlopen(req, timeout):
|
||||
def fake_urlopen(req: ureq.Request, timeout: float) -> MagicMock: # pylint: disable=unused-argument
|
||||
captured.append(req)
|
||||
return _mock_response(b"{}")
|
||||
|
||||
with patch("urllib.request.urlopen", side_effect=fake_urlopen):
|
||||
self.client.get_issue(1)
|
||||
|
||||
import urllib.request as ureq
|
||||
req = captured[0]
|
||||
assert isinstance(req, ureq.Request)
|
||||
self.assertEqual("token tok", req.get_header("Authorization"))
|
||||
self.assertEqual("token tok", captured[0].get_header("Authorization"))
|
||||
|
||||
|
||||
class GiteaForgeTest(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user