Skip to content

Commit

Permalink
Update ToolsetInfo to handle new layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Dec 2, 2019
1 parent a964b8b commit 8f4b61e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 104 deletions.
22 changes: 1 addition & 21 deletions src/Tests/Common/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static int Main(string[] args)

private static int ShowSdkInfo()
{
var log = new OutputLogger();
var log = new StringTestLogger();
var command = new DotnetCommand(log, "--info");
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkinfo"));

Expand All @@ -78,24 +78,4 @@ private static int ShowSdkInfo()
static partial void AfterTestRun();

static partial void ShowAdditionalHelp();

private class OutputLogger : ITestOutputHelper
{
StringBuilder _stringBuilder = new StringBuilder();

public void WriteLine(string message)
{
_stringBuilder.AppendLine(message);
}

public void WriteLine(string format, params object[] args)
{
_stringBuilder.AppendLine(string.Format(format, args));
}

public override string ToString()
{
return _stringBuilder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void It_resolves_conflicts(bool isSdk, bool usePackagesConfig)

target.Add(new XElement(ns + "FindUnderPath",
new XAttribute("Files", "@(_ConflictPackageFiles)"),
new XAttribute("Path", TestContext.Current.ToolsetUnderTest.GetMicrosoftNETBuildExtensionsPath(Log)),
new XAttribute("Path", TestContext.Current.ToolsetUnderTest.GetMicrosoftNETBuildExtensionsPath()),
new XElement(ns + "Output",
new XAttribute("TaskParameter", "InPath"),
new XAttribute("ItemName", "_ConflictsInSupportLibs"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void It_uses_hintpath_when_replacing_simple_name_references(bool useFacad
string correctHttpReference;
if (useFacades)
{
string microsoftNETBuildExtensionsPath = TestContext.Current.ToolsetUnderTest.GetMicrosoftNETBuildExtensionsPath(Log);
string microsoftNETBuildExtensionsPath = TestContext.Current.ToolsetUnderTest.GetMicrosoftNETBuildExtensionsPath();
correctHttpReference = Path.Combine(microsoftNETBuildExtensionsPath, @"net461\lib\System.Net.Http.dll");
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,8 @@ private CommandResult GenerateDepsAndRunTool(TestProject toolProject, [CallerMem
var args = new List<string>();


string currentToolsetSdksPath;
if (TestContext.Current.ToolsetUnderTest.SdksPath == null)
{
// We don't have an overridden path to the SDKs, so figure out which version of the SDK we're using and
// calculate the path based on that
string dotnetSdkDir = TestContext.Current.ToolsetUnderTest.GetDotnetSdkDir(Log);
currentToolsetSdksPath = Path.Combine(dotnetSdkDir, "Sdks");
}
else
{
currentToolsetSdksPath = TestContext.Current.ToolsetUnderTest.SdksPath;
}
string currentToolsetSdksPath = TestContext.Current.ToolsetUnderTest.SdksPath;

string generateDepsProjectDirectoryPath = Path.Combine(currentToolsetSdksPath, "Microsoft.NET.Sdk", "targets", "GenerateDeps");
string generateDepsProjectFileName = "GenerateDeps.proj";

Expand Down
27 changes: 27 additions & 0 deletions src/Tests/Microsoft.NET.TestFramework/StringTestLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit.Abstractions;

namespace Microsoft.NET.TestFramework
{
public class StringTestLogger : ITestOutputHelper
{
StringBuilder _stringBuilder = new StringBuilder();

public void WriteLine(string message)
{
_stringBuilder.AppendLine(message);
}

public void WriteLine(string format, params object[] args)
{
_stringBuilder.AppendLine(string.Format(format, args));
}

public override string ToString()
{
return _stringBuilder.ToString();
}
}
}
156 changes: 87 additions & 69 deletions src/Tests/Microsoft.NET.TestFramework/ToolsetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,93 +17,109 @@ namespace Microsoft.NET.TestFramework
{
public class ToolsetInfo
{
public string DotNetHostPath { get; set; }
public string DotNetRoot { get; }
public string DotNetHostPath { get; }

public string SdksPath { get; set; }

public string GetMicrosoftNETBuildExtensionsPath(ITestOutputHelper log)
private string _sdkVersion;
public string SdkVersion
{
if (!string.IsNullOrEmpty(SdksPath))
{
var buildExtensionsSdkPath = Path.Combine(SdksPath, "Microsoft.NET.Build.Extensions");
return Path.Combine(buildExtensionsSdkPath, "msbuildExtensions", "Microsoft", "Microsoft.NET.Build.Extensions");
}
else
get
{
var msbuildBinPath = Path.GetDirectoryName(FullFrameworkMSBuildPath);
if (ShouldUseFullFrameworkMSBuild)
{
var msbuildRoot = Directory.GetParent(msbuildBinPath).Parent.FullName;
return Path.Combine(msbuildRoot, @"Microsoft\Microsoft.NET.Build.Extensions");
}
else
if (_sdkVersion == null)
{
var dotnetSdkDir = GetDotnetSdkDir(log);
return Path.Combine(dotnetSdkDir, @"Microsoft\Microsoft.NET.Build.Extensions");
// Initialize SdkVersion lazily, as we call `dotnet --version` to get it, so we need to wait
// for the TestContext to finish being initialize
InitSdkVersion();
}
return _sdkVersion;
}
}

public string SdksPath { get; private set; }

public string MicrosoftNETBuildExtensionsPathOverride { get; set; }

public bool ShouldUseFullFrameworkMSBuild => !string.IsNullOrEmpty(FullFrameworkMSBuildPath);

public string FullFrameworkMSBuildPath { get; set; }

public string GetDotnetSdkDir(ITestOutputHelper log)
public ToolsetInfo(string dotNetRoot)
{
DotNetRoot = dotNetRoot;

DotNetHostPath = Path.Combine(dotNetRoot, $"dotnet{Constants.ExeSuffix}");
}

private void InitSdkVersion()
{
var command = new DotnetCommand(log, "--version");
var logger = new StringTestLogger();
var command = new DotnetCommand(logger, "--version");
var testDirectory = TestDirectory.Create(Path.Combine(TestContext.Current.TestExecutionDirectory, "sdkversion"));

command.WorkingDirectory = testDirectory.Path;

var result = command.Execute();

result.Should().Pass();
if (result.ExitCode != 0)
{
throw new Exception("Failed to get dotnet version" + Environment.NewLine + logger.ToString());
}

var sdkVersion = result.StdOut.Trim();
string dotnetDir = Path.GetDirectoryName(TestContext.Current.ToolsetUnderTest.DotNetHostPath);
return Path.Combine(dotnetDir, "sdk", sdkVersion);
_sdkVersion = result.StdOut.Trim();

SdksPath = Path.Combine(DotNetRoot, "sdk", _sdkVersion, "Sdks");
}
public void AddTestEnvironmentVariables(SdkCommandSpec command)

public string GetMicrosoftNETBuildExtensionsPath()
{
if (SdksPath != null)
if (!string.IsNullOrEmpty(MicrosoftNETBuildExtensionsPathOverride))
{
command.Environment["MSBuildSDKsPath"] = SdksPath;
command.Environment["DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR"] = SdksPath;
return MicrosoftNETBuildExtensionsPathOverride;
}
else
{
if (ShouldUseFullFrameworkMSBuild)
{
var msbuildBinPath = Path.GetDirectoryName(FullFrameworkMSBuildPath);
var msbuildRoot = Directory.GetParent(msbuildBinPath).Parent.FullName;
return Path.Combine(msbuildRoot, @"Microsoft\Microsoft.NET.Build.Extensions");
}
else
{
return Path.Combine(DotNetRoot, "sdk", SdkVersion, @"Microsoft\Microsoft.NET.Build.Extensions");
}
}
}

// OK to pass in null as the logger here because SdksPath is set so it won't go down the code path
// that uses the logger
var microsoftNETBuildExtensionsPath = GetMicrosoftNETBuildExtensionsPath(null);
command.Environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath, "Microsoft.NET.Build.Extensions.targets");
public void AddTestEnvironmentVariables(SdkCommandSpec command)
{
if (ShouldUseFullFrameworkMSBuild)
{
string sdksPath = Path.Combine(DotNetRoot, "sdk", SdkVersion, "Sdks");
command.Environment["DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR"] = sdksPath;

if (UsingFullMSBuildWithoutExtensionsTargets())
if (!string.IsNullOrEmpty(MicrosoftNETBuildExtensionsPathOverride))
{
command.Environment["CustomAfterMicrosoftCommonTargets"] = Path.Combine(SdksPath, "Microsoft.NET.Build.Extensions",
"msbuildExtensions-ver", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets");
var microsoftNETBuildExtensionsPath = GetMicrosoftNETBuildExtensionsPath();
command.Environment["MicrosoftNETBuildExtensionsTargets"] = Path.Combine(microsoftNETBuildExtensionsPath, "Microsoft.NET.Build.Extensions.targets");

if (UsingFullMSBuildWithoutExtensionsTargets())
{
command.Environment["CustomAfterMicrosoftCommonTargets"] = Path.Combine(sdksPath, "Microsoft.NET.Build.Extensions",
"msbuildExtensions-ver", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets");
}
}

}

string dotnetRoot = Path.GetDirectoryName(DotNetHostPath);
if (Environment.Is64BitProcess)
{
command.Environment.Add("DOTNET_ROOT", dotnetRoot);
command.Environment.Add("DOTNET_ROOT", DotNetRoot);
}
else
{
command.Environment.Add("DOTNET_ROOT(x86)", dotnetRoot);
command.Environment.Add("DOTNET_ROOT(x86)", DotNetRoot);
}

DirectoryInfo latestSdk = GetLatestSdk(dotnetRoot);
command.Environment["NETCoreSdkBundledVersionsProps"] = Path.Combine(latestSdk.FullName, "Microsoft.NETCoreSdk.BundledVersions.props");
}

private static DirectoryInfo GetLatestSdk(string dotnetRoot)
{
return new DirectoryInfo(Path.Combine(dotnetRoot, "sdk"))
.EnumerateDirectories()
.Where(d => NuGetVersion.TryParse(d.Name, out _))
.OrderByDescending(d => NuGetVersion.Parse(d.Name))
.First();
}

public SdkCommandSpec CreateCommandForTarget(string target, IEnumerable<string> args)
Expand Down Expand Up @@ -145,36 +161,31 @@ private SdkCommandSpec CreateCommand(params string[] args)
}
public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, string configuration, TestCommandLine commandLine)
{
var ret = new ToolsetInfo();

repoRoot = commandLine.SDKRepoPath ?? repoRoot;
configuration = commandLine.SDKRepoConfiguration ?? configuration;

string dotnetInstallDirFromEnvironment = Environment.GetEnvironmentVariable("DOTNET_INSTALL_DIR");

string dotnetRoot;

if (!string.IsNullOrEmpty(commandLine.DotnetHostPath))
{
ret.DotNetHostPath = commandLine.DotnetHostPath;
dotnetRoot = Path.GetDirectoryName(commandLine.DotnetHostPath);
}
else if (!string.IsNullOrEmpty(dotnetInstallDirFromEnvironment))
else if (repoRoot != null)
{
ret.DotNetHostPath = Path.Combine(dotnetInstallDirFromEnvironment, $"dotnet{Constants.ExeSuffix}");
dotnetRoot = Path.Combine(repoArtifactsDir, "bin", "redist", configuration, "dotnet");
}
else if (repoRoot != null)
else if (!string.IsNullOrEmpty(dotnetInstallDirFromEnvironment))
{
ret.DotNetHostPath = Path.Combine(repoArtifactsDir, "bin", "redist", configuration, "dotnet", $"dotnet{Constants.ExeSuffix}");
dotnetRoot = dotnetInstallDirFromEnvironment;
}
else
{
ret.DotNetHostPath = ResolveCommand("dotnet");
dotnetRoot = Path.GetDirectoryName(ResolveCommand("dotnet"));
}

if (repoRoot != null)
{
string dotnetSdkPath = Path.Combine(Path.GetDirectoryName(ret.DotNetHostPath), "sdk");
string sdkVersionPath = Directory.GetDirectories(dotnetSdkPath).Single();
ret.SdksPath = Path.Combine(sdkVersionPath, "Sdks");
}
var ret = new ToolsetInfo(dotnetRoot);

if (!string.IsNullOrEmpty(commandLine.FullFrameworkMSBuildPath))
{
Expand All @@ -185,6 +196,14 @@ public static ToolsetInfo Create(string repoRoot, string repoArtifactsDir, strin
ret.FullFrameworkMSBuildPath = ResolveCommand("MSBuild");
}

if (repoRoot != null && ret.ShouldUseFullFrameworkMSBuild)
{
// Find path to Microsoft.NET.Build.Extensions for full framework
string sdksPath = Path.Combine(repoArtifactsDir, "bin", configuration, "Sdks");
var buildExtensionsSdkPath = Path.Combine(sdksPath, "Microsoft.NET.Build.Extensions");
ret.MicrosoftNETBuildExtensionsPathOverride = Path.Combine(buildExtensionsSdkPath, "msbuildExtensions", "Microsoft", "Microsoft.NET.Build.Extensions");
}

return ret;
}

Expand Down Expand Up @@ -242,14 +261,13 @@ private static string FindFileInTree(string relativePath, string startPath, bool
}
}

private static bool UsingFullMSBuildWithoutExtensionsTargets()
private bool UsingFullMSBuildWithoutExtensionsTargets()
{
string fullMSBuildPath = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILD_PATH");
if (string.IsNullOrEmpty(fullMSBuildPath))
if (!ShouldUseFullFrameworkMSBuild)
{
return false;
}
string fullMSBuildDirectory = Path.GetDirectoryName(fullMSBuildPath);
string fullMSBuildDirectory = Path.GetDirectoryName(FullFrameworkMSBuildPath);
string extensionsImportAfterPath = Path.Combine(fullMSBuildDirectory, "..", "Microsoft.Common.targets", "ImportAfter", "Microsoft.NET.Build.Extensions.targets");
return !File.Exists(extensionsImportAfterPath);
}
Expand Down

0 comments on commit 8f4b61e

Please sign in to comment.