This repository was archived by the owner on Jul 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
QUA-948: Support .NET DotCover coverage reporting #508
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5421bc5
Add dotcover formatter and tests
camillof e332639
Update man page
camillof 521a0ef
Fix indentation
camillof 6667830
Fix golint warnings
camillof 9b409b1
Fix gofmt warnings
camillof 93a904d
Minor refactor to dotcover.go - avoid multiple return statements
camillof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package dotcover | ||
|
||
import ( | ||
"encoding/xml" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Sirupsen/logrus" | ||
"github.com/codeclimate/test-reporter/env" | ||
"github.com/codeclimate/test-reporter/formatters" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var searchPaths = []string{"dotcover.xml"} | ||
|
||
// Formatter is the exported struct to be used on format-coverage.go | ||
type Formatter struct { | ||
Path string | ||
} | ||
|
||
// Search looks for the dotcover test report file in default paths or provided ones. | ||
func (f *Formatter) Search(paths ...string) (string, error) { | ||
camillof marked this conversation as resolved.
Show resolved
Hide resolved
camillof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paths = append(paths, searchPaths...) | ||
for _, p := range paths { | ||
logrus.Debugf("checking search path %s for dotcover formatter", p) | ||
if _, err := os.Stat(p); err == nil { | ||
f.Path = p | ||
return p, nil | ||
} | ||
} | ||
|
||
return "", errors.WithStack(errors.Errorf("could not find any files in search paths for dotcover. search paths were: %s", strings.Join(paths, ", "))) | ||
} | ||
|
||
// Format transforms the provided test report into a CC readable report format. | ||
func (f Formatter) Format() (formatters.Report, error) { | ||
rep, err := formatters.NewReport() | ||
if err != nil { | ||
return rep, err | ||
} | ||
|
||
c, err := f.readDotCoverXML() | ||
if err != nil { | ||
return rep, err | ||
} | ||
|
||
gitHead, _ := env.GetHead() | ||
|
||
for _, file := range c.Files { | ||
sf, err := formatters.NewSourceFile(file.Path, gitHead) | ||
if err != nil { | ||
err = errors.WithStack(err) | ||
break | ||
} | ||
|
||
for _, statement := range c.Statements { | ||
if file.Index == statement.FileIndex { | ||
if statement.Covered { | ||
sf.Coverage = append(sf.Coverage, formatters.NewNullInt(1)) | ||
} else { | ||
sf.Coverage = append(sf.Coverage, formatters.NewNullInt(0)) | ||
} | ||
} | ||
} | ||
|
||
err = rep.AddSourceFile(sf) | ||
|
||
if err != nil { | ||
err = errors.WithStack(err) | ||
break | ||
} | ||
} | ||
|
||
return rep, err | ||
} | ||
|
||
// readDotCoverXML reads the dotCover XML file and returns its contents. | ||
func (f Formatter) readDotCoverXML() (*xmlDotCover, error) { | ||
fx, err := os.Open(f.Path) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
c := &xmlDotCover{} | ||
if err = xml.NewDecoder(fx).Decode(c); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
return c, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dotcover | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/src-d/go-git.v4/plumbing/object" | ||
|
||
"github.com/codeclimate/test-reporter/env" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Parse(t *testing.T) { | ||
ogb := env.GitBlob | ||
defer func() { | ||
env.GitBlob = ogb | ||
}() | ||
env.GitBlob = func(s string, c *object.Commit) (string, error) { | ||
return s, nil | ||
} | ||
|
||
assert := require.New(t) | ||
|
||
formatter := Formatter{ | ||
Path: "./example.xml", | ||
} | ||
rep, err := formatter.Format() | ||
assert.NoError(err) | ||
|
||
assert.Len(rep.SourceFiles, 3) | ||
assert.InDelta(71, rep.CoveredPercent, 1) | ||
assert.Equal(24, rep.LineCounts.Total) | ||
assert.Equal(17, rep.LineCounts.Covered) | ||
|
||
sf_one := rep.SourceFiles[`C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService\PrimeService.cs`] | ||
assert.InDelta(83, sf_one.CoveredPercent, 1) | ||
|
||
sf_two := rep.SourceFiles[`C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService\SecondService.cs`] | ||
assert.Equal(0.0, sf_two.CoveredPercent) | ||
|
||
sf_three := rep.SourceFiles[`C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService.Tests\PrimeService_IsPrimeShould.cs`] | ||
assert.Equal(100.0, sf_three.CoveredPercent) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Root CoveredStatements="17" TotalStatements="24" CoveragePercent="71" ReportType="DetailedXml" DotCoverVersion="2022.3.2"> | ||
<FileIndices> | ||
<File Index="1" Name="C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService.Tests\PrimeService_IsPrimeShould.cs" ChecksumAlgorithm="SHA256" Checksum="23612935A1229C4721145EACB2122A220B0F761AD0DB20CC980DBDB0D5003C2A" /> | ||
<File Index="2" Name="C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService\PrimeService.cs" ChecksumAlgorithm="SHA256" Checksum="CA22D548019CFF7919E45E7289DC438D453674E827BBFCE249587AD9DDFAD47D" /> | ||
<File Index="3" Name="C:\Users\fulano\Desktop\unit-testing-using-mstest\PrimeService\SecondService.cs" ChecksumAlgorithm="SHA256" Checksum="073781814111C3F1C12EB8C41863004150417FC1A9D1E08D7F48C95214356D6E" /> | ||
</FileIndices> | ||
<Assembly Name="PrimeService" CoveredStatements="5" TotalStatements="12" CoveragePercent="42"> | ||
<Namespace Name="Prime.Services" CoveredStatements="5" TotalStatements="12" CoveragePercent="42"> | ||
<Type Name="PrimeService" CoveredStatements="5" TotalStatements="6" CoveragePercent="83"> | ||
<Method Name="IsPrime(System.Int32):System.Boolean" CoveredStatements="5" TotalStatements="6" CoveragePercent="83"> | ||
<Statement FileIndex="2" Line="8" Column="9" EndLine="8" EndColumn="10" Covered="True" /> | ||
<Statement FileIndex="2" Line="9" Column="13" EndLine="9" EndColumn="31" Covered="True" /> | ||
<Statement FileIndex="2" Line="10" Column="13" EndLine="10" EndColumn="14" Covered="True" /> | ||
<Statement FileIndex="2" Line="11" Column="17" EndLine="11" EndColumn="30" Covered="True" /> | ||
<Statement FileIndex="2" Line="13" Column="13" EndLine="13" EndColumn="77" Covered="False" /> | ||
<Statement FileIndex="2" Line="14" Column="9" EndLine="14" EndColumn="10" Covered="True" /> | ||
</Method> | ||
</Type> | ||
<Type Name="SecondService" CoveredStatements="0" TotalStatements="6" CoveragePercent="0"> | ||
<Method Name="IsPrime(System.Int32):System.Boolean" CoveredStatements="0" TotalStatements="6" CoveragePercent="0"> | ||
<Statement FileIndex="3" Line="8" Column="9" EndLine="8" EndColumn="10" Covered="False" /> | ||
<Statement FileIndex="3" Line="9" Column="13" EndLine="9" EndColumn="31" Covered="False" /> | ||
<Statement FileIndex="3" Line="10" Column="13" EndLine="10" EndColumn="14" Covered="False" /> | ||
<Statement FileIndex="3" Line="11" Column="17" EndLine="11" EndColumn="30" Covered="False" /> | ||
<Statement FileIndex="3" Line="13" Column="13" EndLine="13" EndColumn="77" Covered="False" /> | ||
<Statement FileIndex="3" Line="14" Column="9" EndLine="14" EndColumn="10" Covered="False" /> | ||
</Method> | ||
</Type> | ||
</Namespace> | ||
</Assembly> | ||
<Assembly Name="PrimeService.Tests" CoveredStatements="12" TotalStatements="12" CoveragePercent="100"> | ||
<Namespace Name="Prime.UnitTests.Services" CoveredStatements="12" TotalStatements="12" CoveragePercent="100"> | ||
<Type Name="PrimeService_IsPrimeShould" CoveredStatements="12" TotalStatements="12" CoveragePercent="100"> | ||
<Method Name=".ctor():System.Void" CoveredStatements="4" TotalStatements="4" CoveragePercent="100"> | ||
<Statement FileIndex="1" Line="11" Column="9" EndLine="11" EndColumn="44" Covered="True" /> | ||
<Statement FileIndex="1" Line="12" Column="9" EndLine="12" EndColumn="10" Covered="True" /> | ||
<Statement FileIndex="1" Line="13" Column="13" EndLine="13" EndColumn="48" Covered="True" /> | ||
<Statement FileIndex="1" Line="14" Column="9" EndLine="14" EndColumn="10" Covered="True" /> | ||
</Method> | ||
<Method Name="IsPrime_InputIs1_ReturnFalse():System.Void" CoveredStatements="4" TotalStatements="4" CoveragePercent="100"> | ||
<Statement FileIndex="1" Line="18" Column="9" EndLine="18" EndColumn="10" Covered="True" /> | ||
<Statement FileIndex="1" Line="19" Column="13" EndLine="19" EndColumn="51" Covered="True" /> | ||
<Statement FileIndex="1" Line="21" Column="13" EndLine="21" EndColumn="62" Covered="True" /> | ||
<Statement FileIndex="1" Line="22" Column="9" EndLine="22" EndColumn="10" Covered="True" /> | ||
</Method> | ||
<Method Name="IsPrime_ValuesLessThan2_ReturnFalse(System.Int32):System.Void" CoveredStatements="4" TotalStatements="4" CoveragePercent="100"> | ||
<Statement FileIndex="1" Line="30" Column="9" EndLine="30" EndColumn="10" Covered="True" /> | ||
<Statement FileIndex="1" Line="31" Column="13" EndLine="31" EndColumn="55" Covered="True" /> | ||
<Statement FileIndex="1" Line="33" Column="13" EndLine="33" EndColumn="68" Covered="True" /> | ||
<Statement FileIndex="1" Line="34" Column="9" EndLine="34" EndColumn="10" Covered="True" /> | ||
</Method> | ||
</Type> | ||
</Namespace> | ||
</Assembly> | ||
</Root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dotcover | ||
|
||
import "encoding/xml" | ||
|
||
type xmlDotCover struct { | ||
XMLName xml.Name `xml:"Root"` | ||
Files []struct { | ||
Path string `xml:"Name,attr"` | ||
Index int `xml:"Index,attr"` | ||
} `xml:"FileIndices>File"` | ||
Statements []struct { | ||
FileIndex int `xml:"FileIndex,attr"` | ||
Covered bool `xml:"Covered,attr"` | ||
} `xml:"Assembly>Namespace>Type>Method>Statement"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:7.0 | ||
|
||
# Install GoLang | ||
RUN curl -O https://dl.google.com/go/go1.15.linux-amd64.tar.gz | ||
RUN tar -xzf go1.15.linux-amd64.tar.gz | ||
RUN mv go /usr/local | ||
|
||
ENV PATH $PATH:/usr/local/go/bin | ||
ENV GOBIN="/usr/local/go/bin" | ||
RUN go version | ||
|
||
ENV GOPATH /go | ||
RUN mkdir $GOPATH | ||
ENV PATH $PATH:/go/bin | ||
|
||
ENV CCTR=$GOPATH/src/github.com/codeclimate/test-reporter | ||
RUN mkdir -p $CCTR | ||
WORKDIR $CCTR | ||
COPY . . | ||
RUN go install -v | ||
|
||
ENV PATH $PATH:/root/.dotnet/tools | ||
RUN dotnet tool install JetBrains.dotCover.GlobalTool -g --version "2022.3.2" | ||
|
||
# Clone .NET example repo and run test | ||
RUN git clone https://github.com/codeclimate/dot-net-coverage-test.git | ||
WORKDIR dot-net-coverage-test | ||
RUN dotnet build | ||
RUN dotnet dotcover test --dcReportType=DetailedXML --dcOutput="dotcover.xml" --no-build | ||
|
||
RUN echo "testing" > ignore.me && \ | ||
git config --global user.email "[email protected]" && \ | ||
git config --global user.name "Your Name" && \ | ||
git add ignore.me && \ | ||
git commit -m "testing" | ||
|
||
ENV CC_TEST_REPORTER_ID=49a59a1849364524250d544e098b5d987335376cdb739ea7649c9f8bce968e3b | ||
RUN test-reporter format-coverage -d -t dotcover | ||
RUN cat coverage/codeclimate.json | ||
RUN test-reporter upload-coverage -d -s 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.