@@ -39,13 +39,14 @@ function New-OperationValidationResult
39
39
}
40
40
function new-OperationValidationInfo
41
41
{
42
- param (
42
+ param (
43
43
[Parameter (Mandatory = $true )][string ]$File ,
44
44
[Parameter (Mandatory = $true )][string ]$FilePath ,
45
45
[Parameter (Mandatory = $true )][string []]$Name ,
46
46
[Parameter ()][string []]$TestCases ,
47
47
[Parameter (Mandatory = $true )][ValidateSet (" None" , " Simple" , " Comprehensive" )][string ]$Type ,
48
- [Parameter ()][string ]$modulename
48
+ [Parameter ()][string ]$modulename ,
49
+ [Parameter ()][hashtable ]$Parameters
49
50
)
50
51
$o = [pscustomobject ]@ {
51
52
File = $File
@@ -54,6 +55,7 @@ function new-OperationValidationInfo
54
55
TestCases = $testCases
55
56
Type = $type
56
57
ModuleName = $modulename
58
+ ScriptParameters = $Parameters
57
59
}
58
60
$o.psobject.Typenames.Insert (0 , " OperationValidationInfo" )
59
61
$ToString = { return (" {0} ({1}): {2}" -f $this.testFile , $this.Type , ($this.TestCases -join " ," )) }
@@ -70,13 +72,13 @@ function Get-TestFromScript
70
72
write-verbose - Message $scriptPath
71
73
72
74
for ($i = 0 ; $i -lt $tok.count ; $i ++ ) {
73
- if ( $tok [$i ].type -eq " Command" -and $tok [$i ].content -eq " Describe" )
75
+ if ( $tok [$i ].type -eq " Command" -and $tok [$i ].content -eq " Describe" )
74
76
{
75
77
$i ++
76
78
if ( $tok [$i ].Type -eq " String" ) { $tok [$i ].Content }
77
79
else
78
80
{
79
- # ok - we didn't get the describe text first,
81
+ # ok - we didn't get the describe text first,
80
82
# we likely saw a "-Tags" statement, so that means that
81
83
# the describe text will immediately preceed the scriptblock
82
84
while ($tok [$i ].Type -ne " GroupStart" )
@@ -95,20 +97,20 @@ function Get-TestFromScript
95
97
Retrieve the operational tests from modules
96
98
97
99
. DESCRIPTION
98
- Modules which include a Diagnostics directory are inspected for
100
+ Modules which include a Diagnostics directory are inspected for
99
101
Pester tests in either the "Simple" or "Comprehensive" directories.
100
102
If files are found in those directories, they will be inspected to determine
101
- whether they are Pester tests. If Pester tests are found, the
103
+ whether they are Pester tests. If Pester tests are found, the
102
104
test names in those files will be returned.
103
105
104
106
The module structure required is as follows:
105
107
106
108
ModuleBase\
107
109
Diagnostics\
108
- Simple # simple tests are held in this location
110
+ Simple # simple tests are held in this location
109
111
(e.g., ping, serviceendpoint checks)
110
112
Comprehensive # comprehensive scenario tests should be placed here
111
-
113
+
112
114
. PARAMETER ModuleName
113
115
By default this is * which will retrieve all modules in $env:psmodulepath
114
116
Additional module directories may be added. If you wish to check both
@@ -146,7 +148,8 @@ function Get-OperationValidation
146
148
[CmdletBinding ()]
147
149
param (
148
150
[Parameter (Position = 0 )][string []]$ModuleName = " *" ,
149
- [Parameter ()][ValidateSet (" Simple" , " Comprehensive" )][string []]$TestType = @ (" Simple" , " Comprehensive" )
151
+ [Parameter ()][ValidateSet (" Simple" , " Comprehensive" )][string []]$TestType = @ (" Simple" , " Comprehensive" ),
152
+ [Parameter ()][Version ]$Version
150
153
)
151
154
152
155
BEGIN
@@ -197,9 +200,12 @@ param (
197
200
}
198
201
}
199
202
}
200
- function Get-ModuleList
203
+ function Get-ModuleList
201
204
{
202
- param ( [string []]$Name )
205
+ param (
206
+ [string []]$Name ,
207
+ [version ]$Version
208
+ )
203
209
foreach ($p in $env: psmodulepath.split (" ;" ))
204
210
{
205
211
if ( test-path - path $p )
@@ -213,17 +219,42 @@ param (
213
219
# now determine if there's a diagnostics directory, or a version
214
220
if ( test-path - path ($modDir.FullName + " \Diagnostics" ))
215
221
{
216
- $modDir.FullName
217
- break
222
+ # Did we specify a specific version to find?
223
+ if ($PSBoundParameters.ContainsKey (' Version' ))
224
+ {
225
+ $manifestFile = Get-ChildItem - Path $modDir.FullName - Filter " $modDir .psd1" | Select-Object - First 1
226
+ $manifest = Test-ModuleManifest - Path $manifestFile.FullName
227
+ if ($manifest.Version -eq $Version )
228
+ {
229
+ $modDir.FullName
230
+ break
231
+ }
232
+ }
233
+ else
234
+ {
235
+ $modDir.FullName
236
+ break
237
+ }
238
+ }
239
+
240
+ # Get latest version if no specific version specified
241
+ if ($PSBoundParameters.ContainsKey (' Version' ))
242
+ {
243
+ $versionDirectories = Get-Childitem - path $modDir.FullName - dir |
244
+ where-object { $_.name -as [version ] -and $_.Name -eq $Version }
218
245
}
219
- $versionDirectories = Get-Childitem - path $modDir.FullName - dir |
220
- where-object { $_.name -as [version ] }
246
+ else
247
+ {
248
+ $versionDirectories = Get-Childitem - path $modDir.FullName - dir |
249
+ where-object { $_.name -as [version ] }
250
+ }
251
+
221
252
$potentialDiagnostics = $versionDirectories | where-object {
222
253
test-path ($_.fullname + " \Diagnostics" )
223
254
}
224
255
# now select the most recent module path which has diagnostics
225
- $DiagnosticDir = $potentialDiagnostics |
226
- sort-object {$_.name -as [version ]} |
256
+ $DiagnosticDir = $potentialDiagnostics |
257
+ sort-object {$_.name -as [version ]} |
227
258
Select-Object - Last 1
228
259
if ( $DiagnosticDir )
229
260
{
@@ -240,28 +271,48 @@ param (
240
271
PROCESS
241
272
{
242
273
Write-Progress - Activity " Inspecting Modules" - Status " "
243
- $moduleCollection = Get-ModuleList - Name $ModuleName
244
- $count = 1 ;
274
+ if ($PSBoundParameters.ContainsKey (' Version' ))
275
+ {
276
+ $moduleCollection = Get-ModuleList - Name $ModuleName - Version $Version
277
+ }
278
+ else
279
+ {
280
+ $moduleCollection = Get-ModuleList - Name $ModuleName
281
+ }
282
+
283
+ $count = 1 ;
245
284
$moduleCount = @ ($moduleCollection ).Count
246
285
foreach ($module in $moduleCollection )
247
286
{
248
287
Write-Progress - Activity (" Searching for Diagnostics in " + $module ) - PercentComplete ($count ++/ $moduleCount * 100 ) - status " "
249
- $diagnosticsDir = $module + " \Diagnostics"
288
+ $diagnosticsDir = $module + " \Diagnostics"
250
289
if ( test-path - path $diagnosticsDir )
251
290
{
252
291
foreach ($dir in $testType )
253
292
{
254
293
$testDir = " $diagnosticsDir \$dir "
255
294
write-verbose - Message " SPECIFIC TEST: $testDir "
256
- if ( ! (test-path - path $testDir ) )
295
+ if ( ! (test-path - path $testDir ) )
257
296
{
258
297
continue
259
298
}
260
299
foreach ($file in get-childitem - path $testDir - filter * .tests.ps1)
261
300
{
262
301
Write-Verbose - Message $file.fullname
263
- $testName = Get-TestFromScript - scriptPath $file.FullName
264
- new-OperationValidationInfo - FilePath $file.Fullname - File $file.Name - Type $dir - Name $testName - ModuleName $Module
302
+
303
+ # Pull out parameters to Pester script if they exist
304
+ $script = Get-Command - Name $file.fullname
305
+ $parameters = $script.Parameters
306
+ if ($parameters.Keys.Count -gt 0 )
307
+ {
308
+ Write-Debug - Message ' Test script has overrideable parameters'
309
+ Write-Debug - Message " `n $ ( $parameters.Keys | Out-String ) "
310
+ }
311
+
312
+ $testNames = @ (Get-TestFromScript - scriptPath $file.FullName )
313
+ foreach ($testName in $testNames ) {
314
+ New-OperationValidationInfo - FilePath $file.Fullname - File $file.Name - Type $dir - Name $testName - ModuleName $Module - Parameters $parameters
315
+ }
265
316
}
266
317
}
267
318
}
@@ -324,7 +375,12 @@ function Invoke-OperationValidation
324
375
[Parameter (ParameterSetName = " UseGetOperationTest" )][string []]$ModuleName = " *" ,
325
376
[Parameter (ParameterSetName = " UseGetOperationTest" )]
326
377
[ValidateSet (" Simple" , " Comprehensive" )][string []]$TestType = @ (" Simple" , " Comprehensive" ),
327
- [Parameter ()][switch ]$IncludePesterOutput
378
+ [Parameter ()][switch ]$IncludePesterOutput ,
379
+ [Parameter (ParameterSetName = " UseGetOperationTest" )]
380
+ [Parameter ()][Version ]$Version ,
381
+ [Parameter (ParameterSetName = " FileAndTest" )]
382
+ [Parameter (ParameterSetName = " UseGetOperationTest" )]
383
+ [Parameter ()][hashtable ]$Overrides
328
384
)
329
385
BEGIN
330
386
{
@@ -340,25 +396,22 @@ function Invoke-OperationValidation
340
396
Throw " Cannot load Pester module"
341
397
}
342
398
}
343
- # $resultCollection = @()
344
399
}
345
400
PROCESS
346
401
{
347
402
if ( $PSCmdlet.ParameterSetName -eq " UseGetOperationTest" )
348
403
{
349
- $tests = Get-OperationValidation - ModuleName $ModuleName - TestType $TestType
350
- $tests | Invoke-OperationValidation - IncludePesterOutput:$IncludePesterOutput
351
- return
352
- }
353
-
354
- if ( ($testFilePath -eq $null ) -and ($TestInfo -eq $null ) )
355
- {
356
- Get-OperationValidation | Invoke-OperationValidation - IncludePesterOutput:$IncludePesterOutput
357
- return
404
+ if ($PSBoundParameters.ContainsKey (' Version' ))
405
+ {
406
+ $TestInfo = Get-OperationValidation - ModuleName $ModuleName - TestType $TestType - Version $Version
407
+ }
408
+ else
409
+ {
410
+ $TestInfo = Get-OperationValidation - ModuleName $ModuleName - TestType $TestType
411
+ }
358
412
}
359
413
360
-
361
- if ( $testInfo -ne $null )
414
+ if ( $null -ne $testInfo )
362
415
{
363
416
# first check to be sure all of the TestInfos are sane
364
417
foreach ($ti in $testinfo )
@@ -368,29 +421,73 @@ function Invoke-OperationValidation
368
421
throw " TestInfo must contain the path and the list of tests"
369
422
}
370
423
}
371
-
372
- write-verbose - Message ( " EXECUTING: {0} {1} " -f $ti .FilePath , ( $ti .Name -join " , " ))
373
- foreach ($tname in $ti .Name )
424
+
425
+ # first check to be sure all of the TestInfos are sane
426
+ foreach ($ti in $testinfo )
374
427
{
375
- $testResult = Invoke-pester - Path $ti.FilePath - TestName $tName - quiet:$quiet - PassThru
376
- Add-member - InputObject $testResult - MemberType NoteProperty - Name Path - Value $ti.FilePath
377
- Convert-TestResult $testResult
428
+ if ( ! ($ti.FilePath -and $ti.Name ))
429
+ {
430
+ throw " TestInfo must contain the path and the list of tests"
431
+ }
432
+ }
433
+
434
+ Write-Verbose - Message (" EXECUTING: {0} {1}" -f $ti.FilePath , ($ti.Name -join " ," ))
435
+ foreach ($ti in $testinfo )
436
+ {
437
+ $pesterParams = @ {
438
+ TestName = $ti.Name
439
+ Quiet = $quiet
440
+ PassThru = $true
441
+ Verbose = $false
442
+ }
443
+
444
+ if ($ti.ScriptParameters )
445
+ {
446
+ Write-Verbose - Message ' Test has script parameters'
447
+ if ($PSBoundParameters.ContainsKey (' Overrides' ))
448
+ {
449
+ Write-Verbose - Message " Overriding with parameters:`n $ ( $Overrides | Format-List - Property * | Out-String ) "
450
+ $pesterParams.Script = @ {
451
+ Path = $ti.FilePath
452
+ Parameters = $Overrides
453
+ }
454
+ }
455
+ else
456
+ {
457
+ Write-Verbose - Message ' Using default parameters for test'
458
+ $pesterParams.Path = $ti.FilePath
459
+ }
460
+ }
461
+ else
462
+ {
463
+ $pesterParams.Path = $ti.FilePath
464
+ }
465
+
466
+ if ( $PSCmdlet.ShouldProcess (" $ ( $ti.Name ) [$ ( $ti.FilePath ) ]" ))
467
+ {
468
+ $testResult = Invoke-Pester @pesterParams
469
+ if ($testResult )
470
+ {
471
+ Add-member - InputObject $testResult - MemberType NoteProperty - Name Path - Value $ti.FilePath
472
+ Convert-TestResult $testResult
473
+ }
474
+ }
378
475
}
379
476
return
380
477
}
381
478
382
- foreach ( $test in $testFilePath )
479
+ if ( $testFilePath )
383
480
{
384
- write-progress - Activity " Invoking tests in $test "
385
- if ( $PSCmdlet.ShouldProcess ($test ))
386
- {
387
- $testResult = Invoke-Pester $test - passthru - quiet:$quiet
388
- Add-Member - InputObject $testResult - MemberType NoteProperty - Name Path - Value $test
389
- Convert-TestResult $testResult
481
+ foreach ($filePath in $testFilePath ) {
482
+ write-progress - Activity " Invoking tests in $filePath "
483
+ if ( $PSCmdlet.ShouldProcess ($filePath )) {
484
+ $testResult = Invoke-Pester $filePath - passthru - quiet:$quiet
485
+ Add-Member - InputObject $testResult - MemberType NoteProperty - Name Path - Value $filePath
486
+ Convert-TestResult $testResult
487
+ }
390
488
}
391
489
}
392
490
}
393
-
394
491
}
395
492
396
493
# emit an object which can be used in reporting
0 commit comments