Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ internal static bool HasCorrectTimeout(this MethodInfo method, TimeoutAttribute?
/// <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>
Expand Down Expand Up @@ -201,11 +199,9 @@ internal static void InvokeAsSynchronousTask(this MethodInfo methodInfo, object?
{
task.GetAwaiter().GetResult();
}
#if NET6_0_OR_GREATER
else if (invokeResult is ValueTask valueTask)
{
valueTask.GetAwaiter().GetResult();
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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$" />
Expand Down
2 changes: 2 additions & 0 deletions src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
<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" />
<group targetFramework="netcoreapp3.1">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$TestingPlatformVersion$" />
<dependency id="System.Threading.Tasks.Extensions" version="$SystemThreadingTasksExtensionsVersion$" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs it seems that ValueTask is available for netcore3.1 so no need to add the deps.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The intent was for UWP. Thanks for catching that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Evangelink uap10.0 seems to currently have no dependencies at all. I'm wondering why it's different from all others which have VSTestBridge and MTP.MSBuild?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new platform hasn't been validated as working.

</group>
<group targetFramework="net6.0">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$TestingPlatformVersion$" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +36,7 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.NetCurrent)
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

Expand Down Expand Up @@ -63,41 +64,49 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test
[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();
}
}
Expand Down