Skip to content

Commit b2a89b9

Browse files
committed
Introduce script testing
1 parent 6fcab85 commit b2a89b9

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

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: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t 'temporaryTestDirectory')
45+
testHashFile="${temporaryTestDirectory}/testHashFile.sha"
46+
testFileForChangeDetection="${temporaryTestDirectory}/testFileForChangeDetection.txt"
47+
echo "Some Test Content" > "${testFileForChangeDetection}"
48+
49+
# Test case 1
50+
echo "testDetectChangedFiles: 1.) Create missing hashfile and report that as changed file."
51+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
52+
if [ "${changeDetectionReturnCode}" != "1" ]; then
53+
fail "1.) Test failed: Expected return code 1 for non existing hash file (change detected), but got ${changeDetectionReturnCode}."
54+
fi
55+
56+
# Test case 2
57+
echo "testDetectChangedFiles: 2.) Detect an unchanged file when the hashfile contains the same value as the newly calculated one."
58+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
59+
if [ "${changeDetectionReturnCode}" != "0" ]; then
60+
fail "2.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
61+
fi
62+
63+
# Test case 3
64+
echo "testDetectChangedFiles: 3.) Detect a changed file when the hashfile contains a different value as the current one."
65+
echo "Some CHANGED Test Content" > "${testFileForChangeDetection}"
66+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
67+
if [ "${changeDetectionReturnCode}" != "2" ]; then
68+
fail "3.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
69+
fi
70+
71+
# Test case 4
72+
echo "testDetectChangedFiles: 4.) Detect an unchanged file when the hashfile contains the same value as the current one again. Same as 2.) but different after 3.)."
73+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
74+
if [ "${changeDetectionReturnCode}" != "0" ]; then
75+
fail "4.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
76+
fi
77+
78+
# Test case 5
79+
echo "testDetectChangedFiles: 5.) Detect a changed file when the hashfile contains a different value as the current one in read-only mode."
80+
echo "Some CHANGED AGAIN Test Content" > "${testFileForChangeDetection}"
81+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
82+
if [ "${changeDetectionReturnCode}" != "2" ]; then
83+
fail "5.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
84+
fi
85+
86+
# Test case 6
87+
echo "testDetectChangedFiles: 6.) Detect a changed file when the hashfile hadn't been update with the last change detection in read-only mode."
88+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
89+
if [ "${changeDetectionReturnCode}" != "2" ]; then
90+
fail "6.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
91+
fi
92+
93+
# Test case 7
94+
echo "testDetectChangedFiles: 7.) Fail on not existing first path"
95+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "./nonExistingFile.txt,${testFileForChangeDetection}"); then
96+
fail "7.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
97+
fi
98+
99+
# Test case 8
100+
echo "testDetectChangedFiles: 8.) Fail on not existing second path"
101+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection},./nonExistingFile2.txt"); then
102+
fail "8.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
103+
fi
104+
105+
# Test case 9
106+
echo "testDetectChangedFiles: 9.) Interpret missing paths as 'nothing changed'."
107+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths)
108+
if [ "${changeDetectionReturnCode}" != "0" ]; then
109+
fail "9.) Tests failed: Expected return code 0 if there are no files to check, but got ${changeDetectionReturnCode}."
110+
fi
111+
112+
# Test case 10
113+
echo "testDetectChangedFiles: 10.) Fail on not unknown command-line option"
114+
if changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "./nonExistingFile.txt,${testFileForChangeDetection}" --unknown); then
115+
fail "10.) Tests failed: Expected to fail due to a an unknown command-line option, but got ${changeDetectionReturnCode}."
116+
fi
117+
118+
successful

0 commit comments

Comments
 (0)