Skip to content

Commit 4d2dd80

Browse files
committed
Implemented code coverage support.
1 parent b82d43d commit 4d2dd80

File tree

8 files changed

+190
-57
lines changed

8 files changed

+190
-57
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
)
1515

1616
require (
17+
github.com/easy-up/go-coverage v0.0.0-20241018034313-3de592d59a78 // indirect
1718
github.com/fsnotify/fsnotify v1.7.0 // indirect
1819
github.com/hashicorp/hcl v1.0.0 // indirect
1920
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
55
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
77
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
8+
github.com/easy-up/go-coverage v0.0.0-20241018034313-3de592d59a78 h1:e2x+TfIgebN3zfr8wGqAYI9lK4ql7Rut6OTEhBmJr5k=
9+
github.com/easy-up/go-coverage v0.0.0-20241018034313-3de592d59a78/go.mod h1:fsSINOc273zPnsBaKNjNffZXZpicAArpv/cTiFYgPys=
810
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
911
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
1012
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=

pkg/archive/bundle.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13+
"github.com/olekukonko/tablewriter"
1314
"io"
1415
"log/slog"
1516
"os"
@@ -159,7 +160,10 @@ func (b *Bundle) Content() string {
159160
sort.Sort(matrix)
160161
buf := new(bytes.Buffer)
161162
header := []string{"Label", "Digest", "Tags", "Size"}
162-
matrix.Table(buf, header).Render()
163+
table := tablewriter.NewWriter(buf)
164+
table.SetHeader(header)
165+
matrix.Table(table)
166+
table.Render()
163167
return buf.String()
164168
}
165169

pkg/artifacts/lcov.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package artifacts
2+
3+
import (
4+
"errors"
5+
"github.com/easy-up/go-coverage"
6+
"log/slog"
7+
"strings"
8+
)
9+
10+
func example() (coverage.Report, error) {
11+
lcovParser := coverage.New(coverage.LCOV)
12+
report, err := lcovParser.Parse("./path/to/lcov.info")
13+
if err != nil {
14+
// Handle error
15+
return coverage.Report{}, err
16+
}
17+
// Use the parsed report
18+
return report, nil
19+
}
20+
21+
func IsCoverageReport(inputFilename string) bool {
22+
return strings.Contains(inputFilename, "lcov") ||
23+
strings.HasSuffix(inputFilename, ".info") ||
24+
strings.Contains(inputFilename, "clover") ||
25+
strings.Contains(inputFilename, "cobertura") ||
26+
strings.Contains(inputFilename, "coverage")
27+
}
28+
29+
func GetCoverageMode(inputFilename string) (coverage.CoverageMode, error) {
30+
var coverageFormat coverage.CoverageMode
31+
if strings.Contains(inputFilename, "lcov") || strings.HasSuffix(inputFilename, ".info") {
32+
coverageFormat = coverage.LCOV
33+
} else if strings.Contains(inputFilename, "clover") {
34+
coverageFormat = coverage.CLOVER
35+
} else if strings.HasSuffix(inputFilename, ".xml") {
36+
coverageFormat = coverage.COBERTURA
37+
} else {
38+
slog.Error("unsupported coverage file type, cannot be determined from filename", "filename", inputFilename)
39+
return "", errors.New("failed to list coverage content")
40+
}
41+
return coverageFormat, nil
42+
}

pkg/format/matrix.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package format
22

33
import (
4-
"io"
5-
64
"github.com/olekukonko/tablewriter"
75
)
86

@@ -28,11 +26,8 @@ func (m *SortableMatrix) Matrix() [][]string {
2826
return m.data
2927
}
3028

31-
func (m *SortableMatrix) Table(w io.Writer, header []string) *tablewriter.Table {
32-
table := tablewriter.NewWriter(w)
33-
table.SetHeader(header)
29+
func (m *SortableMatrix) Table(table *tablewriter.Table) {
3430
table.AppendBulk(m.data)
35-
return table
3631
}
3732

3833
func (m *SortableMatrix) Len() int {

pkg/gatecheck/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Config struct {
2828
Cyclonedx reportWithCVEs `json:"cyclonedx" toml:"cyclonedx" yaml:"cyclonedx"`
2929
Semgrep configSemgrepReport `json:"semgrep" toml:"semgrep" yaml:"semgrep"`
3030
Gitleaks configGitleaksReport `json:"gitleaks" toml:"gitleaks" yaml:"gitleaks"`
31+
Coverage configCoverageReport `json:"coverage" toml:"coverage" yaml:"coverage"`
3132
}
3233

3334
func (c *Config) String() string {
@@ -48,6 +49,12 @@ func (c *Config) String() string {
4849
return contentBuf.String()
4950
}
5051

52+
type configCoverageReport struct {
53+
LineThreshold float32 `json:"lineThreshold" toml:"lineThreshold" yaml:"lineThreshold"`
54+
FunctionThreshold float32 `json:"functionThreshold" toml:"functionThreshold" yaml:"functionThreshold"`
55+
BranchThreshold float32 `json:"branchThreshold" toml:"branchThreshold" yaml:"branchThreshold"`
56+
}
57+
5158
type configGitleaksReport struct {
5259
LimitEnabled bool `json:"limitEnabled" toml:"limitEnabled" yaml:"limitEnabled"`
5360
}
@@ -225,6 +232,11 @@ func NewDefaultConfig() *Config {
225232
Gitleaks: configGitleaksReport{
226233
LimitEnabled: false,
227234
},
235+
Coverage: configCoverageReport{
236+
LineThreshold: 0,
237+
FunctionThreshold: 0,
238+
BranchThreshold: 0,
239+
},
228240
}
229241
}
230242

0 commit comments

Comments
 (0)