Skip to content

Commit a7c6ff7

Browse files
use SpecialFolder.UserProfile instead of USERPROFILE (#108559)
* use SpecialFolder.UserProfile instead of USERPROFILE * Update PackageResolverTest.cs * Update PackageResolverTest.cs * Update src/libraries/Microsoft.Extensions.DependencyModel/tests/PackageResolverTest.cs * Update PackageResolverTest.cs --------- Co-authored-by: Viktor Hofer <[email protected]>
1 parent 06205b5 commit a7c6ff7

File tree

2 files changed

+12
-34
lines changed

2 files changed

+12
-34
lines changed

src/libraries/Microsoft.Extensions.DependencyModel/src/Resolution/PackageCompilationAssemblyResolver.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public PackageCompilationAssemblyResolver()
1919
}
2020

2121
public PackageCompilationAssemblyResolver(string nugetPackageDirectory)
22-
: this(FileSystemWrapper.Default, new string[] { nugetPackageDirectory })
22+
: this(FileSystemWrapper.Default, [nugetPackageDirectory])
2323
{
2424
}
2525

@@ -46,32 +46,24 @@ internal static string[] GetDefaultProbeDirectories(IEnvironment environment)
4646

4747
if (!string.IsNullOrEmpty(listOfDirectories))
4848
{
49-
return listOfDirectories.Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
49+
return listOfDirectories.Split([Path.PathSeparator], StringSplitOptions.RemoveEmptyEntries);
5050
}
5151

5252
string? packageDirectory = environment.GetEnvironmentVariable("NUGET_PACKAGES");
5353

5454
if (!string.IsNullOrEmpty(packageDirectory))
5555
{
56-
return new string[] { packageDirectory };
56+
return [packageDirectory];
5757
}
5858

59-
string? basePath;
60-
if (environment.IsWindows())
61-
{
62-
basePath = environment.GetEnvironmentVariable("USERPROFILE");
63-
}
64-
else
65-
{
66-
basePath = environment.GetEnvironmentVariable("HOME");
67-
}
59+
string basePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
6860

6961
if (string.IsNullOrEmpty(basePath))
7062
{
71-
return new string[] { string.Empty };
63+
return [string.Empty];
7264
}
7365

74-
return new string[] { Path.Combine(basePath, ".nuget", "packages") };
66+
return [Path.Combine(basePath, ".nuget", "packages")];
7567
}
7668

7769
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string>? assemblies)
@@ -102,7 +94,7 @@ public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string>? as
10294

10395
private static bool TryResolveFromPackagePath(IFileSystem fileSystem, CompilationLibrary library, string basePath, [MaybeNullWhen(false)] out IEnumerable<string> results)
10496
{
105-
var paths = new List<string>();
97+
List<string> paths = [];
10698

10799
foreach (string assembly in library.Assemblies)
108100
{

src/libraries/Microsoft.Extensions.DependencyModel/tests/PackageResolverTest.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using FluentAssertions;
8+
using Microsoft.DotNet.XUnitExtensions;
79
using Microsoft.Extensions.DependencyModel.Resolution;
810
using Xunit;
911
using F = Microsoft.Extensions.DependencyModel.Tests.TestLibraryFactory;
@@ -26,31 +28,15 @@ public void ShouldUseEnvironmentVariableToGetDefaultLocation()
2628
result.Should().Contain(PackagesPath);
2729
}
2830

29-
30-
[Fact]
31-
public void ShouldUseNugetUnderUserProfileOnWindows()
32-
{
33-
var environment = EnvironmentMockBuilder.Create()
34-
.SetIsWindows(true)
35-
.AddVariable("USERPROFILE", "User Profile")
36-
.AddAppContextData("PROBING_DIRECTORIES", string.Empty)
37-
.Build();
38-
39-
var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(environment);
40-
result.Should().Contain(Path.Combine("User Profile", ".nuget", "packages"));
41-
}
42-
43-
[Fact]
44-
public void ShouldUseNugetUnderHomeOnNonWindows()
31+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))] // https://github.com/dotnet/runtime/issues/21430
32+
public void ShouldUseNugetUnderUserProfile()
4533
{
4634
var environment = EnvironmentMockBuilder.Create()
47-
.SetIsWindows(false)
48-
.AddVariable("HOME", "User Home")
4935
.AddAppContextData("PROBING_DIRECTORIES", string.Empty)
5036
.Build();
5137

5238
var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(environment);
53-
result.Should().Contain(Path.Combine("User Home", ".nuget", "packages"));
39+
result.Should().Contain(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"));
5440
}
5541

5642
[Fact]

0 commit comments

Comments
 (0)