prettycov

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 12 Imported by: 0

README

Prettycov

codecov Go All Contributors

Pretty Golang Coverage.

The other day I wanted to output a pretty overall coverage summary in my terminal. I wanted to show a table or a tree with top-level packages and their corresponding coverage. I tried to search for some ready to use tools which would offer something similar but with not luck. After that, I decided to build it on my own. So here it is :)

Installation

go install github.com/screwyprof/prettycov/cmd/prettycov@latest

How to use

Getting built-in help

Run prettycov or prettycov help or prettycov --help to get build-in usage info.

Run your tests with coverage

prettycov works by parsing coverage profile, so the first thing to do is to run tests with coverage:

go test -cover -coverprofile=coverage.out ./...

Show coverage summary up to the given depth

You must specify the path to the coverage profile via -profile flag.

You may also specify -depth to set the maximum depth (starting from 0) of the resulting tree:

❯ prettycov -profile=coverage.out -depth=5
 github.com - 91.08
 └ screwyprof - 91.08
   └ skeleton - 91.08
     ├ internal - 85.73
     │ ├ delivery - 100.00
     │ │ └ rest - 100.00
     │ ├ app - 57.18
     │ │ ├ modzap - 16.70
     │ │ ├ modcfg - 75.00
     │ │ ├ modrel - 87.00
     │ │ └ fxlogger - 50.00
     │ └ adapter - 100.00
     │   └ postgres - 100.00
     ├ cert - 100.00
     │ └ usecase - 100.00
     │   ├ issuecert - 100.00
     │   └ viewcert - 100.00
     └ tests - 87.50
       └ integration - 87.50
         └ postgres - 87.50
Show coverage summary with replaced paths

Sometimes the project may have a long project path (package path to be more precise) which clutters the output. In this case you may want to replace it with a shorter name:

❯ prettycov -profile=coverage.out -depth=5 -old github.com/screwyprof/skeleton -new unicorn
 unicorn - 91.08
 ├ tests - 87.50
 │ └ integration - 87.50
 │   └ postgres - 87.50
 │     └ app - 87.50
 ├ cert - 100.00
 │ └ usecase - 100.00
 │   ├ issuecert - 100.00
 │   └ viewcert - 100.00
 └ internal - 85.73
   ├ app - 57.18
   │ ├ fxlogger - 50.00
   │ ├ modcfg - 75.00
   │ ├ modrel - 87.00
   │ └ modzap - 16.70
   ├ delivery - 100.00
   │ └ rest - 100.00
   │   ├ apierr - 100.00
   │   ├ req - 100.00
   │   └ handler - 100.00
   └ adapter - 100.00
     └ postgres - 100.00
Getting top-level coverage info

This is what I created this tool for. You may get a nice top-level package coverage:

❯ prettycov -profile=coverage.out -old github.com/screwyprof/skeleton -new unicorn -depth=1
 unicorn - 91.08
 ├ cert - 100.00
 ├ internal - 85.73
 └ tests - 87.50

How it works

It calls go tool cover -html under the hood then parses the output to populate a prefix tree of paths and coverages. Then it traverses the tree from the further leaves to top merging the coverage info. Then it draws the tree up to the given depth.

What's next?
Add support for alternative outputs.

You maybe surprised, but go tool cover will generate different figures for -func and -html params... At the moment -html format is used.

Parse stdin for input instead of calling go tool cover directly

At the moment we call go tool cover under the hood. What if we analysed the stdin instead: go tool cover -html=coverage.out -o report.html && cat report.html | prettycov -depth=1

Configure default coverage calculation behaviour

By default, golang tool simply ignores files with no tests (with zero coverage). At the moment prettycov keeps this in mind and does the same thing. However, if you want to get an honest report there should be an option to include zero-covered files into the summary.

Add tests and run prettycov in the CI to get the report for this project

This project was born spontaneously when I was on vacation. I didn't have much time, so just coded the idea with no tests. Now that it proved to be useful it would be great to add tests. For now, I've got a red coverage badge to remind me about it.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


antongr

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLineFormat = errors.New("invalid line format")

Functions

func Cover

func Cover(profile string) (io.Reader, error)

func CoverHTML

func CoverHTML(profile string) (io.Reader, error)

func DisplayTree

func DisplayTree(w io.Writer, tree *PathTree, depth uint)

Types

type BoxType

type BoxType int
const (
	Regular BoxType = iota
	Last
	AfterLast
	Between
)

func (BoxType) String

func (boxType BoxType) String() string

type Coverage

type Coverage struct {
	Items []CoverageItem
	Total float64
}

func ParseHTML

func ParseHTML(r io.Reader) (Coverage, error)

func ParseText

func ParseText(coverage io.Reader) (Coverage, error)

type CoverageItem

type CoverageItem struct {
	File     string
	Coverage float64
}

type PathTree

type PathTree struct {
	Value    float64
	Children map[string]*PathTree
}

func Process

func Process(items []CoverageItem, curRoot, newRoot string) *PathTree

func (*PathTree) Get

func (n *PathTree) Get(key string) *PathTree

func (*PathTree) Put

func (n *PathTree) Put(key string, value float64) bool

func (*PathTree) Walk

func (n *PathTree) Walk(walker Walker)

type Walker

type Walker func(key string, value float64)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL