-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcovet_test.go
254 lines (242 loc) · 5.53 KB
/
covet_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package covet
import (
"bytes"
"errors"
"io"
goos "os"
"path"
"strings"
"testing"
"testing/iotest"
"github.com/hack-pad/hackpadfs"
"github.com/johnstarich/go/covet/internal/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
diff string
diffReader io.Reader
coverage string
expectDiffCovered float64
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
`,
expectDiffCovered: 0.5,
},
{
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
`,
expectDiffCovered: 0,
},
{
description: "bad diff reader",
diffReader: iotest.ErrReader(errors.New("some error")),
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
`,
expectErr: "some error",
},
{
description: "malformed coverage file",
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: `
foo
`,
expectErr: "bad mode line: foo",
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
if tc.diffReader == nil {
tc.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: tc.diffReader,
DiffBaseDir: wd,
GoCoveragePath: coverFile,
GoCoverageBaseDir: wd,
})
if tc.expectErr != "" {
assert.EqualError(t, err, "covet: "+tc.expectErr)
return
}
require.NoError(t, err)
files := covet.DiffCoverageFiles()
assert.NotEmpty(t, files)
covered := covet.DiffCovered()
assert.Equal(t, tc.expectDiffCovered, covered)
})
}
}
func TestParseInvalidOptions(t *testing.T) {
t.Parallel()
wd, err := goos.Getwd()
require.NoError(t, err)
var (
wdFS, workingDirectory = testhelpers.FromOSToFS(t, wd)
baseDir = path.Join(workingDirectory, "testdata")
coverFile = path.Join(baseDir, "add2.out")
)
for _, tc := range []struct {
description string
options Options
expectErr string
}{
{
description: "invalid diff base dir",
options: Options{
DiffBaseDir: "/os-path/not/ok",
},
expectErr: "invalid diff base directory FS path: /os-path/not/ok",
},
{
description: "invalid go coverage file path",
options: Options{
DiffBaseDir: ".",
GoCoveragePath: "/os-path/not/ok",
},
expectErr: "invalid coverage FS path: /os-path/not/ok",
},
{
description: "fs is optional",
options: Options{
FS: nil,
Diff: bytes.NewReader(nil),
DiffBaseDir: baseDir,
GoCoveragePath: coverFile,
},
},
{
description: "go coverage base dir is optional",
options: Options{
FS: wdFS,
Diff: bytes.NewReader(nil),
DiffBaseDir: ".",
GoCoveragePath: coverFile,
GoCoverageBaseDir: "",
},
},
{
description: "invalid go coverage base dir",
options: Options{
FS: wdFS,
Diff: bytes.NewReader(nil),
DiffBaseDir: ".",
GoCoveragePath: coverFile,
GoCoverageBaseDir: "/os-path/not/ok",
},
expectErr: "invalid coverage base directory FS path: /os-path/not/ok",
},
{
description: "diff is required",
options: Options{
FS: wdFS,
Diff: nil,
DiffBaseDir: ".",
GoCoveragePath: coverFile,
GoCoverageBaseDir: baseDir,
},
expectErr: "diff reader must not be nil",
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
_, err := Parse(tc.options)
if tc.expectErr != "" {
assert.EqualError(t, err, "covet: "+tc.expectErr)
return
}
assert.NoError(t, err)
})
}
}
func TestReportFileCoverage(t *testing.T) {
t.Parallel()
fs := testhelpers.FSWithFiles(t, map[string]string{
"main.go": `
package main
func main() {
println("A")
println("B")
}
`,
})
covet := &Covet{
options: Options{
FS: fs,
GoCoveragePath: "subdir/cover.out",
GoCoverageBaseDir: ".",
},
}
file := File{
Name: "main.go",
Covered: 1,
Uncovered: 1,
Lines: []Line{
{Covered: true, LineNumber: 4},
{Covered: false, LineNumber: 5},
},
}
var buf bytes.Buffer
assert.NoError(t, covet.ReportFileCoverage(&buf, file, ReportFileCoverageOptions{}))
assert.Equal(t, strings.TrimSpace(`
Coverage: 2 to 6
func main() {
+ println("A")
- println("B")
}
`), strings.TrimSpace(buf.String()))
}