From 5c679cd32e2efa3fd8b8dcef5aef4b45f96757ed Mon Sep 17 00:00:00 2001 From: Daniel Plaisted Date: Fri, 21 Apr 2017 16:14:44 -0700 Subject: [PATCH] Test fixes for tools deps.json generation --- .../ProjectToolsCommandResolver.cs | 17 ++++++++++++++++- .../LocalizableStrings.cs | 2 ++ .../GivenAProjectToolsCommandResolver.cs | 10 +++++++++- .../GivenDotnetMSBuildBuildsProjects.cs | 11 +++++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs index d95cfead2..41688de82 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectToolsCommandResolver.cs @@ -316,6 +316,12 @@ namespace Microsoft.DotNet.Cli.Utils SingleProjectInfo toolLibrary, string toolDepsJsonGeneratorProject) { + if (string.IsNullOrEmpty(toolDepsJsonGeneratorProject) || + !File.Exists(toolDepsJsonGeneratorProject)) + { + throw new GracefulException(LocalizableStrings.DepsJsonGeneratorProjectNotSet); + } + Reporter.Verbose.WriteLine(string.Format( LocalizableStrings.GeneratingDepsJson, depsPath)); @@ -332,6 +338,9 @@ namespace Microsoft.DotNet.Cli.Utils args.Add($"/p:ToolName={toolLibrary.Name}"); args.Add($"/p:ProjectDepsFilePath={tempDepsFile}"); + var toolTargetFramework = toolLockFile.Targets.First().TargetFramework.GetShortFolderName(); + args.Add($"/p:TargetFramework={toolTargetFramework}"); + // Look for the .props file in the Microsoft.NETCore.App package, until NuGet // generates .props and .targets files for tool restores (https://github.com/NuGet/Home/issues/5037) @@ -364,7 +373,13 @@ namespace Microsoft.DotNet.Cli.Utils // will think the deps file is up-to-date and skip executing File.Delete(tempDepsFile); - var result = new MSBuildForwardingAppWithoutLogging(args).Execute(); + var msBuildExePath = _environment.GetEnvironmentVariable(Constants.MSBUILD_EXE_PATH); + + msBuildExePath = string.IsNullOrEmpty(msBuildExePath) ? + Path.Combine(AppContext.BaseDirectory, "MSBuild.dll") : + msBuildExePath; + + var result = new MSBuildForwardingAppWithoutLogging(args, msBuildExePath).Execute(); if (result != 0) { diff --git a/src/Microsoft.DotNet.Cli.Utils/LocalizableStrings.cs b/src/Microsoft.DotNet.Cli.Utils/LocalizableStrings.cs index 8a3e20014..4667dd19b 100644 --- a/src/Microsoft.DotNet.Cli.Utils/LocalizableStrings.cs +++ b/src/Microsoft.DotNet.Cli.Utils/LocalizableStrings.cs @@ -61,6 +61,8 @@ namespace Microsoft.DotNet.Cli.Utils public const string UnableToGenerateDepsJson = "unable to generate deps.json, it may have been already generated: {0}"; + public const string DepsJsonGeneratorProjectNotSet = "Unable to find deps.json generator project."; + public const string UnableToDeleteTemporaryDepsJson = "unable to delete temporary deps.json file: {0}"; public const string VersionForPackageCouldNotBeResolved = "Version for package `{0}` could not be resolved."; diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectToolsCommandResolver.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectToolsCommandResolver.cs index 938ad5075..a621e02bb 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectToolsCommandResolver.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectToolsCommandResolver.cs @@ -281,7 +281,8 @@ namespace Microsoft.DotNet.Tests lockFile, s_toolPackageFramework, depsJsonFile, - new SingleProjectInfo("dotnet-portable", "1.0.0", Enumerable.Empty())); + new SingleProjectInfo("dotnet-portable", "1.0.0", Enumerable.Empty()), + GetToolDepsJsonGeneratorProject()); File.ReadAllText(depsJsonFile).Should().Be("temp"); File.Delete(depsJsonFile); @@ -456,5 +457,12 @@ namespace Microsoft.DotNet.Tests return projectToolsCommandResolver; } + + private string GetToolDepsJsonGeneratorProject() + { + // When using the product, the ToolDepsJsonGeneratorProject property is used to get this path, but for testing + // we'll hard code the path inside the SDK since we don't have a project to evaluate here + return Path.Combine(new RepoDirectoriesProvider().Stage2Sdk, @"Sdks\Microsoft.NET.Sdk\build\GenerateDeps\GenerateDeps.proj"); + } } } diff --git a/test/dotnet-msbuild.Tests/GivenDotnetMSBuildBuildsProjects.cs b/test/dotnet-msbuild.Tests/GivenDotnetMSBuildBuildsProjects.cs index 41622c72f..51e7017b1 100644 --- a/test/dotnet-msbuild.Tests/GivenDotnetMSBuildBuildsProjects.cs +++ b/test/dotnet-msbuild.Tests/GivenDotnetMSBuildBuildsProjects.cs @@ -14,6 +14,8 @@ using NuGet.Protocol; using Xunit; using Xunit.Abstractions; using MSBuildCommand = Microsoft.DotNet.Tools.Test.Utilities.MSBuildCommand; +using System.Diagnostics; +using System.Threading; namespace Microsoft.DotNet.Cli.MSBuild.Tests { @@ -164,11 +166,16 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests MSBuildForwardingApp msBuildForwardingApp = new MSBuildForwardingApp(Enumerable.Empty()); - FieldInfo forwardingAppFieldInfo = msBuildForwardingApp + object forwardingAppWithoutLogging = msBuildForwardingApp + .GetType() + .GetField("_forwardingAppWithoutLogging", BindingFlags.Instance | BindingFlags.NonPublic) + ?.GetValue(msBuildForwardingApp); + + FieldInfo forwardingAppFieldInfo = forwardingAppWithoutLogging .GetType() .GetField("_forwardingApp", BindingFlags.Instance | BindingFlags.NonPublic); - ForwardingApp forwardingApp = forwardingAppFieldInfo?.GetValue(msBuildForwardingApp) as ForwardingApp; + object forwardingApp = forwardingAppFieldInfo?.GetValue(forwardingAppWithoutLogging); FieldInfo allArgsFieldinfo = forwardingApp? .GetType()