From 9786d1c6b5acfc5567b312228d3c80947774d137 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 11 Sep 2020 14:48:02 -0700 Subject: [PATCH 1/2] Fix ConciseView for interactive advanced function writing error --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 +- test/powershell/engine/Formatting/ErrorView.Tests.ps1 | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 5729605fe9a..d23555307b0 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1226,7 +1226,7 @@ function Get-ConciseViewPositionMessage { $reason = 'Exception' } # MyCommand can be the script block, so we don't want to show that so check if it's an actual command - elseif ($myinv.MyCommand -and (Get-Command -Name $myinv.MyCommand -ErrorAction Ignore)) + elseif ($myinv.MyCommand -and $myinv.MyCommand.Name -and (Get-Command -Name $myinv.MyCommand -ErrorAction Ignore)) { $reason = $myinv.MyCommand } diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index 1087f3700a7..d156a5033db 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -111,6 +111,13 @@ Describe 'Tests for $ErrorView' -Tag CI { $e = & "$PSHOME/pwsh" -noprofile -command '$PSModuleAutoLoadingPreference = ""none""; cmdletThatDoesntExist' 2>&1 | Out-String $e | Should -BeLike "*cmdletThatDoesntExist*" } + + It "Error shows for advanced function" { + # need to have it virtually interactive so that InvocationInfo.MyCommand is empty + $e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - | Out-String + $e | Should -Not -BeNullOrEmpty + $e | Should -BeLike "*: `e*myTest*" + } } Context 'NormalView tests' { From 18a9794d596e8e64d315dffe416382373246f7bd Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 11 Sep 2020 15:20:26 -0700 Subject: [PATCH 2/2] Fix test for running in CI --- test/powershell/engine/Formatting/ErrorView.Tests.ps1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index d156a5033db..0188666328b 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -114,9 +114,16 @@ Describe 'Tests for $ErrorView' -Tag CI { It "Error shows for advanced function" { # need to have it virtually interactive so that InvocationInfo.MyCommand is empty - $e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - | Out-String + $e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - 2>&1 | Out-String $e | Should -Not -BeNullOrEmpty - $e | Should -BeLike "*: `e*myTest*" + + # need to see if ANSI escape sequences are in the output as ANSI is disabled for CI + if ($e.Contains("`e")) { + $e | Should -BeLike "*: `e*myTest*" + } + else { + $e | Should -BeLike "*: myTest*" + } } }