From 35877aa991989e9a17eadae6bf6e20c9749acd36 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Wed, 2 Mar 2022 09:51:42 +0100 Subject: [PATCH 1/3] Add more info to multi tfm failure --- .../MultitargetingTestHostTests.cs | 2 +- .../UnitTest1.cs | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs index 606f9f1467..3c55e1b5ea 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs @@ -17,7 +17,7 @@ public class MultitargetingTestHostTests : AcceptanceTestBase // xUnit supports net452 onwards, so that is why this starts at net452, I also don't test all framework versions [NetCoreRunner(NETFX452_48)] [NetFrameworkRunner(NETFX452_48)] - public void RunningTestWithAFailingDebugAssertDoesNotCrashTheHostingProcess(RunnerInfo runnerInfo) + public void TestRunInATesthostThatTargetsTheirChosenNETFramework(RunnerInfo runnerInfo) { SetTestEnvironment(_testEnvironment, runnerInfo); using var tempDir = new TempDirectory(); diff --git a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs index 37516d123a..5438268bd2 100644 --- a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs +++ b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs @@ -2,7 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics; using System.IO; +using System.Linq; using System.Net.Security; using System.Security.Authentication; @@ -54,6 +56,7 @@ public class UnitTest1 [Fact] public void FailsUntilNet462ButPassesOnNewerNetFramework() { + var processName = Process.GetCurrentProcess().ProcessName; Exception exception = null; try { @@ -68,18 +71,22 @@ public void FailsUntilNet462ButPassesOnNewerNetFramework() exception = ex; } - switch (TargetFramework) + var shouldThrowException = new[] { "NET451", "NET452", "NET46", "NET461", "NET462" }.Contains(TargetFramework); + + if (shouldThrowException && exception == null) + { + throw new Exception($"Expected the code above to throw exception, " + + $"because it fails when running in <= NET462 versions of .NET Framework, " + + $"and we are running in process that was compiled as {TargetFramework}, " + + $"and is called {processName}, but there was no exception."); + } + + if (!shouldThrowException && exception != null) { - case "NET451": - case "NET452": - case "NET46": - case "NET461": - case "NET462": - Assert.NotNull(exception); - break; - default: - Assert.Null(exception); - break; + throw new Exception($"Expected the code above to not throw an exception, " + + $"because it does not fail when running in > NET462 versions of .NET Framework, " + + $"and we are running in process that was compiled as {TargetFramework}, " + + $"and is called {processName}."); } } From 7089be772891f088f6b1dc8978f3d6f38988aca6 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Wed, 2 Mar 2022 11:46:23 +0100 Subject: [PATCH 2/3] Just use the compiled value, it is more precise for .NET Framework anyway. --- .../MultitargetingTestHostTests.cs | 12 +++++- .../UnitTest1.cs | 40 +------------------ 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs index 3c55e1b5ea..4846b6e0a4 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Collections.Generic; + using Microsoft.TestPlatform.TestUtilities; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -24,7 +26,15 @@ public void TestRunInATesthostThatTargetsTheirChosenNETFramework(RunnerInfo runn var assemblyPath = BuildMultipleAssemblyPath("MultitargetedNetFrameworkProject.dll").Trim('\"'); var arguments = PrepareArguments(assemblyPath, null, null, FrameworkArgValue, runnerInfo.InIsolationValue, resultsDirectory: tempDir.Path); - InvokeVsTest(arguments); + + // Tell the test project which target framework we are expecting it to run as. + // It has this value condionally compiled, so it can compare it. + var env = new Dictionary + { + ["EXPECTED_TARGET_FRAMEWORK"] = runnerInfo.TargetFramework + }; + + InvokeVsTest(arguments, env); ValidateSummaryStatus(passedTestsCount: 1, failedTestsCount: 0, 0); } diff --git a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs index 5438268bd2..35a6b30809 100644 --- a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs +++ b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs @@ -50,48 +50,12 @@ public class UnitTest1 #if NET48 public string TargetFramework { get; } = "NET48"; #endif - - // Using xUnit here because MSTest uses AppDomains by default and fixes this problem for us - // as long as the appdomains are enabled and modern .NET Framework is installed. [Fact] public void FailsUntilNet462ButPassesOnNewerNetFramework() { - var processName = Process.GetCurrentProcess().ProcessName; - Exception exception = null; - try - { - MemoryStream stream = new MemoryStream(); - SslStream sslStream = new SslStream(stream); - - // this throws SSLException on net451-net462, on net471 onwards it passes so we can use it to test that we target correctly - sslStream.BeginAuthenticateAsClient("microsoft.com", null, SslProtocols.None, false, new AsyncCallback(ProcessInformation), null); - } - catch (Exception ex) - { - exception = ex; - } - - var shouldThrowException = new[] { "NET451", "NET452", "NET46", "NET461", "NET462" }.Contains(TargetFramework); - - if (shouldThrowException && exception == null) - { - throw new Exception($"Expected the code above to throw exception, " + - $"because it fails when running in <= NET462 versions of .NET Framework, " + - $"and we are running in process that was compiled as {TargetFramework}, " + - $"and is called {processName}, but there was no exception."); - } + var expected = Environment.GetEnvironmentVariable("EXPECTED_TARGET_FRAMEWORK"); - if (!shouldThrowException && exception != null) - { - throw new Exception($"Expected the code above to not throw an exception, " + - $"because it does not fail when running in > NET462 versions of .NET Framework, " + - $"and we are running in process that was compiled as {TargetFramework}, " + - $"and is called {processName}."); - } - } - - static void ProcessInformation(IAsyncResult result) - { + Assert.Equal(expected, TargetFramework, ignoreCase: true); } } } From f7253dfe4ed582f4d732484d54f6b98678a28669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Wed, 2 Mar 2022 11:50:11 +0100 Subject: [PATCH 3/3] Update test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../MultitargetingTestHostTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs index 4846b6e0a4..f420f861fe 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs @@ -28,7 +28,7 @@ public void TestRunInATesthostThatTargetsTheirChosenNETFramework(RunnerInfo runn var arguments = PrepareArguments(assemblyPath, null, null, FrameworkArgValue, runnerInfo.InIsolationValue, resultsDirectory: tempDir.Path); // Tell the test project which target framework we are expecting it to run as. - // It has this value condionally compiled, so it can compare it. + // It has this value conditionally compiled, so it can compare it. var env = new Dictionary { ["EXPECTED_TARGET_FRAMEWORK"] = runnerInfo.TargetFramework