From cb3a8c316786446ddca28e4a82fe6eadc66a68a2 Mon Sep 17 00:00:00 2001 From: Jan Macku Date: Thu, 9 May 2024 14:53:31 +0200 Subject: [PATCH] test: `generate_SARIF()` --- test/fixtures/generate_SARIF/defects.log | 49 ++++++ test/fixtures/generate_SARIF/test.sarif | 210 +++++++++++++++++++++++ test/generate_SARIF.bats | 40 +++++ 3 files changed, 299 insertions(+) create mode 100644 test/fixtures/generate_SARIF/defects.log create mode 100644 test/fixtures/generate_SARIF/test.sarif create mode 100644 test/generate_SARIF.bats diff --git a/test/fixtures/generate_SARIF/defects.log b/test/fixtures/generate_SARIF/defects.log new file mode 100644 index 00000000..caa2cbc5 --- /dev/null +++ b/test/fixtures/generate_SARIF/defects.log @@ -0,0 +1,49 @@ +{ + "defects": [ + { + "checker": "SHELLCHECK_WARNING", + "language": "shell", + "tool": "shellcheck", + "key_event_idx": 0, + "events": [ + { + "file_name": "innocent-script.sh", + "line": 7, + "event": "warning[SC2034]", + "message": "UNUSED_VAR2 appears unused. Verify use (or export if used externally).", + "verbosity_level": 0 + } + ] + }, + { + "checker": "SHELLCHECK_WARNING", + "language": "shell", + "tool": "shellcheck", + "key_event_idx": 0, + "events": [ + { + "file_name": "innocent-script.sh", + "line": 11, + "event": "warning[SC2115]", + "message": "Use \"${var:?}\" to ensure this never expands to / .", + "verbosity_level": 0 + } + ] + }, + { + "checker": "SHELLCHECK_WARNING", + "language": "shell", + "tool": "shellcheck", + "key_event_idx": 0, + "events": [ + { + "file_name": "innocent-script.sh", + "line": 11, + "event": "warning[SC2115]", + "message": "Use \"${var:?}\" to ensure this never expands to / .", + "verbosity_level": 0 + } + ] + } + ] +} diff --git a/test/fixtures/generate_SARIF/test.sarif b/test/fixtures/generate_SARIF/test.sarif new file mode 100644 index 00000000..cbd3b502 --- /dev/null +++ b/test/fixtures/generate_SARIF/test.sarif @@ -0,0 +1,210 @@ +{ + "$schema": "https://json.schemastore.org/sarif-2.1.0.json", + "version": "2.1.0", + "inlineExternalProperties": [ + { + "externalizedProperties": { + "tool": "ShellCheck", + "tool-url": "https://www.shellcheck.net/wiki/", + "tool-version": "0.9.0" + } + } + ], + "runs": [ + { + "tool": { + "driver": { + "name": "ShellCheck", + "version": "0.9.0", + "informationUri": "https://www.shellcheck.net/wiki/", + "rules": [ + { + "id": "SHELLCHECK_WARNING: warning[SC2034]", + "properties": { + "tags": [ + "ShellCheck" + ] + }, + "name": "SC2034", + "help": { + "text": "Defect reference: https://github.com/koalaman/shellcheck/wiki/SC2034", + "markdown": "Defect reference: [SC2034](https://github.com/koalaman/shellcheck/wiki/SC2034)" + } + }, + { + "id": "SHELLCHECK_WARNING: warning[SC2115]", + "properties": { + "tags": [ + "ShellCheck" + ] + }, + "name": "SC2115", + "help": { + "text": "Defect reference: https://github.com/koalaman/shellcheck/wiki/SC2115", + "markdown": "Defect reference: [SC2115](https://github.com/koalaman/shellcheck/wiki/SC2115)" + } + } + ] + } + }, + "results": [ + { + "ruleId": "SHELLCHECK_WARNING: warning[SC2034]", + "level": "warning", + "locations": [ + { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 7, + "endLine": 7 + } + } + } + ], + "message": { + "text": "UNUSED_VAR2 appears unused. Verify use (or export if used externally)." + }, + "codeFlows": [ + { + "threadFlows": [ + { + "locations": [ + { + "location": { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 7, + "endLine": 7 + } + }, + "message": { + "text": "UNUSED_VAR2 appears unused. Verify use (or export if used externally)." + } + }, + "nestingLevel": 0, + "kinds": [ + "warning[SC2034]" + ] + } + ] + } + ] + } + ] + }, + { + "ruleId": "SHELLCHECK_WARNING: warning[SC2115]", + "level": "warning", + "locations": [ + { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 11, + "endLine": 11 + } + } + } + ], + "message": { + "text": "Use \"${var:?}\" to ensure this never expands to / ." + }, + "codeFlows": [ + { + "threadFlows": [ + { + "locations": [ + { + "location": { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 11, + "endLine": 11 + } + }, + "message": { + "text": "Use \"${var:?}\" to ensure this never expands to / ." + } + }, + "nestingLevel": 0, + "kinds": [ + "warning[SC2115]" + ] + } + ] + } + ] + } + ] + }, + { + "ruleId": "SHELLCHECK_WARNING: warning[SC2115]", + "level": "warning", + "locations": [ + { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 11, + "endLine": 11 + } + } + } + ], + "message": { + "text": "Use \"${var:?}\" to ensure this never expands to / ." + }, + "codeFlows": [ + { + "threadFlows": [ + { + "locations": [ + { + "location": { + "id": 0, + "physicalLocation": { + "artifactLocation": { + "uri": "innocent-script.sh" + }, + "region": { + "startLine": 11, + "endLine": 11 + } + }, + "message": { + "text": "Use \"${var:?}\" to ensure this never expands to / ." + } + }, + "nestingLevel": 0, + "kinds": [ + "warning[SC2115]" + ] + } + ] + } + ] + } + ] + } + ] + } + ] +} diff --git a/test/generate_SARIF.bats b/test/generate_SARIF.bats new file mode 100644 index 00000000..a7b6ad01 --- /dev/null +++ b/test/generate_SARIF.bats @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +setup_file () { + load 'test_helper/common-setup' + _common_setup +} + +setup () { + load 'test_helper/bats-assert/load' + load 'test_helper/bats-support/load' + load 'test_helper/bats-file/load' +} + +@test "generate_SARIF() - arguments" { + source "${PROJECT_ROOT}/src/functions.sh" + + run generate_SARIF + assert_failure 1 + + run generate_SARIF "./test/fixtures/generate_SARIF/defects.log" + assert_failure 1 + + run generate_SARIF "./test/fixtures/generate_SARIF/defects.log" "test.sarif" + assert_success +} + +@test "generate_SARIF()" { + source "${PROJECT_ROOT}/src/functions.sh" + + run generate_SARIF "./test/fixtures/generate_SARIF/defects.log" "./test.sarif" + assert_success + assert_exists "./test.sarif" + + run cmp -s "test.sarif" "./test/fixtures/generate_SARIF/test.sarif" + assert_success +} + +teardown () { + rm -f test.sarif +}