MCPcopy Create free account
hub / github.com/diillson/chatcli / runCoverageScript

Function runCoverageScript

cli/agent/workers/agents_tester.go:130–191  ·  view source on GitHub ↗

runCoverageScript runs go test with coverage and parses the results.

(ctx context.Context, input map[string]string, _ *engine.Engine)

Source from the content-addressed store, hash-verified

128
129// runCoverageScript runs go test with coverage and parses the results.
130func runCoverageScript(ctx context.Context, input map[string]string, _ *engine.Engine) (string, error) {
131 dir := input["dir"]
132 if dir == "" {
133 dir = "."
134 }
135 pkg := input["pkg"]
136 if pkg == "" {
137 pkg = "./..."
138 }
139
140 coverFile := filepath.Join(os.TempDir(), "chatcli_cover.out")
141 cmd := fmt.Sprintf("cd %s && go test -coverprofile=%s %s 2>&1", dir, coverFile, pkg)
142
143 var buf bytes.Buffer
144 outWriter := engine.NewStreamWriter(func(line string) {
145 buf.WriteString(line)
146 buf.WriteString("\n")
147 })
148 errWriter := engine.NewStreamWriter(func(line string) {
149 buf.WriteString(line)
150 buf.WriteString("\n")
151 })
152
153 eng := engine.NewEngine(outWriter, errWriter, "")
154 testErr := eng.Execute(ctx, "exec", []string{"--cmd", cmd})
155 outWriter.Flush()
156 errWriter.Flush()
157
158 var results strings.Builder
159 results.WriteString("## Coverage Report\n\n")
160
161 if testErr != nil {
162 results.WriteString("### Test Execution: FAILED\n")
163 } else {
164 results.WriteString("### Test Execution: PASSED\n")
165 }
166 results.WriteString("```\n")
167 results.WriteString(buf.String())
168 results.WriteString("```\n\n")
169
170 // Try to get per-function coverage
171 funcCmd := fmt.Sprintf("go tool cover -func=%s 2>&1", coverFile)
172 var funcBuf bytes.Buffer
173 funcOut := engine.NewStreamWriter(func(line string) {
174 funcBuf.WriteString(line)
175 funcBuf.WriteString("\n")
176 })
177 funcEng := engine.NewEngine(funcOut, engine.NewStreamWriter(func(string) {}), "")
178 funcErr := funcEng.Execute(ctx, "exec", []string{"--cmd", funcCmd})
179 funcOut.Flush()
180
181 if funcErr == nil && funcBuf.Len() > 0 {
182 results.WriteString("### Per-Function Coverage\n```\n")
183 results.WriteString(funcBuf.String())
184 results.WriteString("```\n")
185 }
186
187 // Clean up temp file

Callers

nothing calls this directly

Calls 7

ExecuteMethod · 0.95
FlushMethod · 0.95
NewStreamWriterFunction · 0.92
NewEngineFunction · 0.92
RemoveMethod · 0.65
StringMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected