Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi targets projects #1742

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<Project>
<PropertyGroup>
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
<NetCurrent>net9.0</NetCurrent>
<NetMinimum>net8.0</NetMinimum>
<FullFrameworkTFM>net472</FullFrameworkTFM>
<!-- enforce CamelCase case string-->
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<!-- SourceLink Setup -->
Expand Down Expand Up @@ -39,12 +42,12 @@
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<VSTestResultsDirectory>$(RepoRoot)artifacts/testresults/$(Configuration.ToLowerInvariant())</VSTestResultsDirectory>
<VSTestResultsDirectory>$(RepoRoot)artifacts/reports/$(Configuration.ToLowerInvariant())</VSTestResultsDirectory>
<VSTestLogger>@(VSTestLogger)</VSTestLogger>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<VSTestResultsDirectory>$(RepoRoot)artifacts\testresults\$(Configuration.ToLowerInvariant())</VSTestResultsDirectory>
<VSTestResultsDirectory>$(RepoRoot)artifacts\reports\$(Configuration.ToLowerInvariant())</VSTestResultsDirectory>
<VSTestLogger>@(VSTestLogger)</VSTestLogger>
</PropertyGroup>

Expand Down
9 changes: 4 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.7.115" />
</ItemGroup>
<PropertyGroup>
<MicrosoftBuildVersion>17.13.9</MicrosoftBuildVersion>
<MicrosoftCodeAnalysisVersion>4.12.0</MicrosoftCodeAnalysisVersion>
<MicrosoftBuildVersion>17.11.4</MicrosoftBuildVersion>
<MicrosoftCodeAnalysisVersion>4.13.0</MicrosoftCodeAnalysisVersion>
<!-- Test Platform, .NET Test SDK and Object Model -->
<MicrosoftNETTestSdkVersion>17.13.0</MicrosoftNETTestSdkVersion>
<NugetPackageVersion>6.13.2</NugetPackageVersion>
Expand All @@ -21,7 +21,7 @@
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildVersion)" />
<PackageVersion Include="Microsoft.Build.Framework" Version="$(MicrosoftBuildVersion)" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.9.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
Expand All @@ -39,7 +39,6 @@
vstest 17.8 version
NuGetFrameworksVersion is defined here https://github.com/microsoft/vstest/blob/9a0c41811637edf4afe0e265e08fdd1cb18109ed/eng/Versions.props#L94C1-L94C1
-->
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NuGet.Frameworks" Version="$(NugetPackageVersion)" />
<PackageVersion Include="NuGet.Packaging" Version="$(NugetPackageVersion)" />
<PackageVersion Include="NuGet.Versioning" Version="$(NugetPackageVersion)" />
Expand All @@ -66,6 +65,6 @@
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.6.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.6.2" />
</ItemGroup>
</Project>
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Coverlet supports only SDK-style projects https://docs.microsoft.com/en-us/visua
```bash
dotnet add package coverlet.collector
```

N.B. You **MUST** add package only to test projects and if you create xunit test projects (`dotnet new xunit`) you'll find the reference already present in `csproj` file because Coverlet is the default coverage tool for every .NET Core and >= .NET 6 applications, you've only to update to last version if needed. Do not add `coverlet.collector` and `coverlet.msbuild` package in a test project.
> [!NOTE]
> You **MUST** add package only to test projects and if you create xunit test projects (`dotnet new xunit`) you will find the reference already present in `csproj` file because Coverlet is the default coverage tool for every .NET Core and >= *.NET 8* applications, you've only to update to last version if needed. Add `coverlet.collector` *OR* `coverlet.msbuild` package in a test project.

### Usage (coverlet.collector)

Expand All @@ -61,11 +61,11 @@ See [documentation](Documentation/VSTestIntegration.md) for advanced usage.

#### Requirements (coverlet.collector)

* _You need to be running .NET 6.0 SDK v6.0.316 or newer_
* _You need to reference version 17.5.0 and above of Microsoft.NET.Test.Sdk_
* _You need to be running .NET 8.0 SDK v8.0.112 or newer_
* _You need to reference version 17.12.0 and above of Microsoft.NET.Test.Sdk_

```xml
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
```

### MSBuild Integration (suffers of possible [known issue](https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/KnownIssues.md#1-vstest-stops-process-execution-earlydotnet-test))
Expand Down Expand Up @@ -120,7 +120,7 @@ See [documentation](Documentation/GlobalTool.md) for advanced usage.

.NET global tools rely on a .NET Core runtime installed on your machine https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#what-could-go-wrong

.NET Coverlet global tool requires _.NET Core 2.2 and above_
.NET Coverlet global tool requires _.NET 8.0 or above_

## How It Works

Expand Down
17 changes: 12 additions & 5 deletions eng/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ steps:
version: 6.0.428
displayName: Install .NET Core SDK 6.0.428

- task: UseDotNet@2
inputs:
version: 8.0.114
displayName: Install .NET Core SDK 8.0.114

- task: UseDotNet@2
inputs:
useGlobalJson: true
displayName: Install .NET Core SDK 8.0.113
displayName: Install .NET Core SDK 9.0.202

# create artifact/package folder
- pwsh: |
Expand All @@ -25,11 +30,13 @@ steps:
displayName: Pack

- script: |
dotnet test test/coverlet.core.tests/coverlet.core.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.core.binlog /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.projectsample]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" -- --results-directory "$(Build.SourcesDirectory)/artifacts/Reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.core.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.core.tests"
dotnet test test/coverlet.core.coverage.tests/coverlet.core.coverage.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.core.coverage.binlog /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.projectsample]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" -- --results-directory "$(Build.SourcesDirectory)/artifacts/Reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.core.coverage.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.core.coverage.tests"
dotnet test test/coverlet.msbuild.tasks.tests/coverlet.msbuild.tasks.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.msbuild.binlog --results-directory:"$(Build.SourcesDirectory)\artifacts\Reports" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.xunit.extensions]*%2c[coverlet.tests.projectsample]*%2c[testgen_]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" --diag:"$(Build.SourcesDirectory)\artifacts\log\$(buildConfiguration)\coverlet.msbuild.test.diag.log;tracelevel=verbose"
dotnet test test/coverlet.core.tests/coverlet.core.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.core.binlog /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.projectsample]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" -- --results-directory "$(Build.SourcesDirectory)/artifacts/reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.core.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.core.tests"
dotnet test test/coverlet.core.coverage.tests/coverlet.core.coverage.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.core.coverage.binlog /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.projectsample]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" -- --results-directory "$(Build.SourcesDirectory)/artifacts/reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.core.coverage.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.core.coverage.tests"
dotnet test test/coverlet.msbuild.tasks.tests/coverlet.msbuild.tasks.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.msbuild.binlog --results-directory:"$(Build.SourcesDirectory)\artifacts\reports" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.xunit.extensions]*%2c[coverlet.tests.projectsample]*%2c[testgen_]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" --diag:"$(Build.SourcesDirectory)\artifacts\log\$(buildConfiguration)\coverlet.msbuild.test.diag.log;tracelevel=verbose"
dotnet test test/coverlet.collector.tests/coverlet.collector.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.collector.binlog /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[coverlet.core.tests.samples.netstandard]*%2c[coverlet.tests.projectsample]*" /p:ExcludeByAttribute="GeneratedCodeAttribute" --diag:"$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)/coverlet.collector.test.diag.log;tracelevel=verbose"
dotnet test test/coverlet.integration.tests/coverlet.integration.tests.csproj -c $(BuildConfiguration) --no-build -bl:test.integration.binlog -- --results-directory "$(Build.SourcesDirectory)/artifacts/Reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.integration.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.integration.tests"
dotnet test test/coverlet.integration.tests/coverlet.integration.tests.csproj -c $(BuildConfiguration) -f net8.0 --no-build -bl:test.integration.binlog -- --results-directory "$(Build.SourcesDirectory)/artifacts/reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.integration.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.integration.tests"
dotnet test test/coverlet.integration.tests/coverlet.integration.tests.csproj -c $(BuildConfiguration) -f net9.0 --no-build -bl:test.integration.binlog -- --results-directory "$(Build.SourcesDirectory)/artifacts/reports" --report-xunit-trx --report-xunit-trx-filename "coverlet.integration.tests.trx" --diagnostic --diagnostic-output-directory "$(Build.SourcesDirectory)/artifacts/log/$(buildConfiguration)" --diagnostic-output-fileprefix "coverlet.integration.tests"

displayName: Run unit tests with coverage
env:
MSBUILDDISABLENODEREUSE: 1
Expand Down
10 changes: 9 additions & 1 deletion eng/publish-coverlet-result-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ steps:
condition: always()
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: '**/*.binlog'
Contents: '*.binlog'
TargetFolder: '$(Build.SourcesDirectory)/artifacts/TestLogs'

- task: CopyFiles@2
displayName: Copy DeterministicBuild binlog files
condition: always()
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: 'test\coverlet.integration.determisticbuild\*.binlog'
TargetFolder: '$(Build.SourcesDirectory)/artifacts/TestLogs'

- task: PublishPipelineArtifact@1
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "8.0.113"
"version": "9.0.202"
}
}
2 changes: 1 addition & 1 deletion src/coverlet.collector/coverlet.collector.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>$(NetMinimum);netstandard2.0</TargetFrameworks>
<AssemblyTitle>coverlet.collector</AssemblyTitle>
<DevelopmentDependency>true</DevelopmentDependency>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
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 @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>$(NetMinimum)</TargetFrameworks>
<ToolCommandName>coverlet</ToolCommandName>
<PackAsTool>true</PackAsTool>
<AssemblyTitle>coverlet.console</AssemblyTitle>
Expand Down
25 changes: 17 additions & 8 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Helpers;
using Coverlet.Core.Instrumentation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Coverlet.Core
{
Expand Down Expand Up @@ -60,6 +60,14 @@ internal class Coverage

public string Identifier { get; }

readonly JsonSerializerOptions _options = new()
{
PropertyNameCaseInsensitive = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
IncludeFields = true,
WriteIndented = true
};

public Coverage(string moduleOrDirectory,
CoverageParameters parameters,
ILogger logger,
Expand Down Expand Up @@ -313,7 +321,7 @@ public CoverageResult GetCoverageResult()
{
_logger.LogInformation($"MergeWith: '{_parameters.MergeWith}'.");
string json = _fileSystem.ReadAllText(_parameters.MergeWith);
coverageResult.Merge(JsonConvert.DeserializeObject<Modules>(json));
coverageResult.Merge(JsonSerializer.Deserialize<Modules>(json, _options));
}
else
{
Expand Down Expand Up @@ -366,8 +374,8 @@ private void CalculateCoverage()
var documents = result.Documents.Values.ToList();
if (_parameters.UseSourceLink && result.SourceLink != null)
{
JToken jObject = JObject.Parse(result.SourceLink)["documents"];
Dictionary<string, string> sourceLinkDocuments = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
JsonNode jObject = JsonNode.Parse(result.SourceLink)["documents"];
Dictionary<string, string> sourceLinkDocuments = JsonSerializer.Deserialize<Dictionary<string, string>>(jObject.ToString());
foreach (Document document in documents)
{
document.Path = GetSourceLinkUrl(sourceLinkDocuments, document.Path);
Expand Down Expand Up @@ -480,9 +488,9 @@ internal string GetSourceLinkUrl(Dictionary<string, string> sourceLinkDocuments,
{
string key = sourceLinkDocument.Key;
if (Path.GetFileName(key) != "*") continue;

#pragma warning disable IDE0057
IReadOnlyList<SourceRootMapping> rootMapping = _sourceRootTranslator.ResolvePathRoot(key.Substring(0, key.Length - 1));

#pragma warning restore IDE0057
foreach (string keyMapping in rootMapping is null ? [key] : new List<string>(rootMapping.Select(m => m.OriginalPath)))
{
string directoryDocument = Path.GetDirectoryName(document);
Expand All @@ -494,8 +502,9 @@ internal string GetSourceLinkUrl(Dictionary<string, string> sourceLinkDocuments,
{
if (!directoryDocument.StartsWith(sourceLinkRoot + Path.DirectorySeparatorChar))
continue;

#pragma warning disable IDE0057
relativePath = directoryDocument.Substring(sourceLinkRoot.Length + 1);
#pragma warning restore IDE0057
}

if (relativePathOfBestMatch.Length == 0)
Expand Down
2 changes: 2 additions & 0 deletions src/coverlet.core/CoverageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Coverlet.Core.Enums;
using Coverlet.Core.Instrumentation;

Expand All @@ -22,6 +23,7 @@ internal class Branches : List<BranchInfo> { }

internal class Method
{
[JsonConstructor]
internal Method()
{
Lines = [];
Expand Down
8 changes: 0 additions & 8 deletions src/coverlet.core/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@

namespace Coverlet.Core.Exceptions
{
[Serializable]
public class CoverletException : Exception
{
public CoverletException() { }
public CoverletException(string message) : base(message) { }
public CoverletException(string message, System.Exception inner) : base(message, inner) { }
protected CoverletException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}

[Serializable]
internal class CecilAssemblyResolutionException : CoverletException
{
public CecilAssemblyResolutionException() { }
public CecilAssemblyResolutionException(string message) : base(message) { }
public CecilAssemblyResolutionException(string message, System.Exception inner) : base(message, inner) { }
protected CecilAssemblyResolutionException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}
6 changes: 5 additions & 1 deletion src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,21 @@ private static string GetIncludeModuleKeysForValidFilters(char escapeSymbol, str
}

private static string CreateRegexExcludePattern(IEnumerable<string> filters, char escapeSymbol)
//only look for module filters here, types will be filtered out when instrumenting
//only look for module filters here, types will be filtered out when instrumenting
#pragma warning disable IDE0057 // Use range operator
=> CreateRegexPattern(filters, escapeSymbol, filter => filter.Substring(filter.IndexOf(']') + 1) == "*");
#pragma warning restore IDE0057 // Use range operator

private static string CreateRegexIncludePattern(IEnumerable<string> filters, char escapeSymbol) =>
CreateRegexPattern(filters, escapeSymbol);

private static string CreateRegexPattern(IEnumerable<string> filters, char escapeSymbol, Func<string, bool> filterPredicate = null)
{
IEnumerable<string> filteredFilters = filterPredicate != null ? filters.Where(filterPredicate) : filters;
#pragma warning disable IDE0057 // Use range operator
IEnumerable<string> regexPatterns = filteredFilters.Select(x =>
$"{escapeSymbol}{WildcardToRegex(x.Substring(1, x.IndexOf(']') - 1)).Trim('^', '$')}{escapeSymbol}");
#pragma warning restore IDE0057 // Use range operator
return string.Join("|", regexPatterns);
}

Expand Down
Loading
Loading