-
Notifications
You must be signed in to change notification settings - Fork 2
/
all_coverage.go
72 lines (63 loc) · 2.21 KB
/
all_coverage.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
package main
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"golang.org/x/tools/cover"
)
// CoverageResult represents the structure of a coverage result
type CoverageResult struct {
Package string `json:"package"`
File string `json:"file"`
StartLine int `json:"start_line"`
StartColumn int `json:"start_col"`
EndLine int `json:"end_line"`
EndColumn int `json:"end_col"`
StatementNumber int `json:"stmt_num"`
Count int `json:"count"`
FunctionName string `json:"function_name"`
}
func collectCoverageResults(pkgDir string) ([]CoverageResult, error) {
profiles, err := cover.ParseProfiles("coverage.out")
if err != nil {
return nil, err
}
var results []CoverageResult
for _, profile := range profiles {
packageName := filepath.Dir(profile.FileName)
fileName := filepath.Base(profile.FileName)
for _, block := range profile.Blocks {
functionName, err := getFunctionName(pkgDir+"/"+fileName, block.StartLine)
if err != nil {
return nil, fmt.Errorf("failed to retrieve function name: %w", err)
}
results = append(results, CoverageResult{
Package: packageName,
File: fileName,
StartLine: block.StartLine,
StartColumn: block.StartCol,
EndLine: block.EndLine,
EndColumn: block.EndCol,
StatementNumber: block.NumStmt,
Count: block.Count,
FunctionName: functionName,
})
}
}
return results, nil
}
func populateCoverageResults(ctx context.Context, db *sql.DB, pkgDir string) error {
coverageResults, err := collectCoverageResults(pkgDir)
if err != nil {
return fmt.Errorf("failed to collect coverage results: %w", err)
}
for _, result := range coverageResults {
insertSQL := `INSERT INTO all_coverage (package, file, start_line, start_col, end_line, end_col, stmt_num, count, function_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);`
_, err := db.ExecContext(ctx, insertSQL, result.Package, result.File, result.StartLine, result.StartColumn, result.EndLine, result.EndColumn, result.StatementNumber, result.Count, result.FunctionName)
if err != nil {
return fmt.Errorf("failed to insert coverage results: %w", err)
}
}
return nil
}