Files
nuwiki/development/tests/test-calendar.sh
T

107 lines
3.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
#
# development/tests/test-calendar.sh — calendar-vim integration harness (Neovim).
#
# Drives development/tests/test-calendar.lua under headless Neovim to cover the
# pieces specific to the Lua entry point:
# * require('nuwiki').setup() wiring g:calendar_action / g:calendar_sign and
# overriding calendar-vim's own defaults,
# * calendar_action's window-close behaviour under the real window API,
# * opting out with use_calendar = false.
#
# A stub calendar-vim on the runtimepath provides :Calendar and seeds the
# default hook names. The LSP binary is built (setup() registers the client);
# the calendar paths themselves don't need the server.
#
# Exit: 0 when both runs are green, 1 otherwise. Skips (0) without nvim.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
TMP="$(mktemp -d -t nuwiki-calendar-test-XXXXXX)"
trap 'rm -rf "$TMP"' EXIT
log() { printf '\033[1;34m[calendar]\033[0m %s\n' "$*"; }
if ! command -v nvim >/dev/null 2>&1; then
log 'nvim not installed — skipping'
exit 0
fi
# setup() registers the LSP client, so build the binary like the other
# Neovim harnesses do (incremental, fast).
BIN="$REPO_ROOT/bin/nuwiki-ls"
log 'building nuwiki-ls (release, incremental)…'
cargo build --release -p nuwiki-ls --manifest-path "$REPO_ROOT/Cargo.toml" >/dev/null
mkdir -p "$REPO_ROOT/bin"
ln -sf "$REPO_ROOT/target/release/nuwiki-ls" "$BIN"
# --- Scratch wiki with one seeded diary entry (2026-05-30) ----------------
WIKI="$TMP/wiki"
mkdir -p "$WIKI/diary"
echo "= index =" > "$WIKI/index.wiki"
echo "= 2026-05-30 =" > "$WIKI/diary/2026-05-30.wiki"
# --- Stub calendar-vim ----------------------------------------------------
CAL="$TMP/calendar-vim"
mkdir -p "$CAL/plugin" "$CAL/autoload"
cat > "$CAL/plugin/calendar.vim" <<'EOF'
if exists('g:loaded_calendar_stub') | finish | endif
let g:loaded_calendar_stub = 1
let g:calendar_action = get(g:, 'calendar_action', 'calendar#diary')
let g:calendar_sign = get(g:, 'calendar_sign', 'calendar#sign')
command! -nargs=* Calendar echo 'stub calendar'
EOF
cat > "$CAL/autoload/calendar.vim" <<'EOF'
function! calendar#open(...) abort
endfunction
function! calendar#diary(...) abort
endfunction
function! calendar#sign(...) abort
return 0
endfunction
EOF
write_init() {
local file="$1" use_calendar="$2"
cat > "$file" <<EOF
vim.opt.runtimepath:prepend('$REPO_ROOT')
vim.opt.runtimepath:append('$CAL')
vim.g.nuwiki_binary_path = '$BIN'
vim.g.nuwiki_wiki_root = '$WIKI'
vim.g.nuwiki_file_extension = '.wiki'
vim.g.nuwiki_diary_rel_path = 'diary'
require('nuwiki').setup({ wiki_root = '$WIKI', use_calendar = $use_calendar })
EOF
}
run() {
local label="$1" init="$2" mode="$3"
local results="$TMP/results-$label.txt"
log "running $label harness…"
NUWIKI_CALENDAR_RESULTS="$results" \
NUWIKI_CALENDAR_WIKI="$WIKI" \
NUWIKI_CALENDAR_MODE="$mode" \
timeout 60 nvim --clean -u "$init" --headless \
-c "luafile $REPO_ROOT/development/tests/test-calendar.lua" \
>"$TMP/$label.log" 2>&1 || true
if [[ ! -f "$results" ]]; then
echo "calendar $label harness produced no output" >&2
echo "--- nvim log ---" >&2
cat "$TMP/$label.log" >&2 || true
return 1
fi
cat "$results"
grep -qE '^SUMMARY: [0-9]+ passed, 0 failed' "$results"
}
INIT_ON="$TMP/init-on.lua"
INIT_OFF="$TMP/init-off.lua"
write_init "$INIT_ON" 'true'
write_init "$INIT_OFF" 'false'
rc=0
run 'default' "$INIT_ON" 'default' || rc=1
run 'optout' "$INIT_OFF" 'optout' || rc=1
exit $rc