| 1 |
from sarif import * |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
@pytest.fixture(scope='function', autouse=True) |
| 6 |
def sarif(): |
| 7 |
return sarif_from_env() |
| 8 |
|
| 9 |
def test_basics(sarif): |
| 10 |
schema = sarif['$schema'] |
| 11 |
assert schema == "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" |
| 12 |
|
| 13 |
version = sarif['version'] |
| 14 |
assert version == "2.1.0" |
| 15 |
|
| 16 |
def test_execution_unsuccessful(sarif): |
| 17 |
runs = sarif['runs'] |
| 18 |
run = runs[0] |
| 19 |
|
| 20 |
invocations = run['invocations'] |
| 21 |
assert len(invocations) == 1 |
| 22 |
invocation = invocations[0] |
| 23 |
|
| 24 |
# We expect the 'error' to make executionSuccessful be false |
| 25 |
assert invocation['executionSuccessful'] == False |
| 26 |
|
| 27 |
def test_error_location(sarif): |
| 28 |
runs = sarif['runs'] |
| 29 |
run = runs[0] |
| 30 |
results = run['results'] |
| 31 |
|
| 32 |
# We expect a single error with annotations. |
| 33 |
# |
| 34 |
# The textual form of the diagnostic looks like this: |
| 35 |
# . PATH/bad-binary-ops.c: In function 'test_4': |
| 36 |
# . PATH/bad-binary-ops.c:64:23: error: invalid operands to binary + (have 'S' {aka 'struct s'} and 'T' {aka 'struct t'}) |
| 37 |
# . return callee_4a () + callee_4b (); /* { dg-error "invalid operands to binary \+" } */ |
| 38 |
# . ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~ |
| 39 |
# . | | |
| 40 |
# . | T {aka struct t} |
| 41 |
# . S {aka struct s} |
| 42 |
assert len(results) == 1 |
| 43 |
|
| 44 |
result = results[0] |
| 45 |
assert result['level'] == 'error' |
| 46 |
|
| 47 |
assert result['message']['text'] \ |
| 48 |
== "invalid operands to binary + (have 'S' {{aka 'struct s'}} and 'T' {{aka 'struct t'}})" |
| 49 |
locations = result['locations'] |
| 50 |
assert len(locations) == 1 |
| 51 |
|
| 52 |
location = locations[0] |
| 53 |
assert get_location_artifact_uri(location).endswith('bad-binary-op.c') |
| 54 |
assert get_location_snippet_text(location) \ |
| 55 |
== " return callee_4a () + callee_4b (); /* { dg-error \"invalid operands to binary \\+\" } */\n" |
| 56 |
EXPECTED_LINE = 14 |
| 57 |
assert get_location_physical_region(location)['startLine'] == EXPECTED_LINE |
| 58 |
assert get_location_physical_region(location)['startColumn'] == 23 |
| 59 |
assert get_location_physical_region(location)['endColumn'] == 24 |
| 60 |
|
| 61 |
annotations = location['annotations'] |
| 62 |
assert len(annotations) == 2 |
| 63 |
assert annotations[0]['startLine'] == EXPECTED_LINE |
| 64 |
assert annotations[0]['startColumn'] == 10 |
| 65 |
assert annotations[0]['endColumn'] == 22 |
| 66 |
assert annotations[0]['message']['text'] == "S {{aka struct s}}" |
| 67 |
assert annotations[1]['startLine'] == EXPECTED_LINE |
| 68 |
assert annotations[1]['startColumn'] == 25 |
| 69 |
assert annotations[1]['endColumn'] == 37 |
| 70 |
assert annotations[1]['message']['text'] == "T {{aka struct t}}" |