|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -using System; |
6 | 5 | using System.IO; |
7 | 6 | using System.Runtime.InteropServices; |
| 7 | +using Microsoft.CodeAnalysis.CommandLine; |
8 | 8 | using Microsoft.CodeAnalysis.Test.Utilities; |
9 | 9 | using Roslyn.Test.Utilities; |
10 | 10 | using Roslyn.Utilities; |
|
13 | 13 |
|
14 | 14 | namespace Microsoft.CodeAnalysis.BuildTasks.UnitTests; |
15 | 15 |
|
16 | | -public sealed class RuntimeHostInfoTests(ITestOutputHelper output) |
| 16 | +public sealed class RuntimeHostInfoTests(ITestOutputHelper output) : TestBase |
17 | 17 | { |
18 | 18 | private readonly ITestOutputHelper _output = output; |
19 | 19 |
|
| 20 | + /// <summary> |
| 21 | + /// Normalizes a path by resolving any symbolic links or subst drives (on Windows). |
| 22 | + /// This ensures paths can be compared even when one is a subst drive (e.g., T:\ -> D:\a\_work\1\s\artifacts\tmp\Debug). |
| 23 | + /// </summary> |
| 24 | + private static string NormalizePath(string path) |
| 25 | + { |
| 26 | + // Our test infra uses `subst` which the .NET Core's implementation of `ResolveLinkTarget` wouldn't resolve, |
| 27 | + // hence we always use our Win32 polyfill on Windows to ensure paths are fully normalized and can be compared in tests. |
| 28 | + var resolvedPath = PlatformInformation.IsWindows |
| 29 | + ? NativeMethods.ResolveLinkTargetWin32(path, returnFinalTarget: true) |
| 30 | + : File.ResolveLinkTarget(path, returnFinalTarget: true); |
| 31 | + if (resolvedPath != null) |
| 32 | + { |
| 33 | + return resolvedPath.FullName; |
| 34 | + } |
| 35 | + |
| 36 | + return path; |
| 37 | + } |
| 38 | + |
20 | 39 | [Fact, WorkItem("https://github.com/dotnet/msbuild/issues/12669")] |
21 | 40 | public void DotNetInPath() |
22 | 41 | { |
23 | | - var previousPath = Environment.GetEnvironmentVariable("PATH"); |
24 | | - try |
25 | | - { |
26 | | - using var tempRoot = new TempRoot(); |
27 | | - var testDir = tempRoot.CreateDirectory(); |
28 | | - var globalDotNetDir = testDir.CreateDirectory("global-dotnet"); |
29 | | - var globalDotNetExe = globalDotNetDir.CreateFile($"dotnet{PlatformInformation.ExeExtension}"); |
30 | | - Environment.SetEnvironmentVariable("PATH", globalDotNetDir.Path); |
| 42 | + using var tempRoot = new TempRoot(); |
| 43 | + var testDir = tempRoot.CreateDirectory(); |
| 44 | + var globalDotNetDir = testDir.CreateDirectory("global-dotnet"); |
| 45 | + var globalDotNetExe = globalDotNetDir.CreateFile($"dotnet{PlatformInformation.ExeExtension}"); |
31 | 46 |
|
32 | | - Assert.Equal(globalDotNetDir.Path, RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
33 | | - } |
34 | | - finally |
35 | | - { |
36 | | - Environment.SetEnvironmentVariable("PATH", previousPath); |
37 | | - } |
| 47 | + var result = ApplyEnvironmentVariables( |
| 48 | + [ |
| 49 | + new("PATH", globalDotNetDir.Path), |
| 50 | + new(RuntimeHostInfo.DotNetHostPathEnvironmentName, ""), |
| 51 | + new(RuntimeHostInfo.DotNetExperimentalHostPathEnvironmentName, ""), |
| 52 | + ], |
| 53 | + () => RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
| 54 | + |
| 55 | + Assert.NotNull(result); |
| 56 | + AssertEx.Equal(NormalizePath(globalDotNetDir.Path), NormalizePath(result)); |
38 | 57 | } |
39 | 58 |
|
40 | 59 | [Fact, WorkItem("https://github.com/dotnet/msbuild/issues/12669")] |
41 | 60 | public void DotNetInPath_None() |
42 | 61 | { |
43 | | - var previousPath = Environment.GetEnvironmentVariable("PATH"); |
44 | | - try |
45 | | - { |
46 | | - Environment.SetEnvironmentVariable("PATH", ""); |
| 62 | + var result = ApplyEnvironmentVariables( |
| 63 | + [ |
| 64 | + new("PATH", ""), |
| 65 | + new(RuntimeHostInfo.DotNetHostPathEnvironmentName, ""), |
| 66 | + new(RuntimeHostInfo.DotNetExperimentalHostPathEnvironmentName, ""), |
| 67 | + ], |
| 68 | + () => RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
47 | 69 |
|
48 | | - Assert.Null(RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
49 | | - } |
50 | | - finally |
51 | | - { |
52 | | - Environment.SetEnvironmentVariable("PATH", previousPath); |
53 | | - } |
| 70 | + Assert.Null(result); |
54 | 71 | } |
55 | 72 |
|
56 | 73 | [Fact, WorkItem("https://github.com/dotnet/msbuild/issues/12669")] |
57 | 74 | public void DotNetInPath_Symlinked() |
58 | 75 | { |
59 | | - var previousPath = Environment.GetEnvironmentVariable("PATH"); |
60 | | - try |
61 | | - { |
62 | | - using var tempRoot = new TempRoot(); |
63 | | - var testDir = tempRoot.CreateDirectory(); |
64 | | - var globalDotNetDir = testDir.CreateDirectory("global-dotnet"); |
65 | | - var globalDotNetExe = globalDotNetDir.CreateFile($"dotnet{PlatformInformation.ExeExtension}"); |
66 | | - var binDir = testDir.CreateDirectory("bin"); |
67 | | - var symlinkPath = Path.Combine(binDir.Path, $"dotnet{PlatformInformation.ExeExtension}"); |
| 76 | + using var tempRoot = new TempRoot(); |
| 77 | + var testDir = tempRoot.CreateDirectory(); |
| 78 | + var globalDotNetDir = testDir.CreateDirectory("global-dotnet"); |
| 79 | + var globalDotNetExe = globalDotNetDir.CreateFile($"dotnet{PlatformInformation.ExeExtension}"); |
| 80 | + var binDir = testDir.CreateDirectory("bin"); |
| 81 | + var symlinkPath = Path.Combine(binDir.Path, $"dotnet{PlatformInformation.ExeExtension}"); |
68 | 82 |
|
69 | | - // Create symlink from binDir to the actual dotnet executable |
70 | | - File.CreateSymbolicLink(path: symlinkPath, pathToTarget: globalDotNetExe.Path); |
| 83 | + // Create symlink from binDir to the actual dotnet executable |
| 84 | + File.CreateSymbolicLink(path: symlinkPath, pathToTarget: globalDotNetExe.Path); |
71 | 85 |
|
72 | | - Environment.SetEnvironmentVariable("PATH", binDir.Path); |
| 86 | + var result = ApplyEnvironmentVariables( |
| 87 | + [ |
| 88 | + new("PATH", binDir.Path), |
| 89 | + new(RuntimeHostInfo.DotNetHostPathEnvironmentName, ""), |
| 90 | + new(RuntimeHostInfo.DotNetExperimentalHostPathEnvironmentName, ""), |
| 91 | + ], |
| 92 | + () => RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
73 | 93 |
|
74 | | - Assert.Equal(globalDotNetDir.Path, RuntimeHostInfo.GetToolDotNetRoot(_output.WriteLine)); |
75 | | - } |
76 | | - finally |
77 | | - { |
78 | | - Environment.SetEnvironmentVariable("PATH", previousPath); |
79 | | - } |
| 94 | + Assert.NotNull(result); |
| 95 | + AssertEx.Equal(NormalizePath(globalDotNetDir.Path), NormalizePath(result)); |
80 | 96 | } |
81 | 97 | } |
82 | 98 |
|
83 | 99 | #if !NET |
84 | | -file static class NativeMethods |
| 100 | +file static class TestNativeMethods |
85 | 101 | { |
86 | 102 | extension(File) |
87 | 103 | { |
|
0 commit comments