Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/snippets/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
LocalSourcePolicyIssue,
SnippetSourceAttributeIssue,
SnippetSourceAttributeRule,
SnippetSourceSafetyIssue,
SnippetSourceSafetyRule,
)

LOCAL_SOURCE_REMEDIATION = (
Expand All @@ -20,6 +22,10 @@
SnippetSourceAttributeRule.LOCAL_PATH_FORBIDDEN: "SNIP006",
SnippetSourceAttributeRule.UNSUPPORTED_SOURCE: "SNIP008",
}
SOURCE_SAFETY_CODES = {
SnippetSourceSafetyRule.UNREGISTERED_REPOSITORY: "SNIP009",
SnippetSourceSafetyRule.UNSAFE_PATH: "SNIP010",
}


def local_source_policy_diagnostic(
Expand Down Expand Up @@ -50,3 +56,19 @@ def snippet_source_attribute_diagnostics(
)
for issue in issues
)


def snippet_source_safety_diagnostics(
path: Path, issues: tuple[SnippetSourceSafetyIssue, ...]
) -> tuple[Diagnostic, ...]:
"""Assign stable diagnostic codes to source-safety failures."""

return tuple(
Diagnostic(
path=path,
span=issue.span,
code=SOURCE_SAFETY_CODES[issue.rule],
message=issue.message,
)
for issue in issues
)
38 changes: 38 additions & 0 deletions tests/test_snippet_source_safety_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations

from pathlib import Path

import pytest

from scripts.snippets.diagnostics import snippet_source_safety_diagnostics
from scripts.snippets.model import (
SnippetSourceSafetyIssue,
SnippetSourceSafetyRule,
Span,
)

PATH = Path("docs-main/validator.source.mdx")
SPAN = Span(start=20, end=30, line=7, column=3)


@pytest.mark.parametrize(
("rule", "code"),
[
(SnippetSourceSafetyRule.UNREGISTERED_REPOSITORY, "SNIP009"),
(SnippetSourceSafetyRule.UNSAFE_PATH, "SNIP010"),
],
)
def test_maps_source_safety_rule_to_stable_code(
rule: SnippetSourceSafetyRule, code: str
) -> None:
diagnostics = snippet_source_safety_diagnostics(
PATH,
(
SnippetSourceSafetyIssue(
rule=rule, span=SPAN, message="failure"
),
),
)

assert diagnostics[0].code == code
assert diagnostics[0].span == SPAN