@@ -23,91 +23,87 @@ Describe 'PSModule - SourceCode tests' {
2323 It ' Should contain one function or filter' {
2424 $issues = @ (' ' )
2525 $functionFiles | ForEach-Object {
26- $path = $_.FullName
27- $Ast = [System.Management.Automation.Language.Parser ]::ParseFile($path , [ref ]$null , [ref ]$null )
26+ $filePath = $_.FullName
27+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
28+ $Ast = [System.Management.Automation.Language.Parser ]::ParseFile($filePath , [ref ]$null , [ref ]$null )
2829 $tokens = $Ast.FindAll ( { $args [0 ] -is [System.Management.Automation.Language.FunctionDefinitionAst ] } , $true )
2930 if ($tokens.count -ne 1 ) {
30- $issues += " - $path "
31+ $issues += " - $relativePath - $ ( $tokens .Name ) "
3132 }
3233 }
3334 $issues -join [Environment ]::NewLine |
3435 Should - BeNullOrEmpty - Because ' the script should contain one function or filter'
3536 }
3637
37- It ' Script filename and function/filter name should match' {
38- $scriptFiles = @ ()
39- Get-ChildItem - Path $Path - Filter ' *.ps1' - Recurse - File | ForEach-Object {
40- $fileContent = Get-Content - Path $_.FullName - Raw
41- if ($fileContent -match ' ^(?:function|filter)\s+([a-zA-Z][a-zA-Z0-9-]*)' ) {
42- $functionName = $matches [1 ]
43- $fileName = $_.BaseName
44- $relativePath = $_.FullName.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
45- $scriptFiles += @ {
46- fileName = $fileName
47- filePath = $relativePath
48- functionName = $functionName
49- }
50- }
51- }
52-
38+ It ' Should have matching filename and function/filter name' {
5339 $issues = @ (' ' )
54- $issues += $scriptFiles | Where-Object { $_.filename -ne $_.functionName } | ForEach-Object {
55- " - $ ( $_.filePath ) : Function/filter name [$ ( $_.functionName ) ]. Change file name or function/filter name so they match."
40+ $functionFiles | ForEach-Object {
41+ $filePath = $_.FullName
42+ $fileName = $_.BaseName
43+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
44+ $Ast = [System.Management.Automation.Language.Parser ]::ParseFile($filePath , [ref ]$null , [ref ]$null )
45+ $tokens = $Ast.FindAll ( { $args [0 ] -is [System.Management.Automation.Language.FunctionDefinitionAst ] } , $true )
46+ if ($tokens.Name -ne $fileName ) {
47+ $issues += " - $relativePath - $ ( $tokens.Name ) "
48+ }
5649 }
5750 $issues -join [Environment ]::NewLine |
5851 Should - BeNullOrEmpty - Because ' the script files should be called the same as the function they contain'
5952 }
6053
61-
62-
6354 # It 'All script files have tests' {} # Look for the folder name in tests called the same as section/folder name of functions
6455
6556 It " Should not contain '-Verbose' unless it is disabled using ':`$ false' qualifier after it" {
6657 $issues = @ (' ' )
6758 $scriptFiles | ForEach-Object {
68- Select-String - Path $_.FullName - Pattern ' \s(-Verbose(?::\$true)?)\b(?!:\$false)' - AllMatches | ForEach-Object {
69- $issues += " - $ ( $_.Path ) :L$ ( $_.LineNumber ) "
59+ $filePath = $_.FullName
60+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
61+ Select-String - Path $filePath - Pattern ' \s(-Verbose(?::\$true)?)\b(?!:\$false)' - AllMatches | ForEach-Object {
62+ $issues += " - $relativePath `:L$ ( $_.LineNumber ) - $ ( $_.Line ) "
7063 }
7164 }
7265 $issues -join [Environment ]::NewLine |
7366 Should - BeNullOrEmpty - Because " the script should not contain '-Verbose' unless it is disabled using ':`$ false' qualifier after it."
7467 }
7568
76- It " Should use '`$ null = <commands> ' instead of '<commands> | Out-Null'" {
69+ It " Should use '`$ null = ... ' instead of '... | Out-Null'" {
7770 $issues = @ (' ' )
7871 $scriptFiles | ForEach-Object {
79- Select-String - Path $_.FullName - Pattern ' Out-Null' - AllMatches | ForEach-Object {
80- $issues += " - $ ( $_.Path ) :L$ ( $_.LineNumber ) "
72+ $filePath = $_.FullName
73+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
74+ Select-String - Path $filePath - Pattern ' Out-Null' - AllMatches | ForEach-Object {
75+ $issues += " - $relativePath `:L$ ( $_.LineNumber ) - $ ( $_.Line ) "
8176 }
8277 }
8378 $issues -join [Environment ]::NewLine |
84- Should - BeNullOrEmpty - Because " the script should use '`$ null = <commands> ' instead of '<commands> | Out-Null'"
79+ Should - BeNullOrEmpty - Because " the script should use '`$ null = ... ' instead of '... | Out-Null'"
8580 }
8681
8782 It ' Should not use ternary operations for compatability reasons' {
8883 $issues = @ (' ' )
8984 $scriptFiles | ForEach-Object {
90- Select-String - Path $_.FullName - Pattern ' (?<!\|)\s+\?' - AllMatches | ForEach-Object {
91- $issues += " - $ ( $_.Path ) :L$ ( $_.LineNumber ) "
85+ $filePath = $_.FullName
86+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
87+ Select-String - Path $filePath - Pattern ' (?<!\|)\s+\?' - AllMatches | ForEach-Object {
88+ $issues += " - $relativePath `:L$ ( $_.LineNumber ) - $ ( $_.Line ) "
9289 }
9390 }
9491 $issues -join [Environment ]::NewLine |
9592 Should - BeNullOrEmpty - Because ' the script should not use ternary operations for compatability with PS 5.1 and below'
9693 }
97- }
9894
99- Context ' Function/filter design' {
10095 # It 'comment based doc block start is indented with 4 spaces' {}
10196 # It 'comment based doc is indented with 8 spaces' {}
10297 # It 'has synopsis for all functions' {}
10398 # It 'has description for all functions' {}
10499 # It 'has examples for all functions' {}
105100
106- It ' should have [CmdletBinding()] attribute' {
101+ It ' Should have [CmdletBinding()] attribute' {
107102 $issues = @ (' ' )
108103 $functionFiles | ForEach-Object {
109104 $found = $false
110105 $filePath = $_.FullName
106+ $relativePath = $filePath.Replace ($Path , ' ' ).Trim(' \' ).Trim(' /' )
111107 $scriptAst = [System.Management.Automation.Language.Parser ]::ParseFile($filePath , [ref ]$null , [ref ]$null )
112108 $tokens = $scriptAst.FindAll ({ $true }, $true )
113109 foreach ($token in $tokens ) {
@@ -116,7 +112,7 @@ Describe 'PSModule - SourceCode tests' {
116112 }
117113 }
118114 if (-not $found ) {
119- $issues += " - $filePath "
115+ $issues += " - $relativePath "
120116 }
121117 }
122118 $issues -join [Environment ]::NewLine |
0 commit comments