返回 CodeWhale
test_check_reqwest_builders.py
根目录 / scripts / test_check_reqwest_builders.py
1 #!/usr/bin/env python3
2 """Hermetic tests for the bare-reqwest-constructor gate (#6153)."""
3
4 from __future__ import annotations
5
6 import importlib.util
7 import sys
8 import tempfile
9 import unittest
10 from pathlib import Path
11
12 ROOT = Path(__file__).resolve().parents[1]
13 SCRIPT = ROOT / "scripts" / "check-reqwest-builders.py"
14 SPEC = importlib.util.spec_from_file_location("reqwest_builders", SCRIPT)
15 assert SPEC and SPEC.loader
16 mod = importlib.util.module_from_spec(SPEC)
17 sys.modules[SPEC.name] = mod
18 SPEC.loader.exec_module(mod)
19
20
21 class ReqwestBuilderTests(unittest.TestCase):
22 def test_qualified_constructors_fail(self) -> None:
23 with tempfile.TemporaryDirectory() as tmp:
24 root = Path(tmp)
25 victim = root / "tui.rs"
26 victim.write_text(
27 "let a = reqwest::Client::builder();\n"
28 "let b = reqwest::Client::new();\n"
29 "let c = reqwest::blocking::Client::builder();\n"
30 "let d = reqwest::blocking::Client::new();\n",
31 encoding="utf-8",
32 )
33 self.assertEqual(len(mod.find_violations(root)), 4)
34
35 def test_bare_client_spelling_fails_with_reqwest_import(self) -> None:
36 with tempfile.TemporaryDirectory() as tmp:
37 root = Path(tmp)
38 victim = root / "remote.rs"
39 victim.write_text(
40 "use reqwest::{Client, Method};\nlet client = Client::builder();\n",
41 encoding="utf-8",
42 )
43 violations = mod.find_violations(root)
44 self.assertEqual(len(violations), 1)
45 self.assertIn("remote.rs:2", violations[0])
46
47 def test_bare_client_without_reqwest_import_passes(self) -> None:
48 with tempfile.TemporaryDirectory() as tmp:
49 root = Path(tmp)
50 victim = root / "custom.rs"
51 victim.write_text(
52 "struct Client;\nimpl Client {\n fn new() -> Self {\n Client\n }\n}\n"
53 "let c = Client::new();\n",
54 encoding="utf-8",
55 )
56 # No `use reqwest...Client` import, so this is a custom type.
57 self.assertEqual(mod.find_violations(root), [])
58
59 def test_tls_builders_and_comments_pass(self) -> None:
60 with tempfile.TemporaryDirectory() as tmp:
61 root = Path(tmp)
62 victim = root / "ok.rs"
63 victim.write_text(
64 "// `reqwest::Client::builder()` panics under `rustls-no-provider`.\n"
65 "let a = crate::tls::reqwest_client_builder();\n"
66 "let b = codewhale_release::tls::reqwest_client();\n"
67 "let c = codewhale_release::platform_blocking_http_client_builder();\n"
68 "let d = FixedSummaryClient::default();\n",
69 encoding="utf-8",
70 )
71 self.assertEqual(mod.find_violations(root), [])
72
73 def test_release_crate_is_exempt(self) -> None:
74 with tempfile.TemporaryDirectory() as tmp:
75 root = Path(tmp)
76 allowed_dir = root / "release" / "src"
77 allowed_dir.mkdir(parents=True)
78 sanctioned = allowed_dir / "lib.rs"
79 sanctioned.write_text(
80 "let builder = reqwest::Client::builder();\n", encoding="utf-8"
81 )
82 self.assertEqual(mod.find_violations(root, allowed_dir), [])
83
84
85 if __name__ == "__main__":
86 unittest.main()
87
87 lines PYTHON