Skip to content

Commit be937bd

Browse files
committed
deterministic test multi target framework
1 parent 7a2b774 commit be937bd

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

src/coverlet.core/coverlet.core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFrameworks>$(NetMinimum);netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks>$(NetCurrent);$(NetMinimum);netstandard2.0</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
<NoWarn>$(NoWarn);IDE0057</NoWarn>
88
</PropertyGroup>

src/coverlet.msbuild.tasks/coverlet.msbuild.tasks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFrameworks>netstandard2.0;$(NetCurrent)</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;$(NetMinimum);$(NetCurrent)</TargetFrameworks>
66
<AssemblyTitle>coverlet.msbuild.tasks</AssemblyTitle>
77
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
88
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>

test/coverlet.integration.determisticbuild/coverlet.integration.determisticbuild.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Import Project="$(MSBuildThisFileDirectory)\DeterministicTest.props" />
44

55
<PropertyGroup>
6-
<TargetFramework>net8.0</TargetFramework>
6+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
77
<IsPackable>false</IsPackable>
88
<AssemblyName>coverletsample.integration.determisticbuild</AssemblyName>
99
<MSBuildWarningsAsMessages>NU1604;NU1701</MSBuildWarningsAsMessages>
@@ -29,6 +29,6 @@
2929
<PrivateAssets>all</PrivateAssets>
3030
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
3131
</PackageReference>
32-
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.5" />
32+
<!-- <PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.5" /> -->
3333
</ItemGroup>
3434
</Project>

test/coverlet.integration.tests/DeterministicBuild.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace Coverlet.Integration.Tests
1616
public class DeterministicBuild : BaseTest, IDisposable
1717
{
1818
private static readonly string s_projectName = "coverlet.integration.determisticbuild";
19-
//private readonly string _buildTargetFramework;
20-
private string? _testProjectTfm;
19+
private readonly string _buildTargetFramework;
20+
private string[] _testProjectTfms = [];
2121
private readonly string _testProjectPath = TestUtils.GetTestProjectPath(s_projectName);
2222
private readonly string _testBinaryPath = TestUtils.GetTestBinaryPath(s_projectName);
2323
private readonly string _testResultsPath = TestUtils.GetTestResultsPath(s_projectName);
@@ -26,10 +26,13 @@ public class DeterministicBuild : BaseTest, IDisposable
2626
private readonly ITestOutputHelper _output;
2727
private readonly Type _type;
2828
private readonly FieldInfo? _testMember;
29+
private readonly string _artifactsPivot;
2930

3031
public DeterministicBuild(ITestOutputHelper output)
3132
{
3233
_buildConfiguration = TestUtils.GetAssemblyBuildConfiguration().ToString();
34+
_buildTargetFramework = TestUtils.GetAssemblyTargetFramework();
35+
_artifactsPivot = _buildConfiguration + "_" + _buildTargetFramework;
3336
_output = output;
3437
_type = output.GetType();
3538
_testMember = _type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
@@ -46,8 +49,14 @@ private void CreateDeterministicTestPropsFile()
4649
new XElement("PropertyGroup",
4750
new XElement("coverletMsbuildVersion", GetPackageVersion("*msbuild*.nupkg")),
4851
new XElement("coverletCollectorsVersion", GetPackageVersion("*collector*.nupkg")))));
49-
_testProjectTfm = XElement.Load(Path.Combine(_testProjectPath, "coverlet.integration.determisticbuild.csproj"))!.
50-
Descendants("PropertyGroup")!.Single().Element("TargetFramework")!.Value;
52+
_testProjectTfms = XElement.Load(Path.Combine(_testProjectPath, "coverlet.integration.determisticbuild.csproj"))!
53+
.Descendants("PropertyGroup")!
54+
.Single()
55+
.Element("TargetFrameworks")!
56+
.Value
57+
.Split(';');
58+
59+
Assert.Contains(_buildTargetFramework, _testProjectTfms);
5160

5261
deterministicTestProps.Save(Path.Combine(propsFile));
5362
}
@@ -58,7 +67,7 @@ private protected void AssertCoverage(string standardOutput = "", bool checkDete
5867
{
5968
bool coverageChecked = false;
6069
string reportFilePath = "";
61-
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput), "coverage.json", SearchOption.AllDirectories))
70+
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput), $"coverage.{_buildTargetFramework}.json", SearchOption.AllDirectories))
6271
{
6372
Classes? document = JsonConvert.DeserializeObject<Modules>(File.ReadAllText(coverageFile))?.Document("DeepThought.cs");
6473
if (document != null)
@@ -93,7 +102,7 @@ public void Msbuild()
93102
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
94103
CreateDeterministicTestPropsFile();
95104

96-
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
105+
DotnetCli($"build -c {_buildConfiguration} -f {_buildTargetFramework} -bl:build.{logFilename} /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
97106
if (!string.IsNullOrEmpty(buildError))
98107
{
99108
_output.WriteLine(buildError);
@@ -103,13 +112,12 @@ public void Msbuild()
103112
_output.WriteLine(buildOutput);
104113
}
105114
Assert.Contains("Build succeeded.", buildOutput);
106-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
115+
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
107116
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
108117
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
109118
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
110119

111-
string testResultFile = Path.Join(testResultPath, "coverage.json");
112-
string cmdArgument = $"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"{testResultFile}\" /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
120+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"coverage.json\" /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
113121
_output.WriteLine($"Command: dotnet {cmdArgument}");
114122
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
115123
if (!string.IsNullOrEmpty(standardError))
@@ -123,6 +131,7 @@ public void Msbuild()
123131
Assert.Equal(0, result);
124132
Assert.Contains("Passed!", standardOutput);
125133
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
134+
string testResultFile = Path.Join(testResultPath, $"coverage.{_buildTargetFramework}.json");
126135
Assert.True(File.Exists(testResultFile));
127136
AssertCoverage(standardOutput);
128137

@@ -136,7 +145,7 @@ public void Msbuild_SourceLink()
136145
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
137146
CreateDeterministicTestPropsFile();
138147

139-
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
148+
DotnetCli($"build -c {_buildConfiguration} -f {_buildTargetFramework} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
140149
if (!string.IsNullOrEmpty(buildError))
141150
{
142151
_output.WriteLine(buildError);
@@ -146,14 +155,13 @@ public void Msbuild_SourceLink()
146155
_output.WriteLine(buildOutput);
147156
}
148157
Assert.Contains("Build succeeded.", buildOutput);
149-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
158+
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
150159

151160
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
152161
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
153162
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
154163

155-
string testResultFile = Path.Join(testResultPath, "coverage.json");
156-
string cmdArgument = $"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"{testResultFile}\" /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
164+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"coverage.json\" /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
157165
_output.WriteLine($"Command: dotnet {cmdArgument}");
158166
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
159167
if (!string.IsNullOrEmpty(standardError))
@@ -167,6 +175,7 @@ public void Msbuild_SourceLink()
167175
Assert.Equal(0, result);
168176
Assert.Contains("Passed!", standardOutput);
169177
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
178+
string testResultFile = Path.Join(testResultPath, $"coverage.{_buildTargetFramework}.json");
170179
Assert.True(File.Exists(testResultFile));
171180
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(testResultFile));
172181
AssertCoverage(standardOutput, checkDeterministicReport: false);
@@ -185,7 +194,7 @@ public void Collectors()
185194
DeleteLogFiles(testLogFilesPath);
186195
DeleteCoverageFiles(testResultPath);
187196

188-
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
197+
DotnetCli($"build -c {_buildConfiguration} -f {_buildTargetFramework} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
189198
if (!string.IsNullOrEmpty(buildError))
190199
{
191200
_output.WriteLine(buildError);
@@ -195,14 +204,14 @@ public void Collectors()
195204
_output.WriteLine(buildOutput);
196205
}
197206
Assert.Contains("Build succeeded.", buildOutput);
198-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
207+
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
199208

200209
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
201210
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
202211
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
203212

204213
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", deterministicReport: true);
205-
string cmdArgument = $"test -c {_buildConfiguration} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
214+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
206215
_output.WriteLine($"Command: dotnet {cmdArgument}");
207216
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
208217
if (!string.IsNullOrEmpty(standardError))
@@ -240,7 +249,7 @@ public void Collectors_SourceLink()
240249
DeleteLogFiles(testLogFilesPath);
241250
DeleteCoverageFiles(testResultPath);
242251

243-
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
252+
DotnetCli($"build -c {_buildConfiguration} -f {_buildTargetFramework} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
244253
if (!string.IsNullOrEmpty(buildError))
245254
{
246255
_output.WriteLine(buildError);
@@ -250,14 +259,14 @@ public void Collectors_SourceLink()
250259
_output.WriteLine(buildOutput);
251260
}
252261
Assert.Contains("Build succeeded.", buildOutput);
253-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
262+
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
254263

255264
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
256265
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
257266
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
258267

259268
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", sourceLink: true);
260-
string cmdArgument = $"test -c {_buildConfiguration} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
269+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
261270
_output.WriteLine($"Command: dotnet {cmdArgument}");
262271
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
263272
if (!string.IsNullOrEmpty(standardError))
@@ -370,11 +379,12 @@ private static void DeleteCoverageFiles(string directory)
370379
private string GetReportPath(string standardOutput)
371380
{
372381
string reportPath = "";
373-
if (standardOutput.Contains("coverage.json"))
382+
string reportFileName = $"coverage.{_buildTargetFramework}.json";
383+
if (standardOutput.Contains(reportFileName))
374384
{
375-
reportPath = standardOutput.Split('\n').FirstOrDefault(line => line.Contains("coverage.json"))!.TrimStart();
385+
reportPath = standardOutput.Split('\n').FirstOrDefault(line => line.Contains(reportFileName))!.TrimStart();
376386
reportPath = reportPath[reportPath.IndexOf(Directory.GetDirectoryRoot(_testProjectPath))..];
377-
reportPath = reportPath[..reportPath.IndexOf("coverage.json")];
387+
reportPath = reportPath[..reportPath.IndexOf(reportFileName)];
378388
}
379389
return reportPath;
380390
}

test/coverlet.integration.tests/Msbuild.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TestMsbuild()
4646
Assert.Equal(0, result);
4747
Assert.Contains("Passed!", standardOutput, StringComparison.Ordinal);
4848
Assert.Contains("| coverletsamplelib.integration.template | 100% | 100% | 100% |", standardOutput, StringComparison.Ordinal);
49-
string coverageFileName = $"coverage.{_buildTargetFramework}.json";
49+
string coverageFileName = $"coverage.json";
5050
Assert.True(File.Exists(Path.Combine(clonedTemplateProject.ProjectRootPath, coverageFileName)));
5151
AssertCoverage(clonedTemplateProject, coverageFileName);
5252
}

test/coverlet.msbuild.tasks.tests/coverlet.msbuild.tasks.tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="$(RepoRoot)src\coverlet.msbuild.tasks\coverlet.msbuild.props" />
33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>$(NetMinimum);$(NetCurrent)</TargetFrameworks>
55
<!-- This section should be enabled after using SDK 8.0.0 requirement (Microsoft.Bcl.AsyncInterfaces) -->
6-
<!--<OutputType>Exe</OutputType>
6+
<OutputType>Exe</OutputType>
77
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
8-
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>-->
8+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
99
<Nullable>enable</Nullable>
1010
<IsTestProject>true</IsTestProject>
1111
<IsPackable>false</IsPackable>

0 commit comments

Comments
 (0)