@@ -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
0 commit comments