forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPester.RSpec.ps1
584 lines (494 loc) · 19.3 KB
/
Pester.RSpec.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
function Find-File {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String[]] $Path,
[String[]] $ExcludePath,
[Parameter(Mandatory = $true)]
[string] $Extension
)
$files =
foreach ($p in $Path) {
if ([String]::IsNullOrWhiteSpace($p)) {
continue
}
if ((& $script:SafeCommands['Test-Path'] $p)) {
# This can expand to more than one path when wildcard is used, those paths can be folders or files.
# We want to avoid expanding to paths that are not matching our filters, but also want to ensure that if
# user passes in MyTestFile.ps1 without the .Tests.ps1 it will still run.
# So at this step we look if we expanded the path to more than 1 item and use stricter rules with filtering.
# Or if the file was just a single file, we won't use stricter filtering for files.
# This allows us to use wildcards to get all .Tests.ps1 in the folder and all child folders, which is very useful.
# But prevents a rare scenario where you provide C:\files\*\MyTest.ps1, because in that case only .Tests.ps1 would be included.
$items = & $SafeCommands['Get-Item'] $p
$resolvedToMultipleFiles = $null -ne $items -and 1 -lt @($items).Length
foreach ($item in $items) {
if ($item.PSIsContainer) {
# this is an existing directory search it for tests file
& $SafeCommands['Get-ChildItem'] -Recurse -Path $item -Filter "*$Extension" -File
}
elseif ("FileSystem" -ne $item.PSProvider.Name) {
# item is not a directory and exists but is not a file so we are not interested
}
elseif ($resolvedToMultipleFiles) {
# item was resolved from a wildcarded path only use it if it has test extension
if ($item.FullName -like "*$Extension") {
# add unresolved path to have a note of the original path used to resolve this
& $SafeCommands['Add-Member'] -Name UnresolvedPath -Type NoteProperty -Value $p -InputObject $item
$item
}
}
else {
# this is some file, that was either provided directly, or resolved from wildcarded path as a single item,
# we don't care what type of file it is, or if it has test extension (.Tests.ps1) we should try to run it
# to allow any file that is provided directly to run
if (".ps1" -ne $item.Extension) {
& $SafeCommands['Write-Error'] "Script path '$item' is not a ps1 file." -ErrorAction Stop
}
# add unresolved path to have a note of the original path used to resolve this
& $SafeCommands['Add-Member'] -Name UnresolvedPath -Type NoteProperty -Value $p -InputObject $item
$item
}
}
}
else {
# this is a path that does not exist so let's hope it is
# a wildcarded path that will resolve to some files
& $SafeCommands['Get-ChildItem'] -Recurse -Path $p -Filter "*$Extension" -File
}
}
Filter-Excluded -Files $files -ExcludePath $ExcludePath | & $SafeCommands['Where-Object'] { $_ }
}
function Filter-Excluded ($Files, $ExcludePath) {
if ($null -eq $ExcludePath -or @($ExcludePath).Length -eq 0) {
return @($Files)
}
foreach ($file in @($Files)) {
# normalize backslashes for cross-platform ease of use
$p = $file.FullName -replace "/", "\"
$excluded = $false
foreach ($exclusion in (@($ExcludePath) -replace "/", "\")) {
if ($excluded) {
continue
}
if ($p -like $exclusion) {
$excluded = $true
}
}
if (-not $excluded) {
$file
}
}
}
function Add-RSpecTestObjectProperties {
param ($TestObject)
# adds properties that are specific to RSpec to the result object
# this includes figuring out the result
# formatting the failure message and stacktrace
$discoveryOnly = $PesterPreference.Run.SkipRun.Value
$TestObject.Result = if ($TestObject.Skipped) {
"Skipped"
}
elseif ($TestObject.Inconclusive) {
"Inconclusive"
}
elseif ($TestObject.Passed) {
"Passed"
}
elseif (-not $discoveryOnly -and $TestObject.ShouldRun -and (-not $TestObject.Executed -or -not $TestObject.Passed)) {
"Failed"
}
elseif ($discoveryOnly -and 0 -lt $TestObject.ErrorRecord.Count) {
"Failed"
}
else {
"NotRun"
}
foreach ($e in $TestObject.ErrorRecord) {
$r = ConvertTo-FailureLines $e
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayErrorMessage", [string]($r.Message -join [Environment]::NewLine)))
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayStackTrace", [string]($r.Trace -join [Environment]::NewLine)))
}
}
function Add-RSpecBlockObjectProperties ($BlockObject) {
foreach ($e in $BlockObject.ErrorRecord) {
$r = ConvertTo-FailureLines $e
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayErrorMessage", [string]($r.Message -join [Environment]::NewLine)))
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayStackTrace", [string]($r.Trace -join [Environment]::NewLine)))
}
}
function PostProcess-RspecTestRun ($TestRun) {
$discoveryOnly = $PesterPreference.Run.SkipRun.Value
Fold-Run $Run -OnTest {
param($t)
## decorate
# we already added the RSpec properties as part of the plugin
### summarize
$TestRun.Tests.Add($t)
switch ($t.Result) {
"NotRun" {
$null = $TestRun.NotRun.Add($t)
}
"Passed" {
$null = $TestRun.Passed.Add($t)
}
"Failed" {
$null = $TestRun.Failed.Add($t)
}
"Skipped" {
$null = $TestRun.Skipped.Add($t)
}
"Inconclusive" {
$null = $TestRun.Inconclusive.Add($t)
}
default { throw "Result $($t.Result) is not supported." }
}
} -OnBlock {
param ($b)
## decorate
# we already processed errors in the plugin step to make the available for reporting
$b.Result = if ($b.Skip) {
"Skipped"
}
elseif ($b.Passed) {
"Passed"
}
elseif (-not $discoveryOnly -and $b.ShouldRun -and (-not $b.Executed -or -not $b.Passed)) {
"Failed"
}
elseif ($discoveryOnly -and 0 -lt $b.ErrorRecord.Count) {
"Failed"
}
else {
"NotRun"
}
## summarize
# a block that has errors would write into failed blocks so we can report them
# later we can filter this to only report errors from AfterAll
if (0 -lt $b.ErrorRecord.Count) {
$TestRun.FailedBlocks.Add($b)
}
} -OnContainer {
param ($b)
## decorate
# here we add result
$b.result = if ($b.Skip) {
"Skipped"
}
elseif ($b.Passed) {
"Passed"
}
elseif (0 -lt $b.ErrorRecord.Count) {
"Failed"
}
elseif (-not $discoveryOnly -and $b.ShouldRun -and (-not $b.Executed -or -not $b.Passed)) {
"Failed"
}
else {
"NotRun"
}
foreach ($e in $b.ErrorRecord) {
$r = ConvertTo-FailureLines $e
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayErrorMessage", [string]($r.Message -join [Environment]::NewLine)))
$e.PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty("DisplayStackTrace", [string]($r.Trace -join [Environment]::NewLine)))
}
## summarize
if (0 -lt $b.ErrorRecord.Count) {
$TestRun.FailedContainers.Add($b)
}
$TestRun.Duration += $b.Duration
$TestRun.UserDuration += $b.UserDuration
$TestRun.FrameworkDuration += $b.FrameworkDuration
$TestRun.DiscoveryDuration += $b.DiscoveryDuration
}
$TestRun.PassedCount = $TestRun.Passed.Count
$TestRun.FailedCount = $TestRun.Failed.Count
$TestRun.SkippedCount = $TestRun.Skipped.Count
$TestRun.InconclusiveCount = $TestRun.Inconclusive.Count
$TestRun.NotRunCount = $TestRun.NotRun.Count
$TestRun.TotalCount = $TestRun.Tests.Count
$TestRun.FailedBlocksCount = $TestRun.FailedBlocks.Count
$TestRun.FailedContainersCount = $TestRun.FailedContainers.Count
$TestRun.Result = if (0 -lt ($TestRun.FailedCount + $TestRun.FailedBlocksCount + $TestRun.FailedContainersCount)) {
"Failed"
}
else {
"Passed"
}
}
function Get-RSpecObjectDecoratorPlugin () {
New-PluginObject -Name "RSpecObjectDecoratorPlugin" `
-EachTestTeardownEnd {
param ($Context)
# TODO: consider moving this into the core if those results are just what we need, but look first at Gherkin and how many of those results are RSpec specific and how many are Gherkin specific
#TODO: also this is a plugin because it needs to run before the error processing kicks in, this mixes concerns here imho, and needs to be revisited, because the error writing logic is now dependent on this plugin
Add-RSpecTestObjectProperties $Context.Test
} -EachBlockTeardownEnd {
param($Context)
#TODO: also this is a plugin because it needs to run before the error processing kicks in (to be able to report correctly formatted errors on screen in case teardown failure), this mixes concerns here imho, and needs to be revisited, because the error writing logic is now dependent on this plugin
Add-RSpecBlockObjectProperties $Context.Block
}
}
function New-PesterConfiguration {
<#
.SYNOPSIS
Creates a new PesterConfiguration object for advanced configuration of Invoke-Pester.
.DESCRIPTION
The New-PesterConfiguration function creates a new PesterConfiguration-object
to enable advanced configurations for runnings tests using Invoke-Pester.
Without parameters, the function generates a configuration-object with default
options. The returned PesterConfiguration-object can be modified to suit your
requirements.
Calling New-PesterConfiguration is equivalent to calling [PesterConfiguration]::Default which was used in early versions of Pester 5.
For a complete list of options, see `Get-Help about_PesterConfiguration` or https://pester.dev/docs/usage/configuration
.PARAMETER Hashtable
Override the default values for the options defined in the provided dictionary/hashtable.
See about_PesterConfiguration help topic or inspect a PesterConfiguration-object to learn about the schema and
available options.
.EXAMPLE
```powershell
$config = New-PesterConfiguration
$config.Run.PassThru = $true
Invoke-Pester -Configuration $config
```
Creates a default PesterConfiguration-object and changes the Run.PassThru option
to return the result object after the test run. The configuration object is
provided to Invoke-Pester to alter the default behaviour.
.EXAMPLE
```powershell
$MyOptions = @{
Run = @{ # Run configuration.
PassThru = $true # Return result object after finishing the test run.
}
Filter = @{ # Filter configuration
Tag = "Core","Integration" # Run only Describe/Context/It-blocks with 'Core' or 'Integration' tags
}
}
$config = New-PesterConfiguration -Hashtable $MyOptions
Invoke-Pester -Configuration $config
```
A hashtable is created with custom options and passed to the New-PesterConfiguration to merge
with the default configuration. The options in the hashtable will override the default values.
The configuration object is then provided to Invoke-Pester to begin the test run using
the new configuration.
.LINK
https://pester.dev/docs/commands/New-PesterConfiguration
.LINK
https://pester.dev/docs/usage/Configuration
.LINK
https://pester.dev/docs/commands/Invoke-Pester
.LINK
about_PesterConfiguration
#>
[CmdletBinding()]
param(
[System.Collections.IDictionary] $Hashtable
)
if ($PSBoundParameters.ContainsKey('Hashtable')) {
[PesterConfiguration]$Hashtable
}
else {
[PesterConfiguration]::Default
}
}
function Remove-RSpecNonPublicProperties ($run) {
# $runProperties = @(
# 'Configuration'
# 'Containers'
# 'ExecutedAt'
# 'FailedBlocksCount'
# 'FailedCount'
# 'NotRunCount'
# 'PassedCount'
# 'PSBoundParameters'
# 'Result'
# 'SkippedCount'
# 'TotalCount'
# 'Duration'
# )
# $containerProperties = @(
# 'Blocks'
# 'Content'
# 'ErrorRecord'
# 'Executed'
# 'ExecutedAt'
# 'FailedCount'
# 'NotRunCount'
# 'PassedCount'
# 'Result'
# 'ShouldRun'
# 'Skip'
# 'SkippedCount'
# 'Duration'
# 'Type' # needed because of nunit export path expansion
# 'TotalCount'
# )
# $blockProperties = @(
# 'Blocks'
# 'ErrorRecord'
# 'Executed'
# 'ExecutedAt'
# 'FailedCount'
# 'Name'
# 'NotRunCount'
# 'PassedCount'
# 'Path'
# 'Result'
# 'ScriptBlock'
# 'ShouldRun'
# 'Skip'
# 'SkippedCount'
# 'StandardOutput'
# 'Tag'
# 'Tests'
# 'Duration'
# 'TotalCount'
# )
# $testProperties = @(
# 'Data'
# 'ErrorRecord'
# 'Executed'
# 'ExecutedAt'
# 'ExpandedName'
# 'Id' # needed because of grouping of data driven tests in nunit export
# 'Name'
# 'Path'
# 'Result'
# 'ScriptBlock'
# 'ShouldRun'
# 'Skip'
# 'Skipped'
# 'StandardOutput'
# 'Tag'
# 'Duration'
# )
Fold-Run $run -OnRun {
param($i)
# $ps = $i.PsObject.Properties.Name
# foreach ($p in $ps) {
# if ($p -like 'Plugin*') {
# $i.PsObject.Properties.Remove($p)
# }
# }
$i.PluginConfiguration = $null
$i.PluginData = $null
$i.Plugins = $null
} -OnContainer {
param($i)
# $ps = $i.PsObject.Properties.Name
# foreach ($p in $ps) {
# if ($p -like 'Own*') {
# $i.PsObject.Properties.Remove($p)
# }
# }
# $i.FrameworkData = $null
# $i.PluginConfiguration = $null
# $i.PluginData = $null
# $i.Plugins = $null
} -OnBlock {
param($i)
# $ps = $i.PsObject.Properties.Name
# foreach ($p in $ps) {
# if ($p -eq 'FrameworkData' -or $p -like 'Own*' -or $p -like 'Plugin*') {
# $i.PsObject.Properties.Remove($p)
# }
# }
$i.FrameworkData = $null
$i.PluginData = $null
} -OnTest {
param($i)
# $ps = $i.PsObject.Properties.Name
# foreach ($p in $ps) {
# if ($p -eq 'FrameworkData' -or $p -like 'Plugin*') {
# $i.PsObject.Properties.Remove($p)
# }
# }
$i.FrameworkData = $null
$i.PluginData = $null
}
}
function New-PesterContainer {
<#
.SYNOPSIS
Generates ContainerInfo-objects used as for Invoke-Pester -Container
.DESCRIPTION
Pester 5 supports running tests files and scriptblocks using parameter-input.
To use this feature, Invoke-Pester expects one or more ContainerInfo-objects
created using this function, that specify test containers in the form of paths
to the test files or scriptblocks containing the tests directly.
A optional Data-dictionary can be provided to supply the containers with any
required parameter-values. This is useful in when tests are generated dynamically
based on parameter-input. This method enables complex test-solutions while being
able to re-use a lot of test-code.
.PARAMETER Path
Specifies one or more paths to files containing tests. The value is a path\file
name or name pattern. Wildcards are permitted.
.PARAMETER ScriptBlock
Specifies one or more scriptblocks containing tests.
.PARAMETER Data
Allows a dictionary to be provided with parameter-values that should be used during
execution of the test containers defined in Path or ScriptBlock.
.EXAMPLE
```powershell
$container = New-PesterContainer -Path 'CodingStyle.Tests.ps1' -Data @{ File = "Get-Emoji.ps1" }
Invoke-Pester -Container $container
```
This example runs Pester using a generated ContainerInfo-object referencing a file and
required parameters that's provided to the test-file during execution.
.EXAMPLE
```powershell
$sb = {
Describe 'Testing New-PesterContainer' {
It 'Useless test' {
"foo" | Should -Not -Be "bar"
}
}
}
$container = New-PesterContainer -ScriptBlock $sb
Invoke-Pester -Container $container
```
This example runs Pester against a scriptblock. New-PesterContainer is used to generate
the required ContainerInfo-object that enables us to do this directly.
.LINK
https://pester.dev/docs/commands/New-PesterContainer
.LINK
https://pester.dev/docs/commands/Invoke-Pester
.LINK
https://pester.dev/docs/usage/data-driven-tests
#>
[CmdletBinding(DefaultParameterSetName = "Path")]
param(
[Parameter(Mandatory, ParameterSetName = "Path")]
[String[]] $Path,
[Parameter(Mandatory, ParameterSetName = "ScriptBlock")]
[ScriptBlock[]] $ScriptBlock,
[Collections.IDictionary[]] $Data
)
# it seems that when I don't assign $Data to $dt here the foreach does not always work in 5.1 :/ some voodoo
$dt = $Data
# expand to ContainerInfo user can provide multiple sets of data, but ContainerInfo can hold only one
# to keep the internal logic simple.
$kind = $PSCmdlet.ParameterSetName
if ('ScriptBlock' -eq $kind) {
# the @() is significant here, it will make it iterate even if there are no data
# which allows scriptblocks without data to run
foreach ($d in @($dt)) {
foreach ($sb in $ScriptBlock) {
New-BlockContainerObject -ScriptBlock $sb -Data $d
}
}
}
if ("Path" -eq $kind) {
# the @() is significant here, it will make it iterate even if there are no data
# which allows files without data to run
foreach ($d in @($dt)) {
foreach ($p in $Path) {
# resolve the path we are given in the same way we would resolve -Path on Invoke-Pester
$files = @(Find-File -Path $p -ExcludePath $PesterPreference.Run.ExcludePath.Value -Extension $PesterPreference.Run.TestExtension.Value)
foreach ($file in $files) {
New-BlockContainerObject -File $file -Data $d
}
}
}
}
}