Compare commits

...

72 commits

Author SHA1 Message Date
f6c1f7fa35 auto-notify.plugin.zsh aktualisiert 2024-09-16 22:58:04 +02:00
86deef4ded auto-notify.plugin.zsh aktualisiert 2024-09-16 20:39:21 +02:00
81f5161390 auto-notify.plugin.zsh aktualisiert 2024-09-16 20:37:27 +02:00
e9ca099379 auto-notify.plugin.zsh aktualisiert 2024-09-16 20:35:51 +02:00
0a2b2f4601 auto-notify.plugin.zsh aktualisiert 2024-09-16 20:30:57 +02:00
8a328a37bd auto-notify.plugin.zsh aktualisiert 2024-09-16 20:30:05 +02:00
63f70f3c33 auto-notify.plugin.zsh aktualisiert
Changed Plugin to use ntfy instead of notify-send, to send events to phone.

Signed-off-by: eeleater <nikolasweger@googlemail.com>
2024-09-16 20:20:39 +02:00
Michael Aquilina
27c07dddb4
Merge pull request #63 from Grafcube/ignore-130
Treat code 130 as success
2024-07-08 16:43:31 +01:00
Grafcube
76a527a66b
Add a comment explaining why 130 is treated as success 2024-07-08 19:53:44 +05:30
Grafcube
a79a412f3a
Treat code 130 as success 2024-06-16 00:21:26 +05:30
Michael Aquilina
837b81de92
bump version to 0.10.2 2024-05-13 11:12:38 +01:00
Michael Aquilina
6e5a54c5b5
use array argument expansion 2024-05-13 11:11:51 +01:00
Michael Aquilina
43dbc4eb56
bump version to 0.10.1 2024-05-13 10:54:40 +01:00
Michael Aquilina
055a5fa8ae
Merge pull request #61 from MichaelAquilina/fix/bad-icon-path
fix: correctly omit --icon from notify-send when not set
2024-05-13 11:53:56 +02:00
Michael Aquilina
d41c04f778
remove flakey test 2024-05-13 10:51:50 +01:00
Michael Aquilina
0e4ed6b3c5
fix: correctly omit --icon from notify-send when not set 2024-05-13 10:48:41 +01:00
Michael Aquilina
452eee9454
bump version to 0.10.0 2024-04-24 13:25:36 +01:00
Michael Aquilina
4eb63b7c1f
Merge pull request #56 from CaderIdris/add-icon-support
Add Icon Support to Linux
2024-04-24 13:24:21 +01:00
61825162+CaderIdris@users.noreply.github.com
8d61376a95
Fixed version number
Modified: Version number so ci tests pass
2024-04-23 12:51:59 +01:00
61825162+CaderIdris@users.noreply.github.com
a663a08bca
Bugfix: Order of arguments passed unit tests but caused invalid parsing
of expire time argument

Modified: How icon argument is called
2024-04-23 12:04:05 +01:00
61825162+CaderIdris@users.noreply.github.com
f405233080
Added tests and updated README
Added: Test for running notify-send when declaring success icon
Added: Config information to README

Note: With the way the tests are currently structured, I couldn't find a
way to test the failure icon. I'm not an expert in zunit so there may
be a way that I can't find
2024-04-23 11:50:34 +01:00
61825162+CaderIdris@users.noreply.github.com
2c0059707f
Added icon support for linux
Added: AUTO_NOTIFY_ICON_SUCCESS environmental variable, represents path
to icon for notify-send to display on command success
Added: AUTO_NOTIFY_ICON_FAILURE environmental variable, represents path
to icon for notify-send to display on command failure
Added: --icon option when icon path is present for notify-send, no
additional argument added if environmental variable not specified
Modified: Version number to pass test
2024-04-23 11:15:09 +01:00
Michael Aquilina
f4766d6981
Merge pull request #53 from NoUseFreak/patch-1
Mute notifications over ssh
2024-02-08 15:33:32 +00:00
Dries De Peuter
f4afef6fa9
fix: mute notifications over ssh 2023-12-27 23:01:21 +01:00
Michael Aquilina
22b2c61ed1
fix: transient test case 2023-06-02 14:01:43 +01:00
Michael Aquilina
9b6bc124f0
fix: backwards compatible transient flag 2023-06-02 13:55:02 +01:00
Michael Aquilina
dad1e61c71
Merge pull request #45 from Grafcube/fix-transient
Fix transient argument
2023-05-11 15:24:59 +01:00
Michael Aquilina
993f5de86e
fix: update tests to include --transient flag 2023-05-11 15:23:14 +01:00
Michael Aquilina
00951ac085
build: update docker job image and install libnotify-bin 2023-05-11 15:15:01 +01:00
Grafcube
5a50536e59
Fix transient argument
The transient variable must not be quoted. Otherwise the command fails
with `Invalid number of options.` for unsuccessful error codes.
2023-05-11 17:02:14 +05:30
Michael Aquilina
71e8da986f
Merge pull request #41 from Grafcube/transient-on-success
Make notifications transient if successful
2023-04-27 16:47:46 +01:00
Grafcube
9a9f8f21bb
Quote transient 2023-04-27 21:12:11 +05:30
Grafcube
8418a6e3b2
Make notifications transient if successful 2023-04-04 12:38:28 +05:30
Michael Aquilina
1f64cb6544
Merge pull request #38 from brendanfalk/fig
Add Fig as an installation method to the README
2022-06-29 09:42:29 +01:00
Brendan Falk
b15ae822e6
Update README.rst 2022-06-28 10:44:09 -07:00
Brendan Falk
5060c0757f
Add Fig as an installation method to the README 2022-06-27 17:12:45 -07:00
Michael Aquilina
30c3c60939
Merge pull request #36 from spikespaz/master
Fix the environment variables being reset upon sourcing the plugin
2022-02-21 10:28:22 +00:00
Jacob Birkett
86849ede2a Fix config being overwritten when source 2022-01-27 18:05:20 -07:00
Michael Aquilina
fb38802d33
Merge pull request #25 from MichaelAquilina/develop
Release 0.8.0
2019-09-06 10:00:50 +01:00
Michael Aquilina
f8cd00f2b6
Bump version to 0.8.0 2019-09-06 09:58:23 +01:00
Michael Aquilina
47a0b1727f
Merge pull request #23 from Avi-D-coder/app-name
Notify app-name=zsh
2019-09-06 09:57:07 +01:00
Avi Dessauer
68fed1af8b Notify app-name=zsh 2019-09-05 18:27:38 -04:00
Michael Aquilina
8ab1d0d8d2
Merge pull request #22 from MichaelAquilina/next
Release 0.7.0
2019-08-26 14:29:51 +03:00
Michael Aquilina
67df6534c0
Bump version to 0.7.0 2019-08-26 12:28:13 +01:00
Michael Aquilina
2b46020293
Merge pull request #21 from MichaelAquilina/AUTO_NOTIFY_WHITELIST
Add support for specifying a WHITELIST
2019-08-26 14:27:01 +03:00
Michael Aquilina
cbd4e2bcac
Remove sudo prefix from command if detected 2019-08-26 12:25:43 +01:00
Michael Aquilina
a77777987d
Add support for specifying a WHITELIST 2019-08-24 21:36:35 +01:00
Michael Aquilina
8aa4c42932
Merge pull request #19 from MichaelAquilina/next
Release 0.6.0
2019-08-17 18:54:14 +03:00
Michael Aquilina
0b7a039689
Bump version to 0.6.0 2019-08-17 16:50:58 +01:00
Michael Aquilina
7f7027034c
Merge pull request #18 from MichaelAquilina/print_warning_missing_dep
Print warning if notify-send is a missing dependency
2019-08-17 18:49:47 +03:00
Michael Aquilina
5c39901fc7
Print warning if notify-send is a missing dependency 2019-08-17 16:46:36 +01:00
Michael Aquilina
4668a7fbd4
Merge pull request #17 from MichaelAquilina/next
Release 0.5.1
2019-08-01 13:41:22 +01:00
Michael Aquilina
c029e8e4c1
Bump to version 0.5.1 2019-08-01 13:40:03 +01:00
Michael Aquilina
83b7646472
Merge pull request #16 from dmitmel/master
Improve notifications on macOS
2019-08-01 13:38:53 +01:00
Dmytro Meleshko
bada173f10 Change MacOSX to macOS 2019-08-01 11:40:17 +03:00
Dmytro Meleshko
73da2d107e Fix tests 2019-08-01 11:39:33 +03:00
Dmytro Meleshko
4a9e4b8e03 Improve notifications on macOS 2019-08-01 11:25:31 +03:00
Michael Aquilina
2292476f93
Fix minor bug in _is_auto_notify_ignored 2019-07-31 15:53:33 +01:00
Michael Aquilina
bad7321d4a
Merge pull request #15 from MichaelAquilina/next
Release 0.5.0
2019-07-31 13:41:40 +01:00
Michael Aquilina
d039eea61b
Fix to README for notification formatting 2019-07-31 13:40:37 +01:00
Michael Aquilina
7721d45abe
Bump version to 0.5.0 2019-07-31 13:37:47 +01:00
Michael Aquilina
aed7176e73
Add support for custom message formats 2019-07-31 13:37:42 +01:00
Michael Aquilina
eaf5936f1e
Merge pull request #14 from MichaelAquilina/next
Release 0.4.0
2019-07-21 22:00:45 +01:00
Michael Aquilina
87d1e9bd0c
Test configurable expiry time 2019-07-21 21:59:14 +01:00
Michael Aquilina
9596b87899
Bump version to 0.4.0 2019-07-21 21:56:02 +01:00
Michael Aquilina
831c0b0b00
Add entry in README about AUTO_NOTIFY_EXPIRE_TIME 2019-07-21 21:53:43 +01:00
Michael Aquilina
cac8315195
Improvements to notification formatting and options 2019-07-21 21:50:45 +01:00
Michael Aquilina
7b6d59479a
Rename main test file to test_plugin.zunit 2019-07-20 21:34:17 +01:00
Michael Aquilina
e25849a979
Move auto_notify_send tests to their own file 2019-07-20 19:17:30 +01:00
Michael Aquilina
61a838cbcc
Remove note about git backup during tests as it does not apply 2019-07-19 13:36:16 +01:00
Michael Aquilina
1eb37d2a99
Add ISSUE_TEMPLATE 2019-07-18 10:38:10 +01:00
Michael Aquilina
8b0ae499a7
Merge pull request #12 from MichaelAquilina/next
Release 0.3.0
2019-07-18 10:35:00 +01:00
9 changed files with 367 additions and 66 deletions

View file

@ -6,7 +6,7 @@ test: &test
- checkout - checkout
- run: - run:
name: Install Dependencies name: Install Dependencies
command: apt-get update && apt-get install -y curl git ncurses-bin command: apt-get update && apt-get install -y curl git ncurses-bin libnotify-bin
- run: - run:
name: Download requirements name: Download requirements
command: | command: |
@ -25,7 +25,7 @@ test: &test
jobs: jobs:
lint: lint:
docker: docker:
- image: "python:3.7-stretch" - image: "python:3.11"
steps: steps:
- checkout - checkout
- run: - run:

View file

@ -1,6 +1,45 @@
Changelog for zsh-auto-notify Changelog for zsh-auto-notify
============================= =============================
0.10.2
------
* Use preferable array argument expansion for flexible parameters
0.10.1
------
* Fix regression where not setting icon on Linux would cause issues (#59 #58)
0.10.0
-----
* Allow specifying an icon with notify-send backends
0.8.0
-----
* Change notify-send application title to `zsh`
0.7.0
-----
* Allow alternate `AUTO_NOTIFY_WHITELIST` for specifying commands to allow
0.6.0
-----
* Display warning and disable auto-notify if notify-send is not installed (Linux only)
0.5.1
-----
* Improved handling of MacOS notifications via #16 (Thanks @dmitmel!)
0.5.0
-----
* Support changing notification title and body using AUTO_NOTIFY_TITLE and AUTO_NOTIFY_BODY
0.4.0
-----
* Add `AUTO_NOTIFY_EXPIRE_TIME` configuration option
* Improvements to notification formatting
* Exit code is now displayed in notifications
* Notifications on linux now show as critical if long command exits with non-zero exit code
0.3.0 0.3.0
----- -----
* Add support for environments where standard history is disabled. Fixed in #10 * Add support for environments where standard history is disabled. Fixed in #10

20
ISSUE_TEMPLATE.md Normal file
View file

@ -0,0 +1,20 @@
# Issue Details
Please provide the following details when opening an issue:
## Operating System (uname -a)
## zsh version (zsh --version)
## auto-notify version (echo "$AUTO_NOTIFY_VERSION")
## How is auto-notify installed?
- [ ] zplug
- [ ] oh-my-zsh
- [ ] Antigen
- [ ] Other (please specify)
## Steps to reproduce the issue
## gist link to your zshrc

View file

@ -64,6 +64,14 @@ Zgen_
zgen load "MichaelAquilina/zsh-auto-notify" zgen load "MichaelAquilina/zsh-auto-notify"
Fig_
Install ``zsh-auto-notify`` with Fig in just one click.
.. image:: https://fig.io/badges/install-with-fig.svg
:target: https://fig.io/plugins/other/zsh-auto-notify_MichaelAquilina
:alt: Install with Fig
oh-my-zsh_ oh-my-zsh_
Copy this repository to ``$ZSH_CUSTOM/custom/plugins``, where ``$ZSH_CUSTOM`` Copy this repository to ``$ZSH_CUSTOM/custom/plugins``, where ``$ZSH_CUSTOM``
@ -93,6 +101,34 @@ can configure this value by setting the environment variable ``AUTO_NOTIFY_THRES
# Set threshold to 20seconds # Set threshold to 20seconds
export AUTO_NOTIFY_THRESHOLD=20 export AUTO_NOTIFY_THRESHOLD=20
**Notification Formatting**
You can change the formatting of notifications by setting the values for ``AUTO_NOTIFY_TITLE`` and
``AUTO_NOTIFY_BODY``. When writing these values, the following variables will be replaced according to
the data that ``auto-notify`` has detected:
* ``%command`` - the command that the user executed
* ``%elapsed`` - number of seconds that elapsed
* ``%exit_code`` - the exit code of the command that was executed
An example of how these values can be set is shown below:
::
export AUTO_NOTIFY_TITLE="Hey! %command has just finished"
export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code"
**Notification Expiration Time**
You can set how long a notification sent by ``auto-notify`` will remain showing by setting the environment
variable ``AUTO_NOTIFY_EXPIRE_TIME`` to a custom value in milliseconds. The default value is set to 8 seconds.
NOTE: This configuration option currently only works for Linux.
::
# Set notification expiry to 10 seconds
export AUTO_NOTIFY_EXPIRE_TIME=10000
**Ignored Commands** **Ignored Commands**
@ -115,6 +151,28 @@ a new array.
# redefine what is ignored by auto-notify # redefine what is ignored by auto-notify
export AUTO_NOTIFY_IGNORE=("docker" "man" "sleep") export AUTO_NOTIFY_IGNORE=("docker" "man" "sleep")
**Using a Whitelist to ignore commands**
If you wish to use a whitelist approach instead of the default blacklist approach used by ``AUTO_NOTIFY_IGNORE``,
you can do so by defining the environment variable ``AUTO_NOTIFY_WHITELIST`` with the elements you wish to
allow ``auto-notify`` to track and send notifications for. NOTE: If ``AUTO_NOTIFY_WHITELIST`` is defined,
then all the values in ``AUTO_NOTIFY_IGNORE`` are not used.
::
export AUTO_NOTIFY_WHITELIST=("apt-get" "docker")
**Adding an icon - Linux**
If you wish to have an icon displayed on command success and/or failure, you can do so by defining the environmental variables ``AUTO_NOTIFY_ICON_SUCCESS`` and ``AUTO_NOTIFY_ICON_FAILURE`` respectively.
::
export AUTO_NOTIFY_ICON_SUCCESS=/path/to/success/icon.png
export AUTO_NOTIFY_ICON_FAILURE=/path/to/failure/icon.png
Temporarily Disabling Notifications Temporarily Disabling Notifications
----------------------------------- -----------------------------------
@ -157,19 +215,14 @@ directory of the repo.
NOTE: It is required that you use a minimum zunit version of 0.8.2 NOTE: It is required that you use a minimum zunit version of 0.8.2
NOTE: The tests that run move your git configuration to a temporary
location during the test process so that your user configuration does
not interfere with tests. This will be restored at the end of each test
so there is no risk in losing your settings. If for some strange reason
zunit crashes or does not restore your git configuration automatically,
all you need to do is run ``mv ~/.gitconfig.bak ~/.gitconfig``
.. _Zplug: https://github.com/zplug/zplug .. _Zplug: https://github.com/zplug/zplug
.. _Antigen: https://github.com/zsh-users/antigen .. _Antigen: https://github.com/zsh-users/antigen
.. _ZGen: https://github.com/tarjoilija/zgen .. _ZGen: https://github.com/tarjoilija/zgen
.. _Fig: https://fig.io
.. _oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh .. _oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh
.. |GPLv3| image:: https://img.shields.io/badge/License-GPL%20v3-blue.svg .. |GPLv3| image:: https://img.shields.io/badge/License-GPL%20v3-blue.svg

View file

@ -1,29 +1,73 @@
export AUTO_NOTIFY_VERSION="0.3.0" export AUTO_NOTIFY_VERSION="0.10.2"
# Time it takes for a notification to expire
[[ -z "$AUTO_NOTIFY_EXPIRE_TIME" ]] &&
export AUTO_NOTIFY_EXPIRE_TIME=8000
# Threshold in seconds for when to automatically show a notification # Threshold in seconds for when to automatically show a notification
export AUTO_NOTIFY_THRESHOLD=10 [[ -z "$AUTO_NOTIFY_THRESHOLD" ]] &&
export AUTO_NOTIFY_THRESHOLD=10
[[ -z "$AUTO_NOTIFY_PUBLISH" ]] &&
export AUTO_NOTIFY_PUBLISH="zsh_events"
# List of commands/programs to ignore sending notifications for # List of commands/programs to ignore sending notifications for
export AUTO_NOTIFY_IGNORE=( [[ -z "$AUTO_NOTIFY_IGNORE" ]] &&
"vim" "nvim" "less" "more" "man" "tig" "watch" "git commit" "top" "htop" "ssh" "nano" export AUTO_NOTIFY_IGNORE=(
) 'vim'
'nvim'
'less'
'more'
'man'
'tig'
'watch'
'git commit'
'top'
'htop'
'ssh'
'nano'
)
function _auto_notify_format() {
local MESSAGE="$1"
local command="$2"
local elapsed="$3"
local exit_code="$4"
MESSAGE="${MESSAGE//\%command/$command}"
MESSAGE="${MESSAGE//\%elapsed/$elapsed}"
MESSAGE="${MESSAGE//\%exit_code/$exit_code}"
printf "%s" "$MESSAGE"
}
function _auto_notify_message() { function _auto_notify_message() {
local command="$1" local command="$1"
local elapsed="$2" local elapsed="$2"
local exit_code="$3"
local platform="$(uname)" local platform="$(uname)"
# Run using echo -e in order to make sure notify-send picks up new line # Run using echo -e in order to make sure notify-send picks up new line
local text="$(echo -e "\"$command\" has completed\n(Total time: $elapsed seconds)")" local DEFAULT_TITLE="ZSH: '%command' finished"
local DEFAULT_BODY="$(echo -e "Time: %elapsed seconds elapsed\nExit code: %exit_code")"
local DEFAULT_PUBLISH="zsh_events"
if [[ "$platform" == "Linux" ]]; then local title="${AUTO_NOTIFY_TITLE:-$DEFAULT_TITLE}"
notify-send "$text" local text="${AUTO_NOTIFY_BODY:-$DEFAULT_BODY}"
elif [[ "$platform" == "Darwin" ]]; then local publish="${AUTO_NOTIFY_PUBLISH:-$DEFAULT_PUBLISH}"
# We need to escape quotes since we are passing a script into a command
text="${text//\"/\\\"}" title="$(_auto_notify_format "$title" "$command" "$elapsed" "$exit_code")"
osascript -e "display notification \"$text\" with title \"Command Completed\"" body="$(_auto_notify_format "$text" "$command" "$elapsed" "$exit_code")"
else
printf "Unknown platform for sending notifications: $platform\n" local urgency="3"
printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n" local icon=${AUTO_NOTIFY_ICON_SUCCESS:-""}
# Exit code 130 is returned when a process is terminated with SIGINT.
# Since the user is already interacting with the program, there is no
# need to make the notification persistent.
if [[ "$exit_code" != "0" ]] && [[ "$exit_code" != "130" ]]; then
urgency="4"
icon=${AUTO_NOTIFY_ICON_FAILURE:-""}
fi fi
local arguments=("--title='$title'" "--priority=$urgency" $publish "$body")
ntfy publish -q ${arguments[@]}
} }
function _is_auto_notify_ignored() { function _is_auto_notify_ignored() {
@ -32,18 +76,47 @@ function _is_auto_notify_ignored() {
local command_list=("${(@s/|/)command}") local command_list=("${(@s/|/)command}")
local target_command="${command_list[-1]}" local target_command="${command_list[-1]}"
# Remove leading whitespace # Remove leading whitespace
target_command="$(echo "$target_command" | sed -e 's/^ *//')}" target_command="$(echo "$target_command" | sed -e 's/^ *//')"
for ignore in $AUTO_NOTIFY_IGNORE; do # If the command is being run over SSH, then ignore it
if [[ "$target_command" == "$ignore"* ]]; then if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]]; then
print "yes" print "yes"
return return
fi fi
done
print "no" # Remove sudo prefix from command if detected
if [[ "$target_command" == "sudo "* ]]; then
target_command="${target_command/sudo /}"
fi
# If AUTO_NOTIFY_WHITELIST is defined, then auto-notify will ignore
# any item not defined in the white list
# Otherwise - the alternative (default) approach is used where the
# AUTO_NOTIFY_IGNORE blacklist is used to ignore commands
if [[ -n "$AUTO_NOTIFY_WHITELIST" ]]; then
for allowed in $AUTO_NOTIFY_WHITELIST; do
if [[ "$target_command" == "$allowed"* ]]; then
print "no"
return
fi
done
print "yes"
else
for ignore in $AUTO_NOTIFY_IGNORE; do
if [[ "$target_command" == "$ignore"* ]]; then
print "yes"
return
fi
done
print "no"
fi
} }
function _auto_notify_send() { function _auto_notify_send() {
# Immediately store the exit code before it goes away
local exit_code="$?"
if [[ -z "$AUTO_COMMAND" && -z "$AUTO_COMMAND_START" ]]; then if [[ -z "$AUTO_COMMAND" && -z "$AUTO_COMMAND_START" ]]; then
return return
fi fi
@ -53,7 +126,7 @@ function _auto_notify_send() {
let "elapsed = current - AUTO_COMMAND_START" let "elapsed = current - AUTO_COMMAND_START"
if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then
_auto_notify_message "$AUTO_COMMAND" "$elapsed" _auto_notify_message "$AUTO_COMMAND" "$elapsed" "$exit_code"
fi fi
fi fi
@ -86,11 +159,11 @@ function disable_auto_notify() {
} }
function enable_auto_notify() { function enable_auto_notify() {
autoload -Uz add-zsh-hook
add-zsh-hook preexec _auto_notify_track add-zsh-hook preexec _auto_notify_track
add-zsh-hook precmd _auto_notify_send add-zsh-hook precmd _auto_notify_send
} }
autoload -Uz add-zsh-hook
_auto_notify_reset_tracking _auto_notify_reset_tracking
enable_auto_notify
enable_auto_notify

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

@ -4,7 +4,9 @@
load "../auto-notify.plugin.zsh" load "../auto-notify.plugin.zsh"
function notify-send { function notify-send {
echo - "Notification: $@" echo - "Notification Title: $1"
echo - "Notification Body: $2"
echo - "${@:3}"
} }
function uname { function uname {
@ -21,29 +23,6 @@
} }
} }
@test 'version exported' {
git_version="$(git tag --list | sort -V | tail -1)"
git tag --list
assert "$AUTO_NOTIFY_VERSION" is_not_empty
assert "$AUTO_NOTIFY_VERSION" same_as "$git_version"
}
@test 'hook functions are loaded by default' {
assert '_auto_notify_track' in $preexec_functions
assert '_auto_notify_send' in $precmd_functions
}
@test 'enable/disable auto-notify' {
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
}
@test 'auto-notify-send does not send notification if tracking not set' { @test 'auto-notify-send does not send notification if tracking not set' {
unset AUTO_COMMAND unset AUTO_COMMAND
unset AUTO_COMMAND_FULL unset AUTO_COMMAND_FULL
@ -103,14 +82,32 @@
AUTO_COMMAND="f bar -r" AUTO_COMMAND="f bar -r"
AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080 AUTO_COMMAND_START=11080
AUTO_NOTIFY_EXPIRE_TIME=15000
run _auto_notify_send run _auto_notify_send
assert $state equals 0 assert $state equals 0
assert "$lines[1]" same_as 'Notification: "f bar -r" has completed' assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed'
assert "$lines[2]" same_as "(Total time: 20 seconds)" 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 on MacOSX' { @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="f bar -r"
AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_FULL="foo bar -r"
AUTO_COMMAND_START=11080 AUTO_COMMAND_START=11080
@ -120,14 +117,14 @@
} }
function osascript { function osascript {
echo - $@ echo - "${@}"
} }
run _auto_notify_send run _auto_notify_send
assert $state equals 0 assert $state equals 0
assert "$lines[1]" same_as '-e display notification "\"f bar -r\" has completed' 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 '(Total time: 20 seconds)" with title "Command Completed"' assert "$lines[2]" same_as 'Exit code: 0 "f bar -r" Completed'
} }
@test 'auto-notify-send sends warning on unsupported platform' { @test 'auto-notify-send sends warning on unsupported platform' {
@ -144,3 +141,17 @@
assert "$lines[1]" same_as "Unknown platform for sending notifications: Hal9000" 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/" 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

@ -33,3 +33,30 @@
assert $state equals 0 assert $state equals 0
assert "$output" same_as "yes" 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"
}

52
tests/test_plugin.zunit Normal file
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
}