Skip to content

Commit 4f81f71

Browse files
committed
Address comment and try to fix test in CI
1 parent 43e6e7b commit 4f81f71

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ private static void UpdateProcessEnvPath(string oldUserPath, string oldSystemPat
332332
string newSystemEnvPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
333333
string procEnvPath = Environment.GetEnvironmentVariable("Path");
334334

335-
ReadOnlySpan<char> userPathChange = GetAddedPartOfString(oldUserPath, newUserEnvPath).TrimEnd(';');
336-
ReadOnlySpan<char> systemPathChange = GetAddedPartOfString(oldSystemPath, newSystemEnvPath).TrimEnd(';');
335+
ReadOnlySpan<char> userPathChange = GetAddedPartOfString(oldUserPath, newUserEnvPath).Trim(';');
336+
ReadOnlySpan<char> systemPathChange = GetAddedPartOfString(oldSystemPath, newSystemEnvPath).Trim(';');
337337

338338
// Add 2 to account for the path separators we may need to add.
339339
int maxLength = procEnvPath.Length + userPathChange.Length + systemPathChange.Length + 2;
@@ -361,12 +361,6 @@ void CreateNewProcEnvPath(ReadOnlySpan<char> newChange)
361361
newPath ??= new StringBuilder(procEnvPath, capacity: maxLength);
362362

363363
if (newPath.Length is 0 || newPath[^1] is ';')
364-
{
365-
newPath.Append(newChange[0] is ';'
366-
? newChange.Slice(1)
367-
: newChange);
368-
}
369-
else if (newChange[0] is ';')
370364
{
371365
newPath.Append(newChange);
372366
}

test/powershell/Language/Scripting/NativeExecution/NativeCommandPathUpdate.Tests.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ function UpdatePackageManager {
7070
[string] $Name
7171
)
7272

73-
try {
74-
$regKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('Software\Microsoft\Command Processor\KnownPackageManagers', $true)
73+
$regKeyPath = 'HKLM:\Software\Microsoft\Command Processor\KnownPackageManagers'
74+
$keyExists = Test-Path -Path $regKeyPath
75+
if (-not $keyExists) {
76+
Write-Host -ForegroundColor Cyan "The registry key 'KnownPackageManagers' doesn't exist."
77+
}
7578

76-
if ($Add) {
77-
$null = $regKey.CreateSubKey($Name)
78-
}
79-
elseif ($Remove) {
80-
$regKey.DeleteSubKey($Name)
81-
}
79+
$subKeyPath = "$regKeyPath\$Name"
80+
if ($Add) {
81+
$null = New-Item $subKeyPath -Force -ErrorAction Stop
8282
}
83-
finally {
84-
${regKey}?.Dispose()
83+
elseif ($Remove -and $keyExists) {
84+
Remove-Item $subKeyPath -Recurse -Force -ErrorAction Stop
8585
}
8686
}
8787

0 commit comments

Comments
 (0)