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] 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 +}