Support ValueTask-returning test methods for all target frameworks (#4059)

This commit is contained in:
Youssef Victor 2024-11-20 09:18:30 +01:00 коммит произвёл GitHub
Родитель 01609e7871
Коммит bb5de817fe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 32 добавлений и 18 удалений

Просмотреть файл

@ -19,6 +19,7 @@
<!-- CVE-2017-0247 -->
<SystemNetWebSocketsClientVersion>4.3.1</SystemNetWebSocketsClientVersion>
<SystemTextRegularExpressionsVersion>4.3.1</SystemTextRegularExpressionsVersion>
<SystemThreadingTasksExtensionsVersion>4.5.4</SystemThreadingTasksExtensionsVersion>
</PropertyGroup>
<PropertyGroup Label="Test dependencies">
<MicrosoftCodeAnalysisAnalyzerTestingVersion>1.1.3-beta1.24423.1</MicrosoftCodeAnalysisAnalyzerTestingVersion>
@ -45,6 +46,7 @@
<PackageVersion Include="Microsoft.TestPlatform.TranslationLayer" Version="$(MicrosoftNETTestSdkVersion)" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageVersion Include="Polyfill" Version="7.4.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
<!-- CVE-2019-0820 -->
<PackageVersion Include="System.Diagnostics.TextWriterTraceListener" Version="4.3.0" />
<!-- CVE-2019-0981, CVE-2019-0980, CVE-2019-0657 -->

Просмотреть файл

@ -103,9 +103,7 @@ internal static class MethodInfoExtensions
/// <returns>True if the method has a void/task return type..</returns>
internal static bool IsValidReturnType(this MethodInfo method)
=> ReflectHelper.MatchReturnType(method, typeof(Task))
#if NETCOREAPP
|| ReflectHelper.MatchReturnType(method, typeof(ValueTask))
#endif
|| (ReflectHelper.MatchReturnType(method, typeof(void)) && method.GetAsyncTypeName() == null);
/// <summary>
@ -201,11 +199,9 @@ internal static class MethodInfoExtensions
{
task.GetAwaiter().GetResult();
}
#if NET6_0_OR_GREATER
else if (invokeResult is ValueTask valueTask)
{
valueTask.GetAwaiter().GetResult();
}
#endif
}
}

Просмотреть файл

@ -6,6 +6,7 @@
<group targetFramework="netstandard2.0">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$TestingPlatformVersion$" />
<dependency id="System.Threading.Tasks.Extensions" version="$SystemThreadingTasksExtensionsVersion$" />
</group>
<group targetFramework="netcoreapp3.1">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />

Просмотреть файл

@ -53,6 +53,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.AdapterUtilities" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="'$(TargetFramework)'=='netstandard2.0' OR '$(TargetFramework)' == '$(NetFrameworkMinimum)' OR '$(TargetFramework)'=='$(UwpMinimum)'" />
</ItemGroup>
<ItemGroup>
@ -80,6 +81,7 @@
<NuspecProperty Include="ArtifactsBinDir=$(ArtifactsBinDir)" />
<NuspecProperty Include="Configuration=$(Configuration)" />
<NuspecProperty Include="TestingPlatformVersion=$(Version.Replace('$(VersionPrefix)', '$(TestingPlatformVersionPrefix)'))" />
<NuspecProperty Include="SystemThreadingTasksExtensionsVersion=$(SystemThreadingTasksExtensionsVersion)" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -6,12 +6,16 @@
<group targetFramework="netstandard2.0">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$TestingPlatformVersion$" />
<dependency id="System.Threading.Tasks.Extensions" version="$SystemThreadingTasksExtensionsVersion$" />
</group>
<group targetFramework="net462">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$TestingPlatformVersion$" />
<dependency id="System.Threading.Tasks.Extensions" version="$SystemThreadingTasksExtensionsVersion$" />
</group>
<group targetFramework="uap10.0">
<dependency id="System.Threading.Tasks.Extensions" version="$SystemThreadingTasksExtensionsVersion$" />
</group>
<group targetFramework="uap10.0" />
<group targetFramework="netcoreapp3.1">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$TestingPlatformVersion$" />

Просмотреть файл

@ -14,9 +14,10 @@ public sealed class ValueTaskTests : AcceptanceTestBase
public ValueTaskTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
: base(testExecutionContext) => _testAssetFixture = testAssetFixture;
public async Task CanUseValueTaskForAllKnownLocations()
[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task CanUseValueTaskForAllKnownLocations(string tfm)
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent.Arguments);
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync();
// Assert
@ -35,7 +36,7 @@ public sealed class ValueTaskTests : AcceptanceTestBase
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.NetCurrent)
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}
@ -63,41 +64,49 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class UnitTest1
{
private static ValueTask CompletedTask =>
#if !NET5_0_OR_GREATER
// ValueTask.CompletedTask is only available in .NET 5 and later
default;
#else
ValueTask.CompletedTask;
#endif
[AssemblyInitialize]
public static ValueTask AssemblyInitialize(TestContext testContext) => ValueTask.CompletedTask;
public static ValueTask AssemblyInitialize(TestContext testContext) => CompletedTask;
[AssemblyCleanup]
public static ValueTask AssemblyCleanup() => ValueTask.CompletedTask;
public static ValueTask AssemblyCleanup() => CompletedTask;
[ClassInitialize]
public static ValueTask ClassInitialize(TestContext testContext) => ValueTask.CompletedTask;
public static ValueTask ClassInitialize(TestContext testContext) => CompletedTask;
[ClassCleanup]
public static ValueTask ClassCleanup() => ValueTask.CompletedTask;
public static ValueTask ClassCleanup() => CompletedTask;
[TestInitialize]
public ValueTask TestInit() => ValueTask.CompletedTask;
public ValueTask TestInit() => CompletedTask;
[TestCleanup]
public ValueTask TestCleanup() => ValueTask.CompletedTask;
public ValueTask TestCleanup() => CompletedTask;
[TestMethod]
public async ValueTask TestMethod1() => await ValueTask.CompletedTask;
public async ValueTask TestMethod1() => await CompletedTask;
[TestMethod]
public ValueTask TestMethod2() => ValueTask.CompletedTask;
public ValueTask TestMethod2() => CompletedTask;
[TestMethod]
public async ValueTask FailedTestMethod()
{
await ValueTask.CompletedTask;
await CompletedTask;
Assert.Fail();
}
[TestMethod]
public async ValueTask InconclusiveTestMethod()
{
await ValueTask.CompletedTask;
await CompletedTask;
Assert.Inconclusive();
}
}