wren-linux / tools / test-kanshi.sh
#!/bin/bash
# Checks extras/kanshi.sh with a pretend sudo and apt, in a temp home: the
# config it writes (a monitor plugged in: only the monitor), never replacing
# yours, sway starting kanshi, and --remove. Nothing outside a temp folder changes.
# Run by test.sh, or: bash tools/test-kanshi.sh
# shellcheck disable=SC2016 # the inner shells and pretend programs expand their own variables
HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
T=$(mktemp -d) || { echo " FAIL couldn't make a temp folder (is /tmp full?)"; exit 1; }
trap 'rm -rf "$T"' EXIT
fails=0
check() { # DESCRIPTION CONDITION-COMMAND...
local what=$1
shift
if "$@" >/dev/null 2>&1; then printf ' ok %s\n' "$what"; else printf ' FAIL %s\n' "$what"; fails=$((fails + 1)); fi
}
mkdir -p "$T/bin" "$T/home"
printf '#!/bin/sh\n[ "$1" = -v ] && exit 0\necho "sudo $*" >>"%s/calls"\n' "$T" >"$T/bin/sudo"
printf '#!/bin/sh\ncase "$*" in *Status*kanshi*) exit 1 ;; *Status*) echo "install ok installed" ;; esac\n' >"$T/bin/dpkg-query"
chmod +x "$T/bin/"*
R=$T/r
mkdir -p "$R/extras" "$R/lib"
sed 's|^export PATH=.*|export PATH="$KN_TEST_PATH"|' "$HERE/lib.sh" >"$R/lib.sh"
grep -q KN_TEST_PATH "$R/lib.sh" || { echo " FAIL lib.sh's PATH line wasn't found"; exit 1; }
for f in pins.sh packages.sh data bin docs; do ln -s "$HERE/$f" "$R/$f"; done
for f in "$HERE"/lib/*; do ln -s "$f" "$R/lib/${f##*/}"; done
cp "$HERE/extras/kanshi.sh" "$R/extras/"
export KN_TEST_PATH="$T/bin:$PATH"
kn() { env -u SWAYSOCK HOME="$T/home" XDG_CONFIG_HOME= bash "$R/extras/kanshi.sh" "$@" 2>&1; }
C=$T/home/.config/kanshi/config
S=$T/home/.config/sway/config.d/kanshi.conf
out=$(kn)
check "it installs kanshi" grep -q 'apt-get.* install .*kanshi' "$T/calls"
check "...and writes the config" cmp "$HERE/data/kanshi-config" "$C"
check "...where the laptop alone uses its own screen" bash -c 'grep -A2 "^profile laptop {" "$1" | grep -qx " output eDP-1 enable"' _ "$C"
check "...and with a monitor plugged in only the monitor is on" bash -c '
m=$(sed -n "/^profile monitor {/,/^}/p" "$1"); grep -qx " output eDP-1 disable" <<<"$m" && grep -qx " output \* enable" <<<"$m"' _ "$C"
check "...and no profile turns every screen off (each enables one)" bash -c '
awk "/^profile/{p=1;e=0} /enable/{e=1} /^}/{if(p&&!e)bad=1;p=0} END{exit bad}" "$1"' _ "$C"
# (a sway reload turns every screen on again, and kanshi only looks when one is
# plugged in or out: so it starts again on each reload, and puts its profile back)
check "sway starts kanshi at login, and again at every reload (one kanshi, the profile put back)" grep -qxF "exec_always sh -c 'pkill -x -u \"\$(id -u)\" kanshi; exec kanshi'" "$S"
echo '# mine' >"$C"
kn >/dev/null
check "run again: your own config is never replaced" grep -qx '# mine' "$C"
kn --remove >/dev/null
check "--remove: sway's line goes, and kanshi with it (apt_purge, as the other extras)" bash -c '
[ ! -e "$1" ] && sed -n "/--remove ]/,/^fi/p" "$2" | grep -qx " apt_purge kanshi"' _ "$S" "$HERE/extras/kanshi.sh"
check "...and your config stays" grep -qx '# mine' "$C"
check "its manual page has the same config as the file" bash -c '
printf "%s\n" "profile laptop {" " output eDP-1 enable" "}" "profile monitor {" " output eDP-1 disable" " output * enable" "}" >"$2"
grep -vE "^(#|$)" "$1" | cmp - "$2" && grep -qF "'"'"'profile laptop {'"'"' '"'"' output eDP-1 enable'"'"'" "$3"' _ "$HERE/data/kanshi-config" "$T/expect" "$HERE/docs/extras/kanshi.md"
[ "$fails" -eq 0 ]