< Summary - Neospec Coverage

Information
Line coverage
91%
Covered lines: 33
Uncovered lines: 3
Coverable lines: 36
Total lines: 79
Line coverage: 91.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Write0%0091.67%

File(s)

/home/runner/work/neospec/neospec/internal/adapters/reporter/coveralls.go

#LineLine coverage
 1package reporter
 2
 3import (
 4  "context"
 5  "encoding/json"
 6  "fmt"
 7  "io"
 8  "sort"
 9
 10  "github.com/jedi-knights/neospec/internal/domain"
 11)
 12
 13// Coveralls writes coverage data in the Coveralls JSON API format.
 14// https://docs.coveralls.io/api-reference
 15type Coveralls struct{}
 16
 17// NewCoveralls creates a Coveralls reporter.
 18func NewCoveralls() *Coveralls { return &Coveralls{} }
 19
 20// coverallsPayload is the top-level Coveralls JSON structure.
 21type coverallsPayload struct {
 22  RepoToken   string            `json:"repo_token,omitempty"`
 23  ServiceName string            `json:"service_name"`
 24  SourceFiles []coverallsSource `json:"source_files"`
 25}
 26
 27// coverallsSource represents a single source file in the Coveralls format.
 28// Coverage is a sparse array where index is line number - 1, value is hit count
 29// or null for non-executable lines.
 30type coverallsSource struct {
 31  Name     string `json:"name"`
 32  Coverage []*int `json:"coverage"` // nil = not executable
 33}
 34
 435func (c *Coveralls) Write(_ context.Context, w io.Writer, _ *domain.SuiteResult, cov *domain.CoverageData) error {
 236  if cov == nil {
 237    cov = &domain.CoverageData{}
 238  }
 39
 440  payload := coverallsPayload{
 441    ServiceName: "neospec",
 442  }
 443
 344  for _, file := range cov.Files {
 145    if len(file.Lines) == 0 {
 146      continue
 47    }
 48
 49    // Find the maximum line number to size the coverage array.
 250    maxLine := 0
 251    lines := make([]int, 0, len(file.Lines))
 252    for ln := range file.Lines {
 453      lines = append(lines, ln)
 454      if ln > maxLine {
 455        maxLine = ln
 456      }
 57    }
 258    sort.Ints(lines)
 259
 260    // Coveralls coverage array is 0-indexed (line N is at index N-1).
 261    coverage := make([]*int, maxLine)
 262    for _, ln := range lines {
 463      hits := file.Lines[ln]
 464      coverage[ln-1] = &hits
 465    }
 66
 267    payload.SourceFiles = append(payload.SourceFiles, coverallsSource{
 268      Name:     file.Path,
 269      Coverage: coverage,
 270    })
 71  }
 72
 473  data, err := json.MarshalIndent(payload, "", "  ")
 074  if err != nil {
 075    return fmt.Errorf("marshaling coveralls JSON: %w", err)
 076  }
 477  _, err = fmt.Fprintln(w, string(data))
 478  return err
 79}

Methods/Properties

Write