test-clipmenu (2008B)
1#!/usr/bin/env bash 2 3set -x 4set -e 5set -o pipefail 6 7dir=$(./clipctl cache-dir) 8cache_file=$dir/line_cache 9 10if [[ $0 == /* ]]; then 11 location=${0%/*} 12else 13 location=$PWD/${0#./} 14 location=${location%/*} 15fi 16 17cat - "$location/../clipmenu" > /tmp/clipmenu << 'EOF' 18#!/usr/bin/env bash 19 20shopt -s expand_aliases 21 22shim() { 23 printf '%s args:' "$1" >&2 24 printf ' %q' "${@:2}" >&2 25 printf '\n' >&2 26 27 i=0 28 29 while IFS= read -r line; do 30 let i++ 31 printf '%s line %d stdin: %s\n' "$1" "$i" "$line" >&2 32 done 33 34 if [[ -v SHIM_STDOUT ]]; then 35 printf '%s\n' "$SHIM_STDOUT" 36 fi 37} 38 39# Cannot be an alias due to expansion order with $CM_LAUNCHER 40dmenu() { 41 SHIM_STDOUT="Selected text. (2 lines)" shim dmenu "$@" 42} 43 44rofi() { 45 SHIM_STDOUT="Selected text. (2 lines)" shim rofi "$@" 46} 47 48alias xsel='shim xsel' 49alias xclip='shim xclip' 50alias clipctl='./clipctl' 51EOF 52 53chmod a+x /tmp/clipmenu 54 55rm -rf "$dir" 56mkdir -p "$dir" 57 58cat > "$cache_file" << 'EOF' 591234 Selected text. (2 lines) 601235 Selected text 2. (2 lines) 61EOF 62 63cat > "$dir/$(cksum <<< 'Selected text. (2 lines)')" << 'EOF' 64Selected text. 65Yes, it's selected text. 66EOF 67 68### TESTS ### 69 70temp=$(mktemp) 71 72trap 'cat "$temp"' EXIT 73 74/tmp/clipmenu --foo bar > "$temp" 2>&1 75 76# Arguments are transparently passed to dmenu 77grep -Fxq 'dmenu args: -l 8 --foo bar' "$temp" 78 79# Output from cache file should get to dmenu, reversed 80grep -Fxq 'dmenu line 1 stdin: Selected text 2. (2 lines)' "$temp" 81grep -Fxq 'dmenu line 2 stdin: Selected text. (2 lines)' "$temp" 82 83# xsel should copy both to clipboard *and* primary 84grep -Fxq 'xsel args: --logfile /dev/null -i --clipboard' "$temp" 85grep -Fxq 'xsel args: --logfile /dev/null -i --primary' "$temp" 86 87grep -Fxq 'xsel line 1 stdin: Selected text.' "$temp" 88grep -Fxq "xsel line 2 stdin: Yes, it's selected text." "$temp" 89 90CM_LAUNCHER=rofi /tmp/clipmenu --foo bar > "$temp" 2>&1 91 92# We have a special case to add -dmenu for rofi 93grep -Fxq 'rofi args: -l 8 -dmenu -p clipmenu --foo bar' "$temp"