mirror of
https://github.com/eth-p/bat-extras.git
synced 2024-12-04 21:40:44 +01:00
Add tests for pager library
This commit is contained in:
parent
3494c9fc2d
commit
7eac8b265d
96
lib/pager.sh
96
lib/pager.sh
@ -18,23 +18,18 @@ is_pager_disabled() {
|
||||
return $?
|
||||
}
|
||||
|
||||
# Gets the name of the pager command.
|
||||
# Prints the detected pager name.
|
||||
pager_name() {
|
||||
if [[ -z "${SCRIPT_PAGER_CMD[0]}" ]]; then return; fi
|
||||
if [[ -z "$_SCRIPT_PAGER_NAME" ]]; then
|
||||
local output
|
||||
output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)"
|
||||
|
||||
if head -n 1 <<<"$output" | grep '^less \d' &>/dev/null; then
|
||||
_SCRIPT_PAGER_NAME="less"
|
||||
else
|
||||
_SCRIPT_PAGER_NAME="$(basename "${SCRIPT_PAGER_CMD[0]}")"
|
||||
fi
|
||||
fi
|
||||
|
||||
_detect_pager
|
||||
echo "$_SCRIPT_PAGER_NAME"
|
||||
}
|
||||
|
||||
# Prints the detected pager version.
|
||||
pager_version() {
|
||||
_detect_pager
|
||||
echo "$_SCRIPT_PAGER_VERSION"
|
||||
}
|
||||
|
||||
# Executes a command or function, and pipes its output to the pager (if it exists).
|
||||
#
|
||||
# Returns: The exit code of the command.
|
||||
@ -66,25 +61,70 @@ pager_display() {
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Defaults.
|
||||
SCRIPT_PAGER_CMD=("$PAGER")
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
# Detect the pager information.
|
||||
# shellcheck disable=SC2120
|
||||
_detect_pager() {
|
||||
if [[ "$_SCRIPT_PAGER_DETECTED" = "true" && "$1" != "--force" ]]; then return; fi
|
||||
_SCRIPT_PAGER_DETECTED=true
|
||||
|
||||
# Add arguments for the less pager.
|
||||
if is_pager_less; then
|
||||
SCRIPT_PAGER_ARGS=(-R)
|
||||
fi
|
||||
# If the pager command is empty, the pager is disabled.
|
||||
if [[ -z "${SCRIPT_PAGER_CMD[0]}" ]]; then
|
||||
_SCRIPT_PAGER_VERSION=0
|
||||
_SCRIPT_PAGER_NAME=""
|
||||
return;
|
||||
fi
|
||||
|
||||
# Prefer the bat pager.
|
||||
if [[ -n "${BAT_PAGER+x}" ]]; then
|
||||
# [note]: This is intentional.
|
||||
# shellcheck disable=SC2206
|
||||
SCRIPT_PAGER_CMD=($BAT_PAGER)
|
||||
# Determine the pager name and version.
|
||||
local output
|
||||
local output1
|
||||
output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)"
|
||||
output1="$(head -n 1 <<<"$output")"
|
||||
|
||||
if [[ "$output1" =~ ^less[[:blank:]]([[:digit:]]+) ]]; then
|
||||
# shellcheck disable=SC2001
|
||||
_SCRIPT_PAGER_VERSION="${BASH_REMATCH[1]}"
|
||||
_SCRIPT_PAGER_NAME="less"
|
||||
else
|
||||
_SCRIPT_PAGER_VERSION=0
|
||||
_SCRIPT_PAGER_NAME="$(basename "${SCRIPT_PAGER_CMD[0]}")"
|
||||
fi
|
||||
}
|
||||
|
||||
# Configure the script pager.
|
||||
# This attempts to mimic how bat determines the pager and pager arguments.
|
||||
#
|
||||
# 1. Use BAT_PAGER
|
||||
# 2. Use PAGER with special arguments for less
|
||||
# 3. Use PAGER
|
||||
_configure_pager() {
|
||||
SCRIPT_PAGER_CMD=("$PAGER")
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
fi
|
||||
|
||||
# Prefer no pager if not a tty.
|
||||
if ! [[ -t 1 ]]; then
|
||||
# Prefer the bat pager.
|
||||
if [[ -n "${BAT_PAGER+x}" ]]; then
|
||||
# [note]: This is intentional.
|
||||
# shellcheck disable=SC2206
|
||||
SCRIPT_PAGER_CMD=($BAT_PAGER)
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
return
|
||||
fi
|
||||
|
||||
# Add arguments for the less pager.
|
||||
if is_pager_less; then
|
||||
SCRIPT_PAGER_ARGS=(-R --quit-if-one-screen)
|
||||
if [[ "$(pager_version)" -lt 500 ]]; then
|
||||
SCRIPT_PAGER_ARGS+=(--no-init)
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
if [[ -t 1 ]]; then
|
||||
# Detect and choose the arguments for the pager.
|
||||
_configure_pager
|
||||
else
|
||||
# Prefer no pager if not a tty.
|
||||
SCRIPT_PAGER_CMD=()
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
fi
|
||||
|
15
test/shimexec/less
Executable file
15
test/shimexec/less
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
if [[ "$1" = "--version" ]]; then
|
||||
cat <<EOF
|
||||
less ${MOCK_LESS_VERSION:-551} (PCRE regular expressions)
|
||||
Copyright (C) 1984-2019 Mark Nudelman
|
||||
|
||||
less comes with NO WARRANTY, to the extent permitted by law.
|
||||
For information about the terms of redistribution,
|
||||
see the file named README in the less distribution.
|
||||
Home page: http://www.greenwoodsoftware.com/less
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
|
||||
cat "$1"
|
2
test/shimexec/less_but_renamed
Executable file
2
test/shimexec/less_but_renamed
Executable file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
exec less "$@"
|
10
test/shimexec/not_less
Executable file
10
test/shimexec/not_less
Executable file
@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
if [[ "$1" = "--version" ]]; then
|
||||
cat <<EOF
|
||||
not less ${MOCK_LESS_VERSION:-551} (PCRE regular expressions)
|
||||
:(
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
|
||||
cat "$1"
|
74
test/suite/lib_pager.sh
Normal file
74
test/suite/lib_pager.sh
Normal file
@ -0,0 +1,74 @@
|
||||
setup() {
|
||||
source "${LIB}/pager.sh"
|
||||
}
|
||||
|
||||
use_pager() {
|
||||
unset BAT_PAGER
|
||||
export PAGER="$1"
|
||||
|
||||
SCRIPT_PAGER_CMD=("$PAGER")
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
|
||||
_detect_pager --force
|
||||
_configure_pager
|
||||
}
|
||||
|
||||
use_bat_pager() {
|
||||
unset PAGER
|
||||
export BAT_PAGER="$1"
|
||||
|
||||
SCRIPT_PAGER_CMD=($BAT_PAGER)
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
|
||||
_detect_pager --force
|
||||
_configure_pager
|
||||
}
|
||||
|
||||
test:less_detection() {
|
||||
description "Identify less"
|
||||
|
||||
use_pager "less" && expect_equal "$(pager_name)" "less"
|
||||
use_pager "less_but_renamed" && expect_equal "$(pager_name)" "less"
|
||||
use_pager "tput" && expect_equal "$(pager_name)" "tput"
|
||||
}
|
||||
|
||||
test:less_version() {
|
||||
description "Identify less version"
|
||||
|
||||
export MOCK_LESS_VERSION=473
|
||||
use_pager "less" && expect_equal "$(pager_version)" "473"
|
||||
|
||||
export MOCK_LESS_VERSION=551
|
||||
use_pager "less" && expect_equal "$(pager_version)" "551"
|
||||
}
|
||||
|
||||
test:less_args() {
|
||||
description "Automatically select appropriate less args"
|
||||
|
||||
export MOCK_LESS_VERSION=473
|
||||
use_pager "less"
|
||||
expect array_contains "-R" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
expect array_contains "--quit-if-one-screen" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
expect array_contains "--no-init" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
|
||||
export MOCK_LESS_VERSION=551
|
||||
use_pager "less"
|
||||
expect array_contains "-R" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
expect array_contains "--quit-if-one-screen" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
expect ! array_contains "--no-init" in "${SCRIPT_PAGER_ARGS[@]}"
|
||||
|
||||
use_pager "not_less"
|
||||
expect_equal "${#SCRIPT_PAGER_ARGS[@]}" 0
|
||||
}
|
||||
|
||||
test:env_bat_pager() {
|
||||
description "Check that BAT_PAGER is being used"
|
||||
|
||||
use_bat_pager "not_less"
|
||||
expect_equal "$SCRIPT_PAGER_CMD" "not_less"
|
||||
|
||||
use_bat_pager "not_less but not_more"
|
||||
expect_equal "$SCRIPT_PAGER_CMD" "not_less"
|
||||
expect_equal "${SCRIPT_PAGER_CMD[1]}" "but"
|
||||
expect_equal "${SCRIPT_PAGER_CMD[2]}" "not_more"
|
||||
}
|
Loading…
Reference in New Issue
Block a user