@@ -16,6 +16,7 @@ namespace Coverlet.Integration.Tests
16
16
public class DeterministicBuild : BaseTest , IDisposable
17
17
{
18
18
private static readonly string s_projectName = "coverlet.integration.determisticbuild" ;
19
+ private static readonly string s_sutName = "coverletsample.integration.determisticbuild" ;
19
20
private readonly string _buildTargetFramework ;
20
21
private string [ ] _testProjectTfms = [ ] ;
21
22
private readonly string _testProjectPath = TestUtils . GetTestProjectPath ( s_projectName ) ;
@@ -67,7 +68,7 @@ private protected void AssertCoverage(string standardOutput = "", string reportN
67
68
{
68
69
bool coverageChecked = false ;
69
70
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 ) )
71
72
{
72
73
Classes ? document = JsonConvert . DeserializeObject < Modules > ( File . ReadAllText ( coverageFile ) ) ? . Document ( "DeepThought.cs" ) ;
73
74
if ( document != null )
@@ -87,9 +88,9 @@ private protected void AssertCoverage(string standardOutput = "", string reportN
87
88
{
88
89
string newName = reportName . Replace ( "json" , "cobertura.xml" ) ;
89
90
// 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 ) )
91
92
{
92
- Assert . Contains ( "/_/test/coverlet.integration.determisticbuild /DeepThought.cs" , File . ReadAllText ( coverageFile ) ) ;
93
+ Assert . Contains ( $ "/_/test/{ s_projectName } /DeepThought.cs", File . ReadAllText ( coverageFile ) ) ;
93
94
File . Delete ( coverageFile ) ;
94
95
}
95
96
}
@@ -113,12 +114,12 @@ public void Msbuild()
113
114
_output . WriteLine ( buildOutput ) ;
114
115
}
115
116
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 } ") ;
117
118
Assert . True ( File . Exists ( sourceRootMappingFilePath ) , $ "File not found: { sourceRootMappingFilePath } ") ;
118
119
Assert . False ( string . IsNullOrEmpty ( File . ReadAllText ( sourceRootMappingFilePath ) ) ) ;
119
120
Assert . Contains ( "=/_/" , File . ReadAllText ( sourceRootMappingFilePath ) ) ;
120
121
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 } ";
122
123
_output . WriteLine ( $ "Command: dotnet { cmdArgument } ") ;
123
124
int result = DotnetCli ( cmdArgument , out string standardOutput , out string standardError , _testProjectPath ) ;
124
125
if ( ! string . IsNullOrEmpty ( standardError ) )
@@ -131,10 +132,11 @@ public void Msbuild()
131
132
}
132
133
Assert . Equal ( 0 , result ) ;
133
134
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 ;
136
138
Assert . True ( File . Exists ( testResultFile ) , $ "File '{ testResultFile } ' does not exist") ;
137
- AssertCoverage ( standardOutput , $ "coverage. { _buildTargetFramework } .json" ) ;
139
+ AssertCoverage ( standardOutput , filename ) ;
138
140
139
141
CleanupBuildOutput ( ) ;
140
142
}
@@ -156,13 +158,13 @@ public void Msbuild_SourceLink()
156
158
_output . WriteLine ( buildOutput ) ;
157
159
}
158
160
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 } ") ;
160
162
161
163
Assert . True ( File . Exists ( sourceRootMappingFilePath ) , $ "File not found: { sourceRootMappingFilePath } ") ;
162
164
Assert . False ( string . IsNullOrEmpty ( File . ReadAllText ( sourceRootMappingFilePath ) ) ) ;
163
165
Assert . Contains ( "=/_/" , File . ReadAllText ( sourceRootMappingFilePath ) ) ;
164
166
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 } ";
166
168
_output . WriteLine ( $ "Command: dotnet { cmdArgument } ") ;
167
169
int result = DotnetCli ( cmdArgument , out string standardOutput , out string standardError , _testProjectPath ) ;
168
170
if ( ! string . IsNullOrEmpty ( standardError ) )
@@ -175,11 +177,12 @@ public void Msbuild_SourceLink()
175
177
}
176
178
Assert . Equal ( 0 , result ) ;
177
179
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 ;
180
183
Assert . True ( File . Exists ( testResultFile ) , $ "File '{ testResultFile } ' does not exist") ;
181
184
Assert . Contains ( "raw.githubusercontent.com" , File . ReadAllText ( testResultFile ) ) ;
182
- AssertCoverage ( standardOutput , $ "coverage. { _buildTargetFramework } .json" , checkDeterministicReport : false ) ;
185
+ AssertCoverage ( standardOutput , filename , checkDeterministicReport : false ) ;
183
186
184
187
CleanupBuildOutput ( ) ;
185
188
}
@@ -205,13 +208,13 @@ public void Collectors()
205
208
_output . WriteLine ( buildOutput ) ;
206
209
}
207
210
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 } ") ;
209
212
210
213
Assert . True ( File . Exists ( sourceRootMappingFilePath ) , $ "File not found: { sourceRootMappingFilePath } ") ;
211
214
Assert . NotEmpty ( File . ReadAllText ( sourceRootMappingFilePath ) ) ;
212
215
Assert . Contains ( "=/_/" , File . ReadAllText ( sourceRootMappingFilePath ) ) ;
213
216
214
- string runSettingsPath = AddCollectorRunsettingsFile ( _testProjectPath , "[coverletsample.integration.determisticbuild ]*DeepThought", deterministicReport : true ) ;
217
+ string runSettingsPath = AddCollectorRunsettingsFile ( _testProjectPath , $ "[ { s_sutName } ]*DeepThought", deterministicReport : true ) ;
215
218
string cmdArgument = $ "test -c { _buildConfiguration } -f { _buildTargetFramework } --no-build --collect:\" XPlat Code Coverage\" --results-directory:\" { testResultPath } \" --settings \" { runSettingsPath } \" --diag:{ Path . Combine ( testLogFilesPath , "log.txt" ) } ";
216
219
_output . WriteLine ( $ "Command: dotnet { cmdArgument } ") ;
217
220
int result = DotnetCli ( cmdArgument , out string standardOutput , out string standardError , _testProjectPath ) ;
@@ -260,13 +263,13 @@ public void Collectors_SourceLink()
260
263
_output . WriteLine ( buildOutput ) ;
261
264
}
262
265
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 } ") ;
264
267
265
268
Assert . True ( File . Exists ( sourceRootMappingFilePath ) , $ "File not found: { sourceRootMappingFilePath } ") ;
266
269
Assert . NotEmpty ( File . ReadAllText ( sourceRootMappingFilePath ) ) ;
267
270
Assert . Contains ( "=/_/" , File . ReadAllText ( sourceRootMappingFilePath ) ) ;
268
271
269
- string runSettingsPath = AddCollectorRunsettingsFile ( _testProjectPath , "[coverletsample.integration.determisticbuild ]*DeepThought", sourceLink : true ) ;
272
+ string runSettingsPath = AddCollectorRunsettingsFile ( _testProjectPath , $ "[ { s_sutName } ]*DeepThought", sourceLink : true ) ;
270
273
string cmdArgument = $ "test -c { _buildConfiguration } -f { _buildTargetFramework } --no-build --collect:\" XPlat Code Coverage\" --results-directory:\" { testResultPath } \" --settings \" { runSettingsPath } \" --diag:{ Path . Combine ( testLogFilesPath , "log.txt" ) } ";
271
274
_output . WriteLine ( $ "Command: dotnet { cmdArgument } ") ;
272
275
int result = DotnetCli ( cmdArgument , out string standardOutput , out string standardError , _testProjectPath ) ;
@@ -377,7 +380,7 @@ private static void DeleteCoverageFiles(string directory)
377
380
}
378
381
}
379
382
380
- private string GetReportPath ( string standardOutput , string reportFileName = "" )
383
+ private string GetReportDirectory ( string standardOutput , string reportFileName = "" )
381
384
{
382
385
string reportPath = "" ;
383
386
if ( standardOutput . Contains ( reportFileName ) )
@@ -388,6 +391,42 @@ private string GetReportPath(string standardOutput, string reportFileName = "")
388
391
}
389
392
return reportPath ;
390
393
}
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
+ }
391
430
public void Dispose ( )
392
431
{
393
432
File . Delete ( Path . Combine ( _testProjectPath , PropsFileName ) ) ;
0 commit comments