Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Supported Formats:
* lcov
* opencover
* cobertura
* teamcity

The `--format` option can be specified multiple times to output multiple formats in a single run:

Expand All @@ -117,6 +118,28 @@ The above command will write the results to the supplied path, if no file extens
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output "/custom/directory/" -f json -f lcov
```

#### TeamCity Output

Coverlet can output basic code coverage statistics using [TeamCity service messages](https://confluence.jetbrains.com/display/TCD18/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ServiceMessages).

```bash
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output teamcity
```

The currently supported [TeamCity statistics](https://confluence.jetbrains.com/display/TCD18/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ServiceMessages) are:

| TeamCity Statistic Key | Description |
| :--- | :--- |
| CodeCoverageL | Line-level code coverage |
| CodeCoverageC | Class-level code coverage |
| CodeCoverageM | Method-level code coverage |
| CodeCoverageAbsLTotal | The total number of lines |
| CodeCoverageAbsLCovered | The number of covered lines |
| CodeCoverageAbsCTotal | The total number of classes |
| CodeCoverageAbsCCovered | The number of covered classes |
| CodeCoverageAbsMTotal | The total number of methods |
| CodeCoverageAbsMCovered | The number of covered methods |

#### Merging Results

With Coverlet you can combine the output of multiple coverage runs into a single result.
Expand Down Expand Up @@ -188,7 +211,7 @@ coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude "[cove
Coverlet goes a step in the other direction by also letting you explicitly set what can be included using the `--include` option.

Examples
- `--include "[*]*"` => INcludes all types in all assemblies (nothing is instrumented)
- `--include "[*]*"` => Includes all types in all assemblies (everything is instrumented)
- `--include "[coverlet.*]Coverlet.Core.Coverage"` => Includes the Coverage class in the `Coverlet.Core` namespace belonging to any assembly that matches `coverlet.*` (e.g `coverlet.core`)
- `--include "[coverlet.*.tests?]*"` => Includes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional)

Expand Down Expand Up @@ -234,6 +257,7 @@ Supported Formats:
* lcov
* opencover
* cobertura
* teamcity

You can specify multiple output formats by separating them with a comma (`,`).

Expand Down Expand Up @@ -363,6 +387,15 @@ This will result in the following:

These steps must be followed before you attempt to open the solution in an IDE (e.g. Visual Studio, Rider) for all projects to be loaded successfully.

### Performance testing

There is a performance test for the hit counting instrumentation in the test project `coverlet.core.performancetest`. Build the project with the msbuild step above and then run:

dotnet test /p:CollectCoverage=true test/coverlet.core.performancetest/

The duration of the test can be tweaked by changing the number of iterations in the `[InlineData]` in the `PerformanceTest` class.


## Code of Conduct

This project enforces a code of conduct in line with the contributor covenant. See [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.
Expand Down
2 changes: 1 addition & 1 deletion coverlet.msbuild.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>coverlet.msbuild</id>
<version>2.3.1</version>
<version>2.3.2</version>
<title>coverlet.msbuild</title>
<authors>tonerdo</authors>
<owners>tonerdo</owners>
Expand Down
48 changes: 32 additions & 16 deletions src/coverlet.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.IO;
using System.Text;

using ConsoleTables;
using Coverlet.Console.Logging;
using Coverlet.Core;
Expand Down Expand Up @@ -33,6 +32,7 @@ static int Main(string[] args)
CommandOption thresholdTypes = app.Option("--threshold-type", "Coverage type to apply the threshold to.", CommandOptionType.MultipleValue);
CommandOption excludeFilters = app.Option("--exclude", "Filter expressions to exclude specific modules and types.", CommandOptionType.MultipleValue);
CommandOption includeFilters = app.Option("--include", "Filter expressions to include only specific modules and types.", CommandOptionType.MultipleValue);
CommandOption includeDirectories = app.Option("--include-directories", "Include directories containing additional assemblies to be instrumented.", CommandOptionType.MultipleValue);
CommandOption excludedSourceFiles = app.Option("--exclude-by-file", "Glob patterns specifying source files to exclude.", CommandOptionType.MultipleValue);
CommandOption mergeWith = app.Option("--merge-with", "Path to existing coverage result to merge.", CommandOptionType.SingleValue);

Expand All @@ -44,7 +44,7 @@ static int Main(string[] args)
if (!target.HasValue())
throw new CommandParsingException(app, "Target must be specified.");

Coverage coverage = new Coverage(module.Value, excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), mergeWith.Value());
Coverage coverage = new Coverage(module.Value, excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), includeDirectories.Values.ToArray(), excludedSourceFiles.Values.ToArray(), mergeWith.Value());
coverage.PrepareModules();

Process process = new Process();
Expand All @@ -64,31 +64,47 @@ static int Main(string[] args)

var result = coverage.GetCoverageResult();
var directory = Path.GetDirectoryName(dOutput);
if (!Directory.Exists(directory))
if (directory == string.Empty)
{
directory = Directory.GetCurrentDirectory();
}
else if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

foreach (var format in (formats.HasValue() ? formats.Values : new List<string>(new string[] { "json" })))
{
var reporter = new ReporterFactory(format).CreateReporter();
if (reporter == null)
throw new Exception($"Specified output format '{format}' is not supported");

var filename = Path.GetFileName(dOutput);
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";

var report = Path.Combine(directory, filename);
logger.LogInformation($" Generating report '{report}'");
File.WriteAllText(report, reporter.Report(result));
if (reporter.OutputType == ReporterOutputType.Console)
{
// Output to console
logger.LogInformation(" Outputting results to console");
logger.LogInformation(reporter.Report(result));
}
else
{
// Output to file
var filename = Path.GetFileName(dOutput);
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";

var report = Path.Combine(directory, filename);
logger.LogInformation($" Generating report '{report}'");
File.WriteAllText(report, reporter.Report(result));
}
}

var summary = new CoverageSummary();
var exceptionBuilder = new StringBuilder();
var coverageTable = new ConsoleTable("Module", "Line", "Branch", "Method");
var thresholdFailed = false;
var overallLineCoverage = summary.CalculateLineCoverage(result.Modules).Percent * 100;
var overallBranchCoverage = summary.CalculateBranchCoverage(result.Modules).Percent * 100;
var overallMethodCoverage = summary.CalculateMethodCoverage(result.Modules).Percent * 100;
var overallLineCoverage = summary.CalculateLineCoverage(result.Modules);
var overallBranchCoverage = summary.CalculateBranchCoverage(result.Modules);
var overallMethodCoverage = summary.CalculateMethodCoverage(result.Modules);

foreach (var _module in result.Modules)
{
Expand Down Expand Up @@ -122,9 +138,9 @@ static int Main(string[] args)

logger.LogInformation(string.Empty);
logger.LogInformation(coverageTable.ToStringAlternative());
logger.LogInformation($"Total Line: {overallLineCoverage}%");
logger.LogInformation($"Total Branch: {overallBranchCoverage}%");
logger.LogInformation($"Total Method: {overallMethodCoverage}%");
logger.LogInformation($"Total Line: {overallLineCoverage.Percent * 100}%");
logger.LogInformation($"Total Branch: {overallBranchCoverage.Percent * 100}%");
logger.LogInformation($"Total Method: {overallMethodCoverage.Percent * 100}%");

if (thresholdFailed)
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
Expand Down
2 changes: 1 addition & 1 deletion src/coverlet.console/coverlet.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ToolCommandName>coverlet</ToolCommandName>
<PackAsTool>true</PackAsTool>
<AssemblyTitle>coverlet.console</AssemblyTitle>
<AssemblyVersion>1.2.1</AssemblyVersion>
<AssemblyVersion>1.2.2</AssemblyVersion>
<Authors>tonerdo</Authors>
<PackageId>$(AssemblyTitle)</PackageId>
<Title>$(AssemblyTitle)</Title>
Expand Down
73 changes: 43 additions & 30 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Coverage
private string _identifier;
private string[] _excludeFilters;
private string[] _includeFilters;
private string[] _includeDirectories;
private string[] _excludedSourceFiles;
private string _mergeWith;
private List<InstrumenterResult> _results;
Expand All @@ -26,11 +27,12 @@ public string Identifier
get { return _identifier; }
}

public Coverage(string module, string[] excludeFilters, string[] includeFilters, string[] excludedSourceFiles, string mergeWith)
public Coverage(string module, string[] excludeFilters, string[] includeFilters, string[] includeDirectories, string[] excludedSourceFiles, string mergeWith)
{
_module = module;
_excludeFilters = excludeFilters;
_includeFilters = includeFilters;
_includeDirectories = includeDirectories ?? Array.Empty<string>();
_excludedSourceFiles = excludedSourceFiles;
_mergeWith = mergeWith;

Expand All @@ -40,23 +42,33 @@ public Coverage(string module, string[] excludeFilters, string[] includeFilters,

public void PrepareModules()
{
string[] modules = InstrumentationHelper.GetCoverableModules(_module);
string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories);
string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles);
_excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
_includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();

foreach (var module in modules)
{
if (InstrumentationHelper.IsModuleExcluded(module, _excludeFilters)
|| !InstrumentationHelper.IsModuleIncluded(module, _includeFilters))
if (InstrumentationHelper.IsModuleExcluded(module, _excludeFilters) ||
!InstrumentationHelper.IsModuleIncluded(module, _includeFilters))
continue;

var instrumenter = new Instrumenter(module, _identifier, _excludeFilters, _includeFilters, excludes);
if (instrumenter.CanInstrument())
{
InstrumentationHelper.BackupOriginalModule(module, _identifier);
var result = instrumenter.Instrument();
_results.Add(result);

// Guard code path and restore if instrumentation fails.
try
{
var result = instrumenter.Instrument();
_results.Add(result);
}
catch (Exception)
{
InstrumentationHelper.RestoreOriginalModule(module, _identifier);
throw;
}
}
}
}
Expand Down Expand Up @@ -146,11 +158,12 @@ public CoverageResult GetCoverageResult()
}
}

modules.Add(Path.GetFileName(result.ModulePath), documents);
modules.Add(result.ModulePath, documents);
InstrumentationHelper.RestoreOriginalModule(result.ModulePath, _identifier);
}

var coverageResult = new CoverageResult { Identifier = _identifier, Modules = modules };

if (!string.IsNullOrEmpty(_mergeWith) && !string.IsNullOrWhiteSpace(_mergeWith) && File.Exists(_mergeWith))
{
string json = File.ReadAllText(_mergeWith);
Expand Down Expand Up @@ -205,29 +218,29 @@ private void CalculateCoverage()
}
}

// for MoveNext() compiler autogenerated method we need to patch false positive (IAsyncStateMachine for instance)
// we'll remove all MoveNext() not covered branch
foreach (var document in result.Documents)
{
List<KeyValuePair<(int, int), Branch>> branchesToRemove = new List<KeyValuePair<(int, int), Branch>>();
foreach (var branch in document.Value.Branches)
{
//if one branch is covered we search the other one only if it's not covered
if (CecilSymbolHelper.IsMoveNext(branch.Value.Method) && branch.Value.Hits > 0)
{
foreach (var moveNextBranch in document.Value.Branches)
{
if (moveNextBranch.Value.Method == branch.Value.Method && moveNextBranch.Value != branch.Value && moveNextBranch.Value.Hits == 0)
{
branchesToRemove.Add(moveNextBranch);
}
}
}
}
foreach (var branchToRemove in branchesToRemove)
{
document.Value.Branches.Remove(branchToRemove.Key);
}
// for MoveNext() compiler autogenerated method we need to patch false positive (IAsyncStateMachine for instance)
// we'll remove all MoveNext() not covered branch
foreach (var document in result.Documents)
{
List<KeyValuePair<(int, int), Branch>> branchesToRemove = new List<KeyValuePair<(int, int), Branch>>();
foreach (var branch in document.Value.Branches)
{
//if one branch is covered we search the other one only if it's not covered
if (CecilSymbolHelper.IsMoveNext(branch.Value.Method) && branch.Value.Hits > 0)
{
foreach (var moveNextBranch in document.Value.Branches)
{
if (moveNextBranch.Value.Method == branch.Value.Method && moveNextBranch.Value != branch.Value && moveNextBranch.Value.Hits == 0)
{
branchesToRemove.Add(moveNextBranch);
}
}
}
}
foreach (var branchToRemove in branchesToRemove)
{
document.Value.Branches.Remove(branchToRemove.Key);
}
}

InstrumentationHelper.DeleteHitsFile(result.HitsFilePath);
Expand Down
Loading