< Summary - Neospec Coverage

Information
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 119
Line coverage: 100%
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%00100%

File(s)

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

#LineLine coverage
 1package reporter
 2
 3import (
 4  "context"
 5  "encoding/xml"
 6  "fmt"
 7  "io"
 8  "sort"
 9  "time"
 10
 11  "github.com/jedi-knights/neospec/internal/domain"
 12)
 13
 14// Cobertura writes coverage data in Cobertura XML format.
 15// https://cobertura.github.io/cobertura/
 16type Cobertura struct{}
 17
 18// NewCobertura creates a Cobertura reporter.
 19func NewCobertura() *Cobertura { return &Cobertura{} }
 20
 21// coberturaXML is the root element of the Cobertura XML report.
 22type coberturaXML struct {
 23  XMLName      xml.Name          `xml:"coverage"`
 24  Version      string            `xml:"version,attr"`
 25  Timestamp    int64             `xml:"timestamp,attr"`
 26  LinesValid   int               `xml:"lines-valid,attr"`
 27  LinesCovered int               `xml:"lines-covered,attr"`
 28  LineRate     float64           `xml:"line-rate,attr"`
 29  Packages     coberturaPackages `xml:"packages"`
 30}
 31
 32type coberturaPackages struct {
 33  Packages []coberturaPackage `xml:"package"`
 34}
 35
 36type coberturaPackage struct {
 37  Name     string           `xml:"name,attr"`
 38  LineRate float64          `xml:"line-rate,attr"`
 39  Classes  coberturaClasses `xml:"classes"`
 40}
 41
 42type coberturaClasses struct {
 43  Classes []coberturaClass `xml:"class"`
 44}
 45
 46type coberturaClass struct {
 47  Name     string         `xml:"name,attr"`
 48  Filename string         `xml:"filename,attr"`
 49  LineRate float64        `xml:"line-rate,attr"`
 50  Lines    coberturaLines `xml:"lines"`
 51}
 52
 53type coberturaLines struct {
 54  Lines []coberturaLine `xml:"line"`
 55}
 56
 57type coberturaLine struct {
 58  Number int `xml:"number,attr"`
 59  Hits   int `xml:"hits,attr"`
 60}
 61
 462func (c *Cobertura) Write(_ context.Context, w io.Writer, _ *domain.SuiteResult, cov *domain.CoverageData) error {
 263  if cov == nil {
 264    cov = &domain.CoverageData{}
 265  }
 66
 467  lineRate := 0.0
 268  if cov.TotalLines() > 0 {
 269    lineRate = float64(cov.HitLines()) / float64(cov.TotalLines())
 270  }
 71
 472  report := coberturaXML{
 473    Version:      "neospec-1.0",
 474    Timestamp:    time.Now().Unix(),
 475    LinesValid:   cov.TotalLines(),
 476    LinesCovered: cov.HitLines(),
 477    LineRate:     lineRate,
 478  }
 479
 380  for _, file := range cov.Files {
 381    lineRate := 0.0
 382    if file.TotalLines() > 0 {
 383      lineRate = float64(file.HitLines()) / float64(file.TotalLines())
 384    }
 85
 386    lines := make([]int, 0, len(file.Lines))
 387    for ln := range file.Lines {
 788      lines = append(lines, ln)
 789    }
 390    sort.Ints(lines)
 391
 392    cls := coberturaClass{
 393      Name:     file.Path,
 394      Filename: file.Path,
 395      LineRate: lineRate,
 396    }
 397    for _, ln := range lines {
 798      cls.Lines.Lines = append(cls.Lines.Lines, coberturaLine{
 799        Number: ln,
 7100        Hits:   file.Lines[ln],
 7101      })
 7102    }
 103
 3104    pkg := coberturaPackage{
 3105      Name:     ".",
 3106      LineRate: lineRate,
 3107    }
 3108    pkg.Classes.Classes = append(pkg.Classes.Classes, cls)
 3109    report.Packages.Packages = append(report.Packages.Packages, pkg)
 110  }
 111
 4112  fmt.Fprintln(w, `<?xml version="1.0" encoding="UTF-8"?>`)
 4113  enc := xml.NewEncoder(w)
 4114  enc.Indent("", "  ")
 1115  if err := enc.Encode(report); err != nil {
 1116    return fmt.Errorf("encoding cobertura XML: %w", err)
 1117  }
 3118  return enc.Flush()
 119}

Methods/Properties

Write