wfxr.forgit/tests/preview-context.test.sh
Wenxuan f7e2d436de
fix: use non-interactive pager in fzf preview for TUI pagers (#498)
When users configure a TUI/interactive diff pager (e.g., diffnav, tig)
via `git config pager.diff`, fzf preview panes show blank output because
TUI pagers require a TTY that fzf previews don't provide.

Add FORGIT_PREVIEW_PAGER env var that overrides the diff pager only in
fzf preview context. Preview detection relies on a FORGIT_IN_PREVIEW
marker set by a new `_forgit_preview` wrapper, so the override applies
only to preview commands and leaves fullscreen behavior unchanged.

Fixes #491
2026-03-14 10:38:44 +08:00

69 lines
1.7 KiB
Bash

#!/usr/bin/env bash
function set_up_before_script() {
source bin/git-forgit
FORGIT="bin/git-forgit"
# Ignore global git config files
export GIT_CONFIG_SYSTEM=/dev/null
export GIT_CONFIG_GLOBAL=/dev/null
cd "$(bashunit::temp_dir)" || return 1
git init --quiet
git config user.name "Test User"
git config user.email "test@example.com"
touch tracked.txt
git add tracked.txt
git commit -m "init" --quiet
}
function test_forgit_pager_uses_preview_pager_only_in_explicit_preview_context() {
local without_marker with_marker
without_marker=$(
FORGIT_DIFF_PAGER="printf diff" \
FORGIT_PREVIEW_PAGER="printf preview" \
FZF_PREVIEW_COLUMNS=80 \
_forgit_pager diff
)
with_marker=$(
FORGIT_DIFF_PAGER="printf diff" \
FORGIT_PREVIEW_PAGER="printf preview" \
FORGIT_IN_PREVIEW=1 \
_forgit_pager diff
)
assert_same "diff" "$without_marker"
assert_same "preview" "$with_marker"
}
function _forgit_capture_preview_flag() {
printf '%s' "${FORGIT_IN_PREVIEW:-unset}"
}
function test_forgit_preview_wrapper_marks_preview_context() {
local actual
actual=$(_forgit_preview capture_preview_flag 2>/dev/null || true)
assert_same "1" "$actual"
}
function test_forgit_diff_preview_command_uses_preview_wrapper() {
bashunit::mock "fzf" 'printf "%s" "$FZF_DEFAULT_OPTS"'
local output
output=$(_forgit_diff)
assert_contains "--preview=\"$FORGIT preview diff_view {}" "$output"
}
function test_forgit_show_preview_command_uses_preview_wrapper() {
bashunit::mock "fzf" 'printf "%s" "$FZF_DEFAULT_OPTS"'
local output
output=$(_forgit_show HEAD)
assert_contains "--preview=\"$FORGIT preview show_preview {}" "$output"
}