diff --git a/README.md b/README.md index 11ec91c6..7209046b 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,10 @@ or download from [releases](https://github.com/FortuneN/FineCodeCoverage/release For .Net -FCC supports the new [Microsoft.Testing.Platform](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-platform-intro). -FCC does not support the associated testing platform server mode for when using microsoft as a coverage provider. -You can disable this feature - "Options / Environment / Preview Features / Use testing platform server mode" -but it is not necessary as FCC adds a global msbuild property, DisableTestingPlatformServerCapability true, that removes -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)). +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. +Support for TUnit will be available shortly but will require running tests differently. -When not using Microsoft.TestingPlatform you have added test adapters through nuget packages. For instance, the NUnit Test Adapter extension is not sufficient. +When not using Microsoft.Testing.Platform you have added test adapters through nuget packages. For instance, the NUnit Test Adapter extension is not sufficient. --- diff --git a/SharedProject/Core/MsTestPlatform/TestingPlatform/DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs b/SharedProject/Core/MsTestPlatform/TestingPlatform/DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs index 1c74e4a1..aa349900 100644 --- a/SharedProject/Core/MsTestPlatform/TestingPlatform/DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs +++ b/SharedProject/Core/MsTestPlatform/TestingPlatform/DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs @@ -1,5 +1,4 @@ using FineCodeCoverage.Engine.Model; -using FineCodeCoverage.Engine.MsTestPlatform.TestingPlatform; using FineCodeCoverage.Options; using Microsoft.VisualStudio; using Microsoft.VisualStudio.ProjectSystem; @@ -9,7 +8,9 @@ using System; using System.Collections.Immutable; using System.ComponentModel.Composition; +using System.Linq; using System.Threading; +using System.Threading.Tasks; namespace FineCodeCoverage.Core.MsTestPlatform.TestingPlatform { @@ -24,16 +25,15 @@ Classes exported via MEF can declare the project capabilities under which they a See https://learn.microsoft.com/en-gb/dotnet/api/microsoft.visualstudio.shell.interop.vsprojectcapabilityexpressionmatcher?view=visualstudiosdk-2022 For expression syntax */ - [AppliesTo("TestingPlatformServer.ExitOnProcessExitCapability | TestingPlatformServer.UseListTestsOptionForDiscoveryCapability")] + [AppliesTo("TestContainer")] internal class DisableTestingPlatformServerCapabilityGlobalPropertiesProvider : StaticGlobalPropertiesProviderBase { - private readonly IUseTestingPlatformProtocolFeatureService useTestingPlatformProtocolFeatureService; + private readonly UnconfiguredProject unconfiguredProject; private readonly IAppOptionsProvider appOptionsProvider; private readonly ICoverageProjectSettingsManager coverageProjectSettingsManager; - private CoverageProject coverageProject; + [ImportingConstructor] public DisableTestingPlatformServerCapabilityGlobalPropertiesProvider( - IUseTestingPlatformProtocolFeatureService useTestingPlatformProtocolFeatureService, IProjectService projectService, UnconfiguredProject unconfiguredProject, IAppOptionsProvider appOptionsProvider, @@ -41,72 +41,79 @@ ICoverageProjectSettingsManager coverageProjectSettingsManager ) : base((IProjectCommonServices)projectService.Services) { - ThreadHelper.JoinableTaskFactory.Run(async () => - { - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - var hostObject = unconfiguredProject.Services.HostObject; - - var vsHierarchy = (IVsHierarchy)hostObject; - if (vsHierarchy != null) - { - var success = vsHierarchy.GetGuidProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out Guid projectGuid) == VSConstants.S_OK; - - if (success) - { - // todo - ICoverageProjectSettingsManager.GetSettingsAsync parameter - // to change to what it actually needs - coverageProject = new CoverageProject(appOptionsProvider, null, coverageProjectSettingsManager, null) - { - Id = projectGuid, - ProjectFile = unconfiguredProject.FullPath - }; - } - } - }); - - this.useTestingPlatformProtocolFeatureService = useTestingPlatformProtocolFeatureService; + this.unconfiguredProject = unconfiguredProject; this.appOptionsProvider = appOptionsProvider; this.coverageProjectSettingsManager = coverageProjectSettingsManager; } - // visual studio options states that a restart is required. If this is true then could cache this value - private async System.Threading.Tasks.Task UsingTestingPlatformProtocolAsync() - { - var useTestingPlatformProtocolFeature = await useTestingPlatformProtocolFeatureService.GetAsync(); - return useTestingPlatformProtocolFeature.HasValue && useTestingPlatformProtocolFeature.Value; - } - private bool AllProjectsDisabled() { var appOptions = appOptionsProvider.Get(); return !appOptions.Enabled && appOptions.DisabledNoCoverage; } - private async System.Threading.Tasks.Task ProjectEnabledAsync() + private async Task IsTUnitAsync() + { + var configuredProject = await unconfiguredProject.GetSuggestedConfiguredProjectAsync(); + var references = await configuredProject.Services.PackageReferences.GetUnresolvedReferencesAsync(); + return references.Any(r => r.UnevaluatedInclude == TUnitConstants.TUnitPackageId); + } + + private async Task ProjectEnabledAsync() { + var coverageProject = await GetCoverageProjectAsync(); if (coverageProject != null) { + var isTUnit = await IsTUnitAsync(); + if (isTUnit) + { + return false; + } var projectSettings = await coverageProjectSettingsManager.GetSettingsAsync(coverageProject); return projectSettings.Enabled; } return true; } - public override async System.Threading.Tasks.Task> GetGlobalPropertiesAsync(CancellationToken cancellationToken) + private async Task GetProjectGuidAsync() + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + var hostObject = unconfiguredProject.Services.HostObject; + + var vsHierarchy = (IVsHierarchy)hostObject; + if (vsHierarchy != null) + { + var success = vsHierarchy.GetGuidProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out Guid projectGuid) == VSConstants.S_OK; + + if (success) + { + return projectGuid; + } + } + return null; + } + + private async Task GetCoverageProjectAsync() { - /* - Note that it only matters for ms code coverage but not going to test for that - Main thing is that FCC does not turn off if user has Enterprise which does support - the new feature and has turned off FCC. - */ - if (await UsingTestingPlatformProtocolAsync() && !AllProjectsDisabled() && await ProjectEnabledAsync()) + var projectGuid = await GetProjectGuidAsync(); + if (projectGuid.HasValue) + { + return new CoverageProject(appOptionsProvider, null, coverageProjectSettingsManager, null) + { + Id = projectGuid.Value, + ProjectFile = unconfiguredProject.FullPath + }; + } + return null; + } + + public override async Task> GetGlobalPropertiesAsync(CancellationToken cancellationToken) + { + if (!AllProjectsDisabled() && await ProjectEnabledAsync()) { - // https://github.com/microsoft/testfx/blob/main/src/Platform/Microsoft.Testing.Platform.MSBuild/buildMultiTargeting/Microsoft.Testing.Platform.MSBuild.targets return Empty.PropertiesMap.Add("DisableTestingPlatformServerCapability", "true"); } return Empty.PropertiesMap; } - } - } diff --git a/SharedProject/Core/MsTestPlatform/TestingPlatform/IUseTestingPlatformProtocolFeatureService.cs b/SharedProject/Core/MsTestPlatform/TestingPlatform/IUseTestingPlatformProtocolFeatureService.cs deleted file mode 100644 index a7b9e6bf..00000000 --- a/SharedProject/Core/MsTestPlatform/TestingPlatform/IUseTestingPlatformProtocolFeatureService.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; - -namespace FineCodeCoverage.Engine.MsTestPlatform.TestingPlatform -{ - internal interface IUseTestingPlatformProtocolFeatureService - { - Task GetAsync(); - } - -} diff --git a/SharedProject/Core/MsTestPlatform/TestingPlatform/TUnitConstants.cs b/SharedProject/Core/MsTestPlatform/TestingPlatform/TUnitConstants.cs new file mode 100644 index 00000000..694f1ff6 --- /dev/null +++ b/SharedProject/Core/MsTestPlatform/TestingPlatform/TUnitConstants.cs @@ -0,0 +1,7 @@ +namespace FineCodeCoverage.Core.MsTestPlatform.TestingPlatform +{ + internal abstract class TUnitConstants + { + public const string TUnitPackageId = "TUnit"; + } +} diff --git a/SharedProject/Core/MsTestPlatform/TestingPlatform/UseTestingPlatformProtocolFeatureService.cs b/SharedProject/Core/MsTestPlatform/TestingPlatform/UseTestingPlatformProtocolFeatureService.cs deleted file mode 100644 index 45f6469f..00000000 --- a/SharedProject/Core/MsTestPlatform/TestingPlatform/UseTestingPlatformProtocolFeatureService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FineCodeCoverage.Options; -using System.ComponentModel.Composition; - -namespace FineCodeCoverage.Engine.MsTestPlatform.TestingPlatform -{ - [Export(typeof(IUseTestingPlatformProtocolFeatureService))] - internal class UseTestingPlatformProtocolFeatureService : IUseTestingPlatformProtocolFeatureService - { - private readonly IReadOnlyUserSettingsStoreProvider readableUserSettingsStoreProvider; - - [ImportingConstructor] - public UseTestingPlatformProtocolFeatureService( - IReadOnlyUserSettingsStoreProvider readableUserSettingsStoreProvider - ) - { - this.readableUserSettingsStoreProvider = readableUserSettingsStoreProvider; - } - public async System.Threading.Tasks.Task GetAsync() - { - var store = await readableUserSettingsStoreProvider.ProvideAsync(); - - try - { - var value = store.GetInt32("FeatureFlags\\TestingTools\\UnitTesting\\UseTestingPlatformProtocol", "Value"); - return value == 1; - } - catch - { - return null; - } - } - } -} diff --git a/SharedProject/SharedProject.projitems b/SharedProject/SharedProject.projitems index db733619..65bdd1bb 100644 --- a/SharedProject/SharedProject.projitems +++ b/SharedProject/SharedProject.projitems @@ -135,8 +135,7 @@ - - +