1
2
3
4
5
6
7 package work
8
9 import (
10 "cmd/go/internal/base"
11 "cmd/go/internal/cfg"
12 "cmd/go/internal/str"
13 "cmd/internal/cov/covcmd"
14 "context"
15 "encoding/json"
16 "fmt"
17 "internal/coverage"
18 "io"
19 "os"
20 "path/filepath"
21 )
22
23
24
25 func (b *Builder) CovData(a *Action, cmdargs ...any) ([]byte, error) {
26 cmdline := str.StringList(cmdargs...)
27 args := append([]string{}, cfg.BuildToolexec...)
28 args = append(args, base.Tool("covdata"))
29 args = append(args, cmdline...)
30 return b.Shell(a).runOut(a.Objdir, nil, args)
31 }
32
33
34
35
36
37
38 func BuildActionCoverMetaFile(runAct *Action) (string, error) {
39 p := runAct.Package
40 for i := range runAct.Deps {
41 pred := runAct.Deps[i]
42 if pred.Mode != "build" || pred.Package == nil {
43 continue
44 }
45 if pred.Package.ImportPath == p.ImportPath {
46 metaFile := pred.Objdir + covcmd.MetaFileForPackage(p.ImportPath)
47 if cfg.BuildN {
48 return metaFile, nil
49 }
50 f, err := os.Open(metaFile)
51 if err != nil {
52 return "", err
53 }
54 defer f.Close()
55 fi, err2 := f.Stat()
56 if err2 != nil {
57 return "", err2
58 }
59 if fi.Size() == 0 {
60 return "", nil
61 }
62 return metaFile, nil
63 }
64 }
65 return "", fmt.Errorf("internal error: unable to locate build action for package %q run action", p.ImportPath)
66 }
67
68
69
70
71
72
73
74 func WriteCoveragePercent(b *Builder, runAct *Action, mf string, w io.Writer) error {
75 dir := filepath.Dir(mf)
76 output, cerr := b.CovData(runAct, "percent", "-i", dir)
77 if cerr != nil {
78 return b.Shell(runAct).reportCmd("", "", output, cerr)
79 }
80 _, werr := w.Write(output)
81 return werr
82 }
83
84
85
86
87
88
89 func WriteCoverageProfile(b *Builder, runAct *Action, mf, outf string, w io.Writer) error {
90 dir := filepath.Dir(mf)
91 output, err := b.CovData(runAct, "textfmt", "-i", dir, "-o", outf)
92 if err != nil {
93 return b.Shell(runAct).reportCmd("", "", output, err)
94 }
95 _, werr := w.Write(output)
96 return werr
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111 func WriteCoverMetaFilesFile(b *Builder, ctx context.Context, a *Action) error {
112 sh := b.Shell(a)
113
114
115 var collection coverage.MetaFileCollection
116 for i := range a.Deps {
117 dep := a.Deps[i]
118 if dep.Mode != "build" {
119 panic("unexpected mode " + dep.Mode)
120 }
121 metaFilesFile := dep.Objdir + covcmd.MetaFileForPackage(dep.Package.ImportPath)
122
123
124 if fi, err := os.Stat(metaFilesFile); err != nil {
125 continue
126 } else if fi.Size() == 0 {
127 continue
128 }
129 collection.ImportPaths = append(collection.ImportPaths, dep.Package.ImportPath)
130 collection.MetaFileFragments = append(collection.MetaFileFragments, metaFilesFile)
131 }
132
133
134 data, err := json.Marshal(collection)
135 if err != nil {
136 return fmt.Errorf("marshal MetaFileCollection: %v", err)
137 }
138 data = append(data, '\n')
139
140
141
142
143 if err := sh.Mkdir(a.Objdir); err != nil {
144 return err
145 }
146 mfpath := a.Objdir + coverage.MetaFilesFileName
147 if err := sh.writeFile(mfpath, data); err != nil {
148 return fmt.Errorf("writing metafiles file: %v", err)
149 }
150
151
152 return nil
153 }
154
View as plain text