Skip to content

Commit 6805999

Browse files
committed
fixup! Improve change file detection
1 parent f13df11 commit 6805999

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

scripts/detectChangedFiles.sh

+10-9
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,21 @@ else
8686
fi
8787

8888
# Check if the paths parameter exist
89-
if [ -z "${paths}" ] ; then
90-
echo 0 # 0=No change detected. The path list is empty. There is nothing to compare. Therefore assume that there are no changes.
91-
exit_successful
92-
fi
89+
if [ -z "${paths}" ]; then
90+
echo 0 # 0=No change detected. The path list is empty. There is nothing to compare. Therefore assume that there are no changes.
91+
exit_successful
92+
fi
9393

9494
# Check all paths if they are valid files or valid directories
9595
for path in ${paths//,/ }; do
96-
if [ -f "${path}" ] ; then
96+
pathWithoutProtocolPrefix=${path/#*::/}
97+
if [ -f "${pathWithoutProtocolPrefix}" ] ; then
9798
continue # Valid file
98-
fi
99-
if [ -d "${path}" ] ; then
99+
elif [ -d "${pathWithoutProtocolPrefix}" ] ; then
100100
continue # Valid directory
101101
fi
102102
# Neither a valid directory and file
103-
echo -e "${COLOR_ERROR}detectChangedFiles: Error: Invalid path: ${path}${COLOR_DEFAULT}" >&2
103+
echo -e "${COLOR_ERROR}detectChangedFiles: Error: Invalid path: ${pathWithoutProtocolPrefix}${COLOR_DEFAULT}" >&2
104104
exit_failed
105105
done
106106

@@ -166,7 +166,8 @@ get_md5_checksum_of_all_file_names_and_sizes() {
166166
local processed_paths=0
167167

168168
for path in ${paths//,/ }; do
169-
local files_and_their_size; files_and_their_size=$(file_names_and_sizes "${path}")
169+
pathWithoutProtocolPrefix=${path/#*::/}
170+
local files_and_their_size; files_and_their_size=$(file_names_and_sizes "${pathWithoutProtocolPrefix}")
170171
all_files_and_sizes="${all_files_and_sizes}${files_and_their_size}"
171172
processed_paths=$((processed_paths + 1))
172173
echo -ne "${COLOR_INFO}detectChangedFiles: Calculate checksum progress: ($processed_paths/$total_paths)\r${COLOR_DEFAULT}" >&2

scripts/testDetectChangedFiles.sh

+36-39
Original file line numberDiff line numberDiff line change
@@ -41,95 +41,92 @@ fail() {
4141
echo "testDetectChangedFiles: Starting tests...."
4242

4343
# Create testing resources
44-
testCaseNumber=0
4544
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t 'temporaryTestDirectory')
4645
testHashFile="${temporaryTestDirectory}/testHashFile.sha"
4746
testFileForChangeDetection="${temporaryTestDirectory}/testFileForChangeDetection.txt"
4847
echo "Some Test Content" > "${testFileForChangeDetection}"
4948

5049
# Test cases
51-
testCaseNumber=$((testCaseNumber + 1))
52-
echo "testDetectChangedFiles: ${testCaseNumber}.) Create missing hashfile and report that as changed file."
50+
echo "testDetectChangedFiles: 1.) Create missing hashfile and report that as changed file."
5351
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
5452
if [ "${changeDetectionReturnCode}" != "1" ]; then
55-
fail "${testCaseNumber}.) Test failed: Expected return code 1 for non existing hash file (change detected), but got ${changeDetectionReturnCode}."
53+
fail "1.) Test failed: Expected return code 1 for non existing hash file (change detected), but got ${changeDetectionReturnCode}."
5654
fi
5755

58-
testCaseNumber=$((testCaseNumber + 1))
59-
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect an unchanged file when the hashfile contains the same value as the newly calculated one."
56+
echo "testDetectChangedFiles: 2.) Detect an unchanged file when the hashfile contains the same value as the newly calculated one."
6057
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
6158
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}."
59+
fail "2.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
6360
fi
6461

65-
testCaseNumber=$((testCaseNumber + 1))
66-
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed file when the hashfile contains a different value as the current one."
62+
echo "testDetectChangedFiles: 3.) Detect a changed file when the hashfile contains a different value as the current one."
6763
echo "Some CHANGED Test Content" > "${testFileForChangeDetection}"
6864
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
6965
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}."
66+
fail "3.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
7167
fi
7268

73-
testCaseNumber=$((testCaseNumber + 1))
74-
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect a changed directory when the hashfile contains a different value as the current one."
69+
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.)."
70+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}")
71+
if [ "${changeDetectionReturnCode}" != "0" ]; then
72+
fail "4.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
73+
fi
74+
75+
echo "testDetectChangedFiles: 5.) Detect a changed directory when the hashfile contains a different value as the current one."
7576
echo "Some CHANGED Test Directory Content" > "${testFileForChangeDetection}"
7677
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${temporaryTestDirectory}")
7778
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+
fail "5.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
7980
fi
8081

81-
testCaseNumber=$((testCaseNumber + 1))
82-
echo "testDetectChangedFiles: ${testCaseNumber}.) Detect an unchanged directory when the hashfile contains the same value as the newly calculated one."
82+
echo "testDetectChangedFiles: 6.) Detect an unchanged directory when the hashfile contains the same value as the newly calculated one."
8383
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${temporaryTestDirectory}")
8484
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}."
85+
fail "6.) Tests failed: Expected return code 0 for an existing hash file with matching value (no change detected), but got ${changeDetectionReturnCode}."
9386
fi
9487

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."
88+
echo "testDetectChangedFiles: 7.) Detect a changed file when the hashfile contains a different value as the current one in read-only mode."
9789
echo "Some CHANGED AGAIN Test Content" > "${testFileForChangeDetection}"
9890
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
9991
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}."
92+
fail "7.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
10193
fi
10294

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."
95+
echo "testDetectChangedFiles: 8.) Detect a changed file when the hashfile hadn't been update with the last change detection in read-only mode."
10596
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "${testFileForChangeDetection}" --readonly)
10697
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}."
98+
fail "8.) Tests failed: Expected return code 2 for an existing hash file with differing value (change detected), but got ${changeDetectionReturnCode}."
10899
fi
109100

110-
testCaseNumber=$((testCaseNumber + 1))
111-
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not existing first path"
101+
echo "testDetectChangedFiles: 9.) Fail on not existing first path"
112102
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}."
103+
fail "9.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
114104
fi
115105

116106
testCaseNumber=$((testCaseNumber + 1))
117-
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not existing second path"
107+
echo "testDetectChangedFiles: 10.) Fail on not existing second path"
118108
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}."
109+
fail "10.) Tests failed: Expected to fail due to a wrong paths option, but got ${changeDetectionReturnCode}."
120110
fi
121111

122112
testCaseNumber=$((testCaseNumber + 1))
123-
echo "testDetectChangedFiles: ${testCaseNumber}.) Interpret missing paths as 'nothing changed'."
124-
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths)
113+
echo "testDetectChangedFiles: 11.) Interpret missing paths as 'nothing changed'."
114+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "")
125115
if [ "${changeDetectionReturnCode}" != "0" ]; then
126-
fail "${testCaseNumber}.) Tests failed: Expected return code 0 if there are no files to check, but got ${changeDetectionReturnCode}."
116+
fail "11.) Tests failed: Expected return code 0 if there are no paths to check, but got ${changeDetectionReturnCode}."
127117
fi
128118

129119
testCaseNumber=$((testCaseNumber + 1))
130-
echo "testDetectChangedFiles: ${testCaseNumber}.) Fail on not unknown command-line option"
120+
echo "testDetectChangedFiles: 12.) Fail on not unknown command-line option"
131121
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}."
122+
fail "12.) Tests failed: Expected to fail due to a an unknown command-line option, but got ${changeDetectionReturnCode}."
123+
fi
124+
125+
testCaseNumber=$((testCaseNumber + 1))
126+
echo "testDetectChangedFiles: 13.) Ignore protocol prefix in file path like 'typescript:project::./myfile.txt'."
127+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --hashfile "${testHashFile}" --paths "typescript:project::${testFileForChangeDetection}")
128+
if [ "${changeDetectionReturnCode}" = "0" ]; then
129+
fail "13.) Tests failed: Expected return code 0 if nothing changed with a protocol prefixed file, but got ${changeDetectionReturnCode}."
133130
fi
134131

135132
successful

0 commit comments

Comments
 (0)