Run cppcheck with SARIF output on the given code.
(code, extra_args=None)
| 104 | |
| 105 | |
| 106 | def run_sarif_check(code, extra_args=None): |
| 107 | """Run cppcheck with SARIF output on the given code.""" |
| 108 | with tempfile.TemporaryDirectory() as tmpdir: |
| 109 | tmp_path = os.path.join(tmpdir, "test.cpp") |
| 110 | with open(tmp_path, "w") as f: |
| 111 | f.write(code) |
| 112 | |
| 113 | args = ["--output-format=sarif", "--enable=all", tmp_path] |
| 114 | |
| 115 | if extra_args: |
| 116 | args.extend(extra_args) |
| 117 | |
| 118 | _, _, stderr = cppcheck(args) |
| 119 | |
| 120 | # SARIF output is in stderr |
| 121 | try: |
| 122 | sarif_data = json.loads(stderr) |
| 123 | return sarif_data |
| 124 | except json.JSONDecodeError as e: |
| 125 | pytest.fail(f"Failed to parse SARIF JSON: {e}\nOutput: {stderr}") |
| 126 | return None |
| 127 | |
| 128 | |
| 129 | def test_sarif_basic_structure(): |
no test coverage detected