Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 62e79b1

Browse files
authored
Merge pull request #26 from PowerShell/feat/literalpath
Support Path and LiteralPath parameters to module directories
2 parents 5a29924 + 2f9f009 commit 62e79b1

File tree

5 files changed

+299
-64
lines changed

5 files changed

+299
-64
lines changed

OperationValidation/Private/Get-ModuleList.ps1

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
function Get-ModuleList {
2+
[cmdletbinding(DefaultParameterSetName = 'Name')]
23
param (
4+
[Parameter(Mandatory = $true, ParameterSetName = 'Name')]
35
[string[]]$Name,
46

7+
[Parameter(Mandatory = $true, ParameterSetName = 'Path')]
8+
[string[]]$Path,
9+
510
[version]$Version
611
)
712

8-
foreach($p in $env:psmodulepath.split(";"))
13+
if ($PSCmdlet.ParameterSetName -eq 'Name')
14+
{
15+
$pathsToSearch = $env:PSModulePath.Split(';')
16+
}
17+
elseIf ($PSCmdlet.ParameterSetName -eq 'Path')
18+
{
19+
$pathsToSearch = $Path
20+
}
21+
22+
foreach($p in $pathsToSearch)
923
{
1024
if ( Test-Path -Path $p )
1125
{
1226
foreach($modDir in Get-ChildItem -Path $p -Directory)
1327
{
14-
foreach ($n in $name )
28+
Write-Debug "Checking for OVF in [$modDir]"
29+
30+
if ($PSCmdlet.ParameterSetName -eq 'Path')
31+
{
32+
$Name = $modDir.Name
33+
}
34+
35+
foreach ($n in $Name )
1536
{
1637
if ( $modDir.Name -like $n )
1738
{
@@ -68,5 +89,9 @@ function Get-ModuleList {
6889
}
6990
}
7091
}
92+
else
93+
{
94+
Write-Error -Message "Could not access [$p]. Does it exist?"
95+
}
7196
}
72-
}
97+
}

OperationValidation/Public/Get-OperationValidation.ps1

Lines changed: 152 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function Get-OperationValidation {
66
77
.DESCRIPTION
88
Modules which include a Diagnostics directory are inspected for
9-
Pester tests in either the "Simple" or "Comprehensive" directories.
9+
Pester tests in either the "Simple" or "Comprehensive" subdirectories.
1010
If files are found in those directories, they will be inspected to determine
1111
whether they are Pester tests. If Pester tests are found, the
1212
test names in those files will be returned.
@@ -19,38 +19,29 @@ function Get-OperationValidation {
1919
(e.g., ping, serviceendpoint checks)
2020
Comprehensive # comprehensive scenario tests should be placed here
2121
22-
.PARAMETER ModuleName
23-
By default this is * which will retrieve all modules in $env:psmodulepath
24-
Additional module directories may be added. If you wish to check both
25-
$env:psmodulepath and your own specific locations, use
26-
*,<yourmodulepath>
22+
.PARAMETER Name
23+
One or more module names to inspect and return if they adhere to the OVF Pester test structure.
24+
25+
By default this is [*] which will inspect all modules in $env:PSModulePath.
26+
27+
.PARAMETER Path
28+
One or more paths to search for OVF modules in. This bypasses searching the directories contained in $env:PSModulePath.
29+
30+
.PARAMETER LiteralPath
31+
One or more literal paths to search for OVF modules in. This bypasses searching the directories contained in $env:PSModulePath.
32+
33+
Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed.
34+
No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation
35+
marks tell PowerShell not to interpret any characters as escape sequences.
2736
2837
.PARAMETER TestType
29-
The type of tests to retrieve, this may be either "Simple", "Comprehensive"
30-
or Both ("Simple,Comprehensive"). "Simple,Comprehensive" is the default.
38+
The type of tests to retrieve, this may be either "Simple", "Comprehensive", or Both ("Simple,Comprehensive").
39+
"Simple, Comprehensive" is the default.
3140
3241
.PARAMETER Version
33-
The version of the module to retrieve. If the specified, the latest version
42+
The version of the module to retrieve. If not specified, the latest version
3443
of the module will be retured.
3544
36-
.EXAMPLE
37-
PS> Get-OperationValidation -ModuleName C:\temp\modules\AddNumbers
38-
39-
Type: Simple
40-
File: addnum.tests.ps1
41-
FilePath: C:\temp\modules\AddNumbers\Diagnostics\Simple\addnum.tests.ps1
42-
Name:
43-
Add-Em
44-
Subtract em
45-
Add-Numbers
46-
Type: Comprehensive
47-
File: Comp.Adding.Tests.ps1
48-
FilePath: C:\temp\modules\AddNumbers\Diagnostics\Comprehensive\Comp.Adding.Tests.ps1
49-
Name:
50-
Comprehensive Adding Tests
51-
Comprehensive Subtracting Tests
52-
Comprehensive Examples
53-
5445
.PARAMETER Tag
5546
Executes tests with specified tag parameter values. Wildcard characters and tag values that include spaces
5647
or whitespace characters are not supported.
@@ -64,29 +55,150 @@ function Get-OperationValidation {
6455
6556
When you specify multiple ExcludeTag values, Get-OperationValidation omits tests that have any
6657
of the listed tags. If you use both Tag and ExcludeTag, ExcludeTag takes precedence.
58+
59+
.EXAMPLE
60+
PS> Get-OperationValidation -Name OVF.Windows.Server
61+
62+
Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2
63+
Version: 1.0.2
64+
Type: Simple
65+
Tags: {}
66+
File: LogicalDisk.tests.ps1
67+
FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\LogicalDisk.tests.ps1
68+
Name:
69+
Logical Disks
70+
71+
72+
Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2
73+
Version: 1.0.2
74+
Type: Simple
75+
Tags: {}
76+
File: Memory.tests.ps1
77+
FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Memory.tests.ps1
78+
Name:
79+
Memory
80+
81+
82+
Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2
83+
Version: 1.0.2
84+
Type: Simple
85+
Tags: {}
86+
File: Network.tests.ps1
87+
FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Network.tests.ps1
88+
Name:
89+
Network Adapters
90+
91+
92+
Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2
93+
Version: 1.0.2
94+
Type: Simple
95+
Tags: {}
96+
File: Services.tests.ps1
97+
FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Services.tests.ps1
98+
Name:
99+
Operating System
100+
101+
.EXAMPLE
102+
PS> $tests = Get-OperationValidation
103+
104+
Search in all modules found in $env:PSModulePath for OVF tests.
105+
106+
.EXAMPLE
107+
PS> $tests = Get-OperationValidation -Path C:\MyTests
108+
109+
Search for OVF modules under c:\MyTests
110+
111+
.EXAMPLE
112+
PS> $simpleTests = Get-OperationValidation -ModuleName OVF.Windows.Server -TypeType Simple
113+
114+
Get just the simple tests in the OVF.Windows.Server module.
115+
116+
.EXAMPLE
117+
$tests = Get-OperationValidation -ModuleName OVF.Windows.Server -Version 1.0.2
118+
119+
Get all the tests from version 1.0.2 of the OVF.Windows.Server module.
120+
121+
.EXAMPLE
122+
$storageTests = Get-OperationValidation -Tag Storage
123+
124+
Search in all modules for OVF tests that include the tag Storage.
125+
126+
.EXAMPLE
127+
$tests = Get-OperationValidation -ExcludeTag memory
128+
129+
Search for OVF tests that don't include the tag Memory
130+
67131
.LINK
68132
Invoke-OperationValidation
69133
70134
#>
71-
[CmdletBinding()]
135+
[CmdletBinding(DefaultParameterSetName = 'ModuleName')]
72136
param (
73-
[Parameter(Position=0)][string[]]$ModuleName = "*",
74-
[Parameter()][ValidateSet("Simple","Comprehensive")][string[]]$TestType = @("Simple","Comprehensive"),
75-
[Parameter()][Version]$Version,
76-
[Parameter()][string[]]$Tag,
77-
[Parameter()][string[]]$ExcludeTag
137+
[Parameter(Position = 0, ParameterSetName = 'ModuleName')]
138+
[Alias('ModuleName')]
139+
[string[]]$Name = '*',
140+
141+
[parameter(
142+
Mandatory,
143+
ParameterSetName = 'Path',
144+
Position = 0,
145+
ValueFromPipeline,
146+
ValueFromPipelineByPropertyName
147+
)]
148+
[ValidateNotNullOrEmpty()]
149+
[SupportsWildcards()]
150+
[string[]]$Path,
151+
152+
[parameter(
153+
Mandatory,
154+
ParameterSetName = 'LiteralPath',
155+
Position = 0,
156+
ValueFromPipelineByPropertyName
157+
)]
158+
[ValidateNotNullOrEmpty()]
159+
[Alias('PSPath')]
160+
[string[]]$LiteralPath,
161+
162+
[ValidateSet('Simple', 'Comprehensive')]
163+
[string[]]$TestType = @('Simple', 'Comprehensive'),
164+
165+
[Version]$Version,
166+
167+
[string[]]$Tag,
168+
169+
[string[]]$ExcludeTag
78170
)
79171

80172
PROCESS {
81173
Write-Progress -Activity 'Inspecting Modules' -Status ' '
82-
if ($PSBoundParameters.ContainsKey('Version'))
174+
175+
# Resolve module list either by module name, path, or literalpath
176+
$modListParams = @{}
177+
switch ($PSCmdlet.ParameterSetName)
83178
{
84-
$moduleCollection = @(Get-ModuleList -Name $ModuleName -Version $Version)
179+
'ModuleName'
180+
{
181+
$modListParams.Name = $Name
182+
break
183+
}
184+
'Path'
185+
{
186+
$paths = Resolve-Path -Path $Path | Select-Object -ExpandProperty Path
187+
$modListParams.Path = $paths
188+
}
189+
'LiteralPath'
190+
{
191+
$paths = Resolve-Path -LiteralPath $LiteralPath | Select-Object -ExpandProperty Path
192+
$modListParams.Path = $paths
193+
}
85194
}
86-
else
195+
196+
if ($PSBoundParameters.ContainsKey('Version'))
87197
{
88-
$moduleCollection = @(Get-ModuleList -Name $ModuleName)
198+
$modListParams.Version = $Version
199+
$moduleCollection = @(Get-ModuleList -Name $Name -Version $Version)
89200
}
201+
$moduleCollection = @(Get-ModuleList @modListParams)
90202

91203
$count = 1
92204
$moduleCount = $moduleCollection.Count
@@ -97,10 +209,10 @@ function Get-OperationValidation {
97209
$diagnosticsDir = Join-Path -Path $modulePath -ChildPath 'Diagnostics'
98210

99211
# Get the module manifest so we can pull out the version
100-
$moduleName = Split-Path -Path $modulePath -Leaf
101-
$manifestFile = Get-ChildItem -Path $modulePath -Filter "$($moduleName).psd1"
212+
$modName = Split-Path -Path $modulePath -Leaf
213+
$manifestFile = Get-ChildItem -Path $modulePath -Filter "$($modName).psd1"
102214
if (-not $manifestFile) {
103-
if ("$moduleName" -as [version]) {
215+
if ("$modName" -as [version]) {
104216
# We are in a "version" directory so get the actual module name from the parent directory
105217
$parent = Split-Path -Path (Split-Path -Path $modulePath -Parent) -Leaf
106218
$manifestFile = Get-ChildItem -Path $modulePath -Filter "$($parent).psd1"

0 commit comments

Comments
 (0)