From 80d5da485bc42a5a739a6bca779afa937281182e Mon Sep 17 00:00:00 2001 From: nohwnd Date: Fri, 4 Feb 2022 10:56:06 +0100 Subject: [PATCH 1/3] Fix red color being incorrectly used as foreground color after script ends --- scripts/common.lib.ps1 | 17 +++++++---------- scripts/perf/perf.ps1 | 40 ++++++++++++++++++++-------------------- scripts/test.ps1 | 5 +---- scripts/verify-sign.ps1 | 14 ++++---------- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/scripts/common.lib.ps1 b/scripts/common.lib.ps1 index 75f45f8f07..8fc3b597c3 100644 --- a/scripts/common.lib.ps1 +++ b/scripts/common.lib.ps1 @@ -61,16 +61,10 @@ function Write-Log { $Level = "Success" ) - $currentColor = $Host.UI.RawUI.ForegroundColor - try { - $Host.UI.RawUI.ForegroundColor = if ("Success" -eq $Level) { "Green" } else { "Red" } - if ($message) - { - Write-Output "... $message" - } - } - finally { - $Host.UI.RawUI.ForegroundColor = $currentColor + if ($message) + { + $color = if ("Success" -eq $Level) { "Green" } else { "Red" } + Write-Host "... $message" -ForegroundColor $color } } @@ -233,6 +227,9 @@ function Invoke-Exe { Write-Verbose "Done. Exit code: $exitCode" + if ($exitCode -ne 0) { + + } if ($exitCode -ne 0 -and ($IgnoreExitCode -notcontains $exitCode)) { if($CaptureOutput) { diff --git a/scripts/perf/perf.ps1 b/scripts/perf/perf.ps1 index 88c31e8cfa..348a738ef9 100644 --- a/scripts/perf/perf.ps1 +++ b/scripts/perf/perf.ps1 @@ -175,13 +175,10 @@ function Get-ConsoleRunnerPath($runner, $targetFrameWork) function Write-Log ([string] $message, $messageColor = "Green") { - $currentColor = $Host.UI.RawUI.ForegroundColor - $Host.UI.RawUI.ForegroundColor = $messageColor if ($message) { - Write-Output "... $message" + Write-Host "... $message" -ForegroundColor $messageColor } - $Host.UI.RawUI.ForegroundColor = $currentColor } function Get-ProductVersion($filePath) @@ -352,24 +349,27 @@ function Invoke-PerformanceTests # function Invoke-DisplayResults { - $currentColor = $Host.UI.RawUI.ForegroundColor - $Host.UI.RawUI.ForegroundColor = "Green" - $osDetails = Get-SystemInfo - "`n" - "Machine Configuration" - $osDetails | Format-List 'MachineName', 'OSName', 'OSVersion', 'MachineType' , 'Processor', 'LogicalCores', 'RAMSize' - - if($DefaultAction -eq "Both" -or $DefaultAction -eq "Discovery") - { - $Script:TPT_Results | Where-Object {$_.Action -like "Discovery"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize - } + try { + $currentColor = $Host.UI.RawUI.ForegroundColor + $Host.UI.RawUI.ForegroundColor = "Green" + $osDetails = Get-SystemInfo + "`n" + "Machine Configuration" + $osDetails | Format-List 'MachineName', 'OSName', 'OSVersion', 'MachineType' , 'Processor', 'LogicalCores', 'RAMSize' + + if($DefaultAction -eq "Both" -or $DefaultAction -eq "Discovery") + { + $Script:TPT_Results | Where-Object {$_.Action -like "Discovery"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize + } - if($DefaultAction -eq "Both" -or $DefaultAction -eq "Execution") - { - $Script:TPT_Results | Where-Object {$_.Action -like "Execution"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize + if($DefaultAction -eq "Both" -or $DefaultAction -eq "Execution") + { + $Script:TPT_Results | Where-Object {$_.Action -like "Execution"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize + } + } + finally { + $Host.UI.RawUI.ForegroundColor = $currentColor } - - $Host.UI.RawUI.ForegroundColor = $currentColor if($ExportResults -ne $null -and $ExportResults -eq "csv") { diff --git a/scripts/test.ps1 b/scripts/test.ps1 index ce05418dcf..d0022db77b 100644 --- a/scripts/test.ps1 +++ b/scripts/test.ps1 @@ -119,13 +119,10 @@ $Script:ScriptFailed = $false function Write-Log ([string] $message, $messageColor = "Green") { - $currentColor = $Host.UI.RawUI.ForegroundColor - $Host.UI.RawUI.ForegroundColor = $messageColor if ($message) { - Write-Output "... $message" + Write-Host "... $message" -ForegroundColor $messageColor } - $Host.UI.RawUI.ForegroundColor = $currentColor } function Write-VerboseLog([string] $message) diff --git a/scripts/verify-sign.ps1 b/scripts/verify-sign.ps1 index 136e5e6364..dff004b0b8 100644 --- a/scripts/verify-sign.ps1 +++ b/scripts/verify-sign.ps1 @@ -151,12 +151,6 @@ function Write-Debug ([string] $message) function Write-ToCI ([string] $message, [string]$type, [switch]$vso) { - $currentColor = $Host.UI.RawUI.ForegroundColor - - if($type -eq "error") { - $Host.UI.RawUI.ForegroundColor = "Red" - } - if ($message -or $vso -or $type) { $prefix = "" @@ -164,15 +158,15 @@ function Write-ToCI ([string] $message, [string]$type, [switch]$vso) $prefix = "vso" } - Write-Output "##$prefix[$type]$message" + $color = if($type -eq "error") { "Red" } else { $Host.UI.RawUI.ForegroundColor } + Write-Host "##$prefix[$type]$message" -ForegroundColor $color } - $Host.UI.RawUI.ForegroundColor = $currentColor } Write-Debug "Variables used: " Get-ChildItem variable:TPB_* -Write-Output "" -Write-Output "" +Write-Host "" +Write-Host "" Verify-Assemblies Verify-NugetPackages From cfaa0cb6c4938438fbf5d908a979a567cdc236df Mon Sep 17 00:00:00 2001 From: nohwnd Date: Fri, 4 Feb 2022 11:24:19 +0100 Subject: [PATCH 2/3] Add lock --- scripts/common.lib.ps1 | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/common.lib.ps1 b/scripts/common.lib.ps1 index 8fc3b597c3..fb0bfcdc1b 100644 --- a/scripts/common.lib.ps1 +++ b/scripts/common.lib.ps1 @@ -262,15 +262,20 @@ public class ProcessOutputter { AppendLine(e.Data); if (!suppressOutput) { - var fg = Console.ForegroundColor; - try - { - Console.ForegroundColor = _color; - Console.WriteLine(e.Data); - } - finally - { - Console.ForegroundColor = fg; + // These handlers can run at the same time, + // without lock they sometimes grab the color the other + // one set. + lock (Console.Out) { + var fg = Console.ForegroundColor; + try + { + Console.ForegroundColor = _color; + Console.WriteLine(e.Data); + } + finally + { + Console.ForegroundColor = fg; + } } } }; From 65ec46841b34fdd88610b325bf6508ead37fff71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Fri, 4 Feb 2022 03:04:33 -0800 Subject: [PATCH 3/3] Update scripts/common.lib.ps1 --- scripts/common.lib.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/common.lib.ps1 b/scripts/common.lib.ps1 index fb0bfcdc1b..2d983c67f9 100644 --- a/scripts/common.lib.ps1 +++ b/scripts/common.lib.ps1 @@ -227,9 +227,6 @@ function Invoke-Exe { Write-Verbose "Done. Exit code: $exitCode" - if ($exitCode -ne 0) { - - } if ($exitCode -ne 0 -and ($IgnoreExitCode -notcontains $exitCode)) { if($CaptureOutput) {