Skip to content

Commit

Permalink
test: test linkcheck.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Batchelder committed Oct 19, 2023
1 parent abab6f7 commit bdcb076
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
17 changes: 5 additions & 12 deletions src/scriv/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import concurrent.futures
import logging
import time
from typing import Iterable

import markdown_it
Expand Down Expand Up @@ -37,17 +36,11 @@ def check_markdown_links(markdown_text: str) -> None:

def check_one_link(url: str) -> None:
"""Check if a URL is reachable. Logs a warning if not."""
while True:
try:
resp = requests.head(url, timeout=60, allow_redirects=True)
except requests.RequestException as exc:
logger.warning(f"Failed check for {url!r}: {exc}")
return
if resp.status_code == 429:
wait = int(resp.headers.get("Retry-After", 10))
time.sleep(wait + 1)
else:
break
try:
resp = requests.head(url, timeout=60, allow_redirects=True)
except Exception as exc: # pylint: disable=broad-exception-caught
logger.warning(f"Failed check for {url!r}: {exc}")
return

if resp.status_code == 200:
logger.debug(f"OK link: {url!r}")
Expand Down
60 changes: 58 additions & 2 deletions tests/test_linkcheck.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
"""Tests of scriv/linkcheck.py"""

import logging
import textwrap

import pytest

from scriv.linkcheck import find_links
from scriv.linkcheck import check_markdown_links, find_links


@pytest.mark.parametrize(
"markdown_text, links",
[
("Hello", []),
(
"""\
[one](https://two.com/hello) and
[two](https://one.com/xyzzy).
""",
["https://one.com/xyzzy", "https://two.com/hello"],
),
(
"""\
This is [an example](http://example1.com/ "Title") inline link.
This is [an example] [id] reference-style link.
[id]: http://example2.com/ "Optional Title Here"
""",
["http://example1.com/", "http://example2.com/"],
),
],
)
def test_find_links(markdown_text, links):
found_links = sorted(find_links(markdown_text))
found_links = sorted(find_links(textwrap.dedent(markdown_text)))
assert links == found_links


def test_check_markdown_link(caplog, responses):
caplog.set_level(logging.DEBUG, logger="scriv.linkcheck")
responses.head("https://nedbat.com")
check_markdown_links("""[hey](https://nedbat.com)!""")
assert caplog.record_tuples == [
(
"scriv.linkcheck",
logging.DEBUG,
"OK link: 'https://nedbat.com'",
)
]


def test_check_404_markdown_link(caplog, responses):
responses.head("https://nedbat.com", status=404)
check_markdown_links("""[hey](https://nedbat.com)!""")
assert caplog.record_tuples == [
(
"scriv.linkcheck",
logging.WARNING,
"Failed check for 'https://nedbat.com': status code 404",
)
]


def test_check_failing_markdown_link(caplog, responses):
responses.head("https://nedbat.com", body=Exception("Buh?"))
check_markdown_links("""[hey](https://nedbat.com)!""")
assert caplog.record_tuples == [
(
"scriv.linkcheck",
logging.WARNING,
"Failed check for 'https://nedbat.com': Buh?",
)
]

0 comments on commit bdcb076

Please sign in to comment.