Files
JetCore/tests/runbindtests.sh
T
brad ArantandClaude Opus 5.5 2847ce5fa7 Add jetc --lint-sql to find <sql> that pastes values (phase 2)
Implements phase 2 of docs/design-sql-bound-parameters.md. jetc --lint-sql
[--strict] <file|dir>... lists each <sql> whose statement contains a $[...]
or #[...] reference instead of a <bind>, with file:line and where each value
comes from. Row values ($[.col], $[session.col], $[?N], $[#]) are only
reported with --strict. References in <bind>, <if>, <set> and the other
attribute-only tags, <comment> and |> <| are skipped. A custom tag defined
as <sql><container></sql> (barant.com's <showtable>) is checked at each use.
Exit status is 0 for none found, 1 for findings, 2 for an unreadable path.

- jetc/JetLint.*: the scanner; jetc.cpp: the option
- build scripts and runbindtests.sh: link JetLint.cpp
- tests/runlinttests.sh and tests/lintsql/: fixtures and expected output
- docs: worklist for the local copies of both sites, design doc status,
  manual note on the lint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 20:29:28 -08:00

75 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Tests for <sql>/<bind> (docs/design-sql-bound-parameters.md, phase 1):
# 1. tests/sqlbind_test.cpp - unit tests for SqlBind.cpp (no MySQL needed)
# 2. tests/testbind.jet - end to end through jet-2.0 against a real MySQL
# server (TEMPORARY tables only), compared with
# tests/testbind.expected
# 3. jetc - <bind> is void in the compiler too, and a
# compiled page runs the same <sql>/<bind> code
#
# Build jet-2.0 first (./compile). The MySQL part needs:
# JETTEST_DB_USER, JETTEST_DB_PASSWORD (required, or it is skipped)
# JETTEST_DB_HOST (default localhost), JETTEST_DB_NAME (default test)
set -u
T="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JC="$(cd "$T/.." && pwd)"
CU="$JC/../CoreUtils"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
fail=0
echo "== unit tests (SqlBind) =="
g++ -g -std=c++23 -I"$CU" -I"$JC" "$T/sqlbind_test.cpp" "$JC/SqlBind.cpp" -L"$CU" -lCoreUtils -o "$WORK/sqlbind_test" || exit 1
"$WORK/sqlbind_test" || fail=1
echo "== end to end (jet-2.0 + MySQL) =="
if [ -z "${JETTEST_DB_USER:-}" ] || [ -z "${JETTEST_DB_PASSWORD:-}" ]; then
echo "SKIPPED: set JETTEST_DB_USER and JETTEST_DB_PASSWORD"
else
export JETTEST_DB_HOST="${JETTEST_DB_HOST:-localhost}" JETTEST_DB_NAME="${JETTEST_DB_NAME:-test}"
(cd "$T" && "$JC/jet-2.0" testbind.jet) > "$WORK/testbind.out" 2>&1
if diff -u "$T/testbind.expected" "$WORK/testbind.out"; then
echo "PASS ($(wc -l < "$T/testbind.expected") lines match)"
else
echo "FAIL"; fail=1
fi
fi
echo "== jetc =="
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" "$JC/jetc/JetLint.cpp" || exit 1
# A bare <bind> must not swallow the content after it looking for </bind>.
printf 'A<sql>select ?<bind value="$[x]"></sql>B<bind value="y">C\n' > "$WORK/void.jet"
"$WORK/jetc" "$WORK/void.jet" "$WORK/void.cpp" >/dev/null || fail=1
if grep -q '<sql>select ?<bind value=\\"$\[x\]\\"></sql>' "$WORK/void.cpp" &&
grep -q '"<bind value=\\"y\\">"' "$WORK/void.cpp" &&
grep -q '"C\\n"' "$WORK/void.cpp"; then
echo "PASS (bind is void; sql span intact)"
else
echo "FAIL: unexpected codegen for bind"; grep -n 'interpretSpan\|out.write' "$WORK/void.cpp"; fail=1
fi
if [ -n "${JETTEST_DB_USER:-}" ] && [ -n "${JETTEST_DB_PASSWORD:-}" ]; then
# The whole end-to-end test again, compiled by jetc instead of interpreted.
# Drop the #! line, and the newline after </jet> (the interpreter stops
# at </jet>; a compiled page would emit it as text).
tail -n +2 "$T/testbind.jet" | head -c -1 > "$WORK/compiled.jet"
"$WORK/jetc" "$WORK/compiled.jet" "$WORK/compiled.cpp" >/dev/null || fail=1
objs=""
for f in "$JC"/*.cpp; do
[ "$(basename "$f")" = "jet-2.0.cpp" ] && continue
o="$WORK/$(basename "${f%.cpp}").o"
g++ -g -c -std=c++23 -I"$CU" -I"$JC" "$f" -o "$o" &
objs="$objs $o"
done
wait
g++ -g -std=c++23 -I"$CU" -I"$JC" "$WORK/compiled.cpp" $objs -L"$CU" -lCoreUtils -lmysqlclient -lssl -lcrypto -o "$WORK/compiled" || exit 1
(cd "$T" && "$WORK/compiled") > "$WORK/compiled.out" 2>&1
if diff -u "$T/testbind.expected" "$WORK/compiled.out"; then
echo "PASS (compiled page output matches)"
else
echo "FAIL (compiled page)"; fail=1
fi
fi
[ $fail = 0 ] && echo "ALL PASSED" || echo "FAILURES"
exit $fail