-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (78 loc) · 1.72 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
83
84
85
86
87
88
package main
import (
"fmt"
"log"
"os"
"github.com/makii42/golcov/osadapter"
"github.com/makii42/golcov/runner"
"github.com/makii42/golcov/test"
"github.com/urfave/cli"
)
func main() {
app := cli.App{
Name: "golcov",
Usage: "Runs go test with default coverage options for each package and writes it to standard out",
Commands: []cli.Command{
testCmd,
versionCmd,
},
Before: setup,
Action: testAction,
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "vendor",
Usage: "not sure about this yet",
},
},
}
app.Run(os.Args)
}
var (
osa osadapter.OS
goBinary string
version string = "dev"
revision string = "dev"
)
var testCmd = cli.Command{
Name: "test",
Usage: "Runs go test and collects coverage information for them. If the tests fail, test output is printed, otherwise coverage data.",
Action: testAction,
}
func setup(c *cli.Context) error {
osa = osadapter.RealOS()
bin, err := osa.LookPath("go")
if err != nil {
return err
}
goBinary = bin
return nil
}
func testAction(c *cli.Context) {
args := c.Args()
tests := createTests(args...)
r, err := runner.NewTestRunner(osa, tests...)
if err != nil {
log.Printf("cannot create testrunner: %s", err.Error())
os.Exit(1)
}
coverSource, err := r.Run()
if err != nil {
log.Printf("error running tests: %s", err.Error())
os.Exit(2)
}
osa.Copy(os.Stdout, coverSource)
}
func createTests(pkgs ...string) (tests []test.Test) {
tests = []test.Test{}
for _, pkg := range pkgs {
tests = append(tests, test.NewTest(goBinary, pkg, osa))
}
return
}
var versionCmd = cli.Command{
Name: "version",
Usage: "displays the version",
Action: func(c *cli.Context) {
fmt.Printf("%s version: %s (%s)\n", c.App.Name, version, revision)
},
}