Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tester: Report missing lines in test coverage #2622

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func opaTest(args []string) int {
}
}
} else {
reporter = tester.JSONCoverageReporter{
reporter = tester.PrettyCoverageReporter{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to continue supporting the JSON output format when specified (eg: --format json). This one should be shown when the format is configured to be pretty.

This would introduce an (arguably) breaking change in the CLI behavior... @tsandall thoughts on keeping it default to json if maybe the --format flag was not set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the change I have made? I've kept pretty as the default because the help says so and I'm not really sure if this is the only instance where the default is set to pretty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its probably OK. We should however add a note to the CHANGELOG noting the new behavior.

Cover: cov,
Modules: modules,
Output: os.Stdout,
Expand Down
60 changes: 60 additions & 0 deletions tester/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,66 @@ func (r JSONCoverageReporter) Report(ch chan *Result) error {
return encoder.Encode(report)
}

// PrettyCoverageReporter reports coverage as text.
type PrettyCoverageReporter struct {
Cover *cover.Cover
Modules map[string]*ast.Module
Output io.Writer
Threshold float64
}

// Report prints the test report to the reporter's output.
func (r PrettyCoverageReporter) Report(ch chan *Result) error {
for tr := range ch {
if !tr.Pass() {
if tr.Error != nil {
return tr.Error
}
return errors.New(tr.String())
}
}
report := r.Cover.Report(r.Modules)
if report.Coverage < r.Threshold {
fmt.Fprintln(r.Output, "Missing coverage:")
for name, coverageReport := range report.Files {
if coverageReport.Coverage != 100 {
r.printCoverage(name, coverageReport.NotCovered)
}
}
return &cover.CoverageThresholdError{
Coverage: report.Coverage,
Threshold: r.Threshold,
}
}
for name, coverageReport := range report.Files {
fmt.Fprintf(r.Output, "%v %v%%\n", name, coverageReport.Coverage)
r.hl()
fmt.Fprintln(r.Output, "Covered:")
r.printCoverage(name, coverageReport.Covered)
if len(coverageReport.NotCovered) != 0 {
fmt.Fprintln(r.Output, "Not covered:")
r.printCoverage(name, coverageReport.NotCovered)
}
fmt.Println()
}
return errors.New("")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return nil for a "success" case

}

// Prints the Coverage for a file as text
func (r PrettyCoverageReporter) printCoverage(name string, ar []cover.Range) {
for _, cov := range ar {
if cov.Start.Row == cov.End.Row {
fmt.Fprintf(r.Output, "%v: %v\n", name, cov.Start.Row)
} else {
fmt.Fprintf(r.Output, "%v: %v-%v\n", name, cov.Start.Row, cov.End.Row)
}
}
}

func (r PrettyCoverageReporter) hl() {
fmt.Fprintln(r.Output, strings.Repeat("-", 80))
}

type indentingWriter struct {
w io.Writer
}
Expand Down