1
1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute (
2
- ' PSReviewUnusedParameter' , ' Path ' ,
3
- Justification = ' Path is used to specify the path to the module to test.'
2
+ ' PSReviewUnusedParameter' , ' ' ,
3
+ Justification = ' Parameters are used in the test.'
4
4
)]
5
5
[CmdLetBinding ()]
6
6
Param (
7
7
[Parameter (Mandatory )]
8
- [string ] $Path
8
+ [string ] $Path ,
9
+
10
+ [Parameter (Mandatory )]
11
+ [string ] $TestsPath
9
12
)
10
13
11
14
BeforeAll {
@@ -14,8 +17,11 @@ BeforeAll {
14
17
Where-Object { $_.Name -in ' public' , ' private' } |
15
18
Get-ChildItem - Filter ' *.ps1' - File
16
19
20
+ $publicFunctionFiles = Get-ChildItem - Directory - Path (Join-Path - Path $Path - ChildPath ' public' ) - File - Filter ' *.ps1'
21
+
17
22
Write-Verbose " Found $ ( $scriptFiles.Count ) script files in $Path "
18
23
Write-Verbose " Found $ ( $functionFiles.Count ) function files in $Path "
24
+ Write-Verbose " Found $ ( $publicFunctionFiles.Count ) public function files in $Path "
19
25
}
20
26
21
27
Describe ' PSModule - SourceCode tests' {
@@ -51,7 +57,37 @@ Describe 'PSModule - SourceCode tests' {
51
57
Should - BeNullOrEmpty - Because ' the script files should be called the same as the function they contain'
52
58
}
53
59
54
- # It 'All script files have tests' {} # Look for the folder name in tests called the same as section/folder name of functions
60
+ It ' All public functions/filters have tests' {
61
+ $issues = @ (' ' )
62
+
63
+ $testFiles = Get-ChildItem - Path $TestsPath - Recurse - File - Filter ' *.ps1'
64
+ $functionsInTestFiles = $testFiles | ForEach-Object {
65
+ $ast = [System.Management.Automation.Language.Parser ]::ParseFile($_.FullName , [ref ]$null , [ref ]$null )
66
+ $ast.FindAll (
67
+ {
68
+ param ($node )
69
+ $node -is [System.Management.Automation.Language.CommandAst ] -and
70
+ $node.GetCommandName () -ne $null
71
+ },
72
+ $true
73
+ ) | ForEach-Object {
74
+ $_.GetCommandName ()
75
+ } | Sort-Object - Unique
76
+ }
77
+
78
+ $publicFunctionFiles | ForEach-Object {
79
+ $filePath = $_.FullName
80
+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
81
+ $Ast = [System.Management.Automation.Language.Parser ]::ParseFile($filePath , [ref ]$null , [ref ]$null )
82
+ $tokens = $Ast.FindAll ( { $args [0 ] -is [System.Management.Automation.Language.FunctionDefinitionAst ] } , $true )
83
+ $functionName = $tokens.Name
84
+ if ($functionsInTestFiles -notcontains $functionName ) {
85
+ $issues += " - $relativePath - $functionName "
86
+ }
87
+ }
88
+ $issues -join [Environment ]::NewLine |
89
+ Should - BeNullOrEmpty - Because ' a test should exist for each of the functions in the module'
90
+ }
55
91
56
92
It " Should not contain '-Verbose' unless it is disabled using ':`$ false' qualifier after it" {
57
93
$issues = @ (' ' )
@@ -143,13 +179,11 @@ Describe 'PSModule - SourceCode tests' {
143
179
# It 'boolean parameters in CmdletBinding() attribute are written without assignments' {}
144
180
# I.e. [CmdletBinding(ShouldProcess)] instead of [CmdletBinding(ShouldProcess = $true)]
145
181
# It 'has [OutputType()] attribute' {}
146
- # It 'has verb 'New','Set','Disable','Enable' etc. and uses "ShoudProcess" in the [CmdletBinding()] attribute' {}
147
182
}
148
183
149
184
Context ' Parameter design' {
150
185
# It 'has parameter description for all functions' {}
151
- # It 'has parameter validation for all functions' {}
152
- # It 'parameters have [Parameters()] attribute' {}
186
+ # It 'parameters have [Parameter()] attribute' {}
153
187
# It 'boolean parameters to the [Parameter()] attribute are written without assignments' {}
154
188
# I.e. [Parameter(Mandatory)] instead of [Parameter(Mandatory = $true)]
155
189
# It 'datatype for parameters are written on the same line as the parameter name' {}
0 commit comments