-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
106 lines (94 loc) · 2.09 KB
/
command.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
package main
import (
"bytes"
"errors"
"flag"
"io"
"os"
"os/exec"
"golang.org/x/tools/cover"
)
func command(stdin io.Reader, stdout io.Writer) error {
var fileName string
switch flag.CommandLine.Arg(0) {
case "test":
packages := flag.CommandLine.Arg(1)
output, err := runGoTests(packages)
if err != nil {
return err
}
stdin = output
case "-":
// user explicitly specified to use stdin
default:
fileName = flag.CommandLine.Arg(0)
}
// parse coverage profiles from stdin or file path if provided
profiles, err := parseCoverProfiles(fileName, stdin)
if err != nil {
return err
}
// find path that points to module root which will be needed to construct an
// absolute file path to the Go source files to generate a diff for
moduleInfo, err := findModuleInfo()
if err != nil {
return err
}
if pager := os.ExpandEnv("$PAGER"); pager != "" {
return usePager(pager, stdout, func(w io.Writer) error {
return printDiff(w, profiles, moduleInfo)
})
}
return printDiff(stdout, profiles, moduleInfo)
}
func usePager(
name string,
stdout io.Writer,
printDiff func(io.Writer) error,
) error {
cmd := exec.Command(name)
cmd.Stdout = stdout
w, err := cmd.StdinPipe()
if err != nil {
return err
}
go func() {
defer w.Close()
printDiff(w)
}()
return cmd.Run()
}
func runGoTests(packages string) (*bytes.Buffer, error) {
tempFile, err := createTempFile()
if err != nil {
return nil, err
}
defer os.Remove(tempFile.Name())
cmd := exec.Command(
"go",
"test",
packages,
"-cover",
"-coverprofile="+tempFile.Name(),
)
b, err := cmd.CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
return nil, errors.New(string(b))
}
return nil, err
}
coverprofile, err := os.ReadFile(tempFile.Name())
if err != nil {
return nil, err
}
return bytes.NewBuffer(coverprofile), nil
}
func parseCoverProfiles(fileName string, stdin io.Reader) ([]*cover.Profile, error) {
if fileName != "" {
profiles, err := cover.ParseProfiles(fileName)
return profiles, err
}
profiles, err := cover.ParseProfilesFromReader(stdin)
return profiles, err
}