Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Applies to TestContainer but do not apply for TUnit projects. #499

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Engine.MsTestPlatform.TestingPlatform;
using FineCodeCoverage.Options;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ProjectSystem;
Expand All @@ -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
{
Expand All @@ -24,89 +25,95 @@ 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,
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<bool> 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<bool> ProjectEnabledAsync()
private async Task<bool> IsTUnitAsync()
{
var configuredProject = await unconfiguredProject.GetSuggestedConfiguredProjectAsync();
var references = await configuredProject.Services.PackageReferences.GetUnresolvedReferencesAsync();
return references.Any(r => r.UnevaluatedInclude == TUnitConstants.TUnitPackageId);
}

private async Task<bool> 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<IImmutableDictionary<string, string>> GetGlobalPropertiesAsync(CancellationToken cancellationToken)
private async Task<Guid?> 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<CoverageProject> 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<IImmutableDictionary<string, string>> 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;
}

}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FineCodeCoverage.Core.MsTestPlatform.TestingPlatform
{
internal abstract class TUnitConstants
{
public const string TUnitPackageId = "TUnit";
}
}

This file was deleted.

3 changes: 1 addition & 2 deletions SharedProject/SharedProject.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\MsTestPlatformUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\RunSettingsHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\DisableTestingPlatformServerCapabilityGlobalPropertiesProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\IUseTestingPlatformProtocolFeatureService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\UseTestingPlatformProtocolFeatureService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\TestingPlatform\TUnitConstants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\IOpenCoverExeArgumentsProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\IOpenCoverUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\OpenCover\OpenCoverExeArgumentsProvider.cs" />
Expand Down
Loading