feat: add plugins for zsh

This commit is contained in:
2024-09-22 20:14:22 +04:00
parent f4c1c54e72
commit 6c85cc3b71
515 changed files with 30838 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
#!/usr/bin/env zunit
@setup {
load "../auto-notify.plugin.zsh"
}
@test 'auto-notify-format no parameters' {
run _auto_notify_format "Hello World" "foo" "bar" "baz"
assert $state equals 0
assert "$output" same_as "Hello World"
}
@test 'auto-notify-format correct formatting 1' {
run _auto_notify_format "Command run is - %command (%elapsed seconds)" "zypper up" "10" "0"
assert $state equals 0
assert "$output" same_as "Command run is - zypper up (10 seconds)"
}
@test 'auto-notify-format correct formatting 2' {
run _auto_notify_format "Exit code is %exit_code!" "zypper up" "10" "-101"
assert $state equals 0
assert "$output" same_as "Exit code is -101!"
}

View File

@@ -0,0 +1,157 @@
#!/usr/bin/env zunit
@setup {
load "../auto-notify.plugin.zsh"
function notify-send {
echo - "Notification Title: $1"
echo - "Notification Body: $2"
echo - "${@:3}"
}
function uname {
echo - "Linux"
}
# Mock date function to return a frozen timestamp
function date {
if [[ "$1" == +"%s" ]]; then
echo - "11100"
else
date - "$@"
fi
}
}
@test 'auto-notify-send does not send notification if tracking not set' {
unset AUTO_COMMAND
unset AUTO_COMMAND_FULL
unset AUTO_COMMAND_START
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
}
@test 'auto-notify-send does not send notification for short task' {
AUTO_COMMAND="foo bar -r"
AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11099
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
}
@test 'auto-notify-send does not send notification for ignored commands' {
for command in $AUTO_NOTIFY_IGNORE; do
AUTO_COMMAND="somealias"
AUTO_COMMAND_FULL="$command"
AUTO_COMMAND_START=11000
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
done
}
@test 'auto-notify-send does not send notification for ignored piped commands' {
AUTO_COMMAND="echo hello world | less -r"
AUTO_COMMAND_FULL="echo hello world | less -r"
AUTO_COMMAND_START=11000
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
}
@test 'auto-notify-send does not send notification for ignored commands with arguments' {
for command in $AUTO_NOTIFY_IGNORE; do
AUTO_COMMAND="somealias bar -r"
AUTO_COMMAND_FULL="$command bar -r"
AUTO_COMMAND_START=11000
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
done
}
@test 'auto-notify-send sends notification on Linux' {
AUTO_COMMAND="f bar -r"
AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080
AUTO_NOTIFY_EXPIRE_TIME=15000
run _auto_notify_send
assert $state equals 0
assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed'
assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds"
assert "$lines[3]" same_as "Exit code: 0"
assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000"
}
@test 'auto-notify-send sends notification and icon on Linux on success' {
AUTO_COMMAND="f bar -r"
AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080
AUTO_NOTIFY_EXPIRE_TIME=15000
AUTO_NOTIFY_ICON_SUCCESS=/path/to/success/icon.png
run _auto_notify_send
assert $state equals 0
assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed'
assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds"
assert "$lines[3]" same_as "Exit code: 0"
assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000 --icon=/path/to/success/icon.png"
}
@test 'auto-notify-send sends notification on macOS' {
AUTO_COMMAND="f bar -r"
AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080
function uname {
echo - "Darwin"
}
function osascript {
echo - "${@}"
}
run _auto_notify_send
assert $state equals 0
assert "$lines[1]" same_as '-e on run argv -e display notification (item 1 of argv) with title (item 2 of argv) -e end run Total time: 20 seconds'
assert "$lines[2]" same_as 'Exit code: 0 "f bar -r" Completed'
}
@test 'auto-notify-send sends warning on unsupported platform' {
AUTO_COMMAND="f bar -r"
AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080
function uname {
echo - "Hal9000"
}
run _auto_notify_send
assert $state equals 0
assert "$lines[1]" same_as "Unknown platform for sending notifications: Hal9000"
assert "$lines[2]" same_as "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/"
}
@test 'auto-notify-send sends custom message' {
AUTO_COMMAND="doom -i"
AUTO_COMMAND_FULL="doom -i"
AUTO_COMMAND_START=11055
AUTO_NOTIFY_TITLE="%command has completed in %elapseds yo"
AUTO_NOTIFY_BODY="%command exited with code %exit_code"
run _auto_notify_send
assert $state equals 0
assert "$lines[1]" same_as 'Notification Title: doom -i has completed in 45s yo'
assert "$lines[2]" same_as "Notification Body: doom -i exited with code 0"
}

View File

@@ -0,0 +1,62 @@
#!/usr/bin/env zunit
@setup {
load "../auto-notify.plugin.zsh"
}
@test 'is_auto_notify_ignored - commands outside AUTO_NOTIFY_IGNORE' {
run _is_auto_notify_ignored "echo hello world"
assert $state equals 0
assert "$output" same_as "no"
}
@test 'is_auto_notify_ignored - all commands in AUTO_NOTIFY_IGNORE' {
for command in $AUTO_NOTIFY_IGNORE; do
run _is_auto_notify_ignored $command
assert $state equals 0
assert "$output" same_as "yes"
done
}
@test 'is_auto_notify_ignored - piped commands which are not ignored' {
run _is_auto_notify_ignored "echo hello world | base64 -e"
assert $state equals 0
assert "$output" same_as "no"
}
@test 'is_auto_notify_ignored - piped commands which are ignored' {
run _is_auto_notify_ignored "echo hello world | hexdump | less -R"
assert $state equals 0
assert "$output" same_as "yes"
}
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST disallowed' {
AUTO_NOTIFY_WHITELIST="foobar"
run _is_auto_notify_ignored "boom baz"
assert $state equals 0
assert "$output" same_as "yes"
}
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed' {
AUTO_NOTIFY_WHITELIST="foobar"
run _is_auto_notify_ignored "foobar baz"
assert $state equals 0
assert "$output" same_as "no"
}
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed with sudo' {
AUTO_NOTIFY_WHITELIST="foobar"
run _is_auto_notify_ignored "sudo foobar baz"
assert $state equals 0
assert "$output" same_as "no"
}

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env zunit
@setup {
function uname {
printf "Linux"
}
# Mock function for passing builds where we don't
# really need notify-send to be installed (e.g. Mac OSX)
function notify-send {
}
}
@test 'print warning if notify-send is not installed' {
function type {
return 1
}
run load "../auto-notify.plugin.zsh"
assert "$lines[1]" same_as "'notify-send' must be installed for zsh-auto-notify to work"
assert "$lines[2]" same_as "Please install it with your relevant package manager"
@test 'dont load auto-notify if notify-send is not installed' {
function type {
return 1
}
load "../auto-notify.plugin.zsh"
assert "_auto_notify_track" not_in $preexec_functions
assert "_auto_notify_send" not_in $precmd_functions
}
@test 'hook functions are loaded by default' {
load "../auto-notify.plugin.zsh"
assert '_auto_notify_track' in $preexec_functions
assert '_auto_notify_send' in $precmd_functions
}
@test 'enable/disable auto-notify' {
load "../auto-notify.plugin.zsh"
disable_auto_notify
assert '_auto_notify_track' not_in $preexec_functions
assert '_auto_notify_send' not_in $precmd_functions
enable_auto_notify
assert '_auto_notify_track' in $preexec_functions
assert '_auto_notify_send' in $precmd_functions
}