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
24 changes: 24 additions & 0 deletions scripts/snippets/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from .model import (
Diagnostic,
IfVersionAttributeIssue,
IfVersionAttributeRule,
LocalSourcePolicyIssue,
SnippetAttributeIssue,
SnippetAttributeRule,
Expand Down Expand Up @@ -33,6 +35,12 @@
SnippetAttributeRule.INVALID_LANGUAGE: "SNIP016",
SnippetAttributeRule.INVALID_MARKERS: "SNIP017",
}
IF_VERSION_ATTRIBUTE_CODES = {
IfVersionAttributeRule.UNKNOWN_ATTRIBUTE: "SNIP020",
IfVersionAttributeRule.INVALID_REPOSITORY: "SNIP021",
IfVersionAttributeRule.UNREGISTERED_REPOSITORY: "SNIP022",
IfVersionAttributeRule.INVALID_PULL_REQUEST: "SNIP023",
}


def local_source_policy_diagnostic(
Expand Down Expand Up @@ -95,3 +103,19 @@ def snippet_attribute_diagnostics(
)
for issue in issues
)


def if_version_attribute_diagnostics(
path: Path, issues: tuple[IfVersionAttributeIssue, ...]
) -> tuple[Diagnostic, ...]:
"""Assign stable diagnostic codes to IfVersion attribute failures."""

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

from pathlib import Path

import pytest

from scripts.snippets.diagnostics import if_version_attribute_diagnostics
from scripts.snippets.model import (
IfVersionAttributeIssue,
IfVersionAttributeRule,
Span,
)

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


@pytest.mark.parametrize(
("rule", "code"),
[
(IfVersionAttributeRule.UNKNOWN_ATTRIBUTE, "SNIP020"),
(IfVersionAttributeRule.INVALID_REPOSITORY, "SNIP021"),
(IfVersionAttributeRule.UNREGISTERED_REPOSITORY, "SNIP022"),
(IfVersionAttributeRule.INVALID_PULL_REQUEST, "SNIP023"),
],
)
def test_maps_if_version_attribute_rule_to_stable_code(
rule: IfVersionAttributeRule, code: str
) -> None:
diagnostics = if_version_attribute_diagnostics(
PATH,
(IfVersionAttributeIssue(rule=rule, span=SPAN, message="failure"),),
)

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