Skip to content

Commit

Permalink
Add ANSI support for output (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Aug 11, 2022
1 parent 959851c commit 13fb4a2
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 139 deletions.
23 changes: 21 additions & 2 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,25 @@ function Invoke-Pester {
$pluginConfiguration = @{}
$pluginData = @{}
$plugins = @()

# Verify this before WriteScreenPlugin because of Write-PesterStart and Write-PesterDebugMessage
if ($PesterPreference.Output.RenderMode.Value -notin 'Auto', 'Ansi', 'ConsoleColor', 'Plaintext') {
throw "Unsupported Output.RenderMode option '$($PesterPreference.Output.RenderMode.Value)'"
}

if ($PesterPreference.Output.RenderMode.Value -eq 'Auto') {
if ($null -ne $env:NO_COLOR) {
# https://no-color.org/)
$PesterPreference.Output.RenderMode = 'Plaintext'
}
elseif (($supportsVT = $host.UI.psobject.Properties['SupportsVirtualTerminal']) -and $supportsVT.Value) {
$PesterPreference.Output.RenderMode = 'Ansi'
}
else {
$PesterPreference.Output.RenderMode = 'ConsoleColor'
}
}

if ('None' -ne $PesterPreference.Output.Verbosity.Value) {
$plugins += Get-WriteScreenPlugin -Verbosity $PesterPreference.Output.Verbosity.Value
}
Expand Down Expand Up @@ -1105,7 +1124,7 @@ function Invoke-Pester {
if ($PesterPreference.CodeCoverage.Enabled.Value) {
if ($PesterPreference.Output.Verbosity.Value -ne "None") {
$sw = [Diagnostics.Stopwatch]::StartNew()
& $SafeCommands["Write-Host"] -ForegroundColor Magenta "Processing code coverage result."
Write-PesterHostMessage -ForegroundColor Magenta "Processing code coverage result."
}
$breakpoints = @($run.PluginData.Coverage.CommandCoverage)
$measure = if (-not $PesterPreference.CodeCoverage.UseBreakpoints.Value) { @($run.PluginData.Coverage.Tracer.Hits) }
Expand Down Expand Up @@ -1163,7 +1182,7 @@ function Invoke-Pester {

$stringWriter.ToString() | & $SafeCommands['Out-File'] $resolvedPath -Encoding $PesterPreference.CodeCoverage.OutputEncoding.Value -Force
if ($PesterPreference.Output.Verbosity.Value -in "Detailed", "Diagnostic") {
& $SafeCommands["Write-Host"] -ForegroundColor Magenta "Code Coverage result processed in $($sw.ElapsedMilliseconds) ms."
Write-PesterHostMessage -ForegroundColor Magenta "Code Coverage result processed in $($sw.ElapsedMilliseconds) ms."
}
$reportText = Write-CoverageReport $coverageReport

Expand Down
3 changes: 3 additions & 0 deletions src/Pester.RSpec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ function New-PesterConfiguration {
CIFormat: The CI format of error output in build logs, options are None, Auto, AzureDevops and GithubActions.
Default value: 'Auto'
RenderMode: The mode used to render console output, options are Auto, Ansi, ConsoleColor and Plaintext.
Default value: 'Auto'
TestDrive:
Enabled: Enable TestDrive.
Default value: $true
Expand Down
2 changes: 1 addition & 1 deletion src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ function Assert-Success {

if ($anyFailed) {
$Message = $Message + ":`n$err"
& $SafeCommands["Write-Host"] -ForegroundColor Red $Message
Write-PesterHostMessage -ForegroundColor Red $Message
throw $Message
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pester.Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ function Write-PesterDebugMessage {
$Message = (&$LazyMessage) -join "`n"
}

& $script:SafeCommands['Write-Host'] -ForegroundColor Black -BackgroundColor $color "${Scope}: $Message "
Write-PesterHostMessage -ForegroundColor Black -BackgroundColor $color "${Scope}: $Message "
if ($null -ne $ErrorRecord) {
& $script:SafeCommands['Write-Host'] -ForegroundColor Black -BackgroundColor $color "$ErrorRecord"
Write-PesterHostMessage -ForegroundColor Black -BackgroundColor $color "$ErrorRecord"
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/csharp/Pester/OutputConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class OutputConfiguration : ConfigurationSection
private StringOption _verbosity;
private StringOption _stackTraceVerbosity;
private StringOption _ciFormat;
private StringOption _renderMode;

public static OutputConfiguration Default { get { return new OutputConfiguration(); } }
public static OutputConfiguration ShallowClone(OutputConfiguration configuration)
Expand All @@ -39,6 +40,7 @@ public OutputConfiguration(IDictionary configuration) : this()
configuration.AssignObjectIfNotNull<string>(nameof(Verbosity), v => Verbosity = v);
configuration.AssignObjectIfNotNull<string>(nameof(StackTraceVerbosity), v => StackTraceVerbosity = v);
configuration.AssignObjectIfNotNull<string>(nameof(CIFormat), v => CIFormat = v);
configuration.AssignObjectIfNotNull<string>(nameof(RenderMode), v => RenderMode = v);
}
}

Expand All @@ -47,6 +49,7 @@ public OutputConfiguration() : base("Output configuration")
Verbosity = new StringOption("The verbosity of output, options are None, Normal, Detailed and Diagnostic.", "Normal");
StackTraceVerbosity = new StringOption("The verbosity of stacktrace output, options are None, FirstLine, Filtered and Full.", "Filtered");
CIFormat = new StringOption("The CI format of error output in build logs, options are None, Auto, AzureDevops and GithubActions.", "Auto");
RenderMode = new StringOption("The mode used to render console output, options are Auto, Ansi, ConsoleColor and Plaintext.", "Auto");
}

public StringOption Verbosity
Expand Down Expand Up @@ -97,6 +100,22 @@ public StringOption CIFormat
}
}

public StringOption RenderMode
{
get { return _renderMode; }
set
{
if (_renderMode == null)
{
_renderMode = value;
}
else
{
_renderMode = new StringOption(_renderMode, value?.Value);
}
}
}

private string FixMinimal(string value)
{
return string.Equals(value, "Minimal", StringComparison.OrdinalIgnoreCase) ? "Normal" : value;
Expand Down
4 changes: 2 additions & 2 deletions src/functions/Coverage.Plugin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

if ($PesterPreference.Output.Verbosity.Value -ne "None") {
& $SafeCommands["Write-Host"] -ForegroundColor Magenta "Starting code coverage."
Write-PesterHostMessage -ForegroundColor Magenta "Starting code coverage."
}

$config = $Context.Configuration['Coverage']
Expand Down Expand Up @@ -45,7 +45,7 @@
})

if ($PesterPreference.Output.Verbosity.Value -in "Detailed", "Diagnostic") {
& $SafeCommands["Write-Host"] -ForegroundColor Magenta "Code Coverage preparation finished after $($sw.ElapsedMilliseconds) ms."
Write-PesterHostMessage -ForegroundColor Magenta "Code Coverage preparation finished after $($sw.ElapsedMilliseconds) ms."
}
} -End {
param($Context)
Expand Down
2 changes: 1 addition & 1 deletion src/functions/Get-SkipRemainingOnFailurePlugin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function Get-SkipRemainingOnFailurePlugin {
param($Context)

if ($Context.Configuration.SkipRemainingOnFailureCount -gt 0) {
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.Skipped "Remaining tests skipped after first failure: $($Context.Configuration.SkipRemainingOnFailureCount)"
Write-PesterHostMessage -ForegroundColor $ReportTheme.Skipped "Remaining tests skipped after first failure: $($Context.Configuration.SkipRemainingOnFailureCount)"
}
}
}
Expand Down
Loading

0 comments on commit 13fb4a2

Please sign in to comment.