Skip to content

Commit 9c08990

Browse files
committed
Add option to allow absolute links
1 parent f534ba4 commit 9c08990

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pytest --check-links mynotebook.ipynb
3333
3434
A comma-separated list of extensions to check
3535

36+
#### --check-links-allow-absolute
37+
38+
Allow absolute links (links starting with `/`).
39+
3640
#### --check-anchors
3741

3842
Also check whether links with `#anchors` HTML files (either local, or with

pytest_check_links/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def pytest_addoption(parser: pytest.Parser) -> None:
3434
"""Add options to pytest."""
3535
group = parser.getgroup("general")
3636
group.addoption("--check-links", action="store_true", help="Check links for validity")
37+
group.addoption(
38+
"--check-links-allow-absolute",
39+
action="store_true",
40+
help="Permitabsolute links (links which start with '/')",
41+
)
3742
group.addoption("--check-anchors", action="store_true", help="Check link anchors for validity")
3843
group.addoption(
3944
"--links-ext",
@@ -82,6 +87,7 @@ def pytest_collect_file(file_path: Path, parent: pytest.Collector) -> CheckLinks
8287
"""Add pytest file collection filter."""
8388
config = parent.config
8489
ignore_links = config.option.check_links_ignore
90+
allow_absolute = config.option.check_links_allow_absolute
8591

8692
if config.option.check_links:
8793
requests_session = ensure_requests_session(config)
@@ -94,6 +100,7 @@ def pytest_collect_file(file_path: Path, parent: pytest.Collector) -> CheckLinks
94100
parent,
95101
path=file_path,
96102
requests_session=requests_session,
103+
allow_absolute=allow_absolute,
97104
check_anchors=check_anchors,
98105
ignore_links=ignore_links,
99106
),
@@ -102,6 +109,7 @@ def pytest_collect_file(file_path: Path, parent: pytest.Collector) -> CheckLinks
102109
path=file_path,
103110
parent=parent,
104111
requests_session=requests_session,
112+
allow_absolute=allow_absolute,
105113
check_anchors=check_anchors,
106114
ignore_links=ignore_links,
107115
)
@@ -138,12 +146,14 @@ def __init__(
138146
self,
139147
*,
140148
requests_session: Session | None = None,
149+
allow_absolute: bool = False,
141150
check_anchors: bool = False,
142151
ignore_links: list[str] | None = None,
143152
**kwargs: Any,
144153
) -> None:
145154
"""Initialize."""
146155
super().__init__(**kwargs)
156+
self.allow_absolute = allow_absolute
147157
self.check_anchors = check_anchors
148158
self.requests_session = requests_session
149159
self.ignore_links = ignore_links or []
@@ -415,7 +425,7 @@ def runtest(self) -> None:
415425
parsed = html5lib.parse(response.content, namespaceHTMLElements=False)
416426
return self.handle_anchor(parsed, anchor)
417427
else:
418-
if url.startswith("/"):
428+
if not self.parent.allow_absolute and url.startswith("/"):
419429
raise BrokenLinkError(url, "absolute path link")
420430
# relative URL
421431
anchor = None

test/test_check_links.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ def test_ipynb(pytester):
77
pytester.copy_example("linkcheck.ipynb")
88
result = pytester.runpytest_subprocess("-v", "--check-links")
99
result.assert_outcomes(passed=3, failed=4)
10+
11+
12+
def test_ipynb_with_ignore(pytester):
13+
pytester.copy_example("linkcheck.ipynb")
1014
result = pytester.runpytest_subprocess(
1115
"-v", "--check-links", "--check-links-ignore", "http.*example.com/.*"
1216
)
1317
result.assert_outcomes(passed=3, failed=3)
1418

1519

20+
def test_ipynb_with_allow_absolute(pytester):
21+
pytester.copy_example("linkcheck.ipynb")
22+
result = pytester.runpytest_subprocess(
23+
"-v", "--check-links", "--check-links-allow-absolute"
24+
)
25+
result.assert_outcomes(passed=4, failed=3)
26+
27+
1628
def test_markdown(pytester):
1729
pytester.copy_example("markdown.md")
1830
result = pytester.runpytest_subprocess("-v", "--check-links")

0 commit comments

Comments
 (0)