-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
82 lines (73 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/gotestyourself/gotestyourself/testsum"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
type options struct {
quiet bool
hideFailureRecap bool
hideRunSummary bool
}
var errNonZeroExit = errors.New("")
func main() {
name := os.Args[0]
flags, opts := setupFlags(name)
handleExitError(name, flags.Parse(os.Args[1:]))
handleExitError(name, run(opts))
}
func setupFlags(name string) (*pflag.FlagSet, options) {
opts := options{}
flags := pflag.NewFlagSet(name, pflag.ContinueOnError)
// TODO: set usage func to print more usage
flags.BoolVarP(&opts.quiet, "quiet", "q", false,
"hide verbose test log")
flags.BoolVar(&opts.hideFailureRecap, "hide-failure-recap", false,
"do not print a recap of test failures")
flags.BoolVar(&opts.hideRunSummary, "hide-summary", false,
"do not print test summary")
return flags, opts
}
func run(opts options) error {
echoWriter := getEchoWrite(opts.quiet)
summary, err := testsum.Scan(os.Stdin, echoWriter)
if err != nil {
return err
}
if summary.Total == 0 {
return errors.New("test output was empty. Did you forget to call `go test` with `-v`?")
}
if !opts.hideRunSummary {
fmt.Println(summary.FormatLine())
}
if !opts.hideFailureRecap {
fmt.Print(summary.FormatFailures())
}
if len(summary.Failures) > 0 {
return errNonZeroExit
}
return nil
}
func getEchoWrite(quiet bool) io.Writer {
if quiet {
return ioutil.Discard
}
return os.Stdout
}
func handleExitError(name string, err error) {
switch {
case err == nil:
return
case err == pflag.ErrHelp:
os.Exit(0)
case err == errNonZeroExit:
os.Exit(1)
default:
fmt.Println(name + ": Error: " + err.Error())
os.Exit(3)
}
}