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

Fix multi tfm project tests #3425

Merged
merged 3 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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 condionally compiled, so it can compare it.
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}