Skip to content

Commit

Permalink
Fix multi tfm project tests (#3425)
Browse files Browse the repository at this point in the history
Co-authored-by: Amaury Levé <[email protected]>
  • Loading branch information
nohwnd and Evangelink authored Mar 2, 2022
1 parent ed5cd10 commit d3ae38b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,14 +19,22 @@ 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();

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 conditionally compiled, so it can compare it.
var env = new Dictionary<string, string>
{
["EXPECTED_TARGET_FRAMEWORK"] = runnerInfo.TargetFramework
};

InvokeVsTest(arguments, env);

ValidateSummaryStatus(passedTestsCount: 1, failedTestsCount: 0, 0);
}
Expand Down
37 changes: 4 additions & 33 deletions test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,43 +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()
{
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 expected = Environment.GetEnvironmentVariable("EXPECTED_TARGET_FRAMEWORK");

switch (TargetFramework)
{
case "NET451":
case "NET452":
case "NET46":
case "NET461":
case "NET462":
Assert.NotNull(exception);
break;
default:
Assert.Null(exception);
break;
}
}

static void ProcessInformation(IAsyncResult result)
{
Assert.Equal(expected, TargetFramework, ignoreCase: true);
}
}
}

0 comments on commit d3ae38b

Please sign in to comment.