Skip to content

Commit bef6a9f

Browse files
committed
Update config handling for new MTP API and null safety
Refactor to use Microsoft.Testing.Platform.Configurations.IConfiguration instead of Microsoft.Extensions.Configuration.IConfiguration. Make platform configuration nullable throughout, updating diagnostics and test module resolution logic to handle nulls safely. Remove exception on missing configuration in provider registration. Improves compatibility with latest MTP and robustness when coverage is not enabled.
1 parent 76c74b9 commit bef6a9f

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

src/coverlet.MTP/CoverletExtensionCollector.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
1414
using Coverlet.MTP.CommandLine;
1515
using Coverlet.Core.Instrumentation;
16-
using Microsoft.Extensions.Configuration;
1716

1817
namespace coverlet.Extension.Collector
1918
{
@@ -25,7 +24,7 @@ internal sealed class CoverletExtensionCollector : ITestHostProcessLifetimeHandl
2524
private readonly CoverletLoggerAdapter _logger;
2625
private readonly CoverletExtensionConfiguration _configuration;
2726
private IServiceProvider? _serviceProvider;
28-
private readonly Microsoft.Extensions.Configuration.IConfiguration _platformConfiguration;
27+
private readonly Microsoft.Testing.Platform.Configurations.IConfiguration? _platformConfiguration;
2928
private Coverage? _coverage;
3029
private readonly Microsoft.Testing.Platform.Logging.ILoggerFactory _loggerFactory;
3130
private readonly Microsoft.Testing.Platform.CommandLine.ICommandLineOptions _commandLineOptions;
@@ -51,15 +50,16 @@ internal sealed class CoverletExtensionCollector : ITestHostProcessLifetimeHandl
5150
public CoverletExtensionCollector(
5251
Microsoft.Testing.Platform.Logging.ILoggerFactory loggerFactory,
5352
Microsoft.Testing.Platform.CommandLine.ICommandLineOptions commandLineOptions,
54-
Microsoft.Extensions.Configuration.IConfiguration configuration)
53+
Microsoft.Testing.Platform.Configurations.IConfiguration? configuration)
5554
{
5655
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
5756
_commandLineOptions = commandLineOptions ?? throw new ArgumentNullException(nameof(commandLineOptions));
58-
_platformConfiguration = configuration ?? throw new ArgumentNullException(nameof(configuration));
57+
_platformConfiguration = configuration; // Allow null - coverage flag checked first
5958
_configuration = new CoverletExtensionConfiguration();
6059
_logger = new CoverletLoggerAdapter(_loggerFactory);
6160

62-
// Allow attaching debugger via environment variable
61+
// Only run diagnostics if coverage will be enabled
62+
// The actual check happens in BeforeTestHostProcessStartAsync
6363
AttachDebuggerIfRequested();
6464

6565
LogDiagnostic("CoverletExtensionCollector constructor called");
@@ -241,6 +241,11 @@ Task ITestHostProcessLifetimeHandler.BeforeTestHostProcessStartAsync(Cancellatio
241241
/// </summary>
242242
private void LogConfigurationDiagnostics()
243243
{
244+
if (_platformConfiguration == null)
245+
{
246+
LogDiagnostic("Platform Configuration: null (coverage not enabled)");
247+
return;
248+
}
244249
LogDiagnostic("Platform Configuration values:");
245250
try
246251
{
@@ -260,11 +265,20 @@ private void LogConfigurationDiagnostics()
260265
LogDiagnostic($" [{key}] = {value ?? "(null)"}");
261266
}
262267

263-
// Log all configuration children if accessible
264-
IEnumerable<IConfigurationSection> children = _platformConfiguration.GetChildren();
265-
foreach (IConfigurationSection child in children)
268+
// Try to enumerate known configuration keys (as a fallback)
269+
string[] knownKeys = new[]
270+
{
271+
"TestModule",
272+
"TestHostController:Mode",
273+
"TestHost:Path",
274+
"TestAssembly",
275+
"TargetPath"
276+
};
277+
278+
foreach (string key in knownKeys)
266279
{
267-
LogDiagnostic($" Config child: {child.Key} = {child.Value ?? "(section)"}");
280+
string? value = _platformConfiguration[key];
281+
LogDiagnostic($" Config key: {key} = {value ?? "(null)"}");
268282
}
269283
}
270284
catch (Exception ex)
@@ -353,22 +367,25 @@ private void LogParsedConfigurationDiagnostics()
353367
{
354368
LogDiagnostic("Resolving test module path...");
355369

356-
// Try platform configuration first
357-
string? testModule = _platformConfiguration["TestModule"];
358-
LogDiagnostic($" From config 'TestModule': {testModule ?? "(null)"}");
359-
if (!string.IsNullOrEmpty(testModule) && File.Exists(testModule))
370+
// Only try platform configuration if it exists
371+
if (_platformConfiguration != null)
360372
{
361-
LogDiagnostic($" -> Using TestModule from config");
362-
return testModule;
363-
}
373+
string? testModule = _platformConfiguration["TestModule"];
374+
LogDiagnostic($" From config 'TestModule': {testModule ?? "(null)"}");
375+
if (!string.IsNullOrEmpty(testModule) && File.Exists(testModule))
376+
{
377+
LogDiagnostic($" -> Using TestModule from config");
378+
return testModule;
379+
}
364380

365-
// Try TestHost:Path
366-
testModule = _platformConfiguration["TestHost:Path"];
367-
LogDiagnostic($" From config 'TestHost:Path': {testModule ?? "(null)"}");
368-
if (!string.IsNullOrEmpty(testModule) && File.Exists(testModule))
369-
{
370-
LogDiagnostic($" -> Using TestHost:Path from config");
371-
return testModule;
381+
// Try TestHost:Path
382+
testModule = _platformConfiguration["TestHost:Path"];
383+
LogDiagnostic($" From config 'TestHost:Path': {testModule ?? "(null)"}");
384+
if (!string.IsNullOrEmpty(testModule) && File.Exists(testModule))
385+
{
386+
LogDiagnostic($" -> Using TestHost:Path from config");
387+
return testModule;
388+
}
372389
}
373390

374391
// Try to get from command line arguments

src/coverlet.MTP/CoverletExtensionProvider.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
using coverlet.Extension.Collector;
55
using Coverlet.MTP.CommandLine;
66
using Microsoft.Testing.Platform.Builder;
7+
using Microsoft.Testing.Platform.Configurations;
78
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
89
using Microsoft.Testing.Platform.Services;
910
using Microsoft.Testing.Extensions.Diagnostics;
10-
using Microsoft.Extensions.Configuration;
1111

1212
namespace coverlet.Extension
1313
{
@@ -26,10 +26,9 @@ public static void AddCoverletExtensionProvider(this ITestApplicationBuilder bui
2626
builder.TestHostControllers.AddProcessLifetimeHandler(static serviceProvider
2727
=>
2828
{
29-
if (serviceProvider.GetConfiguration() is not IConfiguration configuration)
30-
{
31-
throw new ArgumentNullException(nameof(configuration), "Configuration cannot be null.");
32-
}
29+
// Configuration may be null when coverlet-coverage is not enabled.
30+
// The collector checks for --coverlet-coverage flag before using configuration.
31+
IConfiguration configuration = serviceProvider.GetConfiguration();
3332
return new CoverletExtensionCollector(
3433
serviceProvider.GetLoggerFactory(),
3534
serviceProvider.GetCommandLineOptions(),

0 commit comments

Comments
 (0)