Skip to content

Commit 5778b22

Browse files
🩹 [Patch]: Add test to check that all PowerShell keywords are lower cased (#62)
## Description - Add test to check that all PowerShell keywords are lower cased. - Fixes #19 ## 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 c300650 commit 5778b22

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

scripts/tests/PSModule/SourceCode.Tests.ps1

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Param(
1212
)
1313

1414
BeforeAll {
15-
$scriptFiles = Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File
15+
$scriptFiles = Get-ChildItem -Path $Path -Include *.psm1, *.ps1 -Recurse -File
1616
$functionFiles = Get-ChildItem -Directory -Path $Path |
1717
Where-Object { $_.Name -in 'public', 'private' } |
1818
Get-ChildItem -Filter '*.ps1' -File
@@ -128,6 +128,28 @@ Describe 'PSModule - SourceCode tests' {
128128
Should -BeNullOrEmpty -Because 'the script should not use ternary operations for compatability with PS 5.1 and below'
129129
}
130130

131+
It 'all powershell keywords are lowercase' {
132+
$issues = @('')
133+
$scriptFiles | ForEach-Object {
134+
$filePath = $_.FullName
135+
$relativePath = $filePath.Replace($Path, '').Trim('\').Trim('/')
136+
137+
$errors = $null
138+
$tokens = $null
139+
[System.Management.Automation.Language.Parser]::ParseFile($FilePath, [ref]$tokens, [ref]$errors)
140+
141+
foreach ($token in $tokens) {
142+
$keyword = $token.Text
143+
$lineNumber = $token.Extent.StartLineNumber
144+
$columnNumber = $token.Extent.StartColumnNumber
145+
if (($token.TokenFlags -match 'Keyword') -and ($keyword -cne $keyword.ToLower())) {
146+
$issues += " - $relativePath`:L$lineNumber`:C$columnNumber - $keyword"
147+
}
148+
}
149+
}
150+
$issues -join [Environment]::NewLine | Should -BeNullOrEmpty -Because 'all powershell keywords should be lowercase'
151+
}
152+
131153
# It 'comment based doc block start is indented with 4 spaces' {}
132154
# It 'comment based doc is indented with 8 spaces' {}
133155
# It 'has synopsis for all functions' {}

tests/src/modules/OtherPSModule.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Get-OtherPSModule {
1+
function Get-OtherPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

tests/src/private/Get-InternalPSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Get-InternalPSModule {
1+
function Get-InternalPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

tests/src/private/Set-InternalPSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Set-InternalPSModule {
1+
function Set-InternalPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

tests/srcWithManifest/modules/OtherPSModule.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Get-OtherPSModule {
1+
function Get-OtherPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

tests/srcWithManifest/private/Get-InternalPSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Get-InternalPSModule {
1+
function Get-InternalPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

tests/srcWithManifest/private/Set-InternalPSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Set-InternalPSModule {
1+
function Set-InternalPSModule {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.

0 commit comments

Comments
 (0)