Skip to content

Commit d2d7334

Browse files
authored
chore: split validation logic in smaller functions (#5892)
- Move USE_FIND_ALGORITHM and VALIDATE_ALL_CODEBASE validation in a dedicated function (ValidateFindMode). - Move ANSIBLE_DIRECTORY validation to a dedicated function (ValidateAnsibleDirectory). - Move VALIDATE_xxxx variables validation to a dedicated function (ValidateValidationVariables). - Mark ANY_SET, ANY_TRUE, ANY_FALSE as local because we don't need to reference them anywhere outside ValidateValidationVariables. - Add some debug statements in validation functions. - Merge the loops to initialize VALIDATE_xxx variables and to print enable/disable language debug messages. - Add tests for these validation functions. - Add test start message for all tests.
1 parent d9d1909 commit d2d7334

7 files changed

Lines changed: 304 additions & 66 deletions

File tree

‎lib/functions/validation.sh‎

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,73 @@ function ValidateGitHubWorkspace() {
4040
info "Successfully validated GITHUB_WORKSPACE: ${GITHUB_WORKSPACE}"
4141
}
4242

43-
function GetValidationInfo() {
44-
info "--------------------------------------------"
45-
info "Validating the configuration"
46-
43+
function ValidateFindMode() {
44+
debug "Validating find mode. USE_FIND_ALGORITHM: ${USE_FIND_ALGORITHM}, VALIDATE_ALL_CODEBASE: ${VALIDATE_ALL_CODEBASE}"
4745
if [[ "${USE_FIND_ALGORITHM}" == "true" ]] && [[ "${VALIDATE_ALL_CODEBASE}" == "false" ]]; then
48-
fatal "Setting USE_FIND_ALGORITHM to true and VALIDATE_ALL_CODEBASE to false is not supported because super-linter relies on Git to validate changed files."
46+
error "Setting USE_FIND_ALGORITHM to true and VALIDATE_ALL_CODEBASE to false is not supported because super-linter relies on Git to validate changed files."
47+
return 1
48+
fi
49+
}
50+
51+
function ValidateAnsibleDirectory() {
52+
if [ -z "${ANSIBLE_DIRECTORY:-}" ]; then
53+
ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/ansible"
54+
debug "Set ANSIBLE_DIRECTORY to the default: ${ANSIBLE_DIRECTORY}"
55+
else
56+
debug "ANSIBLE_DIRECTORY before considering corner cases: ${ANSIBLE_DIRECTORY}"
57+
# Check if first char is '/'
58+
if [[ ${ANSIBLE_DIRECTORY:0:1} == "/" ]]; then
59+
# Remove first char
60+
ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY:1}"
61+
fi
62+
63+
if [ -z "${ANSIBLE_DIRECTORY}" ] || [[ ${ANSIBLE_DIRECTORY} == "." ]]; then
64+
# Catches the case where ANSIBLE_DIRECTORY="/" or ANSIBLE_DIRECTORY="."
65+
TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}"
66+
else
67+
# Need to give it full path
68+
TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/${ANSIBLE_DIRECTORY}"
69+
fi
70+
71+
# Set the value
72+
ANSIBLE_DIRECTORY="${TEMP_ANSIBLE_DIRECTORY}"
73+
debug "Setting Ansible directory to: ${ANSIBLE_DIRECTORY}"
4974
fi
75+
export ANSIBLE_DIRECTORY
76+
}
5077

78+
function ValidateValidationVariables() {
5179
################################################
5280
# Determine if any linters were explicitly set #
5381
################################################
54-
ANY_SET="false"
55-
ANY_TRUE="false"
56-
ANY_FALSE="false"
57-
82+
local ANY_SET="false"
83+
local ANY_TRUE="false"
84+
local ANY_FALSE="false"
5885
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
86+
debug "Check if configuration provided a custom value to enable or disable ${LANGUAGE}"
5987
local VALIDATE_LANGUAGE
6088
VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}"
61-
debug "Set VALIDATE_LANGUAGE while validating the configuration: ${VALIDATE_LANGUAGE}"
6289
if [ -n "${!VALIDATE_LANGUAGE:-}" ]; then
90+
debug "Configuration provided a custom value for ${VALIDATE_LANGUAGE}: ${!VALIDATE_LANGUAGE}"
6391
# Validate if user provided a string representing a valid boolean
6492
ValidateBooleanVariable "${VALIDATE_LANGUAGE}" "${!VALIDATE_LANGUAGE}"
65-
# It was set, need to set flag
6693
ANY_SET="true"
6794
if [ "${!VALIDATE_LANGUAGE}" == "true" ]; then
6895
ANY_TRUE="true"
69-
elif [ "${!VALIDATE_LANGUAGE}" == "false" ]; then
96+
# We already checked that VALIDATE_LANGUAGE is either true or false
97+
else
7098
ANY_FALSE="true"
7199
fi
72100
else
73101
debug "Configuration didn't provide a custom value for ${VALIDATE_LANGUAGE}"
74102
fi
75103
done
76104

105+
debug "ANY_SET: ${ANY_SET}, ANY_TRUE: ${ANY_TRUE}, ANY_FALSE: ${ANY_FALSE}"
106+
77107
if [ $ANY_TRUE == "true" ] && [ $ANY_FALSE == "true" ]; then
78-
fatal "Behavior not supported, please either only include (VALIDATE=true) or exclude (VALIDATE=false) linters, but not both"
108+
error "Behavior not supported, please either only include (VALIDATE=true) or exclude (VALIDATE=false) linters, but not both"
109+
return 1
79110
fi
80111

81112
#########################################################
@@ -85,60 +116,26 @@ function GetValidationInfo() {
85116
local VALIDATE_LANGUAGE
86117
VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}"
87118
if [[ ${ANY_SET} == "true" ]]; then
119+
debug "Configuration contains at least one custom value to enable or disable linters."
88120
if [ -z "${!VALIDATE_LANGUAGE:-}" ]; then
89121
# Flag was not set, default to:
90122
# if ANY_TRUE then set to false
91123
# if ANY_FALSE then set to true
92124
eval "${VALIDATE_LANGUAGE}='$ANY_FALSE'"
93125
fi
94126
else
95-
# No linter flags were set - default all to true
96127
eval "${VALIDATE_LANGUAGE}='true'"
128+
debug "Configuration doesn't include any custom values to enable or disable linters. Setting VALIDATE variable for ${LANGUAGE} to: ${!VALIDATE_LANGUAGE}"
97129
fi
98-
eval "export ${VALIDATE_LANGUAGE}"
99-
done
100130

101-
#######################################
102-
# Print which linters we are enabling #
103-
#######################################
104-
# Loop through all languages
105-
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
106-
local VALIDATE_LANGUAGE
107-
VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}"
108-
if [[ ${!VALIDATE_LANGUAGE} == "true" ]]; then
131+
if [[ "${!VALIDATE_LANGUAGE}" == "true" ]]; then
109132
debug "- Validating [${LANGUAGE}] files in code base..."
110133
else
111134
debug "- Excluding [$LANGUAGE] files in code base..."
112135
fi
113-
done
114-
115-
##############################
116-
# Validate Ansible Directory #
117-
##############################
118-
if [ -z "${ANSIBLE_DIRECTORY:-}" ]; then
119-
ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/ansible"
120-
debug "Set ANSIBLE_DIRECTORY to the default: ${ANSIBLE_DIRECTORY}"
121-
else
122-
debug "ANSIBLE_DIRECTORY before considering corner cases: ${ANSIBLE_DIRECTORY}"
123-
# Check if first char is '/'
124-
if [[ ${ANSIBLE_DIRECTORY:0:1} == "/" ]]; then
125-
# Remove first char
126-
ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY:1}"
127-
fi
128-
129-
if [ -z "${ANSIBLE_DIRECTORY}" ] || [[ ${ANSIBLE_DIRECTORY} == "." ]]; then
130-
# Catches the case where ANSIBLE_DIRECTORY="/" or ANSIBLE_DIRECTORY="."
131-
TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}"
132-
else
133-
# Need to give it full path
134-
TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/${ANSIBLE_DIRECTORY}"
135-
fi
136136

137-
# Set the value
138-
ANSIBLE_DIRECTORY="${TEMP_ANSIBLE_DIRECTORY}"
139-
debug "Setting Ansible directory to: ${ANSIBLE_DIRECTORY}"
140-
fi
141-
export ANSIBLE_DIRECTORY
137+
eval "export ${VALIDATE_LANGUAGE}"
138+
done
142139
}
143140

144141
function CheckIfGitBranchExists() {

‎lib/linter.sh‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,17 @@ mkdir -p "${SUPER_LINTER_PRIVATE_OUTPUT_DIRECTORY_PATH}"
828828
############################
829829
# Validate the environment #
830830
############################
831-
GetValidationInfo
831+
info "--------------------------------------------"
832+
info "Validating the configuration"
833+
if ! ValidateFindMode; then
834+
fatal "Error while validating the configuration."
835+
fi
836+
if ! ValidateValidationVariables; then
837+
fatal "Error while validating the configuration of enabled linters"
838+
fi
839+
if ! ValidateAnsibleDirectory; then
840+
fatal "Error while validating the configuration of enabled linters"
841+
fi
832842

833843
if [[ "${USE_FIND_ALGORITHM}" == "false" ]] || [[ "${IGNORE_GITIGNORED_FILES}" == "true" ]]; then
834844
debug "Validate the local Git environment"

‎test/lib/buildFileListTest.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ function GenerateFileDiffTwoFilesPushEventTest() {
123123
}
124124

125125
function BuildFileArraysAnsibleGitHubWorkspaceTest() {
126+
local FUNCTION_NAME
127+
FUNCTION_NAME="${FUNCNAME[0]}"
128+
info "${FUNCTION_NAME} start"
126129

127130
# shellcheck source=/dev/null
128131
source /action/lib/functions/detectFiles.sh
@@ -158,8 +161,6 @@ function BuildFileArraysAnsibleGitHubWorkspaceTest() {
158161
fatal "${FILE_ARRAY_ANSIBLE_PATH} doesn't contain ${ANSIBLE_DIRECTORY}"
159162
fi
160163

161-
local FUNCTION_NAME
162-
FUNCTION_NAME="${FUNCNAME[0]}"
163164
notice "${FUNCTION_NAME} PASS"
164165
}
165166

‎test/lib/detectFilesTest.sh‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ CREATE_LOG_FILE=false
1717
source "lib/functions/detectFiles.sh"
1818

1919
function RecognizeNoShebangTest() {
20+
local FUNCTION_NAME
21+
FUNCTION_NAME="${FUNCNAME[0]}"
22+
info "${FUNCTION_NAME} start"
2023
local FILE="test/linters/bash_exec/libraries/noShebang_bad.sh"
2124

2225
debug "Confirming ${FILE} has no shebang"
@@ -25,11 +28,13 @@ function RecognizeNoShebangTest() {
2528
fatal "${FILE} is mis-classified as having a shebang"
2629
fi
2730

28-
FUNCTION_NAME="${FUNCNAME[0]}"
2931
notice "${FUNCTION_NAME} PASS"
3032
}
3133

3234
RecognizeCommentIsNotShebangTest() {
35+
local FUNCTION_NAME
36+
FUNCTION_NAME="${FUNCNAME[0]}"
37+
info "${FUNCTION_NAME} start"
3338
local FILE="test/linters/bash_exec/libraries/comment_bad.sh"
3439

3540
debug "Confirming ${FILE} starting with a comment has no shebang"
@@ -38,11 +43,13 @@ RecognizeCommentIsNotShebangTest() {
3843
fatal "${FILE} with a comment is mis-classified as having a shebang"
3944
fi
4045

41-
FUNCTION_NAME="${FUNCNAME[0]}"
4246
notice "${FUNCTION_NAME} PASS"
4347
}
4448

4549
RecognizeIndentedShebangAsCommentTest() {
50+
local FUNCTION_NAME
51+
FUNCTION_NAME="${FUNCNAME[0]}"
52+
info "${FUNCTION_NAME} start"
4653
local FILE="test/linters/bash_exec/libraries/indentedShebang_bad.sh"
4754

4855
debug "Confirming indented shebang in ${FILE} is considered a comment"
@@ -51,11 +58,13 @@ RecognizeIndentedShebangAsCommentTest() {
5158
fatal "${FILE} with a comment is mis-classified as having a shebang"
5259
fi
5360

54-
FUNCTION_NAME="${FUNCNAME[0]}"
5561
notice "${FUNCTION_NAME} PASS"
5662
}
5763

5864
RecognizeSecondLineShebangAsCommentTest() {
65+
local FUNCTION_NAME
66+
FUNCTION_NAME="${FUNCNAME[0]}"
67+
info "${FUNCTION_NAME} start"
5968
local FILE="test/linters/bash_exec/libraries/secondLineShebang_bad.sh"
6069

6170
debug "Confirming shebang on second line in ${FILE} is considered a comment"
@@ -64,11 +73,13 @@ RecognizeSecondLineShebangAsCommentTest() {
6473
fatal "${FILE} with a comment is mis-classified as having a shebang"
6574
fi
6675

67-
FUNCTION_NAME="${FUNCNAME[0]}"
6876
notice "${FUNCTION_NAME} PASS"
6977
}
7078

7179
function RecognizeShebangTest() {
80+
local FUNCTION_NAME
81+
FUNCTION_NAME="${FUNCNAME[0]}"
82+
info "${FUNCTION_NAME} start"
7283
local FILE="test/linters/bash_exec/libraries/shebang_bad.sh"
7384

7485
debug "Confirming ${FILE} has a shebang"
@@ -77,11 +88,13 @@ function RecognizeShebangTest() {
7788
fatal "${FILE} is mis-classified as not having a shebang"
7889
fi
7990

80-
FUNCTION_NAME="${FUNCNAME[0]}"
8191
notice "${FUNCTION_NAME} PASS"
8292
}
8393

8494
function RecognizeShebangWithBlankTest() {
95+
local FUNCTION_NAME
96+
FUNCTION_NAME="${FUNCNAME[0]}"
97+
info "${FUNCTION_NAME} start"
8598
local FILE="test/linters/bash_exec/libraries/shebangWithBlank_bad.sh"
8699

87100
debug "Confirming shebang with blank in ${FILE} is recognized"
@@ -90,11 +103,14 @@ function RecognizeShebangWithBlankTest() {
90103
fatal "${FILE} is mis-classified as not having a shebang"
91104
fi
92105

93-
FUNCTION_NAME="${FUNCNAME[0]}"
94106
notice "${FUNCTION_NAME} PASS"
95107
}
96108

97109
function IsAnsibleDirectoryTest() {
110+
local FUNCTION_NAME
111+
FUNCTION_NAME="${FUNCNAME[0]}"
112+
info "${FUNCTION_NAME} start"
113+
98114
local GITHUB_WORKSPACE
99115
GITHUB_WORKSPACE="$(mktemp -d)"
100116
local FILE="${GITHUB_WORKSPACE}/ansible"
@@ -108,7 +124,6 @@ function IsAnsibleDirectoryTest() {
108124
fatal "${FILE} is not considered to be an Ansible directory"
109125
fi
110126

111-
FUNCTION_NAME="${FUNCNAME[0]}"
112127
notice "${FUNCTION_NAME} PASS"
113128
}
114129

‎test/lib/githubEventTest.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ source "lib/functions/validation.sh"
1919
source "lib/functions/githubEvent.sh"
2020

2121
function GetGithubPushEventCommitCountTest() {
22+
local FUNCTION_NAME
23+
FUNCTION_NAME="${FUNCNAME[0]}"
24+
info "${FUNCTION_NAME} start"
25+
2226
local GITHUB_EVENT_COMMIT_COUNT
2327
GITHUB_EVENT_COMMIT_COUNT=$(GetGithubPushEventCommitCount "test/data/github-event/github-event-push.json")
2428

@@ -28,13 +32,16 @@ function GetGithubPushEventCommitCountTest() {
2832
fatal "GITHUB_EVENT_COMMIT_COUNT is not equal to 1: ${GITHUB_EVENT_COMMIT_COUNT}"
2933
fi
3034

31-
FUNCTION_NAME="${FUNCNAME[0]}"
3235
notice "${FUNCTION_NAME} PASS"
3336
}
3437

3538
GetGithubPushEventCommitCountTest
3639

3740
function GetGithubRepositoryDefaultBranchTest() {
41+
local FUNCTION_NAME
42+
FUNCTION_NAME="${FUNCNAME[0]}"
43+
info "${FUNCTION_NAME} start"
44+
3845
local GITHUB_REPOSITORY_DEFAULT_BRANCH
3946
GITHUB_REPOSITORY_DEFAULT_BRANCH=$(GetGithubRepositoryDefaultBranch "test/data/github-event/github-event-push.json")
4047

@@ -47,7 +54,6 @@ function GetGithubRepositoryDefaultBranchTest() {
4754
fatal "GITHUB_REPOSITORY_DEFAULT_BRANCH (${GITHUB_REPOSITORY_DEFAULT_BRANCH}) is not equal to: ${EXPECTED_GITHUB_REPOSITORY_DEFAULT_BRANCH}"
4855
fi
4956

50-
FUNCTION_NAME="${FUNCNAME[0]}"
5157
notice "${FUNCTION_NAME} PASS"
5258
}
5359

‎test/lib/setupSSHTest.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ GITHUB_META_URL="https://api.${GITHUB_DOMAIN}/meta"
2121
source "lib/functions/setupSSH.sh"
2222

2323
function GetGitHubSshRsaKeyFingerprintTest() {
24+
local FUNCTION_NAME
25+
FUNCTION_NAME="${FUNCNAME[0]}"
26+
info "${FUNCTION_NAME} start"
27+
2428
local SSH_RSA_KEY_FINGERPRINT
2529
SSH_RSA_KEY_FINGERPRINT=$(GetGitHubSshRsaKeyFingerprint)
2630

@@ -33,14 +37,16 @@ function GetGitHubSshRsaKeyFingerprintTest() {
3337
fatal "SSH_RSA_KEY_FINGERPRINT is not equal to ${EXPECTED_GITHUB_RSA_KEY_FINGERPRINT}: ${SSH_RSA_KEY_FINGERPRINT}"
3438
fi
3539

36-
FUNCTION_NAME="${FUNCNAME[0]}"
3740
notice "${FUNCTION_NAME} PASS"
3841
}
3942

4043
function SetupGithubComSshKeysTest() {
44+
local FUNCTION_NAME
45+
FUNCTION_NAME="${FUNCNAME[0]}"
46+
info "${FUNCTION_NAME} start"
47+
4148
SSH_KEY="test_ssh_key" SSH_INSECURE_NO_VERIFY_GITHUB_KEY="false" SetupGithubComSshKeys
4249

43-
FUNCTION_NAME="${FUNCNAME[0]}"
4450
notice "${FUNCTION_NAME} PASS"
4551
}
4652

0 commit comments

Comments
 (0)