From e7e66e9346e83b4d282f39a638fc47f95be1f82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Mon, 21 Aug 2023 11:35:33 +0200 Subject: [PATCH 1/3] Add post-run-command notify implementation for linux --- README.md | 5 +- contrib/notify/.gitignore | 1 + contrib/notify/Makefile | 13 ++++ contrib/notify/icons/README.html | 1 + contrib/notify/icons/test-fail.svg | 1 + contrib/notify/icons/test-pass.svg | 1 + .../{notify-macos.go => notify_darwin.go} | 0 contrib/notify/notify_linux.go | 60 +++++++++++++++++++ 8 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 contrib/notify/.gitignore create mode 100644 contrib/notify/Makefile create mode 100644 contrib/notify/icons/README.html create mode 100644 contrib/notify/icons/test-fail.svg create mode 100644 contrib/notify/icons/test-pass.svg rename contrib/notify/{notify-macos.go => notify_darwin.go} (100%) create mode 100644 contrib/notify/notify_linux.go diff --git a/README.md b/README.md index cca98cf1..d5d7c7f9 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/contrib/notify/.gitignore b/contrib/notify/.gitignore new file mode 100644 index 00000000..f3b695d3 --- /dev/null +++ b/contrib/notify/.gitignore @@ -0,0 +1 @@ +notify diff --git a/contrib/notify/Makefile b/contrib/notify/Makefile new file mode 100644 index 00000000..73771c64 --- /dev/null +++ b/contrib/notify/Makefile @@ -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) diff --git a/contrib/notify/icons/README.html b/contrib/notify/icons/README.html new file mode 100644 index 00000000..4512a24e --- /dev/null +++ b/contrib/notify/icons/README.html @@ -0,0 +1 @@ +Pass Fail Vectors by Vecteezy diff --git a/contrib/notify/icons/test-fail.svg b/contrib/notify/icons/test-fail.svg new file mode 100644 index 00000000..f6b97c74 --- /dev/null +++ b/contrib/notify/icons/test-fail.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/contrib/notify/icons/test-pass.svg b/contrib/notify/icons/test-pass.svg new file mode 100644 index 00000000..2e96bdaf --- /dev/null +++ b/contrib/notify/icons/test-pass.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/contrib/notify/notify-macos.go b/contrib/notify/notify_darwin.go similarity index 100% rename from contrib/notify/notify-macos.go rename to contrib/notify/notify_darwin.go diff --git a/contrib/notify/notify_linux.go b/contrib/notify/notify_linux.go new file mode 100644 index 00000000..5c8ae18e --- /dev/null +++ b/contrib/notify/notify_linux.go @@ -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 +} From 57554cbd33726d7c4f7a5155e7b8dac8a975ca0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 27 Aug 2023 19:27:28 +0200 Subject: [PATCH 2/3] Add post-run-command notify implementation for windows Requires notify-send.exe: http://vaskovsky.net/notify-send Only tested with GOOS=windows and wine, but seems to work. --- README.md | 3 ++ contrib/notify/notify_windows.go | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 contrib/notify/notify_windows.go diff --git a/README.md b/README.md index d5d7c7f9..b4fe867f 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ example `notify` command only works on Linux with `notify-send` and on macOS wit 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 ``` diff --git a/contrib/notify/notify_windows.go b/contrib/notify/notify_windows.go new file mode 100644 index 00000000..d7f938c7 --- /dev/null +++ b/contrib/notify/notify_windows.go @@ -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 := "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) + } + + args := []string{ + "-i", icon, + title, + subtitle, + } + log.Printf("notify-send %#v", args) + err := exec.Command("notify-send.exe", 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 +} From 6a73d4a6e758e5d7e497c51d4e08c11664583afb Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sun, 27 Aug 2023 18:26:37 -0400 Subject: [PATCH 3/3] notify: include stdout/stderr for debugging --- cmd/handler.go | 1 + contrib/notify/notify_darwin.go | 8 +++++--- contrib/notify/notify_linux.go | 13 +++++-------- contrib/notify/notify_windows.go | 13 +++++-------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 0820ee68..b2d74f9f 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -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 diff --git a/contrib/notify/notify_darwin.go b/contrib/notify/notify_darwin.go index 3f6822ca..ad683898 100644 --- a/contrib/notify/notify_darwin.go +++ b/contrib/notify/notify_darwin.go @@ -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) } } diff --git a/contrib/notify/notify_linux.go b/contrib/notify/notify_linux.go index 5c8ae18e..8da6c3e8 100644 --- a/contrib/notify/notify_linux.go +++ b/contrib/notify/notify_linux.go @@ -38,14 +38,11 @@ func main() { 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 { + 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) } } diff --git a/contrib/notify/notify_windows.go b/contrib/notify/notify_windows.go index d7f938c7..f3d384d5 100644 --- a/contrib/notify/notify_windows.go +++ b/contrib/notify/notify_windows.go @@ -38,14 +38,11 @@ func main() { subtitle += fmt.Sprintf(", %d Skipped", skipped) } - args := []string{ - "-i", icon, - title, - subtitle, - } - log.Printf("notify-send %#v", args) - err := exec.Command("notify-send.exe", args...).Run() - if err != nil { + 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) } }