Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed May 9, 2024
1 parent 20503da commit ed65a91
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,13 @@ is_debug () {
# GITHUB_ACTIONS is set when Differential ShellCheck is running in GitHub Actions
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
is_github_actions () {
if [[ -z "${GITHUB_ACTIONS}" ]]; then
return 1
fi
[[ -z "${GITHUB_ACTIONS}" ]] && return 1
return 0
}

# Function to check if the script is run in unit tests environment
is_unit_tests () {
[[ -z "${UNIT_TESTS}" ]] && return 1
return 0
}

Expand Down
4 changes: 2 additions & 2 deletions src/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# TODO set up required variables

export DISPLAY_ENGINE="${INPUT_DISPLAY_ENGINE:-csgrep}"
is_debug && echo "DISPLAY_ENGINE: ${DISPLAY_ENGINE}"
export INPUT_DISPLAY_ENGINE="${INPUT_DISPLAY_ENGINE:='csgrep'}"
is_debug && echo "DISPLAY_ENGINE: ${INPUT_DISPLAY_ENGINE}"

# Set required variables based on the environment
if is_github_actions; then
Expand Down
9 changes: 5 additions & 4 deletions src/validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ print_result () {
[[ $# -le 0 ]] && return 1
local results="$1"

if [[ "${DISPLAY_ENGINE:-'csgrep'}" == "sarif-fmt" ]]; then
if [[ "${INPUT_DISPLAY_ENGINE:-'csgrep'}" == "sarif-fmt" ]]; then
local color="always"
is_unit_tests && color="never"

# When sarif-fmt is used, we need to generate SARIF file first
# only csgrep can utilize JSON output from csdiff
generate_SARIF "${results}" "tmp.sarif"
sarif-fmt --color="always" < "tmp.sarif"
sarif-fmt --color="${color}" < "tmp.sarif"
rm "tmp.sarif"
else
[[ $# -le 1 ]] && return 1
local context="$2"

defect=

csgrep --embed-context "${context}" "${results}"
fi
}
Expand Down
49 changes: 49 additions & 0 deletions test/fixtures/print_result/defects.log
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
34 changes: 34 additions & 0 deletions test/fixtures/print_result/fixes.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"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
}
]
}
]
}
90 changes: 90 additions & 0 deletions test/print_result.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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'
}

@test "print_result() - arguments" {
source "${PROJECT_ROOT}/src/functions.sh"
source "${PROJECT_ROOT}/src/validation.sh"

INPUT_DISPLAY_ENGINE="csgrep"
run print_result
assert_failure 1

run print_result "./test/fixtures/print_result/defects.log"
assert_failure 1

run print_result "./test/fixtures/print_result/defects.log" "4"
assert_success

INPUT_DISPLAY_ENGINE="sarif-fmt"
run print_result "./test/fixtures/print_result/defects.log"
assert_success
}

@test "print_result() - csgrep" {
source "${PROJECT_ROOT}/src/validation.sh"

INPUT_DISPLAY_ENGINE="csgrep"
run print_result "./test/fixtures/print_result/defects.log" "4"
assert_success
assert_output \
'Error: SHELLCHECK_WARNING:
innocent-script.sh:7: warning[SC2034]: UNUSED_VAR2 appears unused. Verify use (or export if used externally).
Error: SHELLCHECK_WARNING:
innocent-script.sh:11: warning[SC2115]: Use "${var:?}" to ensure this never expands to / .
Error: SHELLCHECK_WARNING:
innocent-script.sh:11: warning[SC2115]: Use "${var:?}" to ensure this never expands to / .'

run print_result "./test/fixtures/print_result/fixes.log" "2"
assert_success
assert_output \
'Error: SHELLCHECK_WARNING:
innocent-script.sh:7: warning[SC2034]: UNUSED_VAR2 appears unused. Verify use (or export if used externally).
Error: SHELLCHECK_WARNING:
innocent-script.sh:11: warning[SC2115]: Use "${var:?}" to ensure this never expands to / .'
}

@test "print_result() - sarif-fmt" {
source "${PROJECT_ROOT}/src/functions.sh"
source "${PROJECT_ROOT}/src/validation.sh"

UNIT_TESTS="true"
INPUT_DISPLAY_ENGINE="sarif-fmt"
run print_result "./test/fixtures/print_result/defects.log"
assert_success
assert_output \
'warning: UNUSED_VAR2 appears unused. Verify use (or export if used externally).
warning: Use "${var:?}" to ensure this never expands to / .
warning: Use "${var:?}" to ensure this never expands to / .
warning: 3 warnings emitted'

run print_result "./test/fixtures/print_result/fixes.log"
assert_success
assert_output \
'warning: UNUSED_VAR2 appears unused. Verify use (or export if used externally).
warning: Use "${var:?}" to ensure this never expands to / .
warning: 2 warnings emitted'
}

teardown () {
export \
INPUT_DISPLAY_ENGINE="" \
UNIT_TESTS=""
rm -f tmp.sarif
}

0 comments on commit ed65a91

Please sign in to comment.