From c1026b3ae8dabe710e9a3eb03b0c987e562c4b17 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 4 May 2018 21:25:05 +0500 Subject: [PATCH] Remove 'more' function and move the $env:PAGER capability into the help function (#6059) --- .../engine/InitialSessionState.cs | 46 ++++++------------- .../engine/Basic/DefaultCommands.Tests.ps1 | 4 -- .../engine/Help/HelpSystem.Tests.ps1 | 16 ++++++- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 4bde9021c..27b11fd1a 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4250,8 +4250,12 @@ param( $PSBoundParameters['Full'] = $true } - # Set the outputencoding to Console::OutputEncoding. More.com doesn't work well with Unicode. - $outputEncoding=[System.Console]::OutputEncoding + # Nano needs to use Unicode, but Windows and Linux need the default + $OutputEncoding = if ([System.Management.Automation.Platform]::IsNanoServer -or [System.Management.Automation.Platform]::IsIoT) { + [System.Text.Encoding]::Unicode + } else { + [System.Console]::OutputEncoding + } $help = Get-Help @PSBoundParameters @@ -4262,7 +4266,15 @@ param( } else { - $help | more + # Respect PAGER, use more on Windows, and use less on Linux + $moreCommand,$moreArgs = $env:PAGER -split '\s+' + if ($moreCommand) { + $help | & $moreCommand $moreArgs + } elseif ($IsWindows) { + $help | more.com + } else { + $help | less + } } "; } @@ -4743,32 +4755,6 @@ end # .Link # https://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml -"; - - internal const string DefaultMoreFunctionText = @" -param([string[]]$paths) -# Nano needs to use Unicode, but Windows and Linux need the default -$OutputEncoding = if ([System.Management.Automation.Platform]::IsNanoServer -or [System.Management.Automation.Platform]::IsIoT) { - [System.Text.Encoding]::Unicode -} else { - [System.Console]::OutputEncoding -} - -# Respect PAGER, use more on Windows, and use less on Linux -if (Test-Path env:PAGER) { - $pager,$moreArgs = $env:PAGER -split '\s+' - $moreCommand = (Get-Command -CommandType Application $pager | Select-Object -First 1).Definition -} elseif ($IsWindows) { - $moreCommand = (Get-Command -CommandType Application more | Select-Object -First 1).Definition -} else { - $moreCommand = (Get-Command -CommandType Application less | Select-Object -First 1).Definition -} - -if($paths) { - foreach ($file in $paths) { - Get-Content $file | & $moreCommand $moreArgs - } -} else { $input | & $moreCommand $moreArgs } "; internal const string DefaultSetDriveFunctionText = "Set-Location $MyInvocation.MyCommand.Name"; @@ -4780,8 +4766,6 @@ if($paths) { SessionStateFunctionEntry.GetDelayParsedFunctionEntry("prompt", DefaultPromptFunctionText, isProductCode: true), SessionStateFunctionEntry.GetDelayParsedFunctionEntry("TabExpansion2", s_tabExpansionFunctionText, isProductCode: true), SessionStateFunctionEntry.GetDelayParsedFunctionEntry("Clear-Host", GetClearHostFunctionText(), isProductCode: true), - // Porting note: we keep more because the function acts correctly on Linux - SessionStateFunctionEntry.GetDelayParsedFunctionEntry("more", DefaultMoreFunctionText, isProductCode: true), SessionStateFunctionEntry.GetDelayParsedFunctionEntry("help", GetHelpPagingFunctionText(), isProductCode: true), // Porting note: we remove mkdir on Linux because it is a conflict #if !UNIX diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index dd812efec..64b2b441c 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -529,8 +529,4 @@ Describe "Verify approved aliases list" -Tags "CI" { $result | Write-Host $result | Should -BeNullOrEmpty } - - It "Should have 'more' as a function" { - Test-Path Function:more | Should -BeTrue - } } diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index fb358a919..58722ecf5 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -39,7 +39,7 @@ function GetCurrentUserHelpRoot { Describe "Validate that //default.help.txt is present" -Tags @('CI') { - It "Get-Help returns information about the help system." { + It "Get-Help returns information about the help system" { $help = Get-Help $help.Name | Should -Be "default" @@ -48,6 +48,20 @@ Describe "Validate that //default.help.txt is present" -Tags @( } } +Describe "Validate that the Help function can Run in strict mode" -Tags @('CI') { + + It "Help doesn't fail when strict mode is on" { + + $help = & { + # run in nested scope to keep strict mode from affecting other tests + Set-StrictMode -Version Latest + Help + } + # the help function renders the help content as text so just verify that there is content + $help | Should -Not -BeNullOrEmpty + } +} + Describe "Validate that get-help works for CurrentUserScope" -Tags @('CI') { BeforeAll { $SavedProgressPreference = $ProgressPreference