test_guard_report_formats.py

tests/test_guard_report_formats.py

`clew guard validate --format sarif|gitlab|github` (S1; 2.0.0).

test_github_annotations_are_byte_for_byte_and_escaped

None test_guard_report_formats.test_github_annotations_are_byte_for_byte_and_escaped()

`::error` lines, sorted, with `%`, newlines, `:` and `,` escaped as the runner wants.

tests/test_guard_report_formats.py:79

Show on the map →

Called by none

Calls none

Checks

  • assert render(_VIOLATIONS, "github") == ("::error file=src/a.c,line=3,title=clew guard presence::Function 'close' has no doxygen comment\n" "::error file=src/a.c,line=12,title=clew guard version::" "Function 'open' body changed but @version was not updated\n" "::error file=src/b%2Cc%3Ad.py,line=7,title=clew guard tag::Unknown tag @foo in run()%0A100%25 sure\n" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-GUARD-001measured
Source · 10 lines
def test_github_annotations_are_byte_for_byte_and_escaped() -> None:
    """@brief `::error` lines, sorted, with `%`, newlines, `:` and `,` escaped as the runner wants."""
    assert render(_VIOLATIONS, "github") == (
        "::error file=src/a.c,line=3,title=clew guard presence::Function 'close' has no doxygen comment\n"
        "::error file=src/a.c,line=12,title=clew guard version::"
        "Function 'open' body changed but @version was not updated\n"
        "::error file=src/b%2Cc%3Ad.py,line=7,title=clew guard tag::Unknown tag @foo in run()%0A100%25 sure\n"
    )

Activity · 1 steps
lines 81–86assert render(_VIOLATIONS, "github") == ( "::error file=src/a.c,line=3,title=clew guard presence::Function 'close' has no doxygen comment\n" "::error file=src/a.c,line=12,title=clew guard version::" "Function 'open' body changed but @version was not updated\n" "::error file=src/b%2Cc%3Ad.py,line=7,title=clew guard tag::Unknown tag @foo in run()%0A100%25 sure\n" )

test_gitlab_code_quality_is_byte_for_byte

None test_guard_report_formats.test_gitlab_code_quality_is_byte_for_byte()

The Code Quality document for one violation, exactly.

tests/test_guard_report_formats.py:58

Show on the map →

Called by none

Calls fingerprint

Checks

  • assert render([v], "gitlab") == ("[\n" " {\n" ' "check_name": "presence",\n' ' "description": "Function \'close\' has no doxygen comment",\n' f' "fingerprint": "{fingerprint(v)}",\n' ' "location": {\n' ' "lines": {\n'

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-GUARD-001measuredtest_gitlab_code_quality_is_byte_for_byte → fingerprint
Source · 21 lines
def test_gitlab_code_quality_is_byte_for_byte() -> None:
    """@brief The Code Quality document for one violation, exactly."""
    v = _VIOLATIONS[1]
    assert render([v], "gitlab") == (
        "[\n"
        "  {\n"
        '    "check_name": "presence",\n'
        '    "description": "Function \'close\' has no doxygen comment",\n'
        f'    "fingerprint": "{fingerprint(v)}",\n'
        '    "location": {\n'
        '      "lines": {\n'
        '        "begin": 3\n'
        "      },\n"
        '      "path": "src/a.c"\n'
        "    },\n"
        '    "severity": "major"\n'
        "  }\n"
        "]\n"
    )

Activity · 2 steps
lines 60–60v = _VIOLATIONS[1]lines 61–76assert render([v], "gitlab") == ( "[\n" " {\n" ' "check_name": "presence",\n' ' "description": "Function \'close\' has no doxygen comment",\n' f' "fingerprint": "{fingerprint(v)}",\n' ' "location": {\n' ' "lines": {\n' ' "begin": 3\n' " },\n" ' "path": "src/a.c"\n' " },\n" ' "severity": "major"\n' " }\n" "]\n" )

test_no_violations_is_an_empty_document_not_no_document

None test_guard_report_formats.test_no_violations_is_an_empty_document_not_no_document()

Zero violations: a valid empty log / empty array / no lines.

tests/test_guard_report_formats.py:89

Show on the map →

Called by none

Calls none

Checks

  • assert json.loads(render([], "sarif"))["runs"][0]["results"] == []
  • assert json.loads(render([], "gitlab")) == []
  • assert render([], "github") == ""

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-GUARD-001measured
Source · 8 lines
def test_no_violations_is_an_empty_document_not_no_document() -> None:
    """@brief Zero violations: a valid empty log / empty array / no lines."""
    assert json.loads(render([], "sarif"))["runs"][0]["results"] == []
    assert json.loads(render([], "gitlab")) == []
    assert render([], "github") == ""


@pytest.mark.parametrize("fmt", ["sarif", "gitlab", "github"])

test_sarif_validates_against_the_official_schema

None test_guard_report_formats.test_sarif_validates_against_the_official_schema()

The SARIF log is schema-valid, with a rule for every check class.

tests/test_guard_report_formats.py:38

Show on the map →

Called by none

Calls none

Checks

  • assert {r["id"] for r in run["tool"]["driver"]["rules"]} >= {"presence", "version", "tag"}
  • assert [

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-GUARD-001measured
Source · 11 lines
def test_sarif_validates_against_the_official_schema() -> None:
    """@brief The SARIF log is schema-valid, with a rule for every check class."""
    log = json.loads(render(_VIOLATIONS, "sarif"))
    jsonschema.validate(log, json.loads(_SCHEMA.read_text()))
    (run,) = log["runs"]
    assert {r["id"] for r in run["tool"]["driver"]["rules"]} >= {"presence", "version", "tag"}
    assert [
        r["locations"][0]["physicalLocation"]["region"]["startLine"] for r in run["results"]
    ] == [3, 12, 7]

Activity · 5 steps
lines 40–40log = json.loads(render(_VIOLATIONS, "sarif"))lines 41–41jsonschema.validate(log, json.loads(_SCHEMA.read_text()))lines 42–42(run,) = log["runs"]lines 43–43assert {r["id"] for r in run["tool"]["driver"]["rules"]} >= {"presence", "version", "tag"}lines 44–46assert [ r["locations"][0]["physicalLocation"]["region"]["startLine"] for r in run["results"] ] == [3, 12, 7]

test_the_fingerprint_survives_a_line_move_and_separates_overloads

None test_guard_report_formats.test_the_fingerprint_survives_a_line_move_and_separates_overloads()

Same violation one line down: same fingerprint. Another overload: different.

tests/test_guard_report_formats.py:49

Show on the map →

Called by none

Calls fingerprint

Checks

  • assert fingerprint(v) == fingerprint(moved)
  • assert fingerprint(v) != fingerprint(overload)

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-GUARD-001measuredtest_the_fingerprint_survives_a_line_move_and_separates_overloads → fingerprint
Source · 9 lines
def test_the_fingerprint_survives_a_line_move_and_separates_overloads() -> None:
    """@brief Same violation one line down: same fingerprint. Another overload: different."""
    v = _VIOLATIONS[0]
    moved = Violation(v.file, v.line + 40, v.check, v.message, v.qualified)
    overload = Violation(v.file, v.line, v.check, v.message, "Dev::open(int, int)")
    assert fingerprint(v) == fingerprint(moved)
    assert fingerprint(v) != fingerprint(overload)

Activity · 5 steps
lines 51–51v = _VIOLATIONS[0]lines 52–52moved = Violation(v.file, v.line + 40, v.check, v.message, v.qualified)lines 53–53overload = Violation(v.file, v.line, v.check, v.message, "Dev::open(int, int)")lines 54–54assert fingerprint(v) == fingerprint(moved)lines 55–55assert fingerprint(v) != fingerprint(overload)

test_validate_writes_the_format_to_stdout_and_keeps_the_exit_code

None test_guard_report_formats.test_validate_writes_the_format_to_stdout_and_keeps_the_exit_code(tmp_path, capsys, fmt)

The CLI prints the document on stdout and still exits 1 on a violation.

tests/test_guard_report_formats.py:97

Show on the map →

Called by none

Calls none

Checks

  • assert rc == 1
  • assert "undocumented" in out

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-009measured
REQ-DDB-GUARD-001measured
REQ-DDB-GUARD-003measured
REQ-DDB-GUARD-004measured
REQ-DDB-GUARD-005measured
REQ-DDB-GUARD-006measured
REQ-DDB-GUARD-007measured
REQ-DDB-GUARD-011measured
REQ-DDB-GUARD-012measured
REQ-DDB-GUARD-013measured
REQ-DDB-GUARD-014measured
REQ-DDB-GUARD-016measured
REQ-DDB-GUARD-018measured
REQ-DDB-GUARD-019measured
REQ-DDB-GUARD-020measured
REQ-DDB-GUARD-021measured
REQ-DDB-GUARD-024measured
REQ-DDB-GUARD-025measured
REQ-DDB-GUARD-026measured
REQ-DDB-GUARD-027measured
REQ-DDB-PIPE-011measured
REQ-DDB-SCHEMA-006measured
REQ-DDB-SCHEMA-015measured
Source · 12 lines
def test_validate_writes_the_format_to_stdout_and_keeps_the_exit_code(
    tmp_path, capsys, fmt
) -> None:
    """@brief The CLI prints the document on stdout and still exits 1 on a violation."""
    from clew.guard.main import main

    src = tmp_path / "m.py"
    src.write_text("def undocumented():\n    return 1\n")
    rc = main(["validate", "--no-git", "--format", fmt, str(src)])
    out = capsys.readouterr().out
    assert rc == 1
    assert "undocumented" in out

1 more lines, not shown

Activity · 7 steps
lines 101–101from clew.guard.main import mainlines 103–103src = tmp_path / "m.py"lines 104–104src.write_text("def undocumented():\n return 1\n")lines 105–105rc = main(["validate", "--no-git", "--format", fmt, str(src)])lines 106–106out = capsys.readouterr().outlines 107–107assert rc == 1lines 108–108assert "undocumented" in out