-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestFilenameReferences.sh
More file actions
executable file
·148 lines (123 loc) · 5.38 KB
/
testFilenameReferences.sh
File metadata and controls
executable file
·148 lines (123 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env bash
# Tests: scan all *.sh files (current directory including subdirectories)
# for occurrences of cypher and other filename references without paths and ensure a file with
# the referenced basename exists somewhere in the tree.
# Fail on any error
set -o errexit
# Fail if any command in a pipeline fails (not just the last one)
if set -o pipefail 2>/dev/null; then
set -o pipefail
fi
SCRIPT_NAME="testCypherReferences.sh"
COLOR_ERROR='\033[0;31m'
COLOR_DE_EMPHASIZED='\033[0;90m'
COLOR_SUCCESSFUL="\033[0;32m"
COLOR_DEFAULT='\033[0m'
# Determine this scripts dir (POSIX-friendly)
SCRIPTS_DIR=${SCRIPTS_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)}
tearDown() {
rm -rf "${temporaryTestDirectory}"
}
successful() {
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_DEFAULT} ${COLOR_SUCCESSFUL}✅ Tests finished successfully.${COLOR_DEFAULT}"
tearDown
# If sourced, return to caller; if executed directly, exit.
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
return 0
else
exit 0
fi
}
info() {
local infoMessage="${1}"
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_DEFAULT} ${infoMessage}"
}
fail() {
local errorMessage="${1}"
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}: ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT}"
tearDown
return 1
}
printTestLogFileContent() {
local logFileName="${temporaryTestDirectory}/${SCRIPT_NAME}-${reference_extension}.log"
if [ -f "${logFileName}" ]; then
local logFileContent
logFileContent=$(cat "${logFileName}")
# Remove common color codes for readability
echo -e "${COLOR_DE_EMPHASIZED}${logFileContent}${COLOR_DEFAULT}"
else
echo -e "${COLOR_ERROR}No log file found at expected location: %s${COLOR_DEFAULT}" "${logFileName}"
fi
}
find_missing_file_references() {
# Capture stdout/stderr into a log file for this run
reference_extension="$1"
{
missing_file="${temporaryTestDirectory}/missing_${reference_extension}_references.txt"
: > "${missing_file}"
# Collect all reference filenames without path present in the repo
reference_filenames="${temporaryTestDirectory}/reference_${reference_extension}_filenames.txt"
find . \
\( -type d \( -name "temp" -o -name ".git" -o -name "node_modules" \) -prune \) -o \
\( -type f -name "*.${reference_extension}" -print \) \
2>/dev/null | sed 's#.*/##' > "${reference_filenames}"
echo "pnpm-lock.yaml" >> "${reference_filenames}" # Ignore pnpm-lock file references by pretending it exists since it is not checked in but generated by users locally.
# Iterate over all shell scripts
find . \
\( -type d \( -name "temp" -o -name ".git" -o -name "node_modules" \) -prune \) -o \
\( -type f -name '*.sh' ! -name "test*" -print0 \) \
| while IFS= read -r -d '' script_file; do
# Skip this test file itself
if [ "$(basename "${script_file}")" = "${SCRIPT_NAME}" ]; then
continue
fi
# Use awk to extract all reference file name extension matches from non-comment lines
awk -v reference_extension="${reference_extension}" '
/^[[:space:]]*#/ { next }
{
line = $0
pattern = "(/[^/[:space:]\"]+\\." reference_extension ")|(\"[^/[:space:]\"]+\\." reference_extension "\")"
while (match(line, pattern)) {
ref = substr(line, RSTART, RLENGTH)
gsub(/^"|\"$/, "", ref) # remove surrounding quotes
gsub(/^\//, "", ref) # remove leading slash
if (ref !~ /\$/ && ref !~ /\*/) {
print ref
}
line = substr(line, RSTART + RLENGTH)
}
}
' "${script_file}" | while IFS= read -r reference_file; do
[ -z "${reference_file}" ] && continue
reference_filename=$(basename "${reference_file}")
if ! grep -Fx -- "${reference_filename}" "${reference_filenames}" >/dev/null 2>&1; then
printf '%s\t%s\n' "${script_file#./}" "${reference_file}" >> "${missing_file}"
fi
done
done
} > "${temporaryTestDirectory}/${SCRIPT_NAME}-${reference_extension}.log" 2>&1
if [ -s "${missing_file}" ]; then
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_ERROR} ERROR: Missing referenced ${reference_extension} files (by basename):${COLOR_DEFAULT}"
awk -F"\t" '{ printf(" - In %s -> referenced %s\n", $1, $2) }' "${missing_file}"
# Print content of reference_filenames for debugging purposes
# echo -e "\n${COLOR_DE_EMPHASIZED}List of all ${reference_extension} files found in the repository (by basename):${COLOR_DEFAULT}"
# cat "${reference_filenames}" | while IFS= read -r filename; do
# echo -e " - ${filename}"
# done
# echo ""
printTestLogFileContent
fail "${test_case_number}.) ❌ Test failed. Missing referenced ${reference_extension} files found. See details above."
fi
}
info "Starting tests...."
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t "temporaryTestDirectory_${SCRIPT_NAME}")
mkdir -p "${temporaryTestDirectory}"
# ------- Integration-style Test Case
test_case_number=1
info "${test_case_number}.) Scan all .sh files for '/...*.cypher' references and verify filenames exist."
find_missing_file_references "cypher"
# ------- Integration-style Test Case
test_case_number=2
info "${test_case_number}.) Scan all .sh files for '/...*.yaml' references and verify filenames exist."
find_missing_file_references "yaml"
successful