-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from dnephin/add-post-run-command
Add post-run-command hook
- Loading branch information
Showing
9 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"gotest.tools/gotestsum/testjson" | ||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/env" | ||
"gotest.tools/v3/golden" | ||
) | ||
|
||
func TestPostRunHook(t *testing.T) { | ||
command := &commandValue{} | ||
err := command.Set("go run ./testdata/postrunhook/main.go") | ||
assert.NilError(t, err) | ||
|
||
buf := new(bytes.Buffer) | ||
opts := &options{ | ||
postRunHookCmd: command, | ||
jsonFile: "events.json", | ||
junitFile: "junit.xml", | ||
stdout: buf, | ||
} | ||
|
||
defer env.Patch(t, "GOTESTSUM_FORMAT", "short")() | ||
|
||
exec := newExecFromTestData(t) | ||
err = postRunHook(opts, exec) | ||
assert.NilError(t, err) | ||
golden.Assert(t, buf.String(), "post-run-hook-expected") | ||
} | ||
|
||
func newExecFromTestData(t *testing.T) *testjson.Execution { | ||
t.Helper() | ||
f, err := os.Open("testjson/testdata/go-test-json.out") | ||
assert.NilError(t, err) | ||
defer f.Close() // nolint: errcheck | ||
|
||
exec, err := testjson.ScanTestOutput(testjson.ScanConfig{ | ||
Stdout: f, | ||
Stderr: strings.NewReader(""), | ||
}) | ||
assert.NilError(t, err) | ||
return exec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
GOTESTSUM_FORMAT=short | ||
GOTESTSUM_JSONFILE=events.json | ||
GOTESTSUM_JUNITFILE=junit.xml | ||
TESTS_ERRORS=0 | ||
TESTS_FAILED=5 | ||
TESTS_SKIPPED=4 | ||
TESTS_TOTAL=46 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run() error { | ||
environ := os.Environ() | ||
sort.Strings(environ) | ||
for _, v := range environ { | ||
for _, prefix := range []string{"TESTS_", "GOTESTSUM_"} { | ||
if strings.HasPrefix(v, prefix) { | ||
fmt.Println(v) | ||
} | ||
} | ||
} | ||
|
||
err := os.Getenv("TEST_STUB_ERROR") | ||
if err != "" { | ||
return errors.New(err) | ||
} | ||
return nil | ||
} |