Add $env:__SuppressAnsiEscapeSequences to control whether to… (#10814)

This commit is contained in:
Steve Lee authored and Travis Plunk committed 2019-10-24 12:45:26 -07:00
1 parent 09b5ed3fd0
commit 4ff9924cbf
9 files changed
+220 -72

No files matched your search

@@ -0,0 +1,89 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe '$env:__SuppressAnsiEscapeSequences tests' -Tag CI {
BeforeAll {
$originalSuppressPref = $env:__SuppressAnsiEscapeSequences
}
AfterAll {
$env:__SuppressAnsiEscapeSequences = $originalSuppressPref
}
Context 'Allow Escape Sequences' {
BeforeAll {
Remove-Item env:__SuppressAnsiEscapeSequences -ErrorAction Ignore
}
It 'Select-String emits VT' {
"select this string" | Select-String 'this' | Out-String |Should -BeLikeExactly "*`e*"
}
It 'ConciseView emits VT' {
$oldErrorView = $ErrorView
try {
$ErrorView = 'ConciseView'
Invoke-Expression '1/d'
}
catch {
$e = $_
}
finally {
$ErrorView = $oldErrorView
}
$e | Out-String | Should -BeLikeExactly "*`e*"
}
It 'Get-Error emits VT' {
try {
1/0
}
catch {
# ignore
}
Get-Error | Out-String | Should -BeLikeExactly "*`e*"
}
}
Context 'No Escape Sequences' {
BeforeAll {
$env:__SuppressAnsiEscapeSequences = 1
}
It 'Select-String does not emit VT' {
"select this string" | Select-String 'this' | Out-String | Should -Not -BeLikeExactly "*`e*"
}
It 'ConciseView does not emit VT' {
$oldErrorView = $ErrorView
try {
$ErrorView = 'ConciseView'
Invoke-Expression '1/d'
}
catch {
$e = $_
}
finally {
$ErrorView = $oldErrorView
}
$e | Out-String | Should -Not -BeLikeExactly "*`e*"
}
It 'Get-Error does not emit VT' {
try {
1/0
}
catch {
# ignore
}
Get-Error | Out-String | Should -Not -BeLikeExactly "*`e*"
}
}
}
@@ -7,6 +7,10 @@ Describe "Select-String" -Tags "CI" {
$currentDirectory = $pwd.Path
}
AfterAll {
Push-Location $currentDirectory
}
Context "String actions" {
$testinputone = "hello","Hello","goodbye"
$testinputtwo = "hello","Hello"
@@ -76,47 +80,47 @@ Describe "Select-String" -Tags "CI" {
}
it "Should output a string with the first match highlighted" {
if ($Host.UI.SupportsVirtualTerminal)
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "`nhello`nHello`n`n"
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
it "Should output a string with all matches highlighted when AllMatch is used" {
if ($Host.UI.SupportsVirtualTerminal)
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n"
$result | Should -Be "${nl}he`e[7ml`e[0m`e[7ml`e[0mo${nl}He`e[7ml`e[0m`e[7ml`e[0mo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "`nhello`nHello`n`n"
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
it "Should output a string with the first match highlighted when SimpleMatch is used" {
if ($Host.UI.SupportsVirtualTerminal)
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "`nhello`nHello`n`n"
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
}
it "Should output a string without highlighting when NoEmphasis is used" {
$result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String
$result | Should -Be "`nhello`nHello`n`n"
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
}
it "Should return an array of matching strings without virtual terminal sequences" {
@@ -247,8 +251,4 @@ Describe "Select-String" -Tags "CI" {
Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected
}
}
AfterAll {
Push-Location $currentDirectory
}
}