-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsummary_test.go
160 lines (153 loc) · 4.84 KB
/
summary_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package covet
import (
"bytes"
"path"
"strings"
"testing"
"github.com/hack-pad/hackpadfs"
"github.com/johnstarich/go/covet/internal/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReportSummaryMarkdown(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
diff string
coverage string
expectMarkdown string
expectTerminal string
expectErr string
}{
{
description: "half covered",
diff: `
diff --git a/covet.go b/covet.go
index 0000000..1111111 100644
--- a/covet.go
+++ b/covet.go
@@ -0,0 +1,2 @@
+added 1
+added 2
`,
coverage: `
mode: atomic
github.com/johnstarich/go/covet/covet.go:1.1,1.7 1 1
github.com/johnstarich/go/covet/covet.go:2.1,2.7 1 0
`,
expectMarkdown: `
Diff coverage is below target. Add tests for these files:
| | Lines | Coverage | File |
| --- |:---:| --- | --- |
| 🟠 | ~~1/2~~ | ~~ 50.0% ██▌ ~~ | covet.go |
`,
expectTerminal: `
Diff coverage is below target. Add tests for these files:
┌───────┬──────────────┬──────────┐
│ LINES │ COVERAGE │ FILE │
├───────┼──────────────┼──────────┤
│ 1/2 │ 50.0% ██▌ │ covet.go │
└───────┴──────────────┴──────────┘
`,
},
{
description: "no coverage hits",
diff: `
diff --git a/covet.go b/covet.go
index 0000000..1111111 100644
--- a/covet.go
+++ b/covet.go
@@ -0,0 +1,2 @@
+added 1
+added 2
`,
coverage: `
mode: atomic
github.com/johnstarich/go/covet/covet.go:1.1,1.7 1 0
github.com/johnstarich/go/covet/covet.go:2.1,2.7 1 0
`,
expectMarkdown: `
Diff coverage is below target. Add tests for these files:
| | Lines | Coverage | File |
| --- |:---:| --- | --- |
| 🔴 | ~~0/2~~ | ~~ 0.0% ▏ ~~ | covet.go |
`,
expectTerminal: `
Diff coverage is below target. Add tests for these files:
┌───────┬──────────────┬──────────┐
│ LINES │ COVERAGE │ FILE │
├───────┼──────────────┼──────────┤
│ 0/2 │ 0.0% ▏ │ covet.go │
└───────┴──────────────┴──────────┘
`,
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
diffReader := strings.NewReader(strings.TrimSpace(tc.diff))
fs, wd, tmpDir := testhelpers.OSFSWithTemp(t)
coverFile := path.Join(tmpDir, "cover.out")
{
f, err := hackpadfs.OpenFile(fs, coverFile, hackpadfs.FlagWriteOnly|hackpadfs.FlagCreate, 0600)
require.NoError(t, err)
_, err = hackpadfs.WriteFile(f, []byte(strings.TrimSpace(tc.coverage)))
require.NoError(t, err)
require.NoError(t, f.Close())
}
covet, err := Parse(Options{
FS: fs,
Diff: diffReader,
DiffBaseDir: wd,
GoCoveragePath: coverFile,
GoCoverageBaseDir: wd,
})
require.NoError(t, err)
var buf bytes.Buffer
assert.NoError(t, covet.ReportSummaryMarkdown(&buf, ReportSummaryOptions{
Target: 90,
}))
expectMarkdown := strings.ReplaceAll(tc.expectMarkdown, "~~", "``")
assert.Equal(t, strings.TrimSpace(expectMarkdown), strings.TrimSpace(buf.String()))
buf.Reset()
assert.NoError(t, covet.ReportSummaryColorTerminal(&buf, ReportSummaryOptions{
Target: 90,
}))
assert.Equal(t, strings.TrimSpace(tc.expectTerminal), strings.TrimSpace(buf.String()))
})
}
}
func TestFindReportableUncoveredFiles(t *testing.T) {
t.Parallel()
t.Run("sort and filter just enough files", func(t *testing.T) {
t.Parallel()
files := []File{
{Name: "foo", Covered: 2, Uncovered: 0},
{Name: "bar", Covered: 1, Uncovered: 2},
{Name: "baz", Covered: 1, Uncovered: 2},
{Name: "biff", Covered: 0, Uncovered: 2},
}
reportable := findReportableUncoveredFiles(files, 0.75, 0.4)
assert.Equal(t, []File{
{Name: "bar", Covered: 1, Uncovered: 2},
{Name: "baz", Covered: 1, Uncovered: 2},
{Name: "biff", Covered: 0, Uncovered: 2},
}, reportable)
})
t.Run("include more small files if the biggest chunks are not close enough to target", func(t *testing.T) {
t.Parallel()
files := []File{
{Name: "foo", Covered: 0, Uncovered: 1},
{Name: "bar", Covered: 0, Uncovered: 1},
{Name: "baz", Covered: 0, Uncovered: 1},
{Name: "biff", Covered: 0, Uncovered: 7},
}
reportable := findReportableUncoveredFiles(files, 0.8, 0)
assert.Equal(t, []File{
{Name: "biff", Covered: 0, Uncovered: 7},
{Name: "bar", Covered: 0, Uncovered: 1},
{Name: "baz", Covered: 0, Uncovered: 1},
{Name: "foo", Covered: 0, Uncovered: 1},
}, reportable)
})
}