Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post-run-command notify implementation for linux #362

Merged
merged 3 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ 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`.

```
gotestsum --post-run-command notify
```
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.
File renamed without changes.
60 changes: 60 additions & 0 deletions contrib/notify/notify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)
}

args := []string{
"--icon", icon,
title,
subtitle,
}
log.Printf("notify-send %#v", args)
err := exec.Command("notify-send", args...).Run()
if 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
}