Skip to content

Commit 9730d20

Browse files
authored
Merge pull request #499 from tonyhallett/DisableTestingPlatformServerCapabilityGlobalPropertiesProvider-TestContainer
Applies to TestContainer but do not apply for TUnit projects.
2 parents d3eb00e + e114f5c commit 9730d20

File tree

6 files changed

+65
-98
lines changed

6 files changed

+65
-98
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ or download from [releases](https://github.com/FortuneN/FineCodeCoverage/release
1313

1414
For .Net
1515

16-
FCC supports the new [Microsoft.Testing.Platform](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-platform-intro).
17-
FCC does not support the associated testing platform server mode for when using microsoft as a coverage provider.
18-
You can disable this feature - "Options / Environment / Preview Features / Use testing platform server mode"
19-
but it is not necessary as FCC adds a global msbuild property, DisableTestingPlatformServerCapability true, that removes
20-
the project capability. ([see Microsoft.Testing.Platform.MSBuild.targets](https://github.com/microsoft/testfx/blob/d141931b99fad0617d8435ce321fca0c45c9eb94/src/Platform/Microsoft.Testing.Platform.MSBuild/buildMultiTargeting/Microsoft.Testing.Platform.MSBuild.targets#L10)).
16+
FCC supports the new [Microsoft.Testing.Platform](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-platform-intro) for MsTest, NUnit and xUnit.
17+
Support for TUnit will be available shortly but will require running tests differently.
2118

22-
When not using Microsoft.TestingPlatform you have added test adapters through nuget packages. For instance, the NUnit Test Adapter extension is not sufficient.
19+
When not using Microsoft.Testing.Platform you have added test adapters through nuget packages. For instance, the NUnit Test Adapter extension is not sufficient.
2320

2421
---
2522

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using FineCodeCoverage.Engine.Model;
2-
using FineCodeCoverage.Engine.MsTestPlatform.TestingPlatform;
32
using FineCodeCoverage.Options;
43
using Microsoft.VisualStudio;
54
using Microsoft.VisualStudio.ProjectSystem;
@@ -9,7 +8,9 @@
98
using System;
109
using System.Collections.Immutable;
1110
using System.ComponentModel.Composition;
11+
using System.Linq;
1212
using System.Threading;
13+
using System.Threading.Tasks;
1314

1415
namespace FineCodeCoverage.Core.MsTestPlatform.TestingPlatform
1516
{
@@ -24,89 +25,95 @@ Classes exported via MEF can declare the project capabilities under which they a
2425
See https://learn.microsoft.com/en-gb/dotnet/api/microsoft.visualstudio.shell.interop.vsprojectcapabilityexpressionmatcher?view=visualstudiosdk-2022
2526
For expression syntax
2627
*/
27-
[AppliesTo("TestingPlatformServer.ExitOnProcessExitCapability | TestingPlatformServer.UseListTestsOptionForDiscoveryCapability")]
28+
[AppliesTo("TestContainer")]
2829
internal class DisableTestingPlatformServerCapabilityGlobalPropertiesProvider : StaticGlobalPropertiesProviderBase
2930
{
30-
private readonly IUseTestingPlatformProtocolFeatureService useTestingPlatformProtocolFeatureService;
31+
private readonly UnconfiguredProject unconfiguredProject;
3132
private readonly IAppOptionsProvider appOptionsProvider;
3233
private readonly ICoverageProjectSettingsManager coverageProjectSettingsManager;
33-
private CoverageProject coverageProject;
34+
3435
[ImportingConstructor]
3536
public DisableTestingPlatformServerCapabilityGlobalPropertiesProvider(
36-
IUseTestingPlatformProtocolFeatureService useTestingPlatformProtocolFeatureService,
3737
IProjectService projectService,
3838
UnconfiguredProject unconfiguredProject,
3939
IAppOptionsProvider appOptionsProvider,
4040
ICoverageProjectSettingsManager coverageProjectSettingsManager
4141
)
4242
: base((IProjectCommonServices)projectService.Services)
4343
{
44-
ThreadHelper.JoinableTaskFactory.Run(async () =>
45-
{
46-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
47-
var hostObject = unconfiguredProject.Services.HostObject;
48-
49-
var vsHierarchy = (IVsHierarchy)hostObject;
50-
if (vsHierarchy != null)
51-
{
52-
var success = vsHierarchy.GetGuidProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out Guid projectGuid) == VSConstants.S_OK;
53-
54-
if (success)
55-
{
56-
// todo - ICoverageProjectSettingsManager.GetSettingsAsync parameter
57-
// to change to what it actually needs
58-
coverageProject = new CoverageProject(appOptionsProvider, null, coverageProjectSettingsManager, null)
59-
{
60-
Id = projectGuid,
61-
ProjectFile = unconfiguredProject.FullPath
62-
};
63-
}
64-
}
65-
});
66-
67-
this.useTestingPlatformProtocolFeatureService = useTestingPlatformProtocolFeatureService;
44+
this.unconfiguredProject = unconfiguredProject;
6845
this.appOptionsProvider = appOptionsProvider;
6946
this.coverageProjectSettingsManager = coverageProjectSettingsManager;
7047
}
7148

72-
// visual studio options states that a restart is required. If this is true then could cache this value
73-
private async System.Threading.Tasks.Task<bool> UsingTestingPlatformProtocolAsync()
74-
{
75-
var useTestingPlatformProtocolFeature = await useTestingPlatformProtocolFeatureService.GetAsync();
76-
return useTestingPlatformProtocolFeature.HasValue && useTestingPlatformProtocolFeature.Value;
77-
}
78-
7949
private bool AllProjectsDisabled()
8050
{
8151
var appOptions = appOptionsProvider.Get();
8252
return !appOptions.Enabled && appOptions.DisabledNoCoverage;
8353
}
8454

85-
private async System.Threading.Tasks.Task<bool> ProjectEnabledAsync()
55+
private async Task<bool> IsTUnitAsync()
56+
{
57+
var configuredProject = await unconfiguredProject.GetSuggestedConfiguredProjectAsync();
58+
var references = await configuredProject.Services.PackageReferences.GetUnresolvedReferencesAsync();
59+
return references.Any(r => r.UnevaluatedInclude == TUnitConstants.TUnitPackageId);
60+
}
61+
62+
private async Task<bool> ProjectEnabledAsync()
8663
{
64+
var coverageProject = await GetCoverageProjectAsync();
8765
if (coverageProject != null)
8866
{
67+
var isTUnit = await IsTUnitAsync();
68+
if (isTUnit)
69+
{
70+
return false;
71+
}
8972
var projectSettings = await coverageProjectSettingsManager.GetSettingsAsync(coverageProject);
9073
return projectSettings.Enabled;
9174
}
9275
return true;
9376
}
9477

95-
public override async System.Threading.Tasks.Task<IImmutableDictionary<string, string>> GetGlobalPropertiesAsync(CancellationToken cancellationToken)
78+
private async Task<Guid?> GetProjectGuidAsync()
79+
{
80+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
81+
var hostObject = unconfiguredProject.Services.HostObject;
82+
83+
var vsHierarchy = (IVsHierarchy)hostObject;
84+
if (vsHierarchy != null)
85+
{
86+
var success = vsHierarchy.GetGuidProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out Guid projectGuid) == VSConstants.S_OK;
87+
88+
if (success)
89+
{
90+
return projectGuid;
91+
}
92+
}
93+
return null;
94+
}
95+
96+
private async Task<CoverageProject> GetCoverageProjectAsync()
9697
{
97-
/*
98-
Note that it only matters for ms code coverage but not going to test for that
99-
Main thing is that FCC does not turn off if user has Enterprise which does support
100-
the new feature and has turned off FCC.
101-
*/
102-
if (await UsingTestingPlatformProtocolAsync() && !AllProjectsDisabled() && await ProjectEnabledAsync())
98+
var projectGuid = await GetProjectGuidAsync();
99+
if (projectGuid.HasValue)
100+
{
101+
return new CoverageProject(appOptionsProvider, null, coverageProjectSettingsManager, null)
102+
{
103+
Id = projectGuid.Value,
104+
ProjectFile = unconfiguredProject.FullPath
105+
};
106+
}
107+
return null;
108+
}
109+
110+
public override async Task<IImmutableDictionary<string, string>> GetGlobalPropertiesAsync(CancellationToken cancellationToken)
111+
{
112+
if (!AllProjectsDisabled() && await ProjectEnabledAsync())
103113
{
104-
// https://github.com/microsoft/testfx/blob/main/src/Platform/Microsoft.Testing.Platform.MSBuild/buildMultiTargeting/Microsoft.Testing.Platform.MSBuild.targets
105114
return Empty.PropertiesMap.Add("DisableTestingPlatformServerCapability", "true");
106115
}
107116
return Empty.PropertiesMap;
108117
}
109-
110118
}
111-
112119
}

SharedProject/Core/MsTestPlatform/TestingPlatform/IUseTestingPlatformProtocolFeatureService.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FineCodeCoverage.Core.MsTestPlatform.TestingPlatform
2+
{
3+
internal abstract class TUnitConstants
4+
{
5+
public const string TUnitPackageId = "TUnit";
6+
}
7+
}

SharedProject/Core/MsTestPlatform/TestingPlatform/UseTestingPlatformProtocolFeatureService.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

SharedProject/SharedProject.projitems

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@
135135
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\MsTestPlatformUtil.cs" />
136136
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\RunSettingsHelper.cs" />
137137
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs" />
138-
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\IUseTestingPlatformProtocolFeatureService.cs" />
139-
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\UseTestingPlatformProtocolFeatureService.cs" />
138+
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\TUnitConstants.cs" />
140139
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\IOpenCoverExeArgumentsProvider.cs" />
141140
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\IOpenCoverUtil.cs" />
142141
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\OpenCoverExeArgumentsProvider.cs" />

0 commit comments

Comments
 (0)