Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit output information for failed tests #1975

Merged
merged 23 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6ac2f5b
Added ShowStackTrace property to Output section
ArmaanMcleod Jun 5, 2021
aaee5d0
Added ShowStackTrace parameter to Write-ErrorToScreen and refactored …
ArmaanMcleod Jun 5, 2021
14f1736
ShowStackTrace doesn't limit ScriptStackTrace
ArmaanMcleod Jun 5, 2021
0ded7ad
Add ShowStackTrace output option to every call to Write-ErrorToScreen
ArmaanMcleod Jun 6, 2021
1b165e4
Added configuration tests and made slight improvements in naming of e…
ArmaanMcleod Jun 6, 2021
00a8cc3
Use string builder to create the error message
ArmaanMcleod Jun 6, 2021
de2fc26
Using empty string instead of new() for StringBuilder
ArmaanMcleod Jun 6, 2021
d224f74
Refactored format logic into separate Format-ErrorMessage function
ArmaanMcleod Jun 6, 2021
2efc775
Move first line of trace into Trace property
ArmaanMcleod Jun 7, 2021
bb8bf11
Merge branch 'main' into limit-output-information
ArmaanMcleod Jun 7, 2021
f8fef65
Fixing up tests
ArmaanMcleod Jun 7, 2021
dd70e8b
Added tests for Format-ErrorMessage and Write-ErrorToScreen
ArmaanMcleod Jun 7, 2021
03931e1
Removing unnecessary escaping and typos
ArmaanMcleod Jun 8, 2021
549cf0d
Converted ShowStackTrace to StackTraceVerbosity option to support mul…
ArmaanMcleod Jun 9, 2021
8d17722
Changed code to use StackTraceVerbosity to filter output
ArmaanMcleod Jun 9, 2021
7c76120
Set default for StackTraceVerbosity parameter
ArmaanMcleod Jun 9, 2021
99e3cc5
Updated configuration tests
ArmaanMcleod Jun 9, 2021
e781631
Modified Output tests to test new option
ArmaanMcleod Jun 10, 2021
c5c3766
Add warning message and override for Debug.ShowFullErrors
ArmaanMcleod Jun 10, 2021
51f9652
Removed warning and put deprecation notice in configuration
ArmaanMcleod Jun 12, 2021
b1cd1df
Added more configuration tests
ArmaanMcleod Jun 12, 2021
91704dc
Set Debug.ShowFullErrors to false prevent override for other tests
ArmaanMcleod Jun 12, 2021
f19fc38
Use BeforeEach to simplify output tests
ArmaanMcleod Jun 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,8 @@ function Invoke-Pester {
if ($PSBoundParameters.ContainsKey('Configuration')) {
# Advanced configuration used, merging to get new reference
[PesterConfiguration] $PesterPreference = [PesterConfiguration]::Merge([PesterConfiguration]::Default, $Configuration)
} else {
}
else {
[PesterConfiguration] $PesterPreference = $Configuration
}
}
Expand Down Expand Up @@ -921,6 +922,11 @@ function Invoke-Pester {
$PesterPreference.Debug.WriteDebugMessagesFrom = $PesterPreference.Debug.WriteDebugMessagesFrom.Value + @($missingCategories)
}

if ($PesterPreference.Debug.ShowFullErrors.Value) {
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved
& $SafeCommands['Write-Warning'] "Debug.ShowFullErrors is deprecated. This will be overriden with Output.StackTraceVerbosity = 'Full'."
$PesterPreference.Output.StackTraceVerbosity = "Full"
}

$plugins +=
@(
# decorator plugin needs to be added after output
Expand Down Expand Up @@ -1153,7 +1159,7 @@ function Invoke-Pester {

}
catch {
Write-ErrorToScreen $_ -Throw:$PesterPreference.Run.Throw.Value
Write-ErrorToScreen $_ -Throw:$PesterPreference.Run.Throw.Value -StackTraceVerbosity:$PesterPreference.Output.StackTraceVerbosity.Value
if ($PesterPreference.Run.Exit.Value) {
exit -1
}
Expand Down
3 changes: 3 additions & 0 deletions src/Pester.RSpec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ function New-PesterConfiguration {
Output:
Verbosity: The verbosity of output, options are None, Normal, Detailed and Diagnostic.
Default value: 'Normal'

StackTraceVerbosity: The verbosity of stacktrace output, options are None, FirstLine, Filtered and Full.
Default value: 'Filtered'
```

.PARAMETER Hashtable
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 @@ -23,6 +23,7 @@ namespace Pester
public class OutputConfiguration : ConfigurationSection
{
private StringOption _verbosity;
private StringOption _stackTraceVerbosity;

public static OutputConfiguration Default { get { return new OutputConfiguration(); } }
public static OutputConfiguration ShallowClone(OutputConfiguration configuration)
Expand All @@ -35,12 +36,14 @@ public OutputConfiguration(IDictionary configuration) : this()
if (configuration != null)
{
Verbosity = configuration.GetObjectOrNull<string>("Verbosity") ?? Verbosity;
StackTraceVerbosity = configuration.GetObjectOrNull<string>("StackTraceVerbosity") ?? StackTraceVerbosity;
}
}

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");
}

public StringOption Verbosity
Expand All @@ -59,6 +62,22 @@ public StringOption Verbosity
}
}

public StringOption StackTraceVerbosity
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved
{
get { return _stackTraceVerbosity; }
set
{
if (_stackTraceVerbosity == null)
{
_stackTraceVerbosity = value;
}
else
{
_stackTraceVerbosity = new StringOption(_stackTraceVerbosity, value?.Value);
}
}
}

private string FixMinimal(string value)
{
return string.Equals(value, "Minimal", StringComparison.OrdinalIgnoreCase) ? "Normal" : value;
Expand Down
100 changes: 81 additions & 19 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ function Write-PesterReport {
& $SafeCommands['Write-Host'] ("Container failed: {0}" -f $RunResult.FailedContainersCount) -Foreground $ReportTheme.Fail
& $SafeCommands['Write-Host'] ($cs -join [Environment]::NewLine) -Foreground $ReportTheme.Fail
}
# & $SafeCommands['Write-Host'] ($ReportStrings.TestsPending -f $RunResult.PendingCount) -Foreground $Pending -NoNewLine
# & $SafeCommands['Write-Host'] ($ReportStrings.TestsInconclusive -f $RunResult.InconclusiveCount) -Foreground $Inconclusive
# & $SafeCommands['Write-Host'] ($ReportStrings.TestsPending -f $RunResult.PendingCount) -Foreground $Pending -NoNewLine
# & $SafeCommands['Write-Host'] ($ReportStrings.TestsInconclusive -f $RunResult.InconclusiveCount) -Foreground $Inconclusive
# }
}

Expand Down Expand Up @@ -398,7 +398,7 @@ function ConvertTo-FailureLines {
[array]::Reverse($exceptionLines)
$lines.Message += $exceptionLines
if ($ErrorRecord.FullyQualifiedErrorId -eq 'PesterAssertionFailed') {
$lines.Message += "at $($ErrorRecord.TargetObject.LineText.Trim()), $($ErrorRecord.TargetObject.File):$($ErrorRecord.TargetObject.Line)".Split([string[]]($([System.Environment]::NewLine), "`n"), [System.StringSplitOptions]::RemoveEmptyEntries)
$lines.Trace += "at $($ErrorRecord.TargetObject.LineText.Trim()), $($ErrorRecord.TargetObject.File):$($ErrorRecord.TargetObject.Line)".Split([string[]]($([System.Environment]::NewLine), "`n"), [System.StringSplitOptions]::RemoveEmptyEntries)
}

if ( -not ($ErrorRecord | & $SafeCommands['Get-Member'] -Name ScriptStackTrace) ) {
Expand All @@ -417,7 +417,7 @@ function ConvertTo-FailureLines {
$traceLines = $ErrorRecord.ScriptStackTrace.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
}

if ($ForceFullError -or $PesterPreference.Debug.ShowFullErrors.Value) {
if ($ForceFullError -or $PesterPreference.Debug.ShowFullErrors.Value -or $PesterPreference.Output.StackTraceVerbosity.Value -eq 'Full') {
$lines.Trace += $traceLines
}
else {
Expand Down Expand Up @@ -514,7 +514,7 @@ function Get-WriteScreenPlugin ($Verbosity) {
}

& $SafeCommands["Write-Host"] -ForegroundColor $ReportTheme.Fail "[-] Discovery in $($path) failed with:"
Write-ErrorToScreen $Context.Block.ErrorRecord
Write-ErrorToScreen $Context.Block.ErrorRecord -StackTraceVerbosity:$PesterPreference.Output.StackTraceVerbosity.Value
}
}

Expand Down Expand Up @@ -574,7 +574,7 @@ function Get-WriteScreenPlugin ($Verbosity) {

if ($Context.Result.ErrorRecord.Count -gt 0) {
& $SafeCommands["Write-Host"] -ForegroundColor $ReportTheme.Fail "[-] $($Context.Result.Item) failed with:"
Write-ErrorToScreen $Context.Result.ErrorRecord
Write-ErrorToScreen $Context.Result.ErrorRecord -StackTraceVerbosity:$PesterPreference.Output.StackTraceVerbosity.Value
}

if ('Normal' -eq $PesterPreference.Output.Verbosity.Value) {
Expand Down Expand Up @@ -636,6 +636,10 @@ function Get-WriteScreenPlugin ($Verbosity) {
throw "Unsupported level out output '$($PesterPreference.Output.Verbosity.Value)'"
}

if ($PesterPreference.Output.StackTraceVerbosity.Value -notin 'None', 'FirstLine', 'Filtered', 'Full') {
throw "Unsupported level of stacktrace output '$($PesterPreference.Output.StackTraceVerbosity.Value)'"
}

$humanTime = "$(Get-HumanTime ($_test.Duration)) ($(Get-HumanTime $_test.UserDuration)|$(Get-HumanTime $_test.FrameworkDuration))"

if ($PesterPreference.Debug.ShowNavigationMarkers.Value) {
Expand All @@ -661,16 +665,16 @@ function Get-WriteScreenPlugin ($Verbosity) {
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.Fail "$margin[-] $out" -NoNewLine
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailTime " $humanTime"

& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailDetail $($e.DisplayStackTrace -replace '(?m)^',$error_margin)
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailDetail $($e.DisplayErrorMessage -replace '(?m)^',$error_margin)
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailDetail $($e.DisplayStackTrace -replace '(?m)^', $error_margin)
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailDetail $($e.DisplayErrorMessage -replace '(?m)^', $error_margin)
}

}
else {
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.Fail "$margin[-] $out" -NoNewLine
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.FailTime " $humanTime"

Write-ErrorToScreen $_test.ErrorRecord -ErrorMargin $error_margin
Write-ErrorToScreen $_test.ErrorRecord -ErrorMargin $error_margin -StackTraceVerbosity:$PesterPreference.Output.StackTraceVerbosity.Value
}
break
}
Expand Down Expand Up @@ -757,7 +761,7 @@ function Get-WriteScreenPlugin ($Verbosity) {

foreach ($e in $Context.Block.ErrorRecord) { ConvertTo-FailureLines $e }
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.BlockFail "[-] $($Context.Block.FrameworkData.CommandUsed) $($Context.Block.Path -join ".") failed"
Write-ErrorToScreen $Context.Block.ErrorRecord $error_margin
Write-ErrorToScreen $Context.Block.ErrorRecord $error_margin -StackTraceVerbosity:$PesterPreference.Output.StackTraceVerbosity.Value
}

$p.End = {
Expand All @@ -769,13 +773,13 @@ function Get-WriteScreenPlugin ($Verbosity) {
New-PluginObject @p
}

function Write-ErrorToScreen {
function Format-ErrorMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$Err,
[string] $ErrorMargin,
[switch] $Throw
[string] $StackTraceVerbosity = [PesterConfiguration]::Default.Output.StackTraceVerbosity.Value
)

$multipleErrors = 1 -lt $Err.Count
Expand All @@ -784,21 +788,79 @@ function Write-ErrorToScreen {
$out = if ($multipleErrors) {
$c = 0
$(foreach ($e in $Err) {
$isFormattedError = $null -ne $e.DisplayErrorMessage
"[$(($c++))] $(if ($isFormattedError){ $e.DisplayErrorMessage } else { $e.Exception })$(if ($null -ne $e.DisplayStackTrace) {"$([Environment]::NewLine)$($e.DisplayStackTrace)"})"
}) -join [Environment]::NewLine
$errorMessageSb = [System.Text.StringBuilder]""

if ($null -ne $e.DisplayErrorMessage) {
[void]$errorMessageSb.Append("[$(($c++))] $($e.DisplayErrorMessage)")
}
else {
[void]$errorMessageSb.Append("[$(($c++))] $($e.Exception)")
}

if ($null -ne $e.DisplayStackTrace -and $StackTraceVerbosity -ne 'None') {
$stackTraceLines = $e.DisplayStackTrace -split [Environment]::NewLine

if ($StackTraceVerbosity -eq 'FirstLine') {
[void]$errorMessageSb.Append([Environment]::NewLine + $stackTraceLines[0])
}
else {
[void]$errorMessageSb.Append([Environment]::NewLine + $e.DisplayStackTrace)
}
}

$errorMessageSb.ToString()
}) -join [Environment]::NewLine
}
else {
$isFormattedError = $null -ne $Err.DisplayErrorMessage
"$(if ($isFormattedError){ $Err.DisplayErrorMessage } else { $Err.Exception })$(if ($isFormattedError) { if ($null -ne $Err.DisplayStackTrace) {"$([Environment]::NewLine)$($Err.DisplayStackTrace)"}} else { if ($null -ne $Err.ScriptStackTrace) {"$([Environment]::NewLine)$($Err.ScriptStackTrace)"}})"
$errorMessageSb = [System.Text.StringBuilder]""

if ($null -ne $Err.DisplayErrorMessage) {
[void]$errorMessageSb.Append($Err.DisplayErrorMessage)

if ($null -ne $Err.DisplayStackTrace -and $StackTraceVerbosity -ne 'None') {
$stackTraceLines = $Err.DisplayStackTrace -split [Environment]::NewLine

if ($StackTraceVerbosity -eq 'FirstLine') {
[void]$errorMessageSb.Append([Environment]::NewLine + $stackTraceLines[0])
}
else {
[void]$errorMessageSb.Append([Environment]::NewLine + $Err.DisplayStackTrace)
}
}
}
else {
[void]$errorMessageSb.Append($Err.Exception.ToString())

if ($null -ne $Err.ScriptStackTrace) {
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved
[void]$errorMessageSb.Append([Environment]::NewLine + $Err.ScriptStackTrace)
}
}

$errorMessageSb.ToString()
}

$withMargin = ($out -split [Environment]::NewLine) -replace '(?m)^', $ErrorMargin -join [Environment]::NewLine

return $withMargin
}

function Write-ErrorToScreen {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$Err,
[string] $ErrorMargin,
[switch] $Throw,
[string] $StackTraceVerbosity = [PesterConfiguration]::Default.Output.StackTraceVerbosity.Value
)

$errorMessage = Format-ErrorMessage -Err $Err -ErrorMargin:$ErrorMargin -StackTraceVerbosity:$StackTraceVerbosity

if ($Throw) {
throw $withMargin
throw $errorMessage
}
else {
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.Fail "$withMargin"
& $SafeCommands['Write-Host'] -ForegroundColor $ReportTheme.Fail "$errorMessage"
}
}

Expand Down
29 changes: 23 additions & 6 deletions tst/Pester.RSpec.Configuration.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ i -PassThru:$PassThru {
b "Default configuration" {

# General configuration
t "Exit is `$false" {
t "Run.Exit is `$false" {
[PesterConfiguration]::Default.Run.Exit.Value | Verify-False
}

t "Path is string array, with '.'" {
t "Run.Path is string array, with '.'" {
$value = [PesterConfiguration]::Default.Run.Path.Value

# do not do $value | Verify-NotNull
Expand All @@ -61,7 +61,7 @@ i -PassThru:$PassThru {
$value[0] | Verify-Equal '.'
}

t "ScriptBlock is empty ScriptBlock array" {
t "Run.ScriptBlock is empty ScriptBlock array" {
$value = [PesterConfiguration]::Default.Run.ScriptBlock.Value

# do not do $value | Verify-NotNull
Expand All @@ -71,22 +71,26 @@ i -PassThru:$PassThru {
$value.Count | Verify-Equal 0
}

t "TestExtension is *.Tests.ps1" {
t "Run.TestExtension is *.Tests.ps1" {
[PesterConfiguration]::Default.Run.TestExtension.Value | Verify-Equal ".Tests.ps1"
}


# Output configuration
t "Verbosity is Normal" {
t "Output.Verbosity is Normal" {
[PesterConfiguration]::Default.Output.Verbosity.Value | Verify-Equal "Normal"
}

t "Verbosity Minimal is translated to Normal (backwards compat for currently unsupported option)" {
t "Output.Verbosity Minimal is translated to Normal (backwards compat for currently unsupported option)" {
$p = [PesterConfiguration]::Default
$p.Output.Verbosity = "Minimal"
$p.Output.Verbosity.Value | Verify-Equal "Normal"
}

t "Output.StackTraceVerbosity is Filtered" {
[PesterConfiguration]::Default.Output.StackTraceVerbosity.Value | Verify-Equal Filtered
}

# CodeCoverage configuration
t "CodeCoverage.Enabled is `$false" {
[PesterConfiguration]::Default.CodeCoverage.Enabled.Value | Verify-False
Expand Down Expand Up @@ -238,38 +242,48 @@ i -PassThru:$PassThru {
t "Configuration can be shallow cloned to avoid modifying user values" {
$user = [PesterConfiguration]::Default
$user.Output.Verbosity = "Normal"
$user.Output.StackTraceVerbosity = "Filtered"

$cloned = [PesterConfiguration]::ShallowClone($user)
$cloned.Output.Verbosity = "None"
$cloned.Output.StackTraceVerbosity = "None"

$user.Output.Verbosity.Value | Verify-Equal "Normal"
$user.Output.StackTraceVerbosity.Value | Verify-Equal "Filtered"

$cloned.Output.Verbosity.Value | Verify-Equal "None"
$cloned.Output.StackTraceVerbosity.Value | Verify-Equal "None"
}
}

b "Merging" {
t "configurations can be merged" {
$user = [PesterConfiguration]::Default
$user.Output.Verbosity = "Normal"
$user.Output.StackTraceVerbosity = "Filtered"
$user.Filter.Tag = "abc"

$override = [PesterConfiguration]::Default
$override.Output.Verbosity = "None"
$override.Output.StackTraceVerbosity = "None"
$override.Run.Path = "C:\test.ps1"

$result = [PesterConfiguration]::Merge($user, $override)

$result.Output.Verbosity.Value | Verify-Equal "None"
$result.Output.StackTraceVerbosity.Value | Verify-Equal "None"
$result.Run.Path.Value | Verify-Equal "C:\test.ps1"
$result.Filter.Tag.Value | Verify-Equal "abc"
}

t "merged object is a new instance" {
$user = [PesterConfiguration]::Default
$user.Output.Verbosity = "Normal"
$user.Output.StackTraceVerbosity = "Filtered"

$override = [PesterConfiguration]::Default
$override.Output.Verbosity = "None"
$override.Output.StackTraceVerbosity = "None"

$result = [PesterConfiguration]::Merge($user, $override)

Expand All @@ -280,15 +294,18 @@ i -PassThru:$PassThru {
t "values are overwritten even if they are set to the same value as default" {
$user = [PesterConfiguration]::Default
$user.Output.Verbosity = "Diagnostic"
$user.Output.StackTraceVerbosity = "Full"
$user.Filter.Tag = "abc"

$override = [PesterConfiguration]::Default
$override.Output.Verbosity = [PesterConfiguration]::Default.Output.Verbosity
$override.Output.StackTraceVerbosity = [PesterConfiguration]::Default.Output.StackTraceVerbosity

$result = [PesterConfiguration]::Merge($user, $override)

# has the same value as default but was written so it will override
$result.Output.Verbosity.Value | Verify-Equal "Normal"
$result.Output.StackTraceVerbosity.Value | Verify-Equal "Filtered"
# has value different from default but was not written in override so the
# override does not touch it
$result.Filter.Tag.Value | Verify-Equal "abc"
Expand Down