Skip to content

Commit

Permalink
Add tests for architecture switch feature (#3253)
Browse files Browse the repository at this point in the history
Add tests for architecture switch feature
  • Loading branch information
MarcoRossignoli authored Jan 14, 2022
1 parent 582397c commit 909b176
Show file tree
Hide file tree
Showing 11 changed files with 649 additions and 9 deletions.
2 changes: 1 addition & 1 deletion scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VSSdkBuildToolsVersion>17.0.1600</VSSdkBuildToolsVersion>
Expand Down
4 changes: 3 additions & 1 deletion scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ function Install-DotNetCli
$dotnetInstallPath = Join-Path $env:TP_TOOLS_DIR "dotnet"
New-Item -ItemType directory -Path $dotnetInstallPath -Force | Out-Null
& $dotnetInstallScript -Channel 6.0 -InstallDir $dotnetInstallPath -Version $env:DOTNET_CLI_VERSION


& $dotnetInstallScript -Channel 6.0 -InstallDir "${dotnetInstallPath}_x86" -Version $env:DOTNET_CLI_VERSION -Architecture x86 -NoPath

& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '2.1.30' -Channel '2.1' -Architecture x64 -NoPath
$env:DOTNET_ROOT= $dotnetInstallPath

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !NET451

namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.TestPlatform.TestUtilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;

[TestClass]
[TestCategory("Windows-Review")]
public class DotnetArchitectureSwitchTestsWindowsOnly : AcceptanceTestBase
{
[TestMethod]
[DataRow("X64", "X86")]
[DataRow("X86", "X64")]
public void Use_EnvironmentVariables(string architectureFrom, string architectureTo)
{
using (Workspace workSpace = new Workspace(GetResultsDirectory()))
{
string dotnetPath = GetDownloadedDotnetMuxerFromTools(architectureFrom);
string dotnetPathTo = GetDownloadedDotnetMuxerFromTools(architectureTo);
var vstestConsolePath = GetDotnetRunnerPath();
var dotnetRunnerPath = workSpace.CreateDirectory("dotnetrunner");
workSpace.CopyAll(new DirectoryInfo(Path.GetDirectoryName(vstestConsolePath)), dotnetRunnerPath);

// Patch the runner
string sdkVersion = GetLatestSdkVersion(dotnetPath);
string runtimeConfigFile = Path.Combine(dotnetRunnerPath.FullName, "vstest.console.runtimeconfig.json");
JObject patchRuntimeConfig = JObject.Parse(File.ReadAllText(runtimeConfigFile));
patchRuntimeConfig["runtimeOptions"]["framework"]["version"] = sdkVersion;
File.WriteAllText(runtimeConfigFile, patchRuntimeConfig.ToString());

var environmentVariables = new Dictionary<string, string>
{
["DOTNET_MULTILEVEL_LOOKUP"] = "0",
[$"DOTNET_ROOT_{architectureTo}"] = Path.GetDirectoryName(dotnetPathTo),
["ExpectedArchitecture"] = architectureTo
};
this.ExecuteApplication(dotnetPath, "new mstest", out string stdOut, out string stdError, out int exitCode, environmentVariables, workSpace.Path);

// Patch test file
File.WriteAllText(Path.Combine(workSpace.Path, "UnitTest1.cs"),
@"
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace cfebbc5339cf4c22854e79824e938c74;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(Environment.GetEnvironmentVariable(""ExpectedArchitecture""), System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString());
}
}");

this.ExecuteApplication(dotnetPath, $"test -p:VsTestConsolePath=\"{Path.Combine(dotnetRunnerPath.FullName, Path.GetFileName(vstestConsolePath))}\" --arch {architectureTo.ToLower()} --diag:log.txt", out stdOut, out stdError, out exitCode, environmentVariables, workSpace.Path);
Assert.AreEqual(0, exitCode, stdOut);

environmentVariables = new Dictionary<string, string>
{
["DOTNET_MULTILEVEL_LOOKUP"] = "0",
["DOTNET_ROOT"] = Path.GetDirectoryName(dotnetPathTo),
["ExpectedArchitecture"] = architectureTo
};
this.ExecuteApplication(dotnetPath, $"test -p:VsTestConsolePath=\"{Path.Combine(dotnetRunnerPath.FullName, Path.GetFileName(vstestConsolePath))}\" --arch {architectureTo.ToLower()} --diag:log.txt", out stdOut, out stdError, out exitCode, environmentVariables, workSpace.Path);
Assert.AreEqual(0, exitCode, stdOut);

environmentVariables = new Dictionary<string, string>
{
["DOTNET_MULTILEVEL_LOOKUP"] = "0",
[$"DOTNET_ROOT_{architectureTo}"] = Path.GetDirectoryName(dotnetPathTo),
["DOTNET_ROOT"] = "WE SHOULD PICK THE ABOVE ONE BEFORE FALLBACK TO DOTNET_ROOT",
["ExpectedArchitecture"] = architectureTo
};
this.ExecuteApplication(dotnetPath, $"test -p:VsTestConsolePath=\"{Path.Combine(dotnetRunnerPath.FullName, Path.GetFileName(vstestConsolePath))}\" --arch {architectureTo.ToLower()} --diag:log.txt", out stdOut, out stdError, out exitCode, environmentVariables, workSpace.Path);
Assert.AreEqual(0, exitCode, stdOut);
}
}

private string GetLatestSdkVersion(string dotnetPath)
=> Path.GetFileName(Directory.GetDirectories(Path.Combine(Path.GetDirectoryName(dotnetPath), @"shared/Microsoft.NETCore.App")).OrderByDescending(x => x).First());
}
}

#endif
Loading

0 comments on commit 909b176

Please sign in to comment.