Skip to content

Commit d1a33f3

Browse files
committed
Add test that verifies cypher file references
1 parent e525339 commit d1a33f3

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

scripts/testCypherReferences.sh

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests: scan all *.sh files (current directory including subdirectories)
4+
# for occurrences of '/...*.cypher' references and ensure a file with
5+
# the referenced basename exists somewhere in the tree.
6+
7+
# Fail on any error
8+
set -o errexit
9+
10+
# Fail if any command in a pipeline fails (not just the last one)
11+
if set -o pipefail 2>/dev/null; then
12+
set -o pipefail
13+
fi
14+
15+
SCRIPT_NAME="testCypherReferences.sh"
16+
COLOR_ERROR='\033[0;31m'
17+
COLOR_DE_EMPHASIZED='\033[0;90m'
18+
COLOR_SUCCESSFUL="\033[0;32m"
19+
COLOR_DEFAULT='\033[0m'
20+
21+
# Determine this scripts dir (POSIX-friendly)
22+
SCRIPTS_DIR=${SCRIPTS_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)}
23+
24+
tearDown() {
25+
rm -rf "${temporaryTestDirectory}"
26+
}
27+
28+
successful() {
29+
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_DEFAULT} ${COLOR_SUCCESSFUL}✅ Tests finished successfully.${COLOR_DEFAULT}"
30+
tearDown
31+
}
32+
33+
info() {
34+
local infoMessage="${1}"
35+
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_DEFAULT} ${infoMessage}"
36+
}
37+
38+
fail() {
39+
local errorMessage="${1}"
40+
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}: ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT}"
41+
tearDown
42+
return 1
43+
}
44+
45+
printTestLogFileContent() {
46+
local logFileName="${temporaryTestDirectory}/${SCRIPT_NAME}.log"
47+
if [ -f "${logFileName}" ]; then
48+
local logFileContent
49+
logFileContent=$(cat "${logFileName}")
50+
# Remove common color codes for readability
51+
echo -e "${COLOR_DE_EMPHASIZED}${logFileContent}${COLOR_DEFAULT}"
52+
else
53+
echo -e "${COLOR_ERROR}No log file found at expected location: %s${COLOR_DEFAULT}" "${logFileName}"
54+
fi
55+
}
56+
57+
info "Starting tests...."
58+
59+
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t "temporaryTestDirectory_${SCRIPT_NAME}")
60+
mkdir -p "${temporaryTestDirectory}"
61+
62+
# ------- Integration-style Test Case
63+
info "Scan all .sh files for '/...*.cypher' references and verify filenames exist."
64+
65+
# Capture stdout/stderr into a log file for this run
66+
{
67+
missing_file="${temporaryTestDirectory}/missing_references.txt"
68+
: > "${missing_file}"
69+
70+
# Collect all cypher filenames without path present in the repo
71+
cypher_filenames="${temporaryTestDirectory}/cypher_filenames.txt"
72+
find . -type d -name "temp" -prune -o -type f -name '*.cypher' -print 2>/dev/null | sed 's#.*/##' > "${cypher_filenames}"
73+
74+
# Iterate over all .sh files
75+
find . -type d -name "temp" -prune -o -type f -name '*.sh' -print0 | while IFS= read -r -d '' script_file; do
76+
# Skip this test file itself
77+
if [ "$(basename "${script_file}")" = "${SCRIPT_NAME}" ]; then
78+
continue
79+
fi
80+
# Use awk to extract all '/...*.cypher' matches from non-comment lines
81+
awk '
82+
/^[[:space:]]*#/ { next }
83+
{
84+
line = $0
85+
while (match(line, /\/[^[:space:]]+\.cypher/)) {
86+
ref = substr(line, RSTART, RLENGTH)
87+
if (ref !~ /\$/) {
88+
print ref
89+
}
90+
line = substr(line, RSTART + RLENGTH)
91+
}
92+
}
93+
' "${script_file}" | while IFS= read -r ref; do
94+
[ -z "${ref}" ] && continue
95+
base=$(basename "${ref}")
96+
if ! grep -Fx -- "${base}" "${cypher_filenames}" >/dev/null 2>&1; then
97+
printf '%s\t%s\n' "${script_file#./}" "${ref}" >> "${missing_file}"
98+
fi
99+
done
100+
done
101+
} > "${temporaryTestDirectory}/${SCRIPT_NAME}.log" 2>&1
102+
103+
if [ -s "${missing_file}" ]; then
104+
echo -e "${COLOR_DE_EMPHASIZED}${SCRIPT_NAME}:${COLOR_ERROR}ERROR: Missing referenced cypher files (by basename):\n${COLOR_DEFAULT}"
105+
awk -F"\t" '{ printf(" - In %s -> referenced %s\n", $1, $2) }' "${missing_file}"
106+
printTestLogFileContent
107+
fail "❌ Test failed."
108+
fi
109+
110+
successful
111+
return 0

0 commit comments

Comments
 (0)