Skip to content

Commit

Permalink
Add a sample post-hook-command for osx notifications
Browse files Browse the repository at this point in the history
eg. --post-run-command 'go run scripts/notify_example.go'
  • Loading branch information
Glen Mailer committed Nov 3, 2019
1 parent 1c5c887 commit 4728f41
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ require (
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
gotest.tools v2.1.0+incompatible
)

go 1.13
5 changes: 3 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ func writeJUnitFile(opts *options, execution *testjson.Execution) error {
}

func postRunHook(opts *options, execution *testjson.Execution) error {
if len(opts.postRunHookCmd) == 0 {
if opts.postRunHookCmd == "" {
return nil
}

cmd := exec.Command(opts.postRunHookCmd[0], opts.postRunHookCmd[1:]...)
// TODO: windows?
cmd := exec.Command("/bin/sh", "-c", opts.postRunHookCmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ Formats:
"format the testsuite name field as: "+junitFieldFormatValues)
flags.Var(opts.junitTestCaseClassnameFormat, "junitfile-testcase-classname",
"format the testcase classname field as: "+junitFieldFormatValues)
flags.StringSliceVar(&opts.postRunHookCmd, "post-run-command",
nil, "run this command once the tests have completed")
flags.StringVar(&opts.postRunHookCmd, "post-run-command",
"", "run this command once the tests have completed")
flags.BoolVar(&opts.version, "version", false, "show version and exit")
return flags, opts
}
Expand All @@ -112,7 +112,7 @@ type options struct {
rawCommand bool
jsonFile string
junitFile string
postRunHookCmd []string
postRunHookCmd string
noColor bool
noSummary *noSummaryValue
junitTestSuiteNameFormat *junitFieldFormatValue
Expand Down
60 changes: 60 additions & 0 deletions scripts/notify_example.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")

emoji := "✅"
title := "Passed"
switch {
case errors > 0:
emoji = "⚠️"
title = "Errored"
case failed > 0:
emoji = "❌"
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{
"-title", emoji + " " + title,
"-group", "gotestsum",
"-subtitle", subtitle,
}
log.Printf("terminal-notifier %#v", args)
err := exec.Command("terminal-notifier", 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
}

0 comments on commit 4728f41

Please sign in to comment.