covet

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

covet

Cover it!

covet reads version control diffs and Go coverage files to generate useful stats about their intersection. Currently displays coverage percentage of the diff and highlights any new, uncovered lines.

go install github.com/johnstarich/go/covet/cmd/covet@latest
covet -help

git diff HEAD~10...HEAD > my.diff
go test -coverprofile=cover.out ./...

covet -diff-file my.diff -cover-go cover.out
#  ... includes diff and coverage intersection ...
# Total diff coverage:  84.1%
#
# Diff coverage is below target. Add tests for these files:
#
# ┌─────────┬──────────────┬────────────────────────────────────────────┐
# │ LINES   │ COVERAGE     │ FILE                                       │
# ├─────────┼──────────────┼────────────────────────────────────────────┤
# │ 239/279 │  85.7% ████▎ │ diffcover/cmd/diffcover/run.go             │
# │  16/42  │  38.1% █▉    │ diffcover/cmd/diffcover/coverage_status.go │
# └─────────┴──────────────┴────────────────────────────────────────────┘

Still experimental: Future releases may contain breaking changes.

Thoughts or questions? Please open an issue to discuss.

Integrate with GitHub

Covet includes an automatic GitHub Actions integration and opt-in GitHub comment summary reports.

To enable comment summaries, you need to set a few additional flags.

Here's an example using GitHub Actions:

  test:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
    - uses: actions/checkout@v3
    - name: checkout
      run: |
        commits=${{ github.event.pull_request.commits }}
        if [[ -n "$commits" ]]; then
          # Prepare enough depth for diffs with master
          git fetch --depth="$(( commits + 1 ))"
        fi
    - uses: actions/setup-go@v3
      with:
        go-version: 1.16.x
    - name: Test
      run: |
        git diff origin/master | \
            covet \
                -diff-file - \
                -cover-go ./cover.out \
                -show-diff-coverage \
                -gh-token "$GITHUB_TOKEN" \
                -gh-issue "github.com/${GITHUB_REPOSITORY}/pull/${ISSUE_NUMBER}" 
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

And here's an example using environment variables from Travis CI:

git diff origin/master | \
    covet \
        -diff-file - \
        -cover-go ./cover.out \
        -show-diff-coverage \
        -gh-token "$GITHUB_TOKEN" \
        -gh-issue "github.com/${TRAVIS_PULL_REQUEST_SLUG}/pull/${TRAVIS_PULL_REQUEST}"

Documentation

Overview

Package covet reads version control diffs and Go coverage files to generate reports on their intersection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Covet

type Covet struct {
	// contains filtered or unexported fields
}

Covet generates reports for a diff and coverage combination

func Parse

func Parse(options Options) (covet *Covet, err error)

Parse reads and parses both a diff file and Go coverage file, then returns a Covet instance to render reports

func (*Covet) DiffCoverageFiles

func (c *Covet) DiffCoverageFiles() []File

DiffCoverageFiles generates a report of all files that are in the diff and have coverage information. These files can be used to display diff coverage information.

func (*Covet) DiffCovered

func (c *Covet) DiffCovered() float64

DiffCovered returns the percentage of covered lines in the diff.

func (*Covet) PriorityUncoveredFiles added in v0.1.1

func (c *Covet) PriorityUncoveredFiles(target uint) []File

PriorityUncoveredFiles returns a list of Files prioritized by the largest uncovered sections of the diff. The list contains just enough Files worth of uncovered lines necessary to meet the provided 'target' coverage.

func (*Covet) ReportFileCoverage added in v0.1.1

func (c *Covet) ReportFileCoverage(w io.Writer, f File, options ReportFileCoverageOptions) error

ReportFileCoverage writes a diff-like plain text report with color to 'w'.

func (*Covet) ReportSummaryColorTerminal added in v0.1.1

func (c *Covet) ReportSummaryColorTerminal(w io.Writer, options ReportSummaryOptions) error

ReportSummaryColorTerminal writes a plain text report with color to 'w'.

func (*Covet) ReportSummaryMarkdown added in v0.1.1

func (c *Covet) ReportSummaryMarkdown(w io.Writer, options ReportSummaryOptions) error

ReportSummaryMarkdown writes a markdown report to 'w'. To capture as a string, write to a bytes.Buffer, then call buf.String().

type DiffChunk

type DiffChunk = coverfile.DiffChunk

DiffChunk is a chunk of lines parsed from a Covet report. Includes the associated beginning and end line numbers of the chunk and the diff-like lines of text from the file.

func DiffChunks

func DiffChunks(file File, fileReader io.Reader) ([]DiffChunk, error)

DiffChunks return diff-like Chunks from a covet.File and the file contents' Reader.

type File

type File = coverfile.File

File represents a file parsed in a Covet report. Includes the file name and which lines are covered.

type Line

type Line = coverfile.Line

Line represents a line in a file parsed in a Covet report

type Options

type Options struct {
	// FS is the file system to read files, Go package information, and more.
	// Defaults to hackpadfs's os.NewFS(). If on Windows, the default targets the current working directory's volume (e.g. C:\).
	FS fs.FS
	// Diff is a reader with patch or diff formatted contents
	Diff io.Reader
	// DiffBaseDir is the FS path to the repo's root directory
	DiffBaseDir string
	// GoCoverage is the FS path to a Go coverage file
	GoCoveragePath string
	// GoCoverageBaseDir is the FS path to the coverage file's module. Defaults to the coverage file's directory.
	GoCoverageBaseDir string
}

Options contains parse options

type ReportFileCoverageOptions added in v0.1.1

type ReportFileCoverageOptions struct{}

ReportFileCoverageOptions contains options to format a file coverage report. Reserved for future use.

type ReportSummaryOptions added in v0.1.1

type ReportSummaryOptions struct {
	Target uint
}

ReportSummaryOptions contains summary report options

Directories

Path Synopsis
cmd
covet
Command covet reads version control diffs and Go coverage files to generate reports on their intersection.
Command covet reads version control diffs and Go coverage files to generate reports on their intersection.
internal
coverfile
Package coverfile parses Covet reports and formats files in the diff
Package coverfile parses Covet reports and formats files in the diff
coverstatus
Package coverstatus contains coverage color-coding and labeling for generating reports.
Package coverstatus contains coverage color-coding and labeling for generating reports.
fspath
Package fspath contains FS path manipulation tools, much like the standard library's "path/filepath" package.
Package fspath contains FS path manipulation tools, much like the standard library's "path/filepath" package.
minmax
Package minmax contains several type-specific min and max functions.
Package minmax contains several type-specific min and max functions.
packages
Package packages finds files related to a package pattern string.
Package packages finds files related to a package pattern string.
span
Package span contains Span, a numeric range.
Package span contains Span, a numeric range.
summary
Package summary generates summary reports in various formats.
Package summary generates summary reports in various formats.
testhelpers
Package testhelpers contains file system generators for use in tests.
Package testhelpers contains file system generators for use in tests.

Jump to

Keyboard shortcuts

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