simplecov

package module
v0.0.0-...-00faa55 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

README

go-ruby-simplecov/simplecov

simplecov — go-ruby-simplecov

Docs License Go Coverage

A pure-Go (no cgo) reimplementation of the deterministic core of Ruby's SimpleCov coverage gem — the result engine. It models raw per-file line- and branch-hit data, merges it across runs, filters and groups the files, formats a summary, (de)serialises the .resultset.json format, and enforces the coverage thresholds — without any Ruby runtime.

It is the SimpleCov result engine for go-embedded-ruby/rbgo, but a standalone, reusable module.

What it is — and isn't. SimpleCov splits in two: a collector that instruments the running program and records how many times each line and branch executed, and a result engine that models that raw data and does everything else. The result engine is deterministic and needs no interpreter, so it lives here as pure Go: covered/missed/percent (line + branch), filtering, grouping, merging with a staleness window, the .resultset.json (de)serialiser, a text formatter, and the minimum-coverage / coverage-drop gates. The collector is the host's job — in rbgo the VM already tracks execution, so it hands this package a map[filename]FileCoverage and this package does the rest.

The coverage seam

The unit of input is a FileCoverage: one Hit per source line (a coverable hit count, or null = not coverable) plus optional Branches. rbgo builds the map from its VM coverage tables; nothing here instruments code or opens Ruby.

sc := simplecov.New(
    simplecov.WithRoot("/proj"),
    simplecov.WithClock(time.Now),
)
sc.AddStringFilter("/spec/")                 // add_filter "/spec/"
sc.AddStringGroup("Models", "/app/models/")  // add_group "Models", "app/models"
sc.MinimumCoverage(simplecov.Line, 90)       // minimum_coverage 90

result := sc.NewResult("RSpec", map[string]simplecov.FileCoverage{
    "/proj/app/models/user.rb": {
        Lines: []simplecov.Hit{
            simplecov.Uncoverable(), // null    — e.g. a comment
            simplecov.Coverable(1),  // hit once
            simplecov.Coverable(0),  // missed
        },
    },
})

fmt.Println(result.CoveredPercent())          // 50
code, violations := sc.RunChecks(result, nil) // code == simplecov.MinimumCoverage

Features

Faithful port of SimpleCov's result engine:

  • Value modelHit (coverable count or null), Branches (condition→branch→count), SourceFile (covered/missed/never lines, percent, strength, branch metrics), FileList (aggregates, LeastCoveredFile, CoveredPercentages), and Result (named, timestamped, filtered, grouped).
  • FiltersStringFilter (project-relative substring), RegexFilter, BlockFilter, ArrayFilter (any-of); AddFilter excludes matching files.
  • GroupsAddGroup("Models", filter) partitions the files in definition order, with an automatic "Ungrouped" bucket.
  • MergingMergeResults sums coverage across runs (element-wise lines + branch counts, combined command name); MergeStored prunes results older than the MergeTimeout window before merging, against the injected clock.
  • .resultset.jsonParseResultset / Resultset.JSON round-trip SimpleCov's {"Command": {"coverage": {file: {"lines":[…], "branches":{…}}}, "timestamp": N}} format (and the legacy bare-array line form on read).
  • ThresholdsMinimumCoverage, MinimumCoverageByFile, MaximumCoverageDrop, RefuseCoverageDrop (line + branch), returning a SimpleCov ExitCode and the list of Violations.
  • Formatter — a Formatter interface seam with SimpleFormatter (SimpleCov's console summary); the HTML formatter is out of scope.
  • Seams — the filesystem (FS) and clock are injectable, so resultset load/store, source loading, timestamps, and the merge window are deterministic.

CGO-free, dependency-free (stdlib only), 100% test coverage, gofmt + go vet clean, and green across the six 64-bit Go targets (amd64, arm64, riscv64, loong64, ppc64le, s390x — big-endian) plus js/wasm and wasip1/wasm.

Install

go get github.com/go-ruby-simplecov/simplecov

Ruby surface it stands in for

SimpleCov (Ruby) this package
SimpleCov.start New(opts…)
add_filter "…" / %r{…} / { … } AddStringFilter / AddRegexpFilter / AddBlockFilter
add_group "Models", "app/models" AddGroup / AddStringGroup
minimum_coverage 90 MinimumCoverage(Line, 90)
minimum_coverage_by_file 80 MinimumCoverageByFile(Line, 80)
maximum_coverage_drop 5 MaximumCoverageDrop(Line, 5)
refuse_coverage_drop RefuseCoverageDrop()
SimpleCov::Result / SourceFile / FileList Result / SourceFile / FileList
.resultset.json (de)serialise ParseResultset / Resultset.JSON
SimpleCov::ResultMerger MergeResults / (*SimpleCov).MergeStored
SimpleCov::Formatter::SimpleFormatter SimpleFormatter

Tests & coverage

The suite is deterministic: an in-memory FS and a fixed clock drive resultset load/store, source loading, timestamps, and the merge staleness window, so every branch — filter/group edge cases, merge stale-timeout, threshold pass/fail, resultset round-trip, and FS-error paths — holds coverage at 100% on every OS and arch lane.

COVERPKG=$(go list ./... | paste -sd, -)
go test -race -coverpkg="$COVERPKG" -coverprofile=cover.out ./...
go tool cover -func=cover.out | tail -1   # 100.0%

WebAssembly

Being pure Go (CGO=0), this library also compiles to WebAssembly — both GOOS=js GOARCH=wasm (browser / Node.js) and GOOS=wasip1 GOARCH=wasm (WASI). CI builds both targets on every push, alongside the six 64-bit native/qemu arches.

GOOS=js     GOARCH=wasm go build ./...   # browser / Node
GOOS=wasip1 GOARCH=wasm go build ./...   # WASI (wasmtime, wasmer, wasmedge, …)

License

BSD-3-Clause — see LICENSE. Copyright the go-ruby-simplecov/simplecov authors.

Documentation

Overview

Package simplecov is a pure-Go (CGO-free) reimplementation of the deterministic core of Ruby's SimpleCov coverage gem — the result engine.

SimpleCov splits into two halves: a collector that instruments the running program and records, per file, how many times each line (and branch) was hit; and a result engine that models that raw hit data, merges it across runs, filters and groups the files, formats a summary, and enforces coverage thresholds. Everything in the second half is deterministic and needs no Ruby interpreter, so it lives here as pure Go. The collector is the host's job: in go-embedded-ruby/rbgo the VM already tracks execution, so it feeds this package the per-file line-hit arrays and this package does the rest.

The coverage seam

The unit of input is a FileCoverage: a slice of Hit (one per source line; a Hit is either an integer hit count or "not coverable" — null in the resultset) and, optionally, Branches. rbgo builds a map[filename]FileCoverage from its VM coverage tables and hands it to SimpleCov.NewResult; nothing in this package instruments code or opens Ruby.

Value model

  • Hit — one line's coverage: coverable+count, or null.
  • Branches — nested condition→branch→count map (branch coverage).
  • SourceFile — one file: covered/missed/never lines, percent, strength.
  • FileList — a collection of SourceFile with aggregate metrics.
  • Result — a named, timestamped FileList, filtered and grouped.
  • Resultset — the .resultset.json shape: command → coverage + timestamp.

Ruby surface it stands in for

SimpleCov.start                          -> New(opts...)
SimpleCov.add_filter "…" / %r{…} / {…}   -> AddStringFilter / AddRegexpFilter / AddBlockFilter
SimpleCov.add_group "Models", "app/…"    -> AddGroup
SimpleCov.minimum_coverage 90            -> MinimumCoverage
SimpleCov.minimum_coverage_by_file 80    -> MinimumCoverageByFile
SimpleCov.maximum_coverage_drop 5        -> MaximumCoverageDrop
SimpleCov.refuse_coverage_drop           -> RefuseCoverageDrop
SimpleCov::Result / SourceFile / FileList-> Result / SourceFile / FileList
.resultset.json (de)serialise            -> ParseResultset / Resultset.JSON
SimpleCov::ResultMerger                  -> MergeResults / (*SimpleCov).MergeStored
SimpleCov::Formatter::SimpleFormatter    -> SimpleFormatter

Seams

The filesystem (FS) and the clock (a func() time.Time) are injectable, so resultset load/store, source loading, timestamps and the merge staleness window are all deterministic under test. The default FS is os-backed and the default clock is time.Now.

Index

Constants

View Source
const DefaultMergeTimeout = 600 * time.Second

DefaultMergeTimeout is SimpleCov's default merge window: results older than this (relative to the clock) are pruned before merging.

View Source
const DefaultResultsetPath = ".resultset.json"

DefaultResultsetPath is SimpleCov's default resultset filename.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayFilter

type ArrayFilter struct {
	Filters []Filter
}

ArrayFilter matches when any of its member filters match, mirroring SimpleCov::ArrayFilter.

func (ArrayFilter) Matches

func (f ArrayFilter) Matches(s *SourceFile) bool

Matches reports whether any member filter matches.

type BlockFilter

type BlockFilter struct {
	Fn func(*SourceFile) bool
}

BlockFilter matches when its function returns true for the file, mirroring SimpleCov::BlockFilter (add_filter { |source_file| … }).

func (BlockFilter) Matches

func (f BlockFilter) Matches(s *SourceFile) bool

Matches delegates to the block.

type Branches

type Branches map[string]map[string]int

Branches models SimpleCov's branch-coverage table for one file: a mapping from a condition key (e.g. "[:if, 0, 3, 6, 3, 21]") to that condition's branches (e.g. "[:then, 1, …]" and "[:else, 2, …]"), each carrying its hit count. Every inner entry is one branch; a branch with count 0 is missed, >0 is covered.

func (Branches) Covered

func (b Branches) Covered() int

Covered is the number of branches executed at least once.

func (Branches) CoveredPercent

func (b Branches) CoveredPercent() float64

CoveredPercent is covered/total * 100. A file with no branches is 100%.

func (Branches) Missed

func (b Branches) Missed() int

Missed is the number of branches never executed.

func (Branches) Total

func (b Branches) Total() int

Total is the number of distinct branches across every condition.

type Check

type Check struct {
	MinimumCoverage       map[Criterion]float64
	MinimumCoverageByFile map[Criterion]float64
	MaximumCoverageDrop   map[Criterion]float64
}

Check holds the configured coverage thresholds. Each map is keyed by criterion (Line/Branch); an absent criterion means "no gate on that dimension".

func (Check) Run

func (c Check) Run(result, previous *Result) (ExitCode, []Violation)

Run evaluates the thresholds against result (and, for drop checks, the previous run — pass nil when there is none). It returns the exit code SimpleCov would use and every violation found, sorted deterministically. A minimum-coverage breach outranks a drop breach.

type CommandResult

type CommandResult struct {
	Coverage  map[string]FileCoverage `json:"coverage"`
	Timestamp int64                   `json:"timestamp"`
}

CommandResult is one command's entry in a resultset: the per-file coverage and the Unix timestamp at which it was recorded.

type Criterion

type Criterion string

Criterion names a coverage dimension SimpleCov can gate on.

const (
	// Line gates on line coverage.
	Line Criterion = "line"
	// Branch gates on branch coverage.
	Branch Criterion = "branch"
)

type ExitCode

type ExitCode int

ExitCode mirrors SimpleCov::ExitCodes: the process exit status a coverage run yields.

const (
	// Success — all thresholds met.
	Success ExitCode = 0
	// Exception — an error occurred (reserved; parity with SimpleCov).
	Exception ExitCode = 1
	// MinimumCoverage — an overall or per-file minimum was not met.
	MinimumCoverage ExitCode = 2
	// MaximumCoverageDrop — coverage dropped more than allowed vs the last run.
	MaximumCoverageDrop ExitCode = 3
)

type FS

type FS interface {
	ReadFile(name string) ([]byte, error)
	WriteFile(name string, data []byte, perm fs.FileMode) error
}

FS is the filesystem seam: resultset load/store and source loading go through it, so tests can inject a deterministic, in-memory filesystem and exercise error branches. The default implementation (OSFS) is os-backed.

type FileCoverage

type FileCoverage struct {
	Lines    []Hit
	Branches Branches
}

FileCoverage is the raw per-file coverage the host (rbgo's VM) feeds in: one Hit per source line and, optionally, branch coverage. It is also the on-disk shape inside a resultset's "coverage" map.

func MergeCoverage

func MergeCoverage(a, b FileCoverage) FileCoverage

MergeCoverage merges two files' coverage (lines and branches).

func (FileCoverage) MarshalJSON

func (fc FileCoverage) MarshalJSON() ([]byte, error)

MarshalJSON emits a file's coverage in SimpleCov's current nested form, {"lines":[…]}, adding "branches" only when branch data is present.

func (*FileCoverage) UnmarshalJSON

func (fc *FileCoverage) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts both SimpleCov's current nested form ({"lines":[…]}) and the legacy bare-array form ([null,1,0]).

type FileList

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

FileList is SimpleCov::FileList: an ordered collection of SourceFile with aggregate coverage metrics.

func NewFileList

func NewFileList(files ...*SourceFile) *FileList

NewFileList builds a FileList over the given source files (order preserved).

func (*FileList) BranchesCoveredPercent

func (fl *FileList) BranchesCoveredPercent() float64

BranchesCoveredPercent is total covered / total branches * 100 (100 with no branches).

func (*FileList) CoveredBranches

func (fl *FileList) CoveredBranches() int

CoveredBranches is the total executed branches across all files.

func (*FileList) CoveredLinesCount

func (fl *FileList) CoveredLinesCount() int

CoveredLinesCount is the total covered coverable lines across all files.

func (*FileList) CoveredPercent

func (fl *FileList) CoveredPercent() float64

CoveredPercent is total covered / total coverable * 100. An empty list, or one with no coverable lines, is 100%.

func (*FileList) CoveredPercentages

func (fl *FileList) CoveredPercentages() []float64

CoveredPercentages is each file's CoveredPercent, in file order.

func (*FileList) CoveredStrength

func (fl *FileList) CoveredStrength() float64

CoveredStrength is the average hit count over all coverable lines, rounded to one decimal. Zero for an empty list or one with no coverable lines.

func (*FileList) Files

func (fl *FileList) Files() []*SourceFile

Files returns the underlying source files (do not mutate).

func (*FileList) LeastCoveredFile

func (fl *FileList) LeastCoveredFile() *SourceFile

LeastCoveredFile is the file with the lowest CoveredPercent, or nil if empty.

func (*FileList) Len

func (fl *FileList) Len() int

Len is the number of files.

func (*FileList) LinesOfCode

func (fl *FileList) LinesOfCode() int

LinesOfCode is the total coverable lines across all files.

func (*FileList) MissedBranches

func (fl *FileList) MissedBranches() int

MissedBranches is the total never-executed branches across all files.

func (*FileList) MissedLinesCount

func (fl *FileList) MissedLinesCount() int

MissedLinesCount is the total missed coverable lines across all files.

func (*FileList) NeverLinesCount

func (fl *FileList) NeverLinesCount() int

NeverLinesCount is the total non-coverable lines across all files.

func (*FileList) TotalBranches

func (fl *FileList) TotalBranches() int

TotalBranches is the total branch count across all files.

type Filter

type Filter interface {
	Matches(*SourceFile) bool
}

Filter decides whether a source file matches a rule. A filter added via AddFilter excludes matching files from a result; a filter attached to a group selects the files that belong to that group. This mirrors SimpleCov::Filter.

type Formatter

type Formatter interface {
	Format(*Result) string
}

Formatter renders a result to text. It is the seam SimpleCov's formatter registry fills; the HTML formatter is out of scope, but any consumer can implement this interface.

type Hit

type Hit struct {
	Valid bool
	Count int
}

Hit is one source line's coverage datum. SimpleCov stores each line as either an integer hit count (0 = executed zero times = missed, >0 = covered) or null, meaning the line is not coverable (blank, comment, structural). Valid==false models null; Valid==true carries Count.

func Coverable

func Coverable(count int) Hit

Coverable returns a coverable line with the given hit count (0 = missed).

func Uncoverable

func Uncoverable() Hit

Uncoverable returns a non-coverable line (null in the resultset).

func (Hit) Covered

func (h Hit) Covered() bool

Covered reports whether the line is coverable and was hit at least once.

func (Hit) MarshalJSON

func (h Hit) MarshalJSON() ([]byte, error)

MarshalJSON encodes a coverable line as its integer count and a non-coverable line as JSON null, matching SimpleCov's resultset line arrays.

func (Hit) Missed

func (h Hit) Missed() bool

Missed reports whether the line is coverable and was never hit.

func (*Hit) UnmarshalJSON

func (h *Hit) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes a resultset line entry: null → non-coverable, an integer → coverable with that count.

type NamedFileList

type NamedFileList struct {
	Name  string
	Files *FileList
}

NamedFileList is a group name paired with its files, as produced by grouping.

type OSFS

type OSFS struct{}

OSFS is the production FS, backed by the os package.

func (OSFS) ReadFile

func (OSFS) ReadFile(name string) ([]byte, error)

ReadFile reads a file from disk.

func (OSFS) WriteFile

func (OSFS) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile writes a file to disk.

type Option

type Option func(*SimpleCov)

Option configures a SimpleCov at construction.

func WithClock

func WithClock(clock func() time.Time) Option

WithClock overrides the clock seam used for timestamps and merge staleness.

func WithCommandName

func WithCommandName(name string) Option

WithCommandName sets the default command name for results.

func WithFS

func WithFS(f FS) Option

WithFS overrides the filesystem seam.

func WithMergeTimeout

func WithMergeTimeout(d time.Duration) Option

WithMergeTimeout overrides the merge staleness window.

func WithRoot

func WithRoot(root string) Option

WithRoot sets the project root used to derive project-relative paths.

type RegexFilter

type RegexFilter struct {
	Re *regexp.Regexp
}

RegexFilter matches when its pattern matches the file's absolute filename, mirroring SimpleCov::RegexFilter.

func (RegexFilter) Matches

func (f RegexFilter) Matches(s *SourceFile) bool

Matches reports whether the pattern matches the file's filename.

type Result

type Result struct {
	Files       *FileList
	CommandName string
	CreatedAt   time.Time
	// contains filtered or unexported fields
}

Result is SimpleCov::Result: a named, timestamped, filtered and grouped view over a set of source files.

func MergeResults

func MergeResults(results []*Result) *Result

MergeResults combines several results into one, summing coverage per file across the inputs. The merged command name is the sorted, de-duplicated set of input command names joined by ", " and the timestamp is the most recent input. Merging an empty slice yields an empty result.

func NewResult

func NewResult(commandName string, cov map[string]FileCoverage, t time.Time) *Result

NewResult builds an unfiltered, ungrouped result directly from raw coverage, stamped at t. Use (*SimpleCov).NewResult to also apply filters and groups.

func (*Result) BranchesCoveredPercent

func (r *Result) BranchesCoveredPercent() float64

BranchesCoveredPercent is the result's overall branch coverage percentage.

func (*Result) CoveredLines

func (r *Result) CoveredLines() int

CoveredLines is the total covered coverable lines.

func (*Result) CoveredPercent

func (r *Result) CoveredPercent() float64

CoveredPercent is the result's overall line coverage percentage.

func (*Result) CoveredPercentFor

func (r *Result) CoveredPercentFor(c Criterion) float64

CoveredPercentFor returns the result's coverage percentage for the criterion.

func (*Result) CoveredStrength

func (r *Result) CoveredStrength() float64

CoveredStrength is the result's overall average hit count per coverable line.

func (*Result) Groups

func (r *Result) Groups() []NamedFileList

Groups returns the result's named groups in definition order (empty when no groups were configured), matching SimpleCov::Result#groups.

func (*Result) LeastCoveredFile

func (r *Result) LeastCoveredFile() *SourceFile

LeastCoveredFile is the least-covered file, or nil when there are none.

func (*Result) MissedLines

func (r *Result) MissedLines() int

MissedLines is the total missed coverable lines.

func (*Result) ToResultset

func (r *Result) ToResultset() Resultset

ToResultset serialises a result back into a single-command resultset, ready to merge into or replace an on-disk .resultset.json.

func (*Result) TotalLines

func (r *Result) TotalLines() int

TotalLines is the total coverable lines (covered + missed).

type Resultset

type Resultset map[string]CommandResult

Resultset is SimpleCov's .resultset.json shape: a map from command name to that command's coverage and timestamp, e.g.

{ "RSpec": { "coverage": { "/a.rb": {"lines":[null,1,0]} }, "timestamp": 172… } }

func ParseResultset

func ParseResultset(data []byte) (Resultset, error)

ParseResultset decodes .resultset.json bytes.

func (Resultset) JSON

func (rs Resultset) JSON() ([]byte, error)

JSON encodes the resultset as .resultset.json bytes. Map keys are emitted in sorted order by the stdlib encoder, so output is deterministic.

func (Resultset) Results

func (rs Resultset) Results() []*Result

Results turns a resultset into unfiltered results, one per command, ordered by command name for determinism.

type SimpleCov

type SimpleCov struct {
	CommandName  string
	Root         string
	MergeTimeout time.Duration
	// contains filtered or unexported fields
}

SimpleCov is the configuration object and orchestrator — the Go stand-in for the Ruby SimpleCov module. Build one with New, configure filters, groups and thresholds, then turn raw coverage into a Result with NewResult. The FS and clock seams make every step deterministic.

func New

func New(opts ...Option) *SimpleCov

New builds a SimpleCov with SimpleCov's defaults (os FS, time.Now clock, 600s merge timeout), then applies the options.

func (*SimpleCov) AddBlockFilter

func (sc *SimpleCov) AddBlockFilter(fn func(*SourceFile) bool)

AddBlockFilter excludes files for which fn returns true.

func (*SimpleCov) AddFilter

func (sc *SimpleCov) AddFilter(f Filter)

AddFilter registers a file-exclusion filter (SimpleCov.add_filter).

func (*SimpleCov) AddGroup

func (sc *SimpleCov) AddGroup(name string, filter Filter)

AddGroup registers a named group selected by filter (SimpleCov.add_group).

func (*SimpleCov) AddRegexpFilter

func (sc *SimpleCov) AddRegexpFilter(re *regexp.Regexp)

AddRegexpFilter excludes files whose filename matches re.

func (*SimpleCov) AddStringFilter

func (sc *SimpleCov) AddStringFilter(arg string)

AddStringFilter excludes files whose project path contains arg.

func (*SimpleCov) AddStringGroup

func (sc *SimpleCov) AddStringGroup(name, arg string)

AddStringGroup registers a group selecting files whose project path contains arg — the common SimpleCov.add_group "Name", "path" form.

func (*SimpleCov) Check

func (sc *SimpleCov) Check() Check

Check returns the configured thresholds.

func (*SimpleCov) Filtered

func (sc *SimpleCov) Filtered(fl *FileList) *FileList

Filtered returns a copy of fl with every file matching any exclusion filter removed (SimpleCov.filtered).

func (*SimpleCov) Grouped

func (sc *SimpleCov) Grouped(fl *FileList) []NamedFileList

Grouped partitions fl into the configured groups, in definition order, appending an "Ungrouped" bucket for files in no group. With no groups configured it returns nil, matching SimpleCov.grouped.

func (*SimpleCov) LoadResultset

func (sc *SimpleCov) LoadResultset(path string) (Resultset, error)

LoadResultset reads and parses a resultset file through the FS seam. A missing file is not an error: it yields an empty resultset, matching SimpleCov's treatment of a first run.

func (*SimpleCov) LoadSource

func (sc *SimpleCov) LoadSource(sf *SourceFile) error

LoadSource fills sf.Src by reading sf.Filename through the FS seam.

func (*SimpleCov) MaximumCoverageDrop

func (sc *SimpleCov) MaximumCoverageDrop(c Criterion, pct float64)

MaximumCoverageDrop sets the largest allowed drop vs the last run (maximum_coverage_drop).

func (*SimpleCov) MergeStored

func (sc *SimpleCov) MergeStored(rs Resultset) *Result

MergeStored merges the fresh commands in a resultset into a single result, dropping any command whose timestamp is older than the merge timeout relative to the injected clock — mirroring SimpleCov::ResultMerger's stale-result pruning. Commands are merged in command-name order for determinism.

func (*SimpleCov) MinimumCoverage

func (sc *SimpleCov) MinimumCoverage(c Criterion, pct float64)

MinimumCoverage sets the overall minimum for a criterion (minimum_coverage).

func (*SimpleCov) MinimumCoverageByFile

func (sc *SimpleCov) MinimumCoverageByFile(c Criterion, pct float64)

MinimumCoverageByFile sets the per-file minimum (minimum_coverage_by_file).

func (*SimpleCov) NewResult

func (sc *SimpleCov) NewResult(commandName string, cov map[string]FileCoverage) *Result

NewResult builds a result from raw coverage: it constructs the source files, applies exclusion filters, groups the survivors, and stamps the result with the clock. An empty commandName falls back to the configured CommandName.

func (*SimpleCov) RefuseCoverageDrop

func (sc *SimpleCov) RefuseCoverageDrop(criteria ...Criterion)

RefuseCoverageDrop forbids any drop (drop threshold 0) for the given criteria, defaulting to Line when none are named (refuse_coverage_drop).

func (*SimpleCov) RunChecks

func (sc *SimpleCov) RunChecks(result, previous *Result) (ExitCode, []Violation)

RunChecks evaluates the configured thresholds (see Check.Run).

func (*SimpleCov) StoreResultset

func (sc *SimpleCov) StoreResultset(path string, rs Resultset) error

StoreResultset encodes rs and writes it to path through the FS seam.

type SimpleFormatter

type SimpleFormatter struct{}

SimpleFormatter reproduces SimpleCov::Formatter::SimpleFormatter: for each group it prints the group name, a rule, then one line per file with its coverage percentage. As in SimpleCov, a result with no configured groups produces no output.

func (SimpleFormatter) Format

func (SimpleFormatter) Format(r *Result) string

Format renders the result.

type SourceFile

type SourceFile struct {
	Filename string
	Lines    []Hit
	Branches Branches
	Src      []string
}

SourceFile is SimpleCov::SourceFile: one covered file's line and branch data, plus optional source text. Filename is absolute (as SimpleCov stores it); Src, when loaded through the FS seam, holds the file's lines for rendering.

func (*SourceFile) BranchesCoveredPercent

func (s *SourceFile) BranchesCoveredPercent() float64

BranchesCoveredPercent is covered/total branches * 100 (100 with no branches).

func (*SourceFile) CoveredBranches

func (s *SourceFile) CoveredBranches() int

CoveredBranches is the number of executed branches.

func (*SourceFile) CoveredLinesCount

func (s *SourceFile) CoveredLinesCount() int

CoveredLinesCount is the number of coverable lines hit at least once.

func (*SourceFile) CoveredPercent

func (s *SourceFile) CoveredPercent() float64

CoveredPercent is covered/relevant * 100. A file with no coverable lines is reported as 100%, matching SimpleCov's no_lines? shortcut.

func (*SourceFile) CoveredStrength

func (s *SourceFile) CoveredStrength() float64

CoveredStrength is the average hit count over coverable lines, rounded to one decimal (SimpleCov's covered_strength). Zero when there are no coverable lines.

func (*SourceFile) LinesOfCode

func (s *SourceFile) LinesOfCode() int

LinesOfCode is an alias for RelevantLines, matching SimpleCov's terminology.

func (*SourceFile) LinesStrength

func (s *SourceFile) LinesStrength() int

LinesStrength is the sum of hit counts over coverable lines.

func (*SourceFile) MissedBranches

func (s *SourceFile) MissedBranches() int

MissedBranches is the number of never-executed branches.

func (*SourceFile) MissedLinesCount

func (s *SourceFile) MissedLinesCount() int

MissedLinesCount is the number of coverable lines never hit.

func (*SourceFile) NeverLinesCount

func (s *SourceFile) NeverLinesCount() int

NeverLinesCount is the number of non-coverable (null) lines.

func (*SourceFile) ProjectFilename

func (s *SourceFile) ProjectFilename(root string) string

ProjectFilename is Filename with a leading root prefix stripped, mirroring SimpleCov's project_filename used by string filters and groups.

func (*SourceFile) RelevantLines

func (s *SourceFile) RelevantLines() int

RelevantLines is the number of coverable lines (covered + missed); SimpleCov's lines_of_code.

func (*SourceFile) TotalBranches

func (s *SourceFile) TotalBranches() int

TotalBranches is the file's branch count.

type StringFilter

type StringFilter struct {
	Arg  string
	Root string
}

StringFilter matches when its argument is a substring of the file's project-relative path, mirroring SimpleCov::StringFilter (which compares against project_filename, i.e. the path with the root stripped).

func (StringFilter) Matches

func (f StringFilter) Matches(s *SourceFile) bool

Matches reports whether Arg occurs in the file's project filename.

type Violation

type Violation struct {
	Kind      ViolationKind
	Criterion Criterion
	File      string // set only for BelowMinimumByFile
	Actual    float64
	Expected  float64
}

Violation records a single failed threshold check.

type ViolationKind

type ViolationKind int

ViolationKind classifies a threshold breach.

const (
	// BelowMinimum — overall coverage below minimum_coverage.
	BelowMinimum ViolationKind = iota
	// BelowMinimumByFile — a file below minimum_coverage_by_file.
	BelowMinimumByFile
	// ExceededDrop — coverage dropped more than maximum_coverage_drop.
	ExceededDrop
)

Jump to

Keyboard shortcuts

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