Skip to content

Commit 17fafc6

Browse files
nohwndCopilot
andcommitted
Fix #2682: Auto-enable TestResult/CodeCoverage when non-default options are set
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8aaf860 commit 17fafc6

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

src/Main.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ function Invoke-Pester {
702702

703703
& $SafeCommands['Get-Variable'] 'Configuration' -Scope Local | Remove-Variable
704704

705+
Resolve-AutoEnabledConfiguration -PesterPreference $PesterPreference
706+
705707
# $sessionState = Set-SessionStateHint -PassThru -Hint "Caller - Captured in Invoke-Pester" -SessionState $PSCmdlet.SessionState
706708
$sessionState = $PSCmdlet.SessionState
707709

@@ -1092,6 +1094,13 @@ function Convert-PesterLegacyParameterSet ($BoundParameters) {
10921094
return $Configuration
10931095
}
10941096

1097+
function Resolve-AutoEnabledConfiguration {
1098+
param ([PesterConfiguration] $PesterPreference)
1099+
1100+
$PesterPreference.CodeCoverage.ResolveEnabled()
1101+
$PesterPreference.TestResult.ResolveEnabled()
1102+
}
1103+
10951104

10961105
function ConvertTo-Pester4Result {
10971106
<#

src/csharp/Pester/ConfigurationSection.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// to have in "type accelerator" form, but without the hassle of actually adding it as a type accelerator
1616
// that way you can easily do `[PesterConfiguration]::Default` and then inspect it, or cast a hashtable to it
1717

18+
using System.Reflection;
19+
1820
namespace Pester
1921
{
2022
public abstract class ConfigurationSection
@@ -29,5 +31,36 @@ public override string ToString()
2931
{
3032
return _description;
3133
}
34+
35+
/// <summary>
36+
/// If this section has an Enabled option that was not explicitly modified,
37+
/// and any other option in the section was modified, auto-enable the section.
38+
/// </summary>
39+
public void ResolveEnabled()
40+
{
41+
var enabledProperty = GetType().GetProperty("Enabled", BindingFlags.Public | BindingFlags.Instance);
42+
if (enabledProperty == null || enabledProperty.PropertyType != typeof(BoolOption))
43+
return;
44+
45+
var enabled = (BoolOption)enabledProperty.GetValue(this);
46+
if (enabled == null || enabled.IsModified)
47+
return;
48+
49+
foreach (var property in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
50+
{
51+
if (property.Name == "Enabled")
52+
continue;
53+
54+
if (!typeof(Option).IsAssignableFrom(property.PropertyType))
55+
continue;
56+
57+
var option = (Option)property.GetValue(this);
58+
if (option != null && option.IsModified)
59+
{
60+
enabledProperty.SetValue(this, (BoolOption)true);
61+
return;
62+
}
63+
}
64+
}
3265
}
3366
}

tst/Pester.RSpec.Configuration.ts.ps1

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,150 @@ i -PassThru:$PassThru {
583583
}
584584
}
585585

586+
b "auto-enabling output features" {
587+
t "CodeCoverage.Path auto-enables code coverage" {
588+
$coverageScript = "$PSScriptRoot/CoverageTestFile.ps1"
589+
$coverageOutputPath = [IO.Path]::GetTempFileName()
590+
Remove-Item $coverageOutputPath -Force
591+
592+
try {
593+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
594+
Run = @{
595+
ScriptBlock = {
596+
Describe "auto coverage" {
597+
It "covers script" {
598+
. $coverageScript
599+
}
600+
}
601+
}.GetNewClosure()
602+
PassThru = $true
603+
}
604+
Output = @{
605+
Verbosity = 'None'
606+
}
607+
CodeCoverage = @{
608+
Path = $coverageScript
609+
OutputPath = $coverageOutputPath
610+
}
611+
})
612+
613+
$r.CodeCoverage | Verify-NotNull
614+
(Test-Path $coverageOutputPath) | Verify-True
615+
}
616+
finally {
617+
if (Test-Path $coverageOutputPath) {
618+
Remove-Item $coverageOutputPath -Force -ErrorAction Ignore
619+
}
620+
}
621+
}
622+
623+
t "CodeCoverage.Enabled = `$false still disables code coverage" {
624+
$coverageScript = "$PSScriptRoot/CoverageTestFile.ps1"
625+
$coverageOutputPath = [IO.Path]::GetTempFileName()
626+
Remove-Item $coverageOutputPath -Force
627+
628+
try {
629+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
630+
Run = @{
631+
ScriptBlock = {
632+
Describe "disabled coverage" {
633+
It "does not export coverage" {
634+
. $coverageScript
635+
}
636+
}
637+
}.GetNewClosure()
638+
PassThru = $true
639+
}
640+
Output = @{
641+
Verbosity = 'None'
642+
}
643+
CodeCoverage = @{
644+
Enabled = $false
645+
Path = $coverageScript
646+
OutputPath = $coverageOutputPath
647+
}
648+
})
649+
650+
$r.CodeCoverage | Verify-Null
651+
(Test-Path $coverageOutputPath) | Verify-False
652+
}
653+
finally {
654+
if (Test-Path $coverageOutputPath) {
655+
Remove-Item $coverageOutputPath -Force -ErrorAction Ignore
656+
}
657+
}
658+
}
659+
660+
t "TestResult.OutputPath auto-enables test result export" {
661+
$testResultOutputPath = [IO.Path]::GetTempFileName()
662+
Remove-Item $testResultOutputPath -Force
663+
664+
try {
665+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
666+
Run = @{
667+
ScriptBlock = {
668+
Describe "auto testresult" {
669+
It "exports xml" {
670+
$true | Should -Be $true
671+
}
672+
}
673+
}
674+
PassThru = $true
675+
}
676+
Output = @{
677+
Verbosity = 'None'
678+
}
679+
TestResult = @{
680+
OutputPath = $testResultOutputPath
681+
}
682+
})
683+
684+
$r.Result | Verify-Equal 'Passed'
685+
(Test-Path $testResultOutputPath) | Verify-True
686+
}
687+
finally {
688+
if (Test-Path $testResultOutputPath) {
689+
Remove-Item $testResultOutputPath -Force -ErrorAction Ignore
690+
}
691+
}
692+
}
693+
694+
t "TestResult.Enabled = `$false still disables test result export" {
695+
$testResultOutputPath = [IO.Path]::GetTempFileName()
696+
Remove-Item $testResultOutputPath -Force
697+
698+
try {
699+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
700+
Run = @{
701+
ScriptBlock = {
702+
Describe "disabled testresult" {
703+
It "does not export xml" {
704+
$true | Should -Be $true
705+
}
706+
}
707+
}
708+
PassThru = $true
709+
}
710+
Output = @{
711+
Verbosity = 'None'
712+
}
713+
TestResult = @{
714+
Enabled = $false
715+
OutputPath = $testResultOutputPath
716+
}
717+
})
718+
719+
$r.Result | Verify-Equal 'Passed'
720+
(Test-Path $testResultOutputPath) | Verify-False
721+
}
722+
finally {
723+
if (Test-Path $testResultOutputPath) {
724+
Remove-Item $testResultOutputPath -Force -ErrorAction Ignore
725+
}
726+
}
727+
}
728+
}
729+
586730
b "configuration modified at runtime" {
587731
t "changes at runtime doesn't leak to advanced configuration object" {
588732
$c = [PesterConfiguration] @{

0 commit comments

Comments
 (0)