import copy import json import pytest def render(plugin, tmp_path, data, format="sarif", **kwargs): path = tmp_path / "report.json" path.write_text(json.dumps(data)) return plugin.run(str(path), format=format, **kwargs) def sarif(results=None): return { "version": "2.1.0", "runs": [ { "tool": {"driver": {"name": "test"}}, "results": [] if results is None else results, } ], } @pytest.mark.parametrize( "format,status", [("sarif", "2 findings"), ("eslint", "2 findings"), ("ruff", "1 finding")], ) def test_documented_fixtures(plugin, fixture_file, format, status): assert plugin.run(fixture_file(format + ".json"), format=format).status == status @pytest.mark.parametrize( "data", [ {}, {"version": "2.1.0", "runs": []}, {"version": "2.1.0", "runs": None}, {"version": "1.0", "runs": []}, ], ) def test_no_scan_is_not_green(plugin, tmp_path, data): with pytest.raises(ValueError): render(plugin, tmp_path, data) @pytest.mark.parametrize( "invocation", [ {"executionSuccessful": False}, {}, { "executionSuccessful": True, "toolExecutionNotifications": [{"level": "error"}], }, { "executionSuccessful": True, "toolConfigurationNotifications": [{"level": "error"}], }, ], ) def test_incomplete_sarif_rejected(plugin, tmp_path, invocation): data = sarif() data["runs"][0]["invocations"] = [invocation] with pytest.raises(ValueError): render(plugin, tmp_path, data) def test_null_results_rejected(plugin, tmp_path): data = sarif() data["runs"][0]["results"] = None with pytest.raises(ValueError): render(plugin, tmp_path, data) def test_rule_default_and_multiple_runs(plugin, tmp_path): data = sarif([{"ruleId": "R1", "message": {"text": "issue"}}]) data["runs"][0]["tool"]["driver"]["rules"] = [ {"id": "R1", "defaultConfiguration": {"level": "error"}} ] data["runs"].append(copy.deepcopy(data["runs"][0])) value = render(plugin, tmp_path, data) assert value.status == "2 findings" assert value.secondary_color == "#d73a49" def test_pass_results_excluded_suppressed_findings_included(plugin, tmp_path): results = [ {"kind": "pass", "message": {"text": "passed"}}, {"kind": "notApplicable", "message": {"text": "n/a"}}, { "message": {"text": "suppressed"}, "suppressions": [{"kind": "inSource", "status": "accepted"}], }, ] assert render(plugin, tmp_path, sarif(results)).status == "1 finding" @pytest.mark.parametrize( "data", [ [{}], [ { "filePath": "x", "messages": [{"severity": True}], "errorCount": 1, "warningCount": 0, } ], [ { "filePath": "x", "messages": [{"severity": 2}], "errorCount": 0, "warningCount": 0, } ], ], ) def test_bad_eslint_rejected(plugin, tmp_path, data): with pytest.raises(ValueError): render(plugin, tmp_path, data, "eslint") @pytest.mark.parametrize( "format,data", [ ("ruff", [{}]), ("ruff", {}), ("eslint", {}), ("sarif", sarif([{"message": {}, "level": "critical"}])), ], ) def test_schema_rejected(plugin, tmp_path, format, data): with pytest.raises(ValueError): render(plugin, tmp_path, data, format) @pytest.mark.parametrize( "format,data", [("ruff", []), ("eslint", []), ("sarif", sarif())] ) def test_explicit_empty_result_is_zero_reported(plugin, tmp_path, format, data): assert render(plugin, tmp_path, data, format).status == "0 reported" def test_level_filters_do_not_change_other_severity(plugin, fixture_file): result = plugin.run(fixture_file("sarif.json"), level="warning") assert result.status == "1 finding" assert result.secondary_color == "#dfb317" def test_rule_index_default_severity(plugin, tmp_path): data = sarif([{"ruleIndex": 0, "message": {"text": "issue"}}]) data["runs"][0]["tool"]["driver"]["rules"] = [ {"id": "R1", "defaultConfiguration": {"level": "error"}} ] assert render(plugin, tmp_path, data).secondary_color == "#d73a49" for index in (-1, True, 1): data["runs"][0]["results"][0]["ruleIndex"] = index with pytest.raises(ValueError): render(plugin, tmp_path, data) def test_external_sarif_results_are_not_silently_omitted(plugin, tmp_path): data = sarif() data["runs"][0]["externalPropertyFileReferences"] = { "results": [{"location": {"uri": "results.sarif-external-properties"}}] } with pytest.raises(ValueError): render(plugin, tmp_path, data) def test_empty_sarif_tool_name_is_invalid(plugin, tmp_path): data = sarif() data["runs"][0]["tool"]["driver"]["name"] = " " with pytest.raises(ValueError): render(plugin, tmp_path, data) @pytest.mark.parametrize("rules", [{}, [{"id": "R1"}, {"id": "R1"}], [{"id": ""}]]) def test_malformed_rule_catalog_rejected(plugin, tmp_path, rules): data = sarif() data["runs"][0]["tool"]["driver"]["rules"] = rules with pytest.raises(ValueError): render(plugin, tmp_path, data) @pytest.mark.parametrize("invocations", [{}, "", None]) def test_invalid_invocations_cannot_hide_failed_analysis(plugin, tmp_path, invocations): data = sarif() data["runs"][0]["invocations"] = invocations with pytest.raises(ValueError, match="invocations"): render(plugin, tmp_path, data) def test_invalid_notification_collection_is_not_empty(plugin, tmp_path): data = sarif() data["runs"][0]["invocations"] = [ {"executionSuccessful": True, "toolExecutionNotifications": {}} ] with pytest.raises(ValueError, match="notifications"): render(plugin, tmp_path, data) @pytest.mark.parametrize("fatal", [1, -1, True]) def test_eslint_fatal_count_cannot_contradict_zero_findings(plugin, tmp_path, fatal): data = [ { "filePath": "x.js", "messages": [], "errorCount": 0, "warningCount": 0, "fatalErrorCount": fatal, } ] with pytest.raises(ValueError, match="fatal"): render(plugin, tmp_path, data, "eslint") def test_eslint_valid_fatal_error_is_a_red_finding(plugin, tmp_path): data = [ { "filePath": "x.js", "messages": [{"severity": 2, "fatal": True}], "errorCount": 1, "warningCount": 0, "fatalErrorCount": 1, } ] result = render(plugin, tmp_path, data, "eslint") assert result.status == "1 finding" assert result.secondary_color == "#d73a49"