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

Add option to Invoke-Pester to filter output #647

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Functions/Output.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
InModuleScope -ModuleName Pester -ScriptBlock {
Describe 'Has-Flag' -Fixture {
It 'Returns true when setting and value are the same' {
$setting = [Pester.OutputTypes]::Passed
$value = [Pester.OutputTypes]::Passed

$value | Has-Flag $setting | Should Be $true
}

It 'Returns false when setting and value are the different' {
$setting = [Pester.OutputTypes]::Passed
$value = [Pester.OutputTypes]::Failed

$value | Has-Flag $setting | Should Be $false
}

It 'Returns true when setting contains value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Passed

$value | Has-Flag $setting | Should Be $true
}

It 'Returns false when setting does not contain the value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary

$value | Has-Flag $setting | Should Be $false
}

It 'Returns true when at least one setting is contained in value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary -bor [Pester.OutputTypes]::Failed

$value | Has-Flag $setting | Should Be $true
}

It 'Returns false when none of settings is contained in value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary -bor [Pester.OutputTypes]::Describe

$value | Has-Flag $setting | Should Be $false
}
}

Describe 'Default OutputTypes' -Fixture {
It 'Fails output type contains all except passed' {
$expected = [Pester.OutputTypes]'Default, Failed, Pending, Skipped, Inconclusive, Describe, Context, Summary'
[Pester.OutputTypes]::Fails | Should Be $expected
}

It 'All output type contains all flags' {
$expected = [Pester.OutputTypes]'Default, Passed, Failed, Pending, Skipped, Inconclusive, Describe, Context, Summary'
[Pester.OutputTypes]::All | Should Be $expected
}
}
}
55 changes: 31 additions & 24 deletions Functions/PesterState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function New-PesterState
[String[]]$TestNameFilter,
[System.Management.Automation.SessionState]$SessionState,
[Switch]$Strict,
[Switch]$Quiet,
[Pester.OutputTypes]$Show = 'All',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe by default show only summary (and there add an information for user how get full output)? Or only failed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov This is for compatibility reasons. Changing this from All to something else would be a breaking change for users.

[object]$PesterOption
)

Expand Down Expand Up @@ -35,7 +35,7 @@ function New-PesterState
[String[]]$_testNameFilter,
[System.Management.Automation.SessionState]$_sessionState,
[Switch]$Strict,
[Switch]$Quiet,
[Pester.OutputTypes]$Show,
[object]$PesterOption
)

Expand All @@ -56,7 +56,7 @@ function New-PesterState
$script:BeforeAll = @()
$script:AfterAll = @()
$script:Strict = $Strict
$script:Quiet = $Quiet
$script:Show = $Show

$script:TestResult = @()

Expand Down Expand Up @@ -223,7 +223,7 @@ function New-PesterState
"BeforeAll",
"AfterAll",
"Strict",
"Quiet",
"Show",
"Time",
"TotalCount",
"PassedCount",
Expand All @@ -242,7 +242,7 @@ function New-PesterState
"AddTestResult"

& $SafeCommands['Export-ModuleMember'] -Variable $ExportedVariables -function $ExportedFunctions
} -ArgumentList $TagFilter, $ExcludeTagFilter, $TestNameFilter, $SessionState, $Strict, $Quiet, $PesterOption |
} -ArgumentList $TagFilter, $ExcludeTagFilter, $TestNameFilter, $SessionState, $Strict, $Show, $PesterOption |
& $SafeCommands['Add-Member'] -MemberType ScriptProperty -Name Scope -Value {
if ($this.CurrentTest) { 'It' }
elseif ($this.CurrentContext) { 'Context' }
Expand Down Expand Up @@ -273,7 +273,7 @@ function Write-Describe
[Parameter(mandatory=$true, valueFromPipeline=$true)]$Name
)
process {
Write-Screen Describing $Name -OutputType Header
Write-Screen Describing $Name -OutputType Describe
}
}

Expand All @@ -284,7 +284,7 @@ function Write-Context
)
process {
$margin = " " * 3
Write-Screen ${margin}Context $Name -OutputType Header
Write-Screen ${margin}Context $Name -OutputType Context
}
}

Expand Down Expand Up @@ -426,13 +426,13 @@ function Write-PesterReport
$PesterState
)

Write-Screen "Tests completed in $(Get-HumanTime $PesterState.Time.TotalSeconds)"
Write-Screen "Tests completed in $(Get-HumanTime $PesterState.Time.TotalSeconds)" -OutputType "Summary"
Write-Screen ("Passed: {0} Failed: {1} Skipped: {2} Pending: {3} Inconclusive: {4}" -f
$PesterState.PassedCount,
$PesterState.FailedCount,
$PesterState.SkippedCount,
$PesterState.PendingCount,
$PesterState.InconclusiveCount)
$PesterState.InconclusiveCount) -OutputType "Summary"
}

function Write-Screen {
Expand All @@ -444,29 +444,33 @@ function Write-Screen {
[Switch] $NoNewline,
[Object] $Separator,
#custom parameters
[Switch] $Quiet = $pester.Quiet,
[ValidateSet("Failed","Passed","Skipped","Pending","Inconclusive","Header","Standard")]
[String] $OutputType = "Standard"
[Pester.OutputTypes] $OutputFilter = $pester.Show,
[Pester.OutputTypes] $OutputType = 'Default'
)

begin
{
if ($Quiet) { return }
$quiet = $OutputFilter -eq [Pester.OutputTypes]::None
$writeToScreen = $OutputFilter | Has-Flag $OutputType
$skipOutput = $quiet -or (-not $writeToScreen)

if ($skipOutput)
{
return
}

#make the bound parameters compatible with Write-Host
if ($PSBoundParameters.ContainsKey('Quiet')) { $PSBoundParameters.Remove('Quiet') | & $SafeCommands['Out-Null'] }
if ($PSBoundParameters.ContainsKey('OutputFilter')) { $PSBoundParameters.Remove('OutputFilter') | & $SafeCommands['Out-Null'] }
if ($PSBoundParameters.ContainsKey('OutputType')) { $PSBoundParameters.Remove('OutputType') | & $SafeCommands['Out-Null'] }

if ($OutputType -ne "Standard")
if (-not ($OutputType | Has-Flag 'Default, Summary'))
{
#create the key first to make it work in strict mode
if (-not $PSBoundParameters.ContainsKey('ForegroundColor'))
{
$PSBoundParameters.Add('ForegroundColor', $null)
}



switch ($Host.Name)
{
#light background
Expand All @@ -477,7 +481,8 @@ function Write-Screen {
Skipped = [ConsoleColor]::DarkGray
Pending = [ConsoleColor]::DarkCyan
Inconclusive = [ConsoleColor]::DarkCyan
Header = [ConsoleColor]::Magenta
Describe = [ConsoleColor]::Magenta
Context = [ConsoleColor]::Magenta
}
}
#dark background
Expand All @@ -488,7 +493,8 @@ function Write-Screen {
Skipped = [ConsoleColor]::Gray
Pending = [ConsoleColor]::Cyan
Inconclusive = [ConsoleColor]::Cyan
Header = [ConsoleColor]::Magenta
Describe = [ConsoleColor]::Magenta
Context = [ConsoleColor]::Magenta
}
}
default {
Expand All @@ -498,14 +504,15 @@ function Write-Screen {
Skipped = [ConsoleColor]::Gray
Pending = [ConsoleColor]::Gray
Inconclusive = [ConsoleColor]::Gray
Header = [ConsoleColor]::Magenta
Describe = [ConsoleColor]::Magenta
Context = [ConsoleColor]::Magenta
}
}

}


$PSBoundParameters.ForegroundColor = $ColorSet.$OutputType
# the output type must be forced to become string, otherwise the color is not found
$PSBoundParameters.ForegroundColor = $ColorSet[$OutputType.ToString()]
}

try {
Expand All @@ -525,7 +532,7 @@ function Write-Screen {

process
{
if ($Quiet) { return }
if ($skipOutput) { return }
try {
$steppablePipeline.Process($_)
} catch {
Expand All @@ -535,7 +542,7 @@ function Write-Screen {

end
{
if ($Quiet) { return }
if ($skipOutput) { return }
try {
$steppablePipeline.End()
} catch {
Expand Down
47 changes: 45 additions & 2 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ $moduleRoot = & $script:SafeCommands['Split-Path'] -Path $MyInvocation.MyCommand
& $script:SafeCommands['Where-Object'] { -not ($_.ProviderPath.ToLower().Contains(".tests.")) } |
& $script:SafeCommands['ForEach-Object'] { . $_.ProviderPath }

Add-Type -TypeDefinition @"
using System;

namespace Pester
{
[Flags]
public enum OutputTypes
{
None = 0,
Default = 1,
Passed = 2,
Failed = 4,
Pending = 8,
Skipped = 16,
Inconclusive = 32,
Describe = 64,
Context = 128,
Summary = 256,
All = Default | Passed | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary,
Fails = Default | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary
}
}
"@

function Has-Flag {
param
(
[Parameter(Mandatory = $true)]
$Setting,
[Parameter(Mandatory = $true, ValueFromPipeline=$true)]
$Value
)

0 -ne ($Setting -band $Value)
}

function Invoke-Pester {
<#
.SYNOPSIS
Expand Down Expand Up @@ -419,7 +455,9 @@ New-PesterOption

[Switch]$Quiet,

[object]$PesterOption
[object]$PesterOption,

[Pester.OutputTypes]$Show = 'All'
)

if ($PSBoundParameters.ContainsKey('OutputXml'))
Expand All @@ -434,7 +472,12 @@ New-PesterOption

$script:mockTable = @{}

$pester = New-PesterState -TestNameFilter $TestName -TagFilter ($Tag -split "\s") -ExcludeTagFilter ($ExcludeTag -split "\s") -SessionState $PSCmdlet.SessionState -Strict:$Strict -Quiet:$Quiet -PesterOption $PesterOption
if ($Quiet)
{
$Show = [Pester.OutputTypes]::None
}

$pester = New-PesterState -TestNameFilter $TestName -TagFilter ($Tag -split "\s") -ExcludeTagFilter ($ExcludeTag -split "\s") -SessionState $PSCmdlet.SessionState -Strict:$Strict -Show:$Show -PesterOption $PesterOption
Enter-CoverageAnalysis -CodeCoverage $CodeCoverage -PesterState $pester

Write-Screen "`r`n`r`n`r`n`r`n"
Expand Down