| 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_location_relationships(sarif): |
| 28 |
runs = sarif['runs'] |
| 29 |
run = runs[0] |
| 30 |
results = run['results'] |
| 31 |
|
| 32 |
# We expect a single error with a secondary location and a fix-it hint. |
| 33 |
# |
| 34 |
# The textual form of the diagnostic would look like this: |
| 35 |
# . PATH/missing-semicolon.c: In function 'missing_semicolon': |
| 36 |
# . PATH/missing-semicolon.c:19:12: error: expected ';' before '}' token |
| 37 |
# . 19 | return 42 |
| 38 |
# . | ^ |
| 39 |
# . | ; |
| 40 |
# . 20 | } |
| 41 |
# . | ~ |
| 42 |
assert len(results) == 1 |
| 43 |
|
| 44 |
result = results[0] |
| 45 |
assert result['level'] == 'error' |
| 46 |
assert result['message']['text'] == "expected ';' before '}}' token" |
| 47 |
locations = result['locations'] |
| 48 |
assert len(locations) == 1 |
| 49 |
|
| 50 |
location = locations[0] |
| 51 |
assert get_location_artifact_uri(location).endswith('missing-semicolon.c') |
| 52 |
assert get_location_snippet_text(location) == ' return 42\n' |
| 53 |
assert get_location_physical_region(location)['startLine'] == 9 |
| 54 |
assert get_location_physical_region(location)['startColumn'] == 12 |
| 55 |
assert get_location_physical_region(location)['endColumn'] == 13 |
| 56 |
|
| 57 |
# We don't expect the secondary location to have a relationship back |
| 58 |
# to the primary location, and so the primary doesn't get an id. |
| 59 |
assert 'id' not in location |
| 60 |
|
| 61 |
# We expect the primary location to reference the secondary location. |
| 62 |
assert len(location['relationships']) == 1 |
| 63 |
assert location['relationships'][0]['target'] == 0 |
| 64 |
assert location['relationships'][0]['kinds'] == ['relevant'] |
| 65 |
|
| 66 |
# We expect one related location, for the closing brace on the next line |
| 67 |
relatedLocations = result['relatedLocations'] |
| 68 |
assert len(relatedLocations) == 1 |
| 69 |
|
| 70 |
rel_loc = relatedLocations[0] |
| 71 |
assert rel_loc['id'] == 0 |
| 72 |
assert get_location_artifact_uri(rel_loc).endswith('missing-semicolon.c') |
| 73 |
assert get_location_snippet_text(rel_loc) == '}\n' |
| 74 |
assert get_location_physical_region(rel_loc)['startLine'] == 10 |
| 75 |
assert get_location_physical_region(rel_loc)['startColumn'] == 1 |
| 76 |
assert get_location_physical_region(rel_loc)['endColumn'] == 2 |
| 77 |
assert 'relatedLocations' not in rel_loc |
| 78 |
assert 'message' not in rel_loc |
| 79 |
|
| 80 |
# We expect one fix-it hint representing an insertion of ';' |
| 81 |
assert len(result['fixes']) == 1 |
| 82 |
assert len(result['fixes'][0]['artifactChanges']) == 1 |
| 83 |
change = result['fixes'][0]['artifactChanges'][0] |
| 84 |
assert change['artifactLocation']['uri'].endswith('missing-semicolon.c') |
| 85 |
assert len(change['replacements']) == 1 |
| 86 |
replacement = change['replacements'][0] |
| 87 |
assert replacement['deletedRegion']['startLine'] == 9 |
| 88 |
assert replacement['deletedRegion']['startColumn'] == 12 |
| 89 |
assert replacement['deletedRegion']['endColumn'] == 12 |
| 90 |
assert replacement['insertedContent']['text'] == ';' |