Skip to content

Commit 7b735b0

Browse files
committed
Introduce script testing
1 parent 41260df commit 7b735b0

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

.github/workflows/internal-java-code-analysis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ jobs:
5656
- name: Checkout GIT Repository
5757
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
5858

59+
- name: Run script tests
60+
id: script-tests
61+
run: ./scripts/runTests.sh
62+
5963
- name: Set Set output variable 'analysis-name'
6064
id: set-analysis-name
6165
run: echo "analysis-name=${{ env.PROJECT_NAME }}-${{ env.AXON_FRAMEWORK_VERSION }}" >> "$GITHUB_OUTPUT"

scripts/runTests.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
# Runs all test scripts (no Python and Chromium required).
4+
# It only considers scripts in the "scripts" directory.
5+
6+
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
7+
set -o errexit -o pipefail
8+
9+
# Overrideable Constants (defaults also defined in sub scripts)
10+
LOG_GROUP_START=${LOG_GROUP_START:-"::group::"} # Prefix to start a log group. Defaults to GitHub Actions log group start command.
11+
LOG_GROUP_END=${LOG_GROUP_END:-"::endgroup::"} # Prefix to end a log group. Defaults to GitHub Actions log group end command.
12+
13+
## Get this "scripts" directory if not already set
14+
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
15+
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
16+
# This way non-standard tools like readlink aren't needed.
17+
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
18+
echo "runTests: SCRIPTS_DIR=${SCRIPTS_DIR}" >&2
19+
20+
# Run all report scripts
21+
for test_script_file in "${SCRIPTS_DIR}"/test*.sh; do
22+
test_script_filename=$(basename -- "${test_script_file}");
23+
test_script_filename="${test_script_filename%.*}" # Remove file extension
24+
25+
echo "${LOG_GROUP_START}Run ${test_script_filename}";
26+
echo "runTests: $(date +'%Y-%m-%dT%H:%M:%S%z') Starting ${test_script_filename}...";
27+
28+
source "${test_script_file}"
29+
30+
echo "runTests: $(date +'%Y-%m-%dT%H:%M:%S%z') Finished ${test_script_filename}";
31+
echo "${LOG_GROUP_END}";
32+
done

scripts/testDetectChangedFiles.sh

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests "detectChangedFiles.sh".
4+
5+
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
6+
set -o errexit -o pipefail
7+
8+
## Get this "scripts" directory if not already set
9+
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
10+
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
11+
# This way non-standard tools like readlink aren't needed.
12+
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
13+
echo "testDetectChangedFiles: SCRIPTS_DIR=${SCRIPTS_DIR}" >&2
14+
15+
tearDown() {
16+
# echo "testDetectChangedFiles: Tear down tests...."
17+
rm -rf "${temporaryTestDirectory}"
18+
}
19+
20+
successful() {
21+
local COLOR_SUCCESSFUL="\033[0;32m" # green
22+
local COLOR_DEFAULT='\033[0m'
23+
24+
echo -e "testDetectChangedFiles: ${COLOR_SUCCESSFUL}Tests finished successfully.${COLOR_DEFAULT}"
25+
26+
tearDown
27+
exit 0
28+
}
29+
30+
fail() {
31+
local COLOR_ERROR='\033[0;31m' # red
32+
local COLOR_DEFAULT='\033[0m'
33+
34+
local errorMessage="${1}"
35+
36+
echo -e "testDetectChangedFiles: ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT}"
37+
tearDown
38+
exit 1
39+
}
40+
41+
echo "testDetectChangedFiles: Starting tests...."
42+
43+
# Create testing resources
44+
testCaseNumber=0
45+
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t 'temporaryTestDirectory')
46+
testHashFile="${temporaryTestDirectory}/testHashFile.sha"
47+
testFileForChangeDetection="${temporaryTestDirectory}/testFileForChangeDetection.txt"
48+
echo "Some Test Content" > "${testFileForChangeDetection}"
49+
50+
# Test cases
51+
testCaseNumber=$((testCaseNumber + 1))
52+
echo "testDetectChangedFiles: ${testCaseNumber}.) Create missing hashfile and report that as changed file."
53+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
54+
if [ "${changeDetectionReturnCode}" != "1" ]; then
55+
fail "${testCaseNumber}.) Test failed: Expected return code 1 for non existing hash file (change detected), but got ${changeDetectionReturnCode}."
56+
fi
57+
58+
testCaseNumber=$((testCaseNumber + 1))
59+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect an unchanged file when the hashfile contains the same value as the newly calculated one."
60+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
61+
if [ "${changeDetectionReturnCode}" != "0" ]; then
62+
fail "${testCaseNumber}.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
63+
fi
64+
65+
testCaseNumber=$((testCaseNumber + 1))
66+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed file when the hashfile contains a different value as the current one."
67+
echo "Some CHANGED Test Content" > "${testFileForChangeDetection}"
68+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
69+
if [ "${changeDetectionReturnCode}" != "2" ]; then
70+
fail "${testCaseNumber}.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
71+
fi
72+
73+
testCaseNumber=$((testCaseNumber + 1))
74+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed directory when the hashfile contains a different value as the current one."
75+
echo "Some CHANGED Test Directory Content" > "${testFileForChangeDetection}"
76+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${temporaryTestDirectory}")
77+
if [ "${changeDetectionReturnCode}" != "2" ]; then
78+
fail "${testCaseNumber}.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
79+
fi
80+
81+
testCaseNumber=$((testCaseNumber + 1))
82+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect an unchanged directory when the hashfile contains the same value as the newly calculated one."
83+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${temporaryTestDirectory}")
84+
if [ "${changeDetectionReturnCode}" != "0" ]; then
85+
fail "${testCaseNumber}.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
86+
fi
87+
88+
testCaseNumber=$((testCaseNumber + 1))
89+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect an unchanged file when the hashfile contains the same value as the current one again. Same as 2.) but different after 3.)."
90+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
91+
if [ "${changeDetectionReturnCode}" != "0" ]; then
92+
fail "${testCaseNumber}.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
93+
fi
94+
95+
testCaseNumber=$((testCaseNumber + 1))
96+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed file when the hashfile contains a different value as the current one in read-only mode."
97+
echo "Some CHANGED AGAIN Test Content" > "${testFileForChangeDetection}"
98+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
99+
if [ "${changeDetectionReturnCode}" != "2" ]; then
100+
fail "${testCaseNumber}.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
101+
fi
102+
103+
testCaseNumber=$((testCaseNumber + 1))
104+
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed file when the hashfile hadn't been update with the last change detection in read-only mode."
105+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
106+
if [ "${changeDetectionReturnCode}" != "2" ]; then
107+
fail "${testCaseNumber}.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
108+
fi
109+
110+
testCaseNumber=$((testCaseNumber + 1))
111+
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not existing first path"
112+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "./nonExistingFile.txt,${testFileForChangeDetection}"); then
113+
fail "${testCaseNumber}.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
114+
fi
115+
116+
testCaseNumber=$((testCaseNumber + 1))
117+
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not existing second path"
118+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection},./nonExistingFile2.txt"); then
119+
fail "${testCaseNumber}.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
120+
fi
121+
122+
testCaseNumber=$((testCaseNumber + 1))
123+
echo "testDetectChangedFiles: ${testCaseNumber}.) Interpret missing paths as 'nothing changed'."
124+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths)
125+
if [ "${changeDetectionReturnCode}" != "0" ]; then
126+
fail "${testCaseNumber}.) Tests failed: Expected return code 0 if there are no files to check, but got ${changeDetectionReturnCode}."
127+
fi
128+
129+
testCaseNumber=$((testCaseNumber + 1))
130+
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not unknown command-line option"
131+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "./nonExistingFile.txt,${testFileForChangeDetection}" --unknown); then
132+
fail "${testCaseNumber}.) Tests failed: Expected to fail due to a an unknown command-line option, but got ${changeDetectionReturnCode}."
133+
fi
134+
135+
successful

0 commit comments

Comments
 (0)