clipmenu-spmenu/tests/test-clipmenu
Chris Down f22fce7f04 Use a single line cache file
In c7c894a0, a per-selection line-cache was introduced in order to
overcome some of the limitations of clipmenu at the time (for example,
missing duplicate detection). However, now we have all the features we
need to have a single line cache again, and having multiple line caches
has caused more trouble than it is worth.

For example, maintaining CM_MAX_CLIPS globally is extremely cumbersome,
so we don't do it, and CM_MAX_CLIPS is actually acted on per-selection.
We also have had bugs where we perform actions on cache files without
properly consulting other line caches, and while those can be fixed, the
simplest thing to do now is just to go back to having a single line
cache.
2020-03-23 13:00:14 +00:00

94 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -x
set -e
set -o pipefail
: "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}"
major_version=6
dir=$CM_DIR/clipmenu.$major_version.$USER
cache_file=$dir/line_cache
if [[ $0 == /* ]]; then
location=${0%/*}
else
location=$PWD/${0#./}
location=${location%/*}
fi
cat - "$location/../clipmenu" > /tmp/clipmenu << 'EOF'
#!/usr/bin/env bash
shopt -s expand_aliases
shim() {
printf '%s args:' "$1" >&2
printf ' %q' "${@:2}" >&2
printf '\n' >&2
i=0
while IFS= read -r line; do
let i++
printf '%s line %d stdin: %s\n' "$1" "$i" "$line" >&2
done
if [[ -v SHIM_STDOUT ]]; then
printf '%s\n' "$SHIM_STDOUT"
fi
}
# Cannot be an alias due to expansion order with $CM_LAUNCHER
dmenu() {
SHIM_STDOUT="Selected text. (2 lines)" shim dmenu "$@"
}
rofi() {
SHIM_STDOUT="Selected text. (2 lines)" shim rofi "$@"
}
alias xsel='shim xsel'
alias xclip='shim xclip'
EOF
chmod a+x /tmp/clipmenu
rm -rf "$dir"
mkdir -p "$dir"
cat > "$cache_file" << 'EOF'
1234 Selected text. (2 lines)
1235 Selected text 2. (2 lines)
EOF
cat > "$dir/$(cksum <<< 'Selected text. (2 lines)')" << 'EOF'
Selected text.
Yes, it's selected text.
EOF
### TESTS ###
temp=$(mktemp)
/tmp/clipmenu --foo bar > "$temp" 2>&1
# Arguments are transparently passed to dmenu
grep -Fxq 'dmenu args: -l 8 --foo bar' "$temp"
# Output from cache file should get to dmenu, reversed
grep -Fxq 'dmenu line 1 stdin: Selected text 2. (2 lines)' "$temp"
grep -Fxq 'dmenu line 2 stdin: Selected text. (2 lines)' "$temp"
# xsel should copy both to clipboard *and* primary
grep -Fxq 'xsel args: --logfile /dev/null -i --clipboard' "$temp"
grep -Fxq 'xsel args: --logfile /dev/null -i --primary' "$temp"
grep -Fxq 'xsel line 1 stdin: Selected text.' "$temp"
grep -Fxq "xsel line 2 stdin: Yes, it's selected text." "$temp"
CM_LAUNCHER=rofi /tmp/clipmenu --foo bar > "$temp" 2>&1
# We have a special case to add -dmenu for rofi
grep -Fxq 'rofi args: -l 8 -dmenu --foo bar' "$temp"