@@ -303,6 +303,8 @@ private static BuildConfiguration GetAssemblyBuildConfiguration()
303303
304304 static class TestInstrumentationHelper
305305 {
306+ private static IServiceProvider _processWideContainer ;
307+
306308 /// <summary>
307309 /// caller sample: TestInstrumentationHelper.GenerateHtmlReport(result, sourceFileFilter: @"+**\Samples\Instrumentation.cs");
308310 /// TestInstrumentationHelper.GenerateHtmlReport(result);
@@ -335,14 +337,16 @@ public static void GenerateHtmlReport(CoverageResult coverageResult, IReporter r
335337
336338 public static CoverageResult GetCoverageResult ( string filePath )
337339 {
340+ SetTestContainer ( ) ;
338341 using var result = new FileStream ( filePath , FileMode . Open ) ;
339342 var logger = new Mock < ILogger > ( ) ;
340343 logger . Setup ( l => l . LogVerbose ( It . IsAny < string > ( ) ) ) . Callback ( ( string message ) =>
341344 {
342345 Assert . DoesNotContain ( "not found for module: " , message ) ;
343346 } ) ;
347+ _processWideContainer . GetRequiredService < IInstrumentationHelper > ( ) . SetLogger ( logger . Object ) ;
344348 CoveragePrepareResult coveragePrepareResultLoaded = CoveragePrepareResult . Deserialize ( result ) ;
345- Coverage coverage = new Coverage ( coveragePrepareResultLoaded , logger . Object , DependencyInjection . Current . GetService < IInstrumentationHelper > ( ) , new FileSystem ( ) ) ;
349+ Coverage coverage = new Coverage ( coveragePrepareResultLoaded , logger . Object , _processWideContainer . GetService < IInstrumentationHelper > ( ) , new FileSystem ( ) ) ;
346350 return coverage . GetCoverageResult ( ) ;
347351 }
348352
@@ -353,22 +357,7 @@ async public static Task<CoveragePrepareResult> Run<T>(Func<dynamic, Task> callM
353357 throw new ArgumentNullException ( nameof ( persistPrepareResultToFile ) ) ;
354358 }
355359
356- var serviceCollection = new ServiceCollection ( ) ;
357- serviceCollection . AddTransient < IRetryHelper , CustomRetryHelper > ( ) ;
358- serviceCollection . AddTransient < IProcessExitHandler , CustomProcessExitHandler > ( ) ;
359- serviceCollection . AddTransient < IFileSystem , FileSystem > ( ) ;
360- if ( disableRestoreModules )
361- {
362- serviceCollection . AddSingleton < IInstrumentationHelper , InstrumentationHelperForDebugging > ( ) ;
363- }
364- else
365- {
366- serviceCollection . AddSingleton < IInstrumentationHelper , InstrumentationHelper > ( ) ;
367- }
368-
369- // Setup correct retry helper to avoid exception in InstrumentationHelper.RestoreOriginalModules on remote process exit
370- DependencyInjection . Set ( serviceCollection . BuildServiceProvider ( ) ) ;
371-
360+ SetTestContainer ( disableRestoreModules ) ;
372361
373362 // Rename test file to avoid locks
374363 string location = typeof ( T ) . Assembly . Location ;
@@ -392,7 +381,7 @@ async public static Task<CoveragePrepareResult> Run<T>(Func<dynamic, Task> callM
392381 {
393382 "[xunit.*]*" ,
394383 "[coverlet.*]*"
395- } ) . ToArray ( ) , Array . Empty < string > ( ) , Array . Empty < string > ( ) , true , false , "" , false , new Logger ( logFile ) , DependencyInjection . Current . GetService < IInstrumentationHelper > ( ) , DependencyInjection . Current . GetService < IFileSystem > ( ) ) ;
384+ } ) . ToArray ( ) , Array . Empty < string > ( ) , Array . Empty < string > ( ) , true , false , "" , false , new Logger ( logFile ) , _processWideContainer . GetService < IInstrumentationHelper > ( ) , _processWideContainer . GetService < IFileSystem > ( ) ) ;
396385 CoveragePrepareResult prepareResult = coverage . PrepareModules ( ) ;
397386
398387 Assert . Single ( prepareResult . Results ) ;
@@ -421,6 +410,30 @@ async public static Task<CoveragePrepareResult> Run<T>(Func<dynamic, Task> callM
421410
422411 return prepareResult ;
423412 }
413+
414+ private static void SetTestContainer ( bool disableRestoreModules = false )
415+ {
416+ LazyInitializer . EnsureInitialized ( ref _processWideContainer , ( ) =>
417+ {
418+ var serviceCollection = new ServiceCollection ( ) ;
419+ serviceCollection . AddTransient < IRetryHelper , CustomRetryHelper > ( ) ;
420+ serviceCollection . AddTransient < IProcessExitHandler , CustomProcessExitHandler > ( ) ;
421+ serviceCollection . AddTransient < IFileSystem , FileSystem > ( ) ;
422+ serviceCollection . AddTransient ( _ => new Mock < ILogger > ( ) . Object ) ;
423+
424+ // We need to keep singleton/static semantics
425+ if ( disableRestoreModules )
426+ {
427+ serviceCollection . AddSingleton < IInstrumentationHelper , InstrumentationHelperForDebugging > ( ) ;
428+ }
429+ else
430+ {
431+ serviceCollection . AddSingleton < IInstrumentationHelper , InstrumentationHelper > ( ) ;
432+ }
433+
434+ return serviceCollection . BuildServiceProvider ( ) ;
435+ } ) ;
436+ }
424437 }
425438
426439 class CustomProcessExitHandler : IProcessExitHandler
@@ -514,8 +527,8 @@ public void LogWarning(string message)
514527
515528 class InstrumentationHelperForDebugging : InstrumentationHelper
516529 {
517- public InstrumentationHelperForDebugging ( IProcessExitHandler processExitHandler , IRetryHelper retryHelper , IFileSystem fileSystem )
518- : base ( processExitHandler , retryHelper , fileSystem )
530+ public InstrumentationHelperForDebugging ( IProcessExitHandler processExitHandler , IRetryHelper retryHelper , IFileSystem fileSystem , ILogger logger )
531+ : base ( processExitHandler , retryHelper , fileSystem , logger )
519532 {
520533
521534 }
0 commit comments