Skip to content

Commit 2d016ac

Browse files
committed
add method GetSourceRootMappingFilePath
1 parent 7e7bb0a commit 2d016ac

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "9.0.203"
3+
"version": "9.0.300"
44
}
55
}

test/coverlet.integration.tests/DeterministicBuild.cs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Coverlet.Integration.Tests
1616
public class DeterministicBuild : BaseTest, IDisposable
1717
{
1818
private static readonly string s_projectName = "coverlet.integration.determisticbuild";
19+
private static readonly string s_sutName = "coverletsample.integration.determisticbuild";
1920
private readonly string _buildTargetFramework;
2021
private string[] _testProjectTfms = [];
2122
private readonly string _testProjectPath = TestUtils.GetTestProjectPath(s_projectName);
@@ -67,7 +68,7 @@ private protected void AssertCoverage(string standardOutput = "", string reportN
6768
{
6869
bool coverageChecked = false;
6970
string reportFilePath = "";
70-
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput, reportName), reportName, SearchOption.AllDirectories))
71+
foreach (string coverageFile in Directory.GetFiles(GetReportDirectory(standardOutput, reportName), reportName, SearchOption.AllDirectories))
7172
{
7273
Classes? document = JsonConvert.DeserializeObject<Modules>(File.ReadAllText(coverageFile))?.Document("DeepThought.cs");
7374
if (document != null)
@@ -87,9 +88,9 @@ private protected void AssertCoverage(string standardOutput = "", string reportN
8788
{
8889
string newName = reportName.Replace("json", "cobertura.xml");
8990
// Verify deterministic report
90-
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput, newName), newName, SearchOption.AllDirectories))
91+
foreach (string coverageFile in Directory.GetFiles(GetReportDirectory(standardOutput, newName), newName, SearchOption.AllDirectories))
9192
{
92-
Assert.Contains("/_/test/coverlet.integration.determisticbuild/DeepThought.cs", File.ReadAllText(coverageFile));
93+
Assert.Contains($"/_/test/{s_projectName}/DeepThought.cs", File.ReadAllText(coverageFile));
9394
File.Delete(coverageFile);
9495
}
9596
}
@@ -113,12 +114,12 @@ public void Msbuild()
113114
_output.WriteLine(buildOutput);
114115
}
115116
Assert.Contains("Build succeeded.", buildOutput);
116-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
117+
string sourceRootMappingFilePath = GetSourceRootMappingFilePath(buildOutput, $"CoverletSourceRootsMapping_{s_sutName}");
117118
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
118119
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
119120
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
120121

121-
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true --results-directory:{testResultPath}";
122+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[{s_sutName}]*DeepThought\" /p:IncludeTestAssembly=true --results-directory:{testResultPath}";
122123
_output.WriteLine($"Command: dotnet {cmdArgument}");
123124
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
124125
if (!string.IsNullOrEmpty(standardError))
@@ -131,10 +132,11 @@ public void Msbuild()
131132
}
132133
Assert.Equal(0, result);
133134
Assert.Contains("Passed!", standardOutput);
134-
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
135-
string testResultFile = Path.Join(_testProjectPath, $"coverage.{_buildTargetFramework}.json");
135+
Assert.Contains($"| {s_sutName} | 100% | 100% | 100% |", standardOutput);
136+
string filename = $"coverage.{_buildTargetFramework}.json";
137+
string testResultFile = GetReportDirectory(standardOutput, filename) + filename;
136138
Assert.True(File.Exists(testResultFile), $"File '{testResultFile}' does not exist");
137-
AssertCoverage(standardOutput, $"coverage.{_buildTargetFramework}.json");
139+
AssertCoverage(standardOutput, filename);
138140

139141
CleanupBuildOutput();
140142
}
@@ -156,13 +158,13 @@ public void Msbuild_SourceLink()
156158
_output.WriteLine(buildOutput);
157159
}
158160
Assert.Contains("Build succeeded.", buildOutput);
159-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
161+
string sourceRootMappingFilePath = GetSourceRootMappingFilePath(buildOutput, $"CoverletSourceRootsMapping_{s_sutName}");
160162

161163
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
162164
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
163165
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
164166

165-
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true --results-directory:{testResultPath}";
167+
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[{s_sutName}]*DeepThought\" /p:IncludeTestAssembly=true --results-directory:{testResultPath}";
166168
_output.WriteLine($"Command: dotnet {cmdArgument}");
167169
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
168170
if (!string.IsNullOrEmpty(standardError))
@@ -175,11 +177,12 @@ public void Msbuild_SourceLink()
175177
}
176178
Assert.Equal(0, result);
177179
Assert.Contains("Passed!", standardOutput);
178-
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
179-
string testResultFile = Path.Join(_testProjectPath, $"coverage.{_buildTargetFramework}.json");
180+
Assert.Contains($"| {s_sutName} | 100% | 100% | 100% |", standardOutput);
181+
string filename = $"coverage.{_buildTargetFramework}.json";
182+
string testResultFile = GetReportDirectory(standardOutput, filename) + filename;
180183
Assert.True(File.Exists(testResultFile), $"File '{testResultFile}' does not exist");
181184
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(testResultFile));
182-
AssertCoverage(standardOutput, $"coverage.{_buildTargetFramework}.json", checkDeterministicReport: false);
185+
AssertCoverage(standardOutput, filename, checkDeterministicReport: false);
183186

184187
CleanupBuildOutput();
185188
}
@@ -205,13 +208,13 @@ public void Collectors()
205208
_output.WriteLine(buildOutput);
206209
}
207210
Assert.Contains("Build succeeded.", buildOutput);
208-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
211+
string sourceRootMappingFilePath = GetSourceRootMappingFilePath(buildOutput, $"CoverletSourceRootsMapping_{s_sutName}");
209212

210213
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
211214
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
212215
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
213216

214-
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", deterministicReport: true);
217+
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, $"[{s_sutName}]*DeepThought", deterministicReport: true);
215218
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
216219
_output.WriteLine($"Command: dotnet {cmdArgument}");
217220
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
@@ -260,13 +263,13 @@ public void Collectors_SourceLink()
260263
_output.WriteLine(buildOutput);
261264
}
262265
Assert.Contains("Build succeeded.", buildOutput);
263-
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _artifactsPivot.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
266+
string sourceRootMappingFilePath = GetSourceRootMappingFilePath(buildOutput, $"CoverletSourceRootsMapping_{s_sutName}");
264267

265268
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
266269
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
267270
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
268271

269-
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", sourceLink: true);
272+
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, $"[{s_sutName}]*DeepThought", sourceLink: true);
270273
string cmdArgument = $"test -c {_buildConfiguration} -f {_buildTargetFramework} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
271274
_output.WriteLine($"Command: dotnet {cmdArgument}");
272275
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
@@ -377,7 +380,7 @@ private static void DeleteCoverageFiles(string directory)
377380
}
378381
}
379382

380-
private string GetReportPath(string standardOutput, string reportFileName = "")
383+
private string GetReportDirectory(string standardOutput, string reportFileName = "")
381384
{
382385
string reportPath = "";
383386
if (standardOutput.Contains(reportFileName))
@@ -388,6 +391,42 @@ private string GetReportPath(string standardOutput, string reportFileName = "")
388391
}
389392
return reportPath;
390393
}
394+
395+
private static string GetSourceRootMappingFilePath(string buildOutput, string pattern)
396+
{
397+
// search file in path using pattern and return full path
398+
// this will also handle filenames with prefix like ".msCoverletSourceRootsMapping_coverletsample.integration.determisticbuild" or "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild"
399+
// today both files exist
400+
string sourceRootMappingFilePath = "";
401+
if (buildOutput.Contains($"{s_sutName}.dll"))
402+
{
403+
string[] lines = buildOutput.Split('\n').Where(line => line.Contains(" -> ")).ToArray();
404+
if (lines.Length == 0)
405+
{
406+
throw new InvalidOperationException("No lines found in build output containing ' -> '.");
407+
}
408+
else
409+
{
410+
string? directory = Path.GetDirectoryName(lines.FirstOrDefault(static line => line.Contains($"{s_sutName}.dll"))?.TrimStart());
411+
if (directory == null)
412+
{
413+
throw new InvalidOperationException($"Directory is null. {s_sutName}.dll not found.");
414+
}
415+
// actual directory value "coverlet.integration.determisticbuild -> C:\GitHub\coverlet\artifacts\bin\coverlet.integration.determisticbuild\debug
416+
// remove everything before '->'
417+
directory = directory[(directory.IndexOf("->") + 2)..].Trim();
418+
if (Directory.Exists(directory))
419+
{
420+
string[] files = Directory.GetFiles(directory, $"{pattern}*", SearchOption.AllDirectories);
421+
if (files.Length > 0)
422+
{
423+
sourceRootMappingFilePath = files[0];
424+
}
425+
}
426+
}
427+
}
428+
return sourceRootMappingFilePath;
429+
}
391430
public void Dispose()
392431
{
393432
File.Delete(Path.Combine(_testProjectPath, PropsFileName));

0 commit comments

Comments
 (0)