11[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.'
44)]
55[CmdLetBinding ()]
66Param (
77 [Parameter (Mandatory )]
8- [string ] $Path
8+ [string ] $Path ,
9+
10+ [Parameter (Mandatory )]
11+ [string ] $TestsPath
912)
1013
1114BeforeAll {
@@ -14,8 +17,11 @@ BeforeAll {
1417 Where-Object { $_.Name -in ' public' , ' private' } |
1518 Get-ChildItem - Filter ' *.ps1' - File
1619
20+ $publicFunctionFiles = Get-ChildItem - Directory - Path (Join-Path - Path $Path - ChildPath ' public' ) - File - Filter ' *.ps1'
21+
1722 Write-Verbose " Found $ ( $scriptFiles.Count ) script files in $Path "
1823 Write-Verbose " Found $ ( $functionFiles.Count ) function files in $Path "
24+ Write-Verbose " Found $ ( $publicFunctionFiles.Count ) public function files in $Path "
1925}
2026
2127Describe ' PSModule - SourceCode tests' {
@@ -51,7 +57,37 @@ Describe 'PSModule - SourceCode tests' {
5157 Should - BeNullOrEmpty - Because ' the script files should be called the same as the function they contain'
5258 }
5359
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+ }
5591
5692 It " Should not contain '-Verbose' unless it is disabled using ':`$ false' qualifier after it" {
5793 $issues = @ (' ' )
@@ -143,13 +179,11 @@ Describe 'PSModule - SourceCode tests' {
143179 # It 'boolean parameters in CmdletBinding() attribute are written without assignments' {}
144180 # I.e. [CmdletBinding(ShouldProcess)] instead of [CmdletBinding(ShouldProcess = $true)]
145181 # It 'has [OutputType()] attribute' {}
146- # It 'has verb 'New','Set','Disable','Enable' etc. and uses "ShoudProcess" in the [CmdletBinding()] attribute' {}
147182 }
148183
149184 Context ' Parameter design' {
150185 # 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' {}
153187 # It 'boolean parameters to the [Parameter()] attribute are written without assignments' {}
154188 # I.e. [Parameter(Mandatory)] instead of [Parameter(Mandatory = $true)]
155189 # It 'datatype for parameters are written on the same line as the parameter name' {}
0 commit comments