From 5dbb704f2d6737fa17fe2e15a8c9e5c4bbad7e67 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 15 Nov 2022 11:23:37 -0800 Subject: [PATCH 1/6] Change `exec` from alias to function to handle arbitrary args --- .../engine/InitialSessionState.cs | 15 +++++++++++---- .../Microsoft.PowerShell.Core/Exec.Tests.ps1 | 11 ++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 6d8ede14ddb..468b919acdf 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4078,6 +4078,13 @@ internal static string GetClearHostFunctionText() } } + internal static string GetExecFunctionText() + { + return @" +Switch-Process -WithCommand $args +"; + } + /// /// This is the default function to use for man/help. It uses /// splatting to pass in the parameters. @@ -4683,10 +4690,6 @@ internal static SessionStateAliasEntry[] BuiltInAliases new SessionStateAliasEntry("sls", "Select-String"), }; -#if UNIX - builtInAliases.Add(new SessionStateAliasEntry("exec", "Switch-Process")); -#endif - return builtInAliases.ToArray(); } } @@ -4716,6 +4719,10 @@ internal static SessionStateAliasEntry[] BuiltInAliases SessionStateFunctionEntry.GetDelayParsedFunctionEntry("help", GetHelpPagingFunctionText(), isProductCode: true, languageMode: systemLanguageMode), SessionStateFunctionEntry.GetDelayParsedFunctionEntry("prompt", DefaultPromptFunctionText, isProductCode: true, languageMode: systemLanguageMode), +#if UNIX + SessionStateFunctionEntry.GetDelayParsedFunctionEntry("exec", GetExecFunctionText(), isProductCode: true, languageMode: systemLanguageMode), +#endif + // Functions that require full language mode and are trusted SessionStateFunctionEntry.GetDelayParsedFunctionEntry("Clear-Host", GetClearHostFunctionText(), isProductCode: true, languageMode: PSLanguageMode.FullLanguage), SessionStateFunctionEntry.GetDelayParsedFunctionEntry("TabExpansion2", s_tabExpansionFunctionText, isProductCode: true, languageMode: PSLanguageMode.FullLanguage), diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 index c4b4ce0e595..48564e9eb38 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 @@ -15,10 +15,10 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' { $global:PSDefaultParameterValues = $originalDefaultParameterValues } - It 'Exec alias should map to Switch-Process' { + It 'Exec function should map to Switch-Process' { $alias = Get-Command exec - $alias | Should -BeOfType [System.Management.Automation.AliasInfo] - $alias.Definition | Should -BeExactly 'Switch-Process' + $alias | Should -BeOfType [System.Management.Automation.CommandInfo] + $alias.Definition | Should -Not -BeNullOrEmpty } It 'Exec by itself does nothing' { @@ -60,6 +60,11 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' { $env = pwsh -noprofile -outputformat text -command { $env:TEST_FOO='my test = value'; Switch-Process bash -c 'echo $TEST_FOO' } $env | Should -BeExactly 'my test = value' } + + It 'The command can include a -w paramter' { + $out = pwsh -noprofile -outputformat text -command { exec /bin/echo 1 -w 2 } + $out | Should -BeExactly '1 -w 2' + } } Describe 'Switch-Process for Windows' -Tag 'CI' { From 0956bb7f5f2b5592aca3544aac0af78817ca755e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 15 Nov 2022 13:37:44 -0800 Subject: [PATCH 2/6] remove `exec` from alias testsy --- test/powershell/engine/Basic/DefaultCommands.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index bdcd5af0000..f5fc66748a7 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -58,7 +58,6 @@ Describe "Verify approved aliases list" -Tags "CI" { "Alias", "epsn", "Export-PSSession", $($FullCLR ), "", "", "" "Alias", "erase", "Remove-Item", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "" "Alias", "etsn", "Enter-PSSession", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "" -"Alias", "exec", "Switch-Process", $( $CoreUnix), "", "", "" "Alias", "exsn", "Exit-PSSession", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "" "Alias", "fc", "Format-Custom", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", "" "Alias", "fhx", "Format-Hex", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "" From a14cae109f6cdf042cf820b7c7c9cf5e5ac0e16e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 15 Nov 2022 15:29:30 -0800 Subject: [PATCH 3/6] Update test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 --- .../powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 index 48564e9eb38..632709910af 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 @@ -61,7 +61,7 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' { $env | Should -BeExactly 'my test = value' } - It 'The command can include a -w paramter' { + It 'The command can include a -w parameter' { $out = pwsh -noprofile -outputformat text -command { exec /bin/echo 1 -w 2 } $out | Should -BeExactly '1 -w 2' } From 357267f60a8482556f82e71694e6dc05c695f207 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 15 Nov 2022 17:55:54 -0800 Subject: [PATCH 4/6] Update src/System.Management.Automation/engine/InitialSessionState.cs --- src/System.Management.Automation/engine/InitialSessionState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 468b919acdf..06388c7e393 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4081,7 +4081,7 @@ internal static string GetClearHostFunctionText() internal static string GetExecFunctionText() { return @" -Switch-Process -WithCommand $args +Switch-Process -WithCommand $args "; } From 9f95aa87b08300618034c36de90424dba03946a3 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 16 Nov 2022 16:10:25 -0800 Subject: [PATCH 5/6] address Ilya's feedback --- src/System.Management.Automation/engine/InitialSessionState.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 06388c7e393..a47d1d5e545 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4078,12 +4078,14 @@ internal static string GetClearHostFunctionText() } } +#if UNIX internal static string GetExecFunctionText() { return @" Switch-Process -WithCommand $args "; } +#endif /// /// This is the default function to use for man/help. It uses From 5d63fbcf916995c501fb36852ec8a3cec6becad7 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 16 Nov 2022 20:51:28 -0800 Subject: [PATCH 6/6] rename variable --- .../Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 index 632709910af..65553d67791 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1 @@ -16,9 +16,9 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' { } It 'Exec function should map to Switch-Process' { - $alias = Get-Command exec - $alias | Should -BeOfType [System.Management.Automation.CommandInfo] - $alias.Definition | Should -Not -BeNullOrEmpty + $func = Get-Command exec + $func | Should -BeOfType [System.Management.Automation.CommandInfo] + $func.Definition | Should -Not -BeNullOrEmpty } It 'Exec by itself does nothing' {