Skip to content

Commit 1de0bfd

Browse files
rayangleri80and
authored andcommitted
DOCSP-5424: Validate include files within repo
1 parent 4c5405e commit 1de0bfd

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

snooty/parser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,31 @@ def handle_directive(
347347
msg = f'Invalid "literalinclude": {err}'
348348
self.diagnostics.append(Diagnostic.error(msg, util.get_line(node)))
349349
return
350+
elif name == "include":
351+
if argument_text is None:
352+
self.diagnostics.append(
353+
Diagnostic.error(
354+
f'"{name}" expected a path argument', util.get_line(node)
355+
)
356+
)
357+
return
358+
359+
fileid, path = util.reroot_path(
360+
Path(argument_text), self.docpath, self.source_path
361+
)
362+
363+
# Validate if file exists
364+
if not path.is_file():
365+
# Check if file is snooty-generated
366+
if (
367+
fileid.match("steps/*.rst")
368+
or fileid.match("extracts/*.rst")
369+
or fileid.match("release/*.rst")
370+
):
371+
pass
372+
else:
373+
msg = f'"{name}" could not open "{argument_text}: No such file exists"'
374+
self.diagnostics.append(Diagnostic.error(msg, util.get_line(node)))
350375

351376
if options:
352377
doc["options"] = options

snooty/test_parser.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,46 @@ def test_literalinclude() -> None:
178178
assert len(diagnostics) == 1
179179

180180

181+
def test_include() -> None:
182+
root = Path("test_data")
183+
path = Path(root).joinpath(Path("test.rst"))
184+
project_config = ProjectConfig(root, "", source="./")
185+
parser = rstparser.Parser(project_config, JSONVisitor)
186+
187+
# Test good include
188+
page, diagnostics = parse_rst(
189+
parser,
190+
path,
191+
"""
192+
.. include:: /driver-examples/rstexample.rst
193+
""",
194+
)
195+
page.finish(diagnostics)
196+
assert diagnostics == []
197+
198+
# Test generated include
199+
page, diagnostics = parse_rst(
200+
parser,
201+
path,
202+
"""
203+
.. include:: /driver-examples/steps/generated-include.rst
204+
""",
205+
)
206+
page.finish(diagnostics)
207+
assert diagnostics == []
208+
209+
# Test bad include
210+
page, diagnostics = parse_rst(
211+
parser,
212+
path,
213+
"""
214+
.. include:: /driver-examples/fake-include.rst
215+
""",
216+
)
217+
page.finish(diagnostics)
218+
assert len(diagnostics) == 1
219+
220+
181221
def test_admonition() -> None:
182222
root = Path("test_data")
183223
path = Path(root).joinpath(Path("test.rst"))

test_data/driver-examples/rstexample.rst

Whitespace-only changes.

0 commit comments

Comments
 (0)