Skip to content

Commit

Permalink
tests: use function to check if scripts run in unit tests environment
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed May 9, 2024
1 parent ed65a91 commit 77a0419
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ pick_base_and_head_hash () {
"push")
export BASE=${INPUT_PUSH_EVENT_BASE:-}
export HEAD=${INPUT_PUSH_EVENT_HEAD:-}
[[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
is_unit_tests && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
;;

"pull_request")
export BASE=${INPUT_PULL_REQUEST_BASE:-}
export HEAD=${INPUT_PULL_REQUEST_HEAD:-}
[[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
is_unit_tests && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
;;

"manual")
export BASE=${INPUT_BASE:-}
export HEAD=${INPUT_HEAD:-}
[[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
is_unit_tests && echo "BASE:\"${BASE}\" ; HEAD:\"${HEAD}\""
;;

*)
Expand Down Expand Up @@ -97,7 +97,7 @@ get_scripts_for_scanning () {
done

eval $output=\("${scripts_for_scanning[*]@Q}"\)
[[ ${UNIT_TESTS:-1} -eq 0 ]] && eval echo "\${${output}[@]@Q}"
is_unit_tests && eval echo "\${${output}[@]@Q}"
}

# Function to check whether the given file has the .{,a,ba,da,k}sh and .bats extension
Expand Down Expand Up @@ -220,7 +220,7 @@ file_to_array () {
output+=("${file}")
done < "${1}"

[[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "${output[@]}"
is_unit_tests && echo "${output[@]}"

eval "${2}"=\("${output[*]@Q}"\)
}
Expand Down
4 changes: 3 additions & 1 deletion test/file_to_array.bats
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ setup () {
@test "file_to_array() - general" {
source "${PROJECT_ROOT}/src/functions.sh"

UNIT_TESTS=0
UNIT_TESTS="true"

file_array=()
run file_to_array "./test/fixtures/file_to_array/files.txt" "file_array"
Expand All @@ -45,5 +45,7 @@ setup () {
}

teardown () {
export \
UNIT_TESTS=""
rm -f file.txt
}
2 changes: 1 addition & 1 deletion test/get_scripts_for_scanning.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setup () {
@test "get_scripts_for_scanning() - general" {
source "${PROJECT_ROOT}/src/functions.sh"

UNIT_TESTS=0
UNIT_TESTS="true"

run get_scripts_for_scanning
assert_failure 1
Expand Down
27 changes: 27 additions & 0 deletions test/is_unit_tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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 "is_unit_tests()" {
source "${PROJECT_ROOT}/src/functions.sh"

run is_unit_tests
assert_failure 1

UNIT_TESTS="true"
run is_unit_tests
assert_success
}

teardown () {
export \
UNIT_TESTS=""
}
6 changes: 3 additions & 3 deletions test/pick_base_and_head_hash.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ setup () {
run pick_base_and_head_hash
assert_failure 2

UNIT_TESTS=0
UNIT_TESTS="true"
INPUT_PUSH_EVENT_BASE="abcdef123456"
INPUT_PUSH_EVENT_HEAD="ghijkl789012"

Expand Down Expand Up @@ -58,7 +58,7 @@ setup () {
run pick_base_and_head_hash
assert_failure 2

UNIT_TESTS=0
UNIT_TESTS="true"
INPUT_PULL_REQUEST_BASE="abcdef123456"
INPUT_PULL_REQUEST_HEAD="ghijkl789012"

Expand Down Expand Up @@ -88,7 +88,7 @@ setup () {
run pick_base_and_head_hash
assert_failure 2

UNIT_TESTS=0
UNIT_TESTS="true"
INPUT_BASE="abcdef123456"
INPUT_HEAD="ghijkl789012"

Expand Down

0 comments on commit 77a0419

Please sign in to comment.