From a903560c3a32284951f211d72a92ab57e77eaecc Mon Sep 17 00:00:00 2001 From: Jan Krivanek Date: Thu, 18 Aug 2022 09:13:44 +0200 Subject: [PATCH] More info in MsBuildResolver tests --- .../GivenAnMSBuildSdkResolver.cs | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index 9a756815255b..d867e84e8e7a 100644 --- a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -16,6 +16,7 @@ using Microsoft.NET.TestFramework; using System.Linq; using Microsoft.DotNet.DotNetSdkResolver; +using System.Diagnostics; [assembly: CollectionBehavior(DisableTestParallelization = true)] @@ -74,7 +75,7 @@ public void ItFindsTheVersionSpecifiedInGlobalJson() new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be("99.99.98"); @@ -99,7 +100,7 @@ public void ItUsesProjectDirectoryIfSolutionFilePathIsNullOrWhitespace(string so new MockContext { ProjectFileDirectory = environment.TestDirectory, SolutionFilePath = solutionFilePath }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().StartWith(environment.TestDirectory.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be(version); @@ -126,7 +127,7 @@ public void ItUsesCurrentDirectoryIfSolutionFilePathAndProjectFilePathIsNullOrWh new MockContext { ProjectFilePath = projectFilePath, SolutionFilePath = solutionFilePath }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().StartWith(environment.TestDirectory.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be(version); @@ -207,7 +208,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Should().BeNull(); @@ -281,7 +282,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Count.Should().Be(2); @@ -352,7 +353,7 @@ public void ItReturnsTheVersionIfItIsEqualToTheMinVersionAndTheVSDefinedMinVersi new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be("99.99.99"); @@ -374,7 +375,7 @@ public void ItReturnsTheVersionIfItIsHigherThanTheMinVersionAndTheVSDefinedMinVe new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be("999.99.99"); @@ -401,7 +402,7 @@ public void ItDisallowsPreviewsBasedOnDefault(bool disallowPreviewsByDefault) new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be(disallowPreviewsByDefault ? "10.0.0" : "11.0.0-preview1"); @@ -429,7 +430,7 @@ public void ItDisallowsPreviewsBasedOnFile(bool disallowPreviews) new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be(disallowPreviews ? "10.0.0" : "11.0.0-preview1"); @@ -503,7 +504,7 @@ public void ItAllowsPreviewWhenGlobalJsonHasPreviewIrrespectiveOfSetting() new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(preview.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be("11.0.0-preview1"); @@ -532,7 +533,7 @@ public void ItRespectsAmbientVSSettings() new MockContext { ProjectFileDirectory = environment.TestDirectory }, new MockFactory()); - result.Success.Should().BeTrue(string.Join(Environment.NewLine, result.Errors)); + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors)}. Current Process Path: {Process.GetCurrentProcess().MainModule.FileName}. Mocked Path: {environment.PathEnvironmentVariable}"); result.Path.Should().Be(expected.FullName); result.AdditionalPaths.Should().BeNull(); result.Version.Should().Be(vsSettings.DisallowPrerelease() ? "10.0.0" : "11.0.0-preview1"); @@ -582,8 +583,7 @@ public TestEnvironment(TestAssetsManager testAssets, string identifier = "", [Ca DeleteMinimumVSDefinedSDKVersionFile(); - string dotnetRootUnderTest = TestContext.Current.ToolsetUnderTest.DotNetRoot; - PathEnvironmentVariable = dotnetRootUnderTest; + PathEnvironmentVariable = string.Empty; } public SdkResolver CreateResolver(bool useAmbientSettings = false) @@ -593,11 +593,14 @@ public SdkResolver CreateResolver(bool useAmbientSettings = false) ? VSSettings.Ambient : new VSSettings(VSSettingsFile?.FullName, DisallowPrereleaseByDefault)); - public DirectoryInfo GetSdkDirectory(ProgramFiles programFiles, string sdkName, string sdkVersion) + public string GetDotnetExeDirectory(ProgramFiles programFiles) => Path.Combine( + TestDirectory.FullName, + GetProgramFilesDirectory(programFiles).FullName, + "dotnet"); + + public DirectoryInfo GetSdkDirectory(string dotnetExeDirectory, string sdkName, string sdkVersion) => new DirectoryInfo(Path.Combine( - TestDirectory.FullName, - GetProgramFilesDirectory(programFiles).FullName, - "dotnet", + dotnetExeDirectory, "sdk", sdkVersion, "Sdks", @@ -613,7 +616,14 @@ public DirectoryInfo CreateSdkDirectory( string sdkVersion, Version minimumMSBuildVersion = null) { - var dir = GetSdkDirectory(programFiles, sdkName, sdkVersion); + string dotnetExeDirectory = GetDotnetExeDirectory(programFiles); + // in order for EnvironemntProvider to pick this dir - it need to be in path and dotnet executable needs to be located in it + PathEnvironmentVariable = $"{dotnetExeDirectory}{Path.PathSeparator}{PathEnvironmentVariable}"; + new FileInfo(Path.Combine( + dotnetExeDirectory, + Microsoft.DotNet.NativeWrapper.Constants.DotNet + Microsoft.DotNet.NativeWrapper.Constants.ExeSuffix) + ).Create(); + var dir = GetSdkDirectory(dotnetExeDirectory, sdkName, sdkVersion); dir.Create(); if (minimumMSBuildVersion != null)