-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (94 loc) · 4.26 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"syscall"
"github.com/AlekSi/gocovermerge/gocovermerge"
)
var (
coverprofileF = flag.String("coverprofile", "cover.out", "Output file.")
mergeFlagSet = flag.NewFlagSet("merge", flag.ExitOnError)
testFlagSet = flag.NewFlagSet("test", flag.ExitOnError)
// The build flags are shared by the build, clean, get, install, list, run, and test commands:
aF = testFlagSet.Bool("a", false, "force rebuilding of packages that are already up-to-date.")
nF = testFlagSet.Bool("n", false, "print the commands but do not run them.")
raceF = testFlagSet.Bool("race", false, "enable data race detection.")
msanF = testFlagSet.Bool("msan", false, "enable interoperation with memory sanitizer.")
workF = testFlagSet.Bool("work", false, "print the name of the temporary work directory and do not delete it when exiting.")
xF = testFlagSet.Bool("x", false, "print the commands.")
tagsF = testFlagSet.String("tags", "", "a list of build tags to consider satisfied during the build.")
// -p is not supported
// -v is redefined below
// The test binary flags:
covermodeF = testFlagSet.String("covermode", "", "set the mode for coverage analysis for the package[s] being tested.")
shortF = testFlagSet.Bool("short", false, "tell long-running tests to shorten their run time.")
timeoutF = testFlagSet.Duration("timeout", 0, "if a test runs longer than t, panic.")
vF = testFlagSet.Bool("v", false, "verbose output: log all tests as they are run.")
// -coverprofile is defined by main comand
// -coverpkg is set by Test method
// TODO add more flags
)
func main() {
log.SetFlags(0)
mergeFlagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "%s merge command merges several go coverage profiles into a single file.\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage:\n\n")
fmt.Fprintf(os.Stderr, " %s [global flags] merge [files]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example:\n\n")
fmt.Fprintf(os.Stderr, " %s -coverprofile=cover.out merge internal/test/package1/package1.out internal/test/package2/package2.out\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Global flags:\n")
flag.PrintDefaults()
}
testFlagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "%s test command runs go test -cover with correct flags and merges profiles.\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Packages list is passed as arguments; they may contain `...` patterns.\n")
fmt.Fprintf(os.Stderr, "The list is expanded, sorted and duplicates are removed.\n")
fmt.Fprintf(os.Stderr, "`go test -coverpkg` flag is set automatically to include all packages.\n")
fmt.Fprintf(os.Stderr, "If tests are failing, %s exits with a correct exit code.\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage:\n\n")
fmt.Fprintf(os.Stderr, " %s [global flags] test [test flags] [packages]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example:\n\n")
fmt.Fprintf(os.Stderr, " %s -coverprofile=cover.out test -v -covermode=count github.com/AlekSi/gocovermerge/internal/test/...\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Global flags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nTest flags:\n")
testFlagSet.PrintDefaults()
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s contains two commands: merge and test.\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Merge command merges several go coverage profiles into a single file.\n")
fmt.Fprintf(os.Stderr, "Run `%s merge -h` for usage information.\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Test command runs go test -cover with correct flags and merges profiles.\n")
fmt.Fprintf(os.Stderr, "Run `%s test -h` for usage information.\n", os.Args[0])
}
flag.Parse()
var err error
switch flag.Arg(0) {
case "merge":
mergeFlagSet.Parse(flag.Args()[1:])
err = gocovermerge.Merge(mergeFlagSet.Args(), *coverprofileF)
case "test":
testFlagSet.Parse(flag.Args()[1:])
logger := log.New(ioutil.Discard, "", 0)
if *nF || *xF || *vF {
logger.SetOutput(os.Stderr)
}
err = gocovermerge.Test(testFlagSet, *coverprofileF, logger)
default:
flag.Usage()
log.Fatalf("\nUnexpected command '%s'.", flag.Arg(0))
}
if err != nil {
log.Print(err)
if eErr, ok := err.(*exec.ExitError); ok {
if ws, ok := eErr.Sys().(*syscall.WaitStatus); ok {
os.Exit(ws.ExitStatus())
}
}
os.Exit(1)
}
}