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

Implement location-based sort order #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 14 additions & 4 deletions cmd/gocov-html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {
listThemes := flag.Bool("lt", false, "list available themes")
theme := flag.String("t", "golang", "theme to use for rendering")
reverseOrder := flag.Bool("r", false, "put lower coverage functions on top")
sortOrder := flag.String("sort", "high-coverage", "sort functions by high-coverage, low-coverage or location")
maxCoverage := flag.Uint64("cmax", 100, "only show functions whose coverage is greater than cmax")
minCoverage := flag.Uint64("cmin", 0, "only show functions whose coverage is smaller than cmin")

Expand Down Expand Up @@ -81,6 +82,15 @@ func main() {
return
}

var sortOrderOpt themes.SortOrder = themes.SortOrder(*sortOrder)
if !sortOrderOpt.Valid() {
log.Fatalf("Invalid sort order: %q\n", sortOrderOpt)
}

if *reverseOrder {
sortOrderOpt = themes.SortOrderLowCoverage
}

switch flag.NArg() {
case 0:
r = os.Stdin
Expand All @@ -94,10 +104,10 @@ func main() {
}

opts := themes.ReportOptions{
LowCoverageOnTop: *reverseOrder,
Stylesheet: *css,
CoverageMin: uint8(*minCoverage),
CoverageMax: uint8(*maxCoverage),
SortOrder: sortOrderOpt,
Stylesheet: *css,
CoverageMin: uint8(*minCoverage),
CoverageMax: uint8(*maxCoverage),
}
if err := themes.HTMLReportCoverage(r, opts); err != nil {
log.Fatal(err)
Expand Down
53 changes: 40 additions & 13 deletions pkg/themes/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
// ReportOptions holds various options used when generating the final
// HTML report.
type ReportOptions struct {
// LowCoverageOnTop puts low coverage functions first.
LowCoverageOnTop bool
// SortOrder decides how to sort functions
SortOrder SortOrder
// Stylesheet is the path to a custom CSS file.
Stylesheet string
// CoverageMin filters out all functions whose code coverage is smaller than it is.
Expand All @@ -31,6 +31,21 @@ type ReportOptions struct {
CoverageMax uint8
}

type SortOrder string

const SortOrderHighCoverage SortOrder = "high-coverage"
const SortOrderLowCoverage SortOrder = "low-coverage"
const SortOrderLocation SortOrder = "location"

func (so SortOrder) Valid() bool {
switch so {
case SortOrderHighCoverage, SortOrderLowCoverage, SortOrderLocation:
return true
default:
return false
}
}

type report struct {
ReportOptions
packages []*gocov.Package
Expand All @@ -45,14 +60,6 @@ func unmarshalJSON(data []byte) (packages []*gocov.Package, err error) {
return
}

type reverse struct {
sort.Interface
}

func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}

// NewReport creates a new report.
func newReport() (r *report) {
r = &report{}
Expand Down Expand Up @@ -98,10 +105,13 @@ func buildReportPackage(pkg *gocov.Package, r *report) reportPackage {
rv.TotalStatements += len(fn.Statements)
rv.ReachedStatements += reached
}
if r.LowCoverageOnTop {
switch r.SortOrder {
case SortOrderHighCoverage:
sort.Sort(rv.Functions)
} else {
sort.Sort(reverse{rv.Functions})
case SortOrderLowCoverage:
sort.Sort(sort.Reverse(rv.Functions))
case SortOrderLocation:
sort.Sort(locationOrderedFunctionList(rv.Functions))
}
return rv
}
Expand Down Expand Up @@ -333,6 +343,23 @@ func (f reportFunction) Lines() []functionLine {
return fls
}

type locationOrderedFunctionList reportFunctionList

func (l locationOrderedFunctionList) Len() int {
return len(l)
}

func (l locationOrderedFunctionList) Less(i, j int) bool {
if l[i].File == l[j].File {
return l[i].Start < l[j].Start
}
return l[i].File < l[j].File
}

func (l locationOrderedFunctionList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

// reportFunctionList is a list of functions for a report.
type reportFunctionList []reportFunction

Expand Down