From fb3a9098b5a8bf7cf304041725441c4b145e376f Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 May 2019 11:00:23 -0700 Subject: [PATCH 1/8] Add `-l` support for pwsh to match POSIX shell compatibility --- .../host/msh/CommandLineParameterParser.cs | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index a04a7edd461..bd08b0e3c7e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -173,28 +173,29 @@ internal class CommandLineParameterParser private const int MaxPipePathLengthMacOS = 104; internal static string[] validParameters = { - "version", - "nologo", - "noexit", #if STAMODE "sta", "mta", #endif - "noprofile", - "noninteractive", - "inputformat", - "outputformat", - "windowstyle", - "encodedcommand", + "command", "configurationname", - "file", + "custompipename", + "encodedcommand", "executionpolicy", - "command", - "settingsfile", + "file", "help", - "workingdirectory", + "inputformat", + "loadprofile", + "noexit", + "nologo", + "noninteractive", + "noprofile", + "outputformat", "removeworkingdirectorytrailingcharacter", - "custompipename" + "settingsfile", + "version", + "windowstyle", + "workingdirectory" }; internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText) @@ -742,6 +743,10 @@ private void ParseHelper(string[] args) _noExit = true; noexitSeen = true; } + else if (MatchSwitch(switchKey, "loadprofile", "l")) + { + // do nothing as we load profile by default, but this is needed to be compatible with POSIX shell expectations + } else if (MatchSwitch(switchKey, "noprofile", "nop")) { _skipUserInit = true; From a69b20be3e60f3270255096fbaf8acea671605e8 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 May 2019 11:12:46 -0700 Subject: [PATCH 2/8] add test --- test/powershell/Host/ConsoleHost.Tests.ps1 | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index f7d1c39ddfb..30529c301d9 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -245,6 +245,33 @@ Describe "ConsoleHost unit tests" -tags "Feature" { } } + Context "-LoadProfile Commandline switch" { + BeforeAll { + if (Test-Path $profile) { + Remove-Item -Path "$profile.backup" -ErrorAction SilentlyContinue + Rename-Item -Path $profile -NewName "$profile.backup" + Set-Content -Path $profile -Value "'profile-loaded'" -Force + } + } + + AfterAll { + if (Test-Path "$profile.backup") { + Remove-Item -Path $profile -ErrorAction SilentlyContinue + Rename-Item -Path "$profile.backup" -NewName $profile + } + } + + It "Verifies pwsh will accept switch" -TestCases @( + @{ switch = "-l"}, + @{ switch = "-loadprofile"} + ){ + param($switch) + + & pwsh $switch -c exit | Should -BeExactly "profile-loaded" + } + } + + Context "-SettingsFile Commandline switch" { BeforeAll { From f2888e9eb6b4f2e06533e070af35b7696591f44b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 May 2019 11:21:02 -0700 Subject: [PATCH 3/8] update console help --- .../resources/ManagedEntranceStrings.resx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx index 7ffba926993..ab6aa961a76 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx @@ -131,7 +131,7 @@ Type 'help' to get help. [-ConfigurationName <string>] [-CustomPipeName <string>] [-EncodedCommand <Base64EncodedCommand>] [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}] - [-Interactive] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] + [-Interactive] [-LoadProfile] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] [-OutputFormat {Text | XML}] [-Version] [-WindowStyle <style>] [-WorkingDirectory <directoryPath>] @@ -176,11 +176,11 @@ All parameters are case-insensitive. Example: pwsh -ConfigurationName AdminRoles -CustomPipeName - Specifies the name to use for an additional IPC server (named pipe) used for debugging + Specifies the name to use for an additional IPC server (named pipe) used for debugging and other cross-process communication. This offers a predictable mechanism for connecting to other PowerShell instances. Typically used with the CustomPipeName parameter on Enter-PSHostProcess. - Example: + Example: # PowerShell instance 1 pwsh -CustomPipeName mydebugpipe # PowerShell instance 2 @@ -224,6 +224,9 @@ All parameters are case-insensitive. -Interactive | -i Present an interactive prompt to the user. Inverse for NonInteractive parameter. +-LoadProfile | -l + Load the PowerShell profiles. This is the default behavior even if this is not specified. + -NoExit | -noe Does not exit after running startup commands. From 2d8a15af28fddab06334f2f9728d92c8fc82d506 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 May 2019 14:13:41 -0700 Subject: [PATCH 4/8] change -l to explicitly load user profile --- .../host/msh/CommandLineParameterParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index bd08b0e3c7e..eb46b6a7e6e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -745,7 +745,7 @@ private void ParseHelper(string[] args) } else if (MatchSwitch(switchKey, "loadprofile", "l")) { - // do nothing as we load profile by default, but this is needed to be compatible with POSIX shell expectations + _skipUserInit = false; } else if (MatchSwitch(switchKey, "noprofile", "nop")) { From 0c1274011c32fd7a45536065a2016f8a919e4756 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 4 May 2019 06:45:43 -0700 Subject: [PATCH 5/8] fix creation of profile --- test/powershell/Host/ConsoleHost.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 30529c301d9..6ed66b522dd 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -250,8 +250,8 @@ Describe "ConsoleHost unit tests" -tags "Feature" { if (Test-Path $profile) { Remove-Item -Path "$profile.backup" -ErrorAction SilentlyContinue Rename-Item -Path $profile -NewName "$profile.backup" - Set-Content -Path $profile -Value "'profile-loaded'" -Force } + Set-Content -Path $profile -Value "'profile-loaded'" -Force } AfterAll { @@ -267,6 +267,7 @@ Describe "ConsoleHost unit tests" -tags "Feature" { ){ param($switch) + Get-Content $profile -Raw | Should -Not -BeNullOrEmpty & pwsh $switch -c exit | Should -BeExactly "profile-loaded" } } From 3b5663ee971a5f2938cc0067a5768ad6f7c7cd4b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 4 May 2019 07:12:53 -0700 Subject: [PATCH 6/8] correctly delete $profile in all cases --- test/powershell/Host/ConsoleHost.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 6ed66b522dd..8187e2e45a6 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -251,12 +251,14 @@ Describe "ConsoleHost unit tests" -tags "Feature" { Remove-Item -Path "$profile.backup" -ErrorAction SilentlyContinue Rename-Item -Path $profile -NewName "$profile.backup" } + Set-Content -Path $profile -Value "'profile-loaded'" -Force } AfterAll { + Remove-Item -Path $profile -ErrorAction SilentlyContinue + if (Test-Path "$profile.backup") { - Remove-Item -Path $profile -ErrorAction SilentlyContinue Rename-Item -Path "$profile.backup" -NewName $profile } } From 592f897d21550e701d0cabaf1800a0f6b4db760b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 4 May 2019 07:44:30 -0700 Subject: [PATCH 7/8] allow test to work in cases where you can't create $profile --- test/powershell/Host/ConsoleHost.Tests.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 8187e2e45a6..4e62fb46284 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -269,8 +269,14 @@ Describe "ConsoleHost unit tests" -tags "Feature" { ){ param($switch) - Get-Content $profile -Raw | Should -Not -BeNullOrEmpty - & pwsh $switch -c exit | Should -BeExactly "profile-loaded" + if (Test-Path $profile) { + & pwsh $switch -command exit | Should -BeExactly "profile-loaded" + } + else { + # In CI, may not be able to write to $profile location, so just verify that the switch is accepted + # and no error message is in the output + & pwsh $switch -command exit *>&1 | Should -BeNullOrEmpty + } } } From 9df0490e2fafcc7b5e3dd1111a4151758ef9cf57 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 4 May 2019 09:31:05 -0700 Subject: [PATCH 8/8] Update src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx Co-Authored-By: SteveL-MSFT --- .../resources/ManagedEntranceStrings.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx index ab6aa961a76..25a7219a481 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx @@ -225,7 +225,7 @@ All parameters are case-insensitive. Present an interactive prompt to the user. Inverse for NonInteractive parameter. -LoadProfile | -l - Load the PowerShell profiles. This is the default behavior even if this is not specified. + Load the PowerShell profiles. This is the default behavior even if this is not specified. -NoExit | -noe Does not exit after running startup commands.