Skip to content

Commit 9679b51

Browse files
authored
Test urls (#610)
* add test to check all urls * allow for redirects
1 parent dbf8579 commit 9679b51

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
3+
import httpx
4+
import pytest
5+
6+
from codemodder.registry import load_registered_codemods
7+
8+
registry = load_registered_codemods()
9+
10+
11+
async def visit_url(client, url):
12+
try:
13+
response = await client.get(url)
14+
return url, response.status_code
15+
except httpx.RequestError:
16+
return url, None
17+
18+
19+
@pytest.mark.asyncio
20+
async def check_accessible_urls(urls):
21+
async with httpx.AsyncClient() as client:
22+
tasks = [visit_url(client, url) for url in urls]
23+
results = await asyncio.gather(*tasks)
24+
25+
if failures := [
26+
(url, status) for url, status in results if status not in (200, 301, 302)
27+
]:
28+
if failures:
29+
failure_messages = [f"{url}: status={status}" for url, status in failures]
30+
pytest.fail(
31+
"Update the following URLs because they are not accessible:\n{}".format(
32+
"\n".join(failure_messages)
33+
)
34+
)
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_codemod_reference_urls():
39+
urls = [
40+
ref.url for codemod in registry.codemods for ref in codemod._metadata.references
41+
]
42+
await check_accessible_urls(urls)
43+
44+
45+
@pytest.mark.asyncio
46+
async def test_tool_rules_urls():
47+
urls = [
48+
rule.url
49+
for codemod in registry.codemods
50+
if (tool := codemod._metadata.tool)
51+
for rule in tool.rules
52+
if rule.url
53+
]
54+
await check_accessible_urls(urls)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ test = [
5252
"coverage>=7.3,<7.6",
5353
"coverage-threshold~=0.4",
5454
"Flask<4",
55+
"httpx~=0.27",
5556
"Jinja2~=3.1.2",
5657
"jsonschema~=4.22.0",
5758
"lxml>=4.9.3,<5.3.0",
@@ -60,6 +61,7 @@ test = [
6061
"pre-commit<4",
6162
"Pyjwt~=2.8.0",
6263
"pytest>=8.2,<9",
64+
"pytest-asyncio~=0.23",
6365
"pytest-cov>=4.1,<5.1",
6466
"pytest-mock>=3.12,<3.15",
6567
"pytest-randomly==3.*",

0 commit comments

Comments
 (0)