Skip to content

Commit

Permalink
Merge pull request #362 from afbjorklund/notify-linux
Browse files Browse the repository at this point in the history
Add post-run-command notify implementation for linux
  • Loading branch information
dnephin authored Aug 27, 2023
2 parents 2098fe3 + 6a73d4a commit 0d1ee6a
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,15 @@ package may be used to parse the JSON file output.

First install the example notification command with `go get gotest.tools/gotestsum/contrib/notify`.
The command will be downloaded to `$GOPATH/bin` as `notify`. Note that this
example `notify` command only works on macOS with
example `notify` command only works on Linux with `notify-send` and on macOS with
[terminal-notifer](https://github.com/julienXX/terminal-notifier) installed.

On Linux, you need to have some "test-pass" and "test-fail" icons installed in your icon theme.
Some sample icons can be found in `contrib/notify`, and can be installed with `make install`.

On Windows, you can install [notify-send.exe](https://github.com/vaskovsky/notify-send)
but it does not support custom icons so will have to use the basic "info" and "error".

```
gotestsum --post-run-command notify
```
Expand Down
1 change: 1 addition & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func postRunHook(opts *options, execution *testjson.Execution) error {
if len(command) == 0 {
return nil
}
log.Debugf("exec: %s", command)

cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout = opts.stdout
Expand Down
1 change: 1 addition & 0 deletions contrib/notify/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notify
13 changes: 13 additions & 0 deletions contrib/notify/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GO = go
INSTALL = install

ICONS = icons/test-pass.svg icons/test-fail.svg
ICONDIR = $(HOME)/.icons # or /usr/share/icons

build:
$(GO) build

install: $(ICONS)
$(GO) install
$(INSTALL) -d $(ICONDIR)
$(INSTALL) $^ $(ICONDIR)
1 change: 1 addition & 0 deletions contrib/notify/icons/README.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="https://www.vecteezy.com/free-vector/pass-fail">Pass Fail Vectors by Vecteezy</a>
1 change: 1 addition & 0 deletions contrib/notify/icons/test-fail.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions contrib/notify/icons/test-pass.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func main() {
"-group", "gotestsum",
"-subtitle", subtitle,
}
log.Printf("terminal-notifier %#v", args)
err := exec.Command("terminal-notifier", args...).Run()
if err != nil {
cmd := exec.Command("terminal-notifier", args...)
log.Printf("%#v", cmd.Args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to exec: %v", err)
}
}
Expand Down
57 changes: 57 additions & 0 deletions contrib/notify/notify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
)

func main() {
total := envInt("TOTAL")
skipped := envInt("SKIPPED")
failed := envInt("FAILED")
errors := envInt("ERRORS")

icon := "test-pass"
title := "Passed"
switch {
case errors > 0:
icon = "dialog-warning"
title = "Errored"
case failed > 0:
icon = "test-fail"
title = "Failed"
case skipped > 0:
title = "Passed with skipped"
}

subtitle := fmt.Sprintf("%d Tests Run", total)
if errors > 0 {
subtitle += fmt.Sprintf(", %d Errored", errors)
}
if failed > 0 {
subtitle += fmt.Sprintf(", %d Failed", failed)
}
if skipped > 0 {
subtitle += fmt.Sprintf(", %d Skipped", skipped)
}

cmd := exec.Command("notify-send", "--icon", icon, title, subtitle)
log.Printf("%#v", cmd.Args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to exec: %v", err)
}
}

func envInt(name string) int {
val := os.Getenv("TESTS_" + name)
n, err := strconv.Atoi(val)
if err != nil {
return 0
}
return n
}
57 changes: 57 additions & 0 deletions contrib/notify/notify_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
)

func main() {
total := envInt("TOTAL")
skipped := envInt("SKIPPED")
failed := envInt("FAILED")
errors := envInt("ERRORS")

icon := "info" // Info 🛈
title := "Passed"
switch {
case errors > 0:
icon = "important" // Warning ⚠
title = "Errored"
case failed > 0:
icon = "error" // Error ⮾
title = "Failed"
case skipped > 0:
title = "Passed with skipped"
}

subtitle := fmt.Sprintf("%d Tests Run", total)
if errors > 0 {
subtitle += fmt.Sprintf(", %d Errored", errors)
}
if failed > 0 {
subtitle += fmt.Sprintf(", %d Failed", failed)
}
if skipped > 0 {
subtitle += fmt.Sprintf(", %d Skipped", skipped)
}

cmd := exec.Command("notify-send.exe", "-i", icon, title, subtitle)
log.Printf("%#v", cmd.Args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to exec: %v", err)
}
}

func envInt(name string) int {
val := os.Getenv("TESTS_" + name)
n, err := strconv.Atoi(val)
if err != nil {
return 0
}
return n
}

0 comments on commit 0d1ee6a

Please sign in to comment.