Skip to content

Commit 76c74b9

Browse files
committed
Improve diagnostics and robustness for MTP Coverlet extension
Add detailed diagnostic logging and debugger attach support via environment variables. Enhance test module path resolution and configuration logging. Refactor DI container creation and introduce MtpProcessExitHandler for in/out-of-process execution. Add default exclude filter, improve report output handling, and expand command line parsing. Integrate Microsoft.Extensions.Configuration and update test project output path for consistency.
1 parent 08bc32a commit 76c74b9

File tree

7 files changed

+467
-36
lines changed

7 files changed

+467
-36
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" />
2828
<PackageVersion Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="5.0.0-1.25277.114" />
2929
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
30+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.1" />
3031
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
3132
<PackageVersion Include="Microsoft.Extensions.FileSystemGlobbing" Version="8.0.0" />
3233
<!--For test TestInstrument_NetstandardAwareAssemblyResolver_PreserveCompilationContext-->

src/coverlet.MTP/CoverletExtensionCollector.cs

Lines changed: 408 additions & 31 deletions
Large diffs are not rendered by default.

src/coverlet.MTP/CoverletExtensionConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ internal class CoverletExtensionConfiguration
2626
public bool SkipAutoProps { get; set; }
2727
public bool DeterministicReport { get; set; }
2828
public string? ExcludeAssembliesWithoutSources { get; set; }
29+
public bool DisableManagedInstrumentationRestore { get; set; }
2930
}
3031
}

src/coverlet.MTP/CoverletExtensionProvider.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
88
using Microsoft.Testing.Platform.Services;
99
using Microsoft.Testing.Extensions.Diagnostics;
10+
using Microsoft.Extensions.Configuration;
1011

1112
namespace coverlet.Extension
1213
{
@@ -23,9 +24,17 @@ public static void AddCoverletExtensionProvider(this ITestApplicationBuilder bui
2324
serviceProvider.GetLoggerFactory()));
2425

2526
builder.TestHostControllers.AddProcessLifetimeHandler(static serviceProvider
26-
=> new CoverletExtensionCollector(
27-
serviceProvider.GetLoggerFactory(),
28-
serviceProvider.GetCommandLineOptions()) as ITestHostProcessLifetimeHandler);
27+
=>
28+
{
29+
if (serviceProvider.GetConfiguration() is not IConfiguration configuration)
30+
{
31+
throw new ArgumentNullException(nameof(configuration), "Configuration cannot be null.");
32+
}
33+
return new CoverletExtensionCollector(
34+
serviceProvider.GetLoggerFactory(),
35+
serviceProvider.GetCommandLineOptions(),
36+
configuration) as ITestHostProcessLifetimeHandler;
37+
});
2938

3039
builder.CommandLine.AddProvider(() => new CoverletExtensionCommandLineProvider(_extension));
3140
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Toni Solarin-Sodara
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Coverlet.Core.Abstractions;
5+
6+
namespace coverlet.Extension.Collector
7+
{
8+
/// <summary>
9+
/// Implementation of IProcessExitHandler for Microsoft Testing Platform.
10+
/// Supports both in-process and out-of-process execution modes.
11+
/// </summary>
12+
internal sealed class MtpProcessExitHandler : IProcessExitHandler
13+
{
14+
private readonly bool _isOutOfProcess;
15+
16+
/// <summary>
17+
/// Creates a new instance of MtpProcessExitHandler.
18+
/// </summary>
19+
/// <param name="isOutOfProcess">
20+
/// True if running in out-of-process mode (controller manages lifecycle).
21+
/// False if running in-process (needs AppDomain.ProcessExit).
22+
/// </param>
23+
public MtpProcessExitHandler(bool isOutOfProcess = true)
24+
{
25+
_isOutOfProcess = isOutOfProcess;
26+
}
27+
28+
public void Add(EventHandler handler)
29+
{
30+
if (_isOutOfProcess)
31+
{
32+
// Out-of-process: MTP handles cleanup via OnTestHostProcessExitedAsync
33+
// No need to register ProcessExit handler
34+
return;
35+
}
36+
37+
// In-process: Register the handler for AppDomain.ProcessExit
38+
AppDomain.CurrentDomain.ProcessExit += handler;
39+
}
40+
}
41+
}

src/coverlet.MTP/coverlet.MTP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</ItemGroup>
5555

5656
<ItemGroup>
57+
<PackageReference Include="Microsoft.Extensions.Configuration" />
5758
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
5859
<PackageReference Include="Microsoft.Testing.Platform" />
5960
<PackageReference Include="System.Buffers" />

test/coverlet.MTP.validation.tests/CollectCoverageTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CollectCoverageTests()
3333
}
3434

3535
[Fact]
36-
public async Task BasicNoCoverage_CollectsDataForCoveredLines()
36+
public async Task TestCodeWithoutCodeCoverage()
3737
{
3838
// Arrange
3939
using var testProject = CreateTestProject(includeSimpleTest: true,
@@ -303,7 +303,8 @@ private TestProject CreateTestProject(
303303

304304
File.WriteAllText(Path.Combine(tempPath, "Tests.cs"), testCode);
305305

306-
return new TestProject(projectFile, Path.Combine(tempPath, "bin", _buildConfiguration, _buildTargetFramework));
306+
string outputPath = Path.Combine(tempPath, "bin", _buildConfiguration.ToLower());
307+
return new TestProject(projectFile, outputPath);
307308
}
308309

309310
private void CreateNuGetConfig(string projectPath)

0 commit comments

Comments
 (0)