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
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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-001 | measured |
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
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
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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-001 | measured | test_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
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
Checks
assert json.loads(render([], "sarif"))["runs"][0]["results"] == []assert json.loads(render([], "gitlab")) == []assert render([], "github") == ""
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-001 | measured |
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
Checks
assert {r["id"] for r in run["tool"]["driver"]["rules"]} >= {"presence", "version", "tag"}assert [
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-001 | measured |
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
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
Checks
assert fingerprint(v) == fingerprint(moved)assert fingerprint(v) != fingerprint(overload)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-001 | measured | test_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
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
Checks
assert rc == 1assert "undocumented" in out
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-009 | measured | |
| REQ-DDB-GUARD-001 | measured | |
| REQ-DDB-GUARD-003 | measured | |
| REQ-DDB-GUARD-004 | measured | |
| REQ-DDB-GUARD-005 | measured | |
| REQ-DDB-GUARD-006 | measured | |
| REQ-DDB-GUARD-007 | measured | |
| REQ-DDB-GUARD-011 | measured | |
| REQ-DDB-GUARD-012 | measured | |
| REQ-DDB-GUARD-013 | measured | |
| REQ-DDB-GUARD-014 | measured | |
| REQ-DDB-GUARD-016 | measured | |
| REQ-DDB-GUARD-018 | measured | |
| REQ-DDB-GUARD-019 | measured | |
| REQ-DDB-GUARD-020 | measured | |
| REQ-DDB-GUARD-021 | measured | |
| REQ-DDB-GUARD-024 | measured | |
| REQ-DDB-GUARD-025 | measured | |
| REQ-DDB-GUARD-026 | measured | |
| REQ-DDB-GUARD-027 | measured | |
| REQ-DDB-PIPE-011 | measured | |
| REQ-DDB-SCHEMA-006 | measured | |
| REQ-DDB-SCHEMA-015 | measured |
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 out1 more lines, not shown