Skip to content

Commit

Permalink
wip: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Aug 22, 2023
1 parent f994631 commit 6bd1f03
Show file tree
Hide file tree
Showing 22 changed files with 771 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARG rpm_shellcheck="ShellCheck-${version_shellcheck}.fc${fedora}.${arch}.rpm"
# --- Install dependencies --- #

RUN dnf -y upgrade
RUN dnf -y install git koji \
RUN dnf -y install git koji jq \
&& dnf clean all

# Download rpms from koji
Expand Down
2 changes: 1 addition & 1 deletion src/summary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Scanned/Changed scripts: \`${#list_of_changed_scripts[@]}\`
get_number_of () {
[[ $# -le 0 ]] && return 1

grep -Eo "[0-9]*" < <(csgrep --mode=stat ../"${1}".log)
jq '.defects | length' "../${1}.log"
}

# Create full Markdown style link to results
Expand Down
12 changes: 8 additions & 4 deletions src/validation.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# shellcheck shell=bash
# SPDX-License-Identifier: GPL-3.0-or-later

# shellcheck source=summary.sh
. "${SCRIPT_DIR=}summary.sh"

# Get file containing fixes based on two input files
# $1 - <string> absolute path to a file containing results from BASE scan
# $2 - <string> absolute path to a file containing results from HEAD scan
Expand All @@ -18,7 +21,7 @@ get_fixes () {
evaluate_and_print_fixes () {
if [[ -s ../fixes.log ]]; then
echo -e "${GREEN}Fixed defects${NOCOLOR}"
csgrep ../fixes.log
csgrep --embed-context 2 ../fixes.log
else
echo -e "ℹ️ ${YELLOW}No Fixes!${NOCOLOR}"
fi
Expand All @@ -41,11 +44,12 @@ get_defects () {
evaluate_and_print_defects () {
gather_statistics "../defects.log"

if [[ -s ../defects.log ]]; then
num_of_defects=$(get_number_of defects)
if [[ -s ../defects.log ]] && [[ "${num_of_defects}" -gt 0 ]] ; then
print_statistics

echo -e "${YELLOW}Defects, NEEDS INSPECTION${NOCOLOR}"
csgrep ../defects.log
csgrep --embed-context 4 ../defects.log
return 1
fi

Expand Down Expand Up @@ -90,6 +94,6 @@ get_number_of_defects_by_severity () {

[[ -f "${logs}" ]] || return 1
# the optional group is workaround for csdiff issue: https://github.com/csutils/csdiff/issues/138
defects=$(grep --count --extended-regexp "^[^:]+:[0-9]+:([0-9]+:)? ${severity}\[SC[0-9]+\].*$" "${logs}")
defects=$(grep --count --extended-regexp "\"event\": \"${severity}\[SC[0-9]+\]\",$" "${logs}")
echo "${defects}"
}
2 changes: 1 addition & 1 deletion test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARG rpm_shellcheck="ShellCheck-${version_shellcheck}.fc${fedora}.${arch}.rpm"
# --- Install dependencies --- #

RUN dnf -y upgrade
RUN dnf -y install git koji kcov bats diffutils \
RUN dnf -y install git koji kcov bats diffutils jq \
&& dnf clean all

# Download rpms from koji
Expand Down
51 changes: 39 additions & 12 deletions test/diff_scan_summary.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,47 @@ setup () {
INPUT_TRIGGERING_EVENT=""

echo -e \
"Error: SHELLCHECK_WARNING:
src/index.sh:53:10: warning[SC2154]: MAIN_HEADING is referenced but not assigned.
Error: SHELLCHECK_WARNING:
src/index.sh:56:14: warning[SC2154]: WHITE is referenced but not assigned.
Error: SHELLCHECK_WARNING:
src/index.sh:56:56: warning[SC2154]: NOCOLOR is referenced but not assigned.
" > ../defects.log
'{
"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
}
]
},
{}, {}
]
}' > ../defects.log

echo -e \
"Error: SHELLCHECK_WARNING:
src/index.sh:7:3: note[SC1091]: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory)
" > ../fixes.log
'{
"defects": [
{
"checker": "SHELLCHECK_WARNING",
"language": "shell",
"tool": "shellcheck",
"key_event_idx": 0,
"events": [
{
"file_name": "innocent-script.sh",
"line": 6,
"event": "warning[SC2034]",
"message": "UNUSED_VAR appears unused. Verify use (or export if used externally).",
"verbosity_level": 0
}
]
}
]
}' > ../fixes.log

run diff_scan_summary
assert_success
Expand Down
23 changes: 10 additions & 13 deletions test/evaluate_and_print_defects.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ setup () {

run evaluate_and_print_defects
assert_failure 1
assert_output "\
assert_output --partial "\
::group::📊 Statistics of defects
Error: 0
Warning: 3
Warning: 2
Note: 0
Style: 0
::endgroup::
✋ Defects, NEEDS INSPECTION
Error: SHELLCHECK_WARNING:
src/index.sh:53:10: warning[SC2154]: MAIN_HEADING is referenced but not assigned.
Error: SHELLCHECK_WARNING:
src/index.sh:56:14: warning[SC2154]: WHITE is referenced but not assigned.
Error: SHELLCHECK_WARNING:
src/index.sh:56:56: warning[SC2154]: NOCOLOR is referenced but not assigned."
::endgroup::"
assert_line --partial "innocent-script.sh:7: warning[SC2034]: UNUSED_VAR2 appears unused. Verify use (or export if used externally)."
assert_line --partial 'innocent-script.sh:11: warning[SC2115]: Use "${var:?}" to ensure this never expands to / .'
}

@test "evaluate_and_print_defects() - no defects" {
source "${PROJECT_ROOT}/src/validation.sh"

echo -e \
'{
"defects": []
}' > ../defects.log

run evaluate_and_print_defects
assert_success
assert_output "🥳 No defects added. Yay!"
Expand Down
5 changes: 2 additions & 3 deletions test/evaluate_and_print_fixes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ setup () {

run evaluate_and_print_fixes
assert_success
assert_output "\
✅ Fixed defects
assert_output --partial "✅ Fixed defects
Error: SHELLCHECK_WARNING:
src/index.sh:7:3: note[SC1091]: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory)"
innocent-script.sh:6: warning[SC2034]: UNUSED_VAR appears unused. Verify use (or export if used externally)."
}

@test "evaluate_and_print_fixes() - no fixes" {
Expand Down
42 changes: 34 additions & 8 deletions test/fixtures/evaluate_and_print_defects/defects.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
Error: SHELLCHECK_WARNING:
src/index.sh:53:10: warning[SC2154]: MAIN_HEADING is referenced but not assigned.

Error: SHELLCHECK_WARNING:
src/index.sh:56:14: warning[SC2154]: WHITE is referenced but not assigned.

Error: SHELLCHECK_WARNING:
src/index.sh:56:56: warning[SC2154]: NOCOLOR is referenced but not assigned.
{
"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
}
]
}
]
}
5 changes: 4 additions & 1 deletion test/fixtures/evaluate_and_print_fixes/fixes.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Error: SHELLCHECK_WARNING:
src/index.sh:7:3: note[SC1091]: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory)
innocent-script.sh:6: warning[SC2034]: UNUSED_VAR appears unused. Verify use (or export if used externally).
# 5| DIR_SUFFIX="*"
# 6|-> UNUSED_VAR=""
# 7| UNUSED_VAR2=""
21 changes: 16 additions & 5 deletions test/fixtures/gather_statistics/defects.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
Error: SHELLCHECK_WARNING:
src/index.sh:53:10: warning[SC2154]: MAIN_HEADING is referenced but not assigned.
innocent-script.sh:7: warning[SC2034]: UNUSED_VAR2 appears unused. Verify use (or export if used externally).
# 4|
# 5| DIR_SUFFIX="*"
# 6| UNUSED_VAR=""
# 7|-> UNUSED_VAR2=""
# 8|
# 9| echo "I'm going to remove directory $1/$DIR_SUFFIX"
# 10|

Error: SHELLCHECK_WARNING:
src/index.sh:56:14: warning[SC2154]: WHITE is referenced but not assigned.

Error: SHELLCHECK_WARNING:
src/index.sh:56:56: warning[SC2154]: NOCOLOR is referenced but not assigned.
innocent-script.sh:11: warning[SC2115]: Use "${var:?}" to ensure this never expands to / .
# 8|
# 9| echo "I'm going to remove directory $1/$DIR_SUFFIX"
# 10|
# 11|-> rm -rf $1/$2/$DIR_SUFFIX
# 12|
# 13| #! FIXME - Call rm -rf $1/$DIR_SUFFIX
# 14|
19 changes: 15 additions & 4 deletions test/fixtures/get_defects/base.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
src/index.sh:7:3: note: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory) [SC1091]
src/index.sh:39:42: warning: BASE is referenced but not assigned. [SC2154]
src/index.sh:39:53: warning: HEAD is referenced but not assigned. [SC2154]
src/index.sh:50:10: warning: VERSIONS_HEADING is referenced but not assigned. [SC2154]
{
"comments": [
{
"file": "innocent-script.sh",
"line": 6,
"endLine": 6,
"column": 1,
"endColumn": 11,
"level": "warning",
"code": 2034,
"message": "UNUSED_VAR appears unused. Verify use (or export if used externally).",
"fix": null
}
]
}
42 changes: 34 additions & 8 deletions test/fixtures/get_defects/defects.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
Error: SHELLCHECK_WARNING:
src/index.sh:53:10: warning[SC2154]: MAIN_HEADING is referenced but not assigned.

Error: SHELLCHECK_WARNING:
src/index.sh:56:14: warning[SC2154]: WHITE is referenced but not assigned.

Error: SHELLCHECK_WARNING:
src/index.sh:56:56: warning[SC2154]: NOCOLOR is referenced but not assigned.
{
"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
}
]
}
]
}
32 changes: 26 additions & 6 deletions test/fixtures/get_defects/head.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
src/index.sh:39:42: warning: BASE is referenced but not assigned. [SC2154]
src/index.sh:39:53: warning: HEAD is referenced but not assigned. [SC2154]
src/index.sh:50:10: warning: VERSIONS_HEADING is referenced but not assigned. [SC2154]
src/index.sh:53:10: warning: MAIN_HEADING is referenced but not assigned. [SC2154]
src/index.sh:56:14: warning: WHITE is referenced but not assigned. [SC2154]
src/index.sh:56:56: warning: NOCOLOR is referenced but not assigned. [SC2154]
{
"comments": [
{
"file": "innocent-script.sh",
"line": 7,
"endLine": 7,
"column": 1,
"endColumn": 12,
"level": "warning",
"code": 2034,
"message": "UNUSED_VAR2 appears unused. Verify use (or export if used externally).",
"fix": null
},
{
"file": "innocent-script.sh",
"line": 11,
"endLine": 11,
"column": 8,
"endColumn": 25,
"level": "warning",
"code": 2115,
"message": "Use \"${var:?}\" to ensure this never expands to / .",
"fix": null
}
]
}
22 changes: 15 additions & 7 deletions test/fixtures/get_fixes/base.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
src/index.sh:7:3: note: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory) [SC1091]
src/index.sh:39:42: warning: BASE is referenced but not assigned. [SC2154]
src/index.sh:39:53: warning: HEAD is referenced but not assigned. [SC2154]
src/index.sh:50:10: warning: VERSIONS_HEADING is referenced but not assigned. [SC2154]
src/index.sh:53:10: warning: MAIN_HEADING is referenced but not assigned. [SC2154]
src/index.sh:56:14: warning: WHITE is referenced but not assigned. [SC2154]
src/index.sh:56:56: warning: NOCOLOR is referenced but not assigned. [SC2154]
{
"comments": [
{
"file": "innocent-script.sh",
"line": 6,
"endLine": 6,
"column": 1,
"endColumn": 11,
"level": "warning",
"code": 2034,
"message": "UNUSED_VAR appears unused. Verify use (or export if used externally).",
"fix": null
}
]
}
21 changes: 19 additions & 2 deletions test/fixtures/get_fixes/fixes.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
Error: SHELLCHECK_WARNING:
src/index.sh:7:3: note[SC1091]: Not following: functions.sh: openBinaryFile: does not exist (No such file or directory)
{
"defects": [
{
"checker": "SHELLCHECK_WARNING",
"language": "shell",
"tool": "shellcheck",
"key_event_idx": 0,
"events": [
{
"file_name": "innocent-script.sh",
"line": 6,
"event": "warning[SC2034]",
"message": "UNUSED_VAR appears unused. Verify use (or export if used externally).",
"verbosity_level": 0
}
]
}
]
}
Loading

0 comments on commit 6bd1f03

Please sign in to comment.