@@ -47,36 +47,100 @@ param(
47
47
# If specified, executes all the tests in the specified test script.
48
48
[Parameter ()]
49
49
[switch ]
50
- $All
50
+ $All ,
51
+
52
+ [Parameter ()]
53
+ [switch ] $MinimumVersion5 ,
54
+
55
+ [Parameter (Mandatory )]
56
+ [string ] $Output
51
57
)
52
58
53
59
$pesterModule = Microsoft.PowerShell.Core\Get-Module Pester
60
+ # add one line, so the subsequent output is not shifted to the side
61
+ Write-Output ' '
54
62
if (! $pesterModule ) {
55
63
Write-Output " Importing Pester module..."
56
- $pesterModule = Microsoft.PowerShell.Core\Import-Module Pester - ErrorAction Ignore - PassThru
64
+ $minimumVersion = if ($MinimumVersion5 ) { " 5.0.0" } else { " 0.0.0" }
65
+ $versionMessage = " version $minimumVersion "
66
+ $pesterModule = Microsoft.PowerShell.Core\Import-Module Pester - ErrorAction Ignore - PassThru - MinimumVersion $minimumVersion
57
67
if (! $pesterModule ) {
58
68
# If we still don't have an imported Pester module, that is (most likely) because Pester is not installed.
59
- Write-Warning " Failed to import the Pester module . You must install Pester to run or debug Pester tests."
60
- Write-Warning " You can install Pester by executing: Install-Module Pester -Scope CurrentUser -Force"
69
+ Write-Warning " Failed to import Pester$ ( if ( $MinimumVersion5 ){ $versionMessage } ) . You must install Pester module to run or debug Pester tests."
70
+ Write-Warning " You can install Pester by executing: Install-Module Pester $ ( if ( $MinimumVersion5 ) { " -MinimumVersion 5.0.0 " } ) -Scope CurrentUser -Force"
61
71
return
62
72
}
63
73
}
64
74
65
- if ($All ) {
66
- Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true }
75
+ $pester4Output = switch ($Output ) {
76
+ " None" { " None" }
77
+ " Minimal" { " Fails" }
78
+ default { " All" }
67
79
}
68
- elseif ($TestName ) {
69
- Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true } - TestName $TestName
80
+
81
+ if ($MinimumVersion5 -and $pesterModule.Version -lt " 5.0.0" ) {
82
+ Write-Warning " Pester 5.0.0 or newer is required because setting PowerShell > Pester: Enable Legacy Code Lens is disabled, but Pester $ ( $pesterModule.Version ) is loaded. Some of the code lense features might not work as expected."
83
+ }
84
+
85
+
86
+ if ($All ) {
87
+ if ($pesterModule.Version -ge ' 5.0.0' ) {
88
+ $configuration = @ {
89
+ Run = @ {
90
+ Path = $ScriptPath
91
+ }
92
+ }
93
+ # only override this if user asks us to do it, to allow Pester to pick up
94
+ # $PesterPreference from caller context and merge it with the configuration
95
+ # we provide below, this way user can specify his output (and other) settings
96
+ # using the standard [PesterConfiguration] object, and we can avoid providing
97
+ # settings for everything
98
+ if (" FromPreference" -ne $Output ) {
99
+ $configuration.Add (' Output' , @ { Verbosity = $Output })
100
+ }
101
+ Pester\Invoke-Pester - Configuration $configuration | Out-Null
102
+ }
103
+ else {
104
+ Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true } - Show $pester4Output
105
+ }
70
106
}
71
107
elseif (($LineNumber -match ' \d+' ) -and ($pesterModule.Version -ge ' 4.6.0' )) {
72
- Pester\Invoke-Pester - Script $ScriptPath - PesterOption (New-PesterOption - ScriptBlockFilter @ {
73
- IncludeVSCodeMarker = $true ; Line = $LineNumber ; Path = $ScriptPath })
108
+ if ($pesterModule.Version -ge ' 5.0.0' ) {
109
+ $configuration = @ {
110
+ Run = @ {
111
+ Path = $ScriptPath
112
+ }
113
+ Filter = @ {
114
+ Line = " ${ScriptPath} :$LineNumber "
115
+ }
116
+ }
117
+ if (" FromPreference" -ne $Output ) {
118
+ $configuration.Add (' Output' , @ { Verbosity = $Output })
119
+ }
120
+ Pester\Invoke-Pester - Configuration $configuration | Out-Null
121
+ }
122
+ else {
123
+ Pester\Invoke-Pester - Script $ScriptPath - PesterOption (New-PesterOption - ScriptBlockFilter @ {
124
+ IncludeVSCodeMarker = $true ; Line = $LineNumber ; Path = $ScriptPath }) - Show $pester4Output
125
+ }
126
+ }
127
+ elseif ($TestName ) {
128
+ if ($pesterModule.Version -ge ' 5.0.0' ) {
129
+ throw " Running tests by test name is unsafe. This should not trigger for Pester 5."
130
+ }
131
+ else {
132
+ Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true } - TestName $TestName - Show $pester4Output
133
+ }
74
134
}
75
135
else {
136
+ if ($pesterModule.Version -ge ' 5.0.0' ) {
137
+ throw " Running tests by expandable string is unsafe. This should not trigger for Pester 5."
138
+ }
139
+
76
140
# We get here when the TestName expression is of type ExpandableStringExpressionAst.
77
141
# PSES will not attempt to "evaluate" the expression so it returns null for the TestName.
78
142
Write-Warning " The Describe block's TestName cannot be evaluated. EXECUTING ALL TESTS instead."
79
143
Write-Warning " To avoid this, install Pester >= 4.6.0 or remove any expressions in the TestName."
80
144
81
- Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true }
145
+ Pester\Invoke-Pester - Script $ScriptPath - PesterOption @ {IncludeVSCodeMarker = $true } - Show $pester4Output
82
146
}
0 commit comments