From ddb9d0b916be7daf8c3db0b52a3d3a19a897bbcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Fr=C3=B3es=20Franco?= Date: Wed, 3 Jun 2026 14:57:14 +0000 Subject: [PATCH] fix(vars): E121 in get_wikilocal no-index path; add regression test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit s:resolve_index referenced an undefined `a:file` (a copy-paste leftover from wiki_root_for, whose parameter is named a:file) inside the buffer-owning-wiki loop. Any get_wikilocal() call with no explicit index while a wiki buffer was current — i.e. the ftplugin BufRead path — hit `E121: undefined variable a:file`. The local is `l:file`, and the surrounding `!empty(l:file)` guard already covers emptiness, so the clause was both wrong and redundant; removed it. Add development/tests/test-vars-vim.{sh,vim} covering the shim: per-wiki-index resolution, global-default fallback, out-of-range clamp, the no-index owning-wiki path (direct guard for this E121), and the single-wiki shorthand. Wired into CI. 14/14 pass. Co-Authored-By: Claude Opus 4.8 --- .gitea/workflows/ci.yaml | 2 + autoload/vimwiki/vars.vim | 2 +- development/tests/test-vars-vim.sh | 67 +++++++++++++++++++++++++++++ development/tests/test-vars-vim.vim | 57 ++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100755 development/tests/test-vars-vim.sh create mode 100644 development/tests/test-vars-vim.vim diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 2ce8798..ae37c90 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -104,3 +104,5 @@ jobs: run: ./development/tests/test-calendar.sh - name: run Vim calendar integration harness run: ./development/tests/test-calendar-vim.sh + - name: run Vim vars-shim harness + run: ./development/tests/test-vars-vim.sh diff --git a/autoload/vimwiki/vars.vim b/autoload/vimwiki/vars.vim index 94abc14..1127520 100644 --- a/autoload/vimwiki/vars.vim +++ b/autoload/vimwiki/vars.vim @@ -21,7 +21,7 @@ function! s:resolve_index(...) abort for l:w in nuwiki#commands#wiki_list() let l:wroot = fnamemodify(expand(l:w.root), ':p') if l:wroot[len(l:wroot) - 1:] !=# '/' | let l:wroot .= '/' | endif - if a:file !=# '' && l:file[: len(l:wroot) - 1] ==# l:wroot + if l:file[: len(l:wroot) - 1] ==# l:wroot return l:n endif let l:n += 1 diff --git a/development/tests/test-vars-vim.sh b/development/tests/test-vars-vim.sh new file mode 100755 index 0000000..ba3b68a --- /dev/null +++ b/development/tests/test-vars-vim.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# development/tests/test-vars-vim.sh — exercises the vimwiki compatibility +# shim (autoload/vimwiki/vars.vim) that third-party plugins (vimwiki-sync, +# vim-zettel) call via vimwiki#vars#get_wikilocal. +# +# Runs test-vars-vim.vim under headless Vim; that script writes PASS/FAIL +# lines to $NUWIKI_VARS_OUT. Covers per-wiki-index resolution, global +# fallback, out-of-range clamp, the no-index "owning wiki" path (the +# BufRead path that previously raised E121), and the single-wiki shorthand. +# +# Exit code: 0 when every check passes, 1 on any FAIL or missing output. +# Skips (exit 0) when vim is not installed. + +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + +log() { printf '\033[1;34m[vars-vim]\033[0m %s\n' "$*"; } + +if ! command -v vim >/dev/null 2>&1; then + log 'vim not installed — skipping' + exit 0 +fi + +TMP="$(mktemp -d -t nuwiki-vars-vim-test-XXXXXX)" +trap 'rm -rf "$TMP"' EXIT + +DIR_A="$TMP/wiki_a" +DIR_B="$TMP/wiki_b" +mkdir -p "$DIR_A" "$DIR_B" +printf '= Home =\n' > "$DIR_A/index.wiki" +printf '= B =\n' > "$DIR_B/index.wiki" + +OUT="$TMP/vars.out" + +VIMRC="$TMP/vimrc" +cat > "$VIMRC" <"$TMP/vim.log" 2>&1 || true + +if [[ ! -f "$OUT" ]]; then + echo 'vars harness produced no output' >&2 + echo '--- vim log ---' >&2 + cat "$TMP/vim.log" >&2 || true + exit 1 +fi + +cat "$OUT" +PASSED="$(grep -c '^PASS ' "$OUT" || true)" +FAILED="$(grep -c '^FAIL ' "$OUT" || true)" +echo "SUMMARY: ${PASSED} passed, ${FAILED} failed" + +if [[ "$FAILED" -ne 0 ]]; then + exit 1 +fi +log 'vars shim OK ✓' +exit 0 diff --git a/development/tests/test-vars-vim.vim b/development/tests/test-vars-vim.vim new file mode 100644 index 0000000..b3083ef --- /dev/null +++ b/development/tests/test-vars-vim.vim @@ -0,0 +1,57 @@ +" development/tests/test-vars-vim.vim — exercises the vimwiki compat shim +" (autoload/vimwiki/vars.vim) that third-party plugins call. +" +" Sourced by test-vars-vim.sh under headless Vim. Writes PASS/FAIL lines to +" $NUWIKI_VARS_OUT; the shell wrapper fails on any FAIL (or missing output). + +let s:out = [] +let s:dir_a = $NUWIKI_VARS_DIR_A +let s:dir_b = $NUWIKI_VARS_DIR_B + +function! s:check(desc, got, want) abort + if a:got ==# a:want + call add(s:out, 'PASS ' . a:desc) + else + call add(s:out, 'FAIL ' . a:desc . ' got=[' . a:got . '] want=[' . a:want . ']') + endif +endfunction + +" ----- multi-wiki: explicit index ----- +let g:nuwiki_wikis = [ + \ {'root': s:dir_a, 'file_extension': 'wiki', 'syntax': 'markdown'}, + \ {'root': s:dir_b, 'file_extension': '.wiki'}, + \ ] +let g:nuwiki_syntax = 'vimwiki' + +call s:check('idx0 path', vimwiki#vars#get_wikilocal('path', 0), s:dir_a . '/') +call s:check('idx0 ext', vimwiki#vars#get_wikilocal('ext', 0), '.wiki') +call s:check('idx0 syntax', vimwiki#vars#get_wikilocal('syntax', 0), 'markdown') +" wiki[1] doesn't set syntax → global default +call s:check('idx1 path', vimwiki#vars#get_wikilocal('path', 1), s:dir_b . '/') +call s:check('idx1 syntax', vimwiki#vars#get_wikilocal('syntax', 1), 'vimwiki') +" out-of-range clamps to first wiki +call s:check('idx9 clamp', vimwiki#vars#get_wikilocal('path', 9), s:dir_a . '/') +" misc keys +call s:check('is_temporary', vimwiki#vars#get_wikilocal('is_temporary_wiki', 0) . '', '0') +call s:check('unknown key', vimwiki#vars#get_wikilocal('bogus', 0), '') + +" ----- no index: resolve the wiki owning the current buffer ----- +" (this is the BufRead path; regression guard for the s:resolve_index E121.) +execute 'edit ' . fnameescape(s:dir_b . '/index.wiki') +call s:check('noarg owning wiki_b', vimwiki#vars#get_wikilocal('path'), s:dir_b . '/') +execute 'edit ' . fnameescape(s:dir_a . '/index.wiki') +call s:check('noarg owning wiki_a', vimwiki#vars#get_wikilocal('path'), s:dir_a . '/') +" buffer outside any wiki → first wiki +enew +call s:check('noarg outside→first', vimwiki#vars#get_wikilocal('path'), s:dir_a . '/') + +" ----- single-wiki shorthand (no g:nuwiki_wikis) ----- +unlet g:nuwiki_wikis +let g:nuwiki_wiki_root = s:dir_a +let g:nuwiki_file_extension = 'txt' +enew +call s:check('shorthand path', vimwiki#vars#get_wikilocal('path'), s:dir_a . '/') +call s:check('shorthand ext', vimwiki#vars#get_wikilocal('ext'), '.txt') +call s:check('shorthand syntax', vimwiki#vars#get_wikilocal('syntax'), 'vimwiki') + +call writefile(s:out, $NUWIKI_VARS_OUT)