From 52a62e9ef2329ab5642bd5c9b20088d104b1fd01 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 2 Nov 2022 19:54:11 -0700 Subject: [PATCH] Fix `Switch-Process` error to include the command that is not found (#18443) --- .../engine/Modules/SwitchProcessCommand.cs | 4 ++-- .../Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 | 7 +++++++ test/xUnit/csharp/test_Feedback.cs | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs b/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs index 4700c425d..5e0cee668 100644 --- a/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs +++ b/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs @@ -46,12 +46,12 @@ namespace Microsoft.PowerShell.Commands string.Format( System.Globalization.CultureInfo.InvariantCulture, CommandBaseStrings.NativeCommandNotFound, - command + WithCommand[0] ) ), "CommandNotFound", ErrorCategory.InvalidArgument, - WithCommand + WithCommand[0] ) ); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 index 858a04fb4..21813a02a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 @@ -48,6 +48,13 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' { ($p | Get-Process).Name -eq 'sleep' } -timeout 60000 -interval 100 | Should -BeTrue } + + It 'Error is returned if target command is not found' { + $invalidCommand = 'doesNotExist' + $e = { Switch-Process $invalidCommand } | Should -Throw -ErrorId 'CommandNotFound,Microsoft.PowerShell.Commands.SwitchProcessCommand' -PassThru + $e.Exception.Message | Should -BeLike "*'$invalidCommand'*" + $e.TargetObject | Should -BeExactly $invalidCommand + } } Describe 'Switch-Process for Windows' -Tag 'CI' { diff --git a/test/xUnit/csharp/test_Feedback.cs b/test/xUnit/csharp/test_Feedback.cs index 9d7588a88..372c9ec8c 100644 --- a/test/xUnit/csharp/test_Feedback.cs +++ b/test/xUnit/csharp/test_Feedback.cs @@ -48,7 +48,7 @@ namespace PSTests.Sequential { // The delay is exaggerated to make the test reliable. // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small. - Thread.Sleep(2000); + Thread.Sleep(2500); } return $"{commandLine}+{errorRecord.FullyQualifiedErrorId}";