Skip to content

Commit dfc63d0

Browse files
🩹 [Patch]: Add test to check that script files have [CmdletBinding()] (#57)
## Description - Add test to check that script files have `[CmdletBinding()]`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent fdd3af9 commit dfc63d0

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

scripts/tests/PSModule/SourceCode.Tests.ps1

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ Param(
99
)
1010

1111
BeforeAll {
12-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
13-
'PSUseDeclaredVarsMoreThanAssignments', 'scriptFiles',
14-
Justification = 'scriptFiles is used in the test.'
15-
)]
1612
$scriptFiles = Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File
13+
$functionFiles = Get-ChildItem -Directory -Path $Path |
14+
Where-Object { $_.Name -in 'public', 'private' } |
15+
Get-ChildItem -Filter '*.ps1' -File
16+
17+
Write-Verbose "Found $($scriptFiles.Count) script files in $Path"
18+
Write-Verbose "Found $($functionFiles.Count) function files in $Path"
1719
}
1820

1921
Describe 'PSModule - SourceCode tests' {
@@ -68,15 +70,15 @@ Describe 'PSModule - SourceCode tests' {
6870
Should -BeNullOrEmpty -Because "the script should use '`$null = <commands>' instead of '<commands> | Out-Null'"
6971
}
7072

71-
It "Should not use ternary operations for compatability reasons" {
73+
It 'Should not use ternary operations for compatability reasons' {
7274
$issues = @('')
7375
$scriptFiles | ForEach-Object {
7476
Select-String -Path $_.FullName -Pattern '(?<!\|)\s+\?' -AllMatches | ForEach-Object {
7577
$issues += " - $($_.Path):L$($_.LineNumber)"
7678
}
7779
}
7880
$issues -join [Environment]::NewLine |
79-
Should -BeNullOrEmpty -Because "the script should not use ternary operations for compatability with PS 5.1 and below"
81+
Should -BeNullOrEmpty -Because 'the script should not use ternary operations for compatability with PS 5.1 and below'
8082
}
8183
}
8284

@@ -86,8 +88,27 @@ Describe 'PSModule - SourceCode tests' {
8688
# It 'has synopsis for all functions' {}
8789
# It 'has description for all functions' {}
8890
# It 'has examples for all functions' {}
89-
# It 'has output documentation for all functions' {}
90-
# It 'has [CmdletBinding()] attribute' {}
91+
92+
It 'should have [CmdletBinding()] attribute' {
93+
$issues = @('')
94+
$functionFiles | ForEach-Object {
95+
$found = $false
96+
$filePath = $_.FullName
97+
$scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null)
98+
$tokens = $scriptAst.FindAll({ $true }, $true)
99+
foreach ($token in $tokens) {
100+
if ($token.TypeName.Name -eq 'CmdletBinding') {
101+
$found = $true
102+
}
103+
}
104+
if (-not $found) {
105+
$issues += " - $filePath"
106+
}
107+
}
108+
$issues -join [Environment]::NewLine |
109+
Should -BeNullOrEmpty -Because 'the script should have [CmdletBinding()] attribute'
110+
}
111+
91112
# It 'boolean parameters in CmdletBinding() attribute are written without assignments' {}
92113
# I.e. [CmdletBinding(ShouldProcess)] instead of [CmdletBinding(ShouldProcess = $true)]
93114
# It 'has [OutputType()] attribute' {}

0 commit comments

Comments
 (0)