From a13c8d7913daad5f557cb70acc9696a5b720a632 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 17 Dec 2019 09:29:02 -0800 Subject: [PATCH] Move Set-StrictMode to outer scriptblock for ErrorView --- .../PowerShellCore_format_ps1xml.cs | 2 +- .../engine/Formatting/ErrorView.Tests.ps1 | 19 +++++++++++++++++++ 2 files changed, 20 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 2237e05f5f7..011818ae3bc 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -991,9 +991,9 @@ private static IEnumerable ViewsOf_System_Management_Autom } ") .AddScriptBlockExpressionBinding(@" + Set-StrictMode -Off function Get-ConciseViewPositionMessage { - Set-StrictMode -Off $resetColor = '' if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences)) { diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index 500ac52ee8e..592fd7ff1c6 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -63,4 +63,23 @@ Describe 'Tests for $ErrorView' -Tag CI { $e | Out-String | Should -Not -BeLike '*pester*' } } + + Context 'NormalView tests' { + + It 'Error shows up when using strict mode' { + try { + $ErrorView = 'NormalView' + Set-StrictMode -Version 2 + throw 'Oops!' + } + catch { + $e = $_ | Out-String + } + finally { + Set-StrictMode -Off + } + + $e | Should -BeLike '*Oops!*' + } + } }