Document language for dates; @eval pathname resolution and :cwd (from dev bc7cd62b6f68)

@date/@datetime gain :lang (German: "16. Juni 1910") over a document-wide
Language state variable, and :number for the numeric form (en 6/16/1910,
de 16.06.1910 per DIN 5008).  @eval finds Python modules next to the file
that names them regardless of the cwd, and the new :cwd option runs an
eval in a chosen working directory (@source_file uses it to resolve
against the document).  Two new test suites ship in tst/.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-27 20:25:49 +02:00
parent f84603ee19
commit 61b21d1397
12 changed files with 309 additions and 15 deletions

View File

@@ -1,10 +1,11 @@
# Klammertext distribution test suite (subset).
#
# Runs the five shell regression suites:
# Runs the six shell regression suites:
# cond_test.sh — @cond argument delimitation
# deftype_test.sh — the four klammer definition modes + redefinition table
# escape_test.sh — target character escaping and quoted specials
# filename_test.sh — filenames with spaces (quoting, " / " lists, rescue)
# modulepath_test.sh — @eval finds modules beside the file that names them
# editor_test.sh — editor support (doc/edit): indentation and table
# alignment; needs python3, uses Emacs when installed
#
@@ -16,4 +17,5 @@ test:
./deftype_test.sh
./escape_test.sh
./filename_test.sh
./modulepath_test.sh
./editor_test.sh

120
tst/modulepath_test.sh Executable file
View File

@@ -0,0 +1,120 @@
#!/bin/bash
#
# modulepath_test.sh — @eval pathname resolution (Klammermachine engine
# tier): module resolution and the :cwd option.
#
# A Python module lives NEXT TO THE FILE whose @eval names it: the directory
# of every file the Machine reads — input files, klammer sets, @read targets
# — joins the Python sys.path (after the SKS directories), so resolution is
# independent of the cwd. Before 2026-07-27 modules were found only via
# PYTHONPATH or the cwd, so `ktext /path/doc.kt` worked from the document's
# directory and failed from anywhere else.
#
# The same directories are searched for a relative `:cpp` library not found
# from the cwd (Eval::eval_command); that path is not exercised here because
# it would mean compiling a DSO inside the test — the directory list is the
# same one these cases prove.
#
# Pass-ordering caveat (deliberate, untested here): at one nesting level
# @eval is processed before @read expands, so a TOP-LEVEL @eval sees only
# files read earlier. A klammer DEFINED in a read file is applied later and
# always sees its own directory — the realistic case, tested below.
#
# Engine tier: -k none, fixtures created inline in a temp tree, no SKS.
# Usage: ./modulepath_test.sh (ktext on PATH)
PASS=0
FAIL=0
red=$'\033[31m'
green=$'\033[32m'
bold=$'\033[1m'
reset=$'\033[0m'
T="$(mktemp -d)"
trap 'rm -rf "$T"' EXIT
mkdir -p "$T/seta" "$T/setb" "$T/elsewhere"
printf 'def hello():\n return "module-next-to-read-file"\n' > "$T/seta/pmod.py"
printf '@@desc : @eval pmod.hello() @ @@#-\n' > "$T/seta/defs.k"
printf 'def here():\n return "module-next-to-input"\n' > "$T/setb/imod.py"
printf '@read %s/seta/defs.k @#-@desc@ / @eval imod.here() @\n' "$T" > "$T/setb/doc.kt"
printf '@eval nosuch_module_xyz.f() @\n' > "$T/setb/missing.kt"
check() { # check NAME EXPECTED ARGS...
local name="$1" expected="$2"; shift 2
local out
out=$(ktext "$@" -k none -d 2>/dev/null)
if [ "$out" = "$expected" ]; then
echo "${green}PASS${reset} $name"; PASS=$((PASS + 1))
else
echo "${red}FAIL${reset} $name"
echo " expected: [$expected]"
echo " got: [$out]"; FAIL=$((FAIL + 1))
fi
}
echo "${bold}@eval module resolution tests${reset}"
echo "======================="
echo
cd "$T/elsewhere"
check " 1. modules beside the @read file and the input file (absolute path)" \
"module-next-to-read-file / module-next-to-input" \
"$T/setb/doc.kt"
check " 2. the same, input given as a relative path" \
"module-next-to-read-file / module-next-to-input" \
"../setb/doc.kt"
name=" 3. an unknown module still reports cleanly"
err=$(ktext "$T/setb/missing.kt" -k none -d 2>&1 >/dev/null)
case "$err" in
*'Cannot import module "nosuch_module_xyz"'*)
echo "${green}PASS${reset} $name"; PASS=$((PASS + 1)) ;;
*)
echo "${red}FAIL${reset} $name"
echo " stderr: [$(echo "$err" | head -3)]"; FAIL=$((FAIL + 1)) ;;
esac
# --- the :cwd option --------------------------------------------------------
# @eval :cwd DIR runs the eval (any mode) with DIR as the working directory
# and restores the process cwd afterwards; *K_input_dir* names the
# document's directory; whitespace-separated tokens are joined until they
# name an existing directory (the filenames-with-spaces convention).
printf 'hello from data file\n' > "$T/setb/data.txt"
printf '@eval :cwd *K_input_dir* :shell cat data.txt @\n' > "$T/setb/shellcwd.kt"
check " 4. :cwd *K_input_dir* for :shell, run from elsewhere" \
"hello from data file" "$T/setb/shellcwd.kt"
printf '@eval :cwd *K_input_dir* os.getcwd() @\n' > "$T/setb/pycwd.kt"
check " 5. :cwd changes the Python working directory" \
"$T/setb" "$T/setb/pycwd.kt"
printf '@eval :cwd *K_input_dir* :shell true @ / @eval :shell pwd @\n' > "$T/setb/restore.kt"
check " 6. the cwd is restored after the eval" \
"/ $T/elsewhere" "$T/setb/restore.kt"
mkdir -p "$T/sp ace"
printf 'spaced\n' > "$T/sp ace/f.txt"
printf '@eval :cwd %s/sp ace :shell cat f.txt @\n' "$T" > "$T/setb/spaces.kt"
check " 7. a :cwd directory containing spaces" \
"spaced" "$T/setb/spaces.kt"
name=" 8. a nonexistent :cwd is a clean argument error"
printf '@eval :cwd /nonexistent_kt_dir :shell true @\n' > "$T/setb/bad.kt"
err=$(ktext "$T/setb/bad.kt" -k none -d 2>&1 >/dev/null)
case "$err" in
*'The :cwd directory does not exist'*)
echo "${green}PASS${reset} $name"; PASS=$((PASS + 1)) ;;
*)
echo "${red}FAIL${reset} $name"
echo " stderr: [$(echo "$err" | head -2)]"; FAIL=$((FAIL + 1)) ;;
esac
echo
echo "======================="
echo "Results: ${green}$PASS passed${reset}, ${red}$FAIL failed${reset}"
[ $FAIL -eq 0 ]