Skip to content

Commit abd9d6c

Browse files
committed
improvements
1 parent 2900eeb commit abd9d6c

File tree

6 files changed

+27
-54
lines changed

6 files changed

+27
-54
lines changed

pkg/testcoverage/check_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ func TestCheck(t *testing.T) {
278278
pass, err := Check(buf, cfg)
279279
assert.False(t, pass)
280280
assert.NoError(t, err)
281-
282-
// Should report missing explanations
283-
assert.Contains(t, buf.String(), "Files with missing explanations for coverage-ignore")
281+
assert.Contains(t, buf.String(), "Files with missing explanation for coverage-ignore")
284282
})
285283

286284
t.Run("valid profile - pass when not checking for explanations", func(t *testing.T) {
@@ -295,8 +293,6 @@ func TestCheck(t *testing.T) {
295293
pass, err := Check(buf, cfg)
296294
assert.True(t, pass)
297295
assert.NoError(t, err)
298-
299-
// Should not report missing explanations
300296
assert.NotContains(t, buf.String(), "Files with missing explanations for coverage-ignore")
301297
})
302298
}
@@ -674,6 +670,7 @@ func Test_Analyze(t *testing.T) {
674670
}
675671
result = Analyze(cfg, stats, nil)
676672
assert.False(t, result.Pass())
673+
assert.True(t, result.PassCoverage())
677674
assert.NotEmpty(t, result.FilesWithMissingExplanations)
678675
})
679676
}

pkg/testcoverage/coverage/cover.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,17 @@ func findAnnotations(source []byte, forceComment bool) ([]extent, []extent, erro
262262
return nil, nil, fmt.Errorf("can't parse comments: %w", err)
263263
}
264264

265-
var validAnnotations []extent
266-
267-
var annotationsWithoutComment []extent
265+
var validAnnotations, annotationsWithoutComment []extent //nolint:prealloc // relax
268266

269267
for _, c := range node.Comments {
270268
if !strings.Contains(c.Text(), IgnoreText) {
271269
continue // does not have annotation continue to next comment
272270
}
273271

274-
if forceComment {
275-
if hasComment(c.Text()) {
276-
validAnnotations = append(validAnnotations, newExtent(fset, c))
277-
} else {
278-
annotationsWithoutComment = append(annotationsWithoutComment, newExtent(fset, c))
279-
}
280-
} else {
281-
validAnnotations = append(validAnnotations, newExtent(fset, c))
272+
validAnnotations = append(validAnnotations, newExtent(fset, c))
273+
274+
if forceComment && !hasComment(c.Text()) {
275+
annotationsWithoutComment = append(annotationsWithoutComment, newExtent(fset, c))
282276
}
283277
}
284278

pkg/testcoverage/coverage/cover_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func Test_findAnnotationsWithComment(t *testing.T) {
169169

170170
validAnnotations, withoutComment, err := FindAnnotations([]byte(source), true)
171171
assert.NoError(t, err)
172-
assert.Equal(t, []int{6}, PluckStartLine(validAnnotations))
172+
assert.Equal(t, []int{3, 6, 9}, PluckStartLine(validAnnotations))
173173
assert.Equal(t, []int{3, 9}, PluckStartLine(withoutComment))
174174

175175
validAnnotations, withoutComment, err = FindAnnotations([]byte(source), false)

pkg/testcoverage/report.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func reportIssuesForHuman(w io.Writer, coverageStats []coverage.Stats) {
6969
}
7070

7171
func reportUncoveredLines(w io.Writer, result AnalyzeResult) {
72-
if result.Pass() || len(result.FilesWithUncoveredLines) == 0 {
72+
if result.PassCoverage() || len(result.FilesWithUncoveredLines) == 0 {
7373
return
7474
}
7575

@@ -97,11 +97,11 @@ func reportMissingExplanations(w io.Writer, result AnalyzeResult) {
9797
tabber := tabwriter.NewWriter(w, 1, 8, 2, '\t', 0) //nolint:mnd // relax
9898
defer tabber.Flush()
9999

100-
fmt.Fprintf(tabber, "\nFiles with missing explanations for coverage-ignore annotations:")
100+
fmt.Fprintf(tabber, "\nFiles with missing explanation for coverage-ignore annotation:")
101101
fmt.Fprintf(tabber, "\n file:\tline numbers:")
102102

103103
for _, stats := range result.FilesWithMissingExplanations {
104-
if len(stats.AnnotationsWithoutComments) == 0 {
104+
if len(stats.AnnotationsWithoutComments) == 0 { // coverage-ignore
105105
continue
106106
}
107107

@@ -155,8 +155,8 @@ func ReportForGithubAction(w io.Writer, result AnalyzeResult) {
155155
out := bufio.NewWriter(w)
156156
defer out.Flush()
157157

158-
reportLineError := func(file, title, msg string) {
159-
fmt.Fprintf(out, "::error file=%s,title=%s,line=1::%s\n", file, title, msg)
158+
reportLineError := func(file, title, msg string, line int) {
159+
fmt.Fprintf(out, "::error file=%s,title=%s,line=%d::%s\n", file, title, line, msg)
160160
}
161161
reportError := func(title, msg string) {
162162
fmt.Fprintf(out, "::error title=%s::%s\n", title, msg)
@@ -168,7 +168,7 @@ func ReportForGithubAction(w io.Writer, result AnalyzeResult) {
168168
"%s: coverage: %s; threshold: %d%%",
169169
title, stats.Str(), stats.Threshold,
170170
)
171-
reportLineError(stats.Name, title, msg)
171+
reportLineError(stats.Name, title, msg, 1)
172172
}
173173

174174
for _, stats := range result.PackagesBelowThreshold {
@@ -189,17 +189,12 @@ func ReportForGithubAction(w io.Writer, result AnalyzeResult) {
189189
reportError(title, msg)
190190
}
191191

192-
// Report missing explanations for coverage-ignore annotations
193192
for _, stats := range result.FilesWithMissingExplanations {
194-
if len(stats.AnnotationsWithoutComments) > 0 {
195-
for _, ann := range stats.AnnotationsWithoutComments {
196-
title := "Missing explanation for coverage-ignore"
197-
msg := title + ": add an explanation after the coverage-ignore annotation"
198-
199-
file := stats.Name
200-
lineNumber := ann
201-
fmt.Fprintf(out, "::error file=%s,title=%s,line=%d::%s\n", file, title, lineNumber, msg)
202-
}
193+
for _, line := range stats.AnnotationsWithoutComments {
194+
title := "Missing explanation for coverage-ignore"
195+
msg := title + ": add an explanation after the coverage-ignore annotation"
196+
197+
reportLineError(stats.Name, title, msg, line)
203198
}
204199
}
205200
}

pkg/testcoverage/report_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -344,31 +344,11 @@ func Test_ReportMissingExplanations(t *testing.T) {
344344
ReportForHuman(buf, result)
345345

346346
output := buf.String()
347-
assert.Contains(t, output, "Files with missing explanations for coverage-ignore annotations:")
347+
assert.Contains(t, output, "Files with missing explanation for coverage-ignore annotation:")
348348
assert.Contains(t, output, "test.go")
349349
assert.Contains(t, output, "10, 20")
350350
})
351351

352-
t.Run("with empty annotations list", func(t *testing.T) {
353-
t.Parallel()
354-
355-
buf := &bytes.Buffer{}
356-
result := AnalyzeResult{
357-
FilesWithMissingExplanations: []coverage.Stats{
358-
{
359-
Name: "test.go",
360-
AnnotationsWithoutComments: []int{},
361-
},
362-
},
363-
}
364-
365-
ReportForHuman(buf, result)
366-
367-
output := buf.String()
368-
// Should not contain the file with empty annotations
369-
assert.NotContains(t, output, "test.go\t")
370-
})
371-
372352
t.Run("with no missing explanations", func(t *testing.T) {
373353
t.Parallel()
374354

pkg/testcoverage/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ func (r *AnalyzeResult) Pass() bool {
3232
len(r.FilesWithMissingExplanations) == 0
3333
}
3434

35+
func (r *AnalyzeResult) PassCoverage() bool {
36+
return r.MeetsTotalCoverage() &&
37+
len(r.FilesBelowThreshold) == 0 &&
38+
len(r.PackagesBelowThreshold) == 0 &&
39+
r.MeetsDiffThreshold()
40+
}
41+
3542
func (r *AnalyzeResult) MeetsDiffThreshold() bool {
3643
if r.DiffThreshold == nil || !r.HasBaseBreakdown {
3744
return true

0 commit comments

Comments
 (0)