Skip to content

Commit

Permalink
🩹 [Patch]: Add test to check that all PowerShell keywords are lower c…
Browse files Browse the repository at this point in the history
…ased (#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
  • Loading branch information
MariusStorhaug authored Apr 14, 2024
1 parent c300650 commit 5778b22
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 7 deletions.
24 changes: 23 additions & 1 deletion scripts/tests/PSModule/SourceCode.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Param(
)

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

It 'all powershell keywords are lowercase' {
$issues = @('')
$scriptFiles | ForEach-Object {
$filePath = $_.FullName
$relativePath = $filePath.Replace($Path, '').Trim('\').Trim('/')

$errors = $null
$tokens = $null
[System.Management.Automation.Language.Parser]::ParseFile($FilePath, [ref]$tokens, [ref]$errors)

foreach ($token in $tokens) {
$keyword = $token.Text
$lineNumber = $token.Extent.StartLineNumber
$columnNumber = $token.Extent.StartColumnNumber
if (($token.TokenFlags -match 'Keyword') -and ($keyword -cne $keyword.ToLower())) {
$issues += " - $relativePath`:L$lineNumber`:C$columnNumber - $keyword"
}
}
}
$issues -join [Environment]::NewLine | Should -BeNullOrEmpty -Because 'all powershell keywords should be lowercase'
}

# It 'comment based doc block start is indented with 4 spaces' {}
# It 'comment based doc is indented with 8 spaces' {}
# It 'has synopsis for all functions' {}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/modules/OtherPSModule.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Get-OtherPSModule {
function Get-OtherPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down
2 changes: 1 addition & 1 deletion tests/src/private/Get-InternalPSModule.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Get-InternalPSModule {
function Get-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down
2 changes: 1 addition & 1 deletion tests/src/private/Set-InternalPSModule.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Set-InternalPSModule {
function Set-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down
2 changes: 1 addition & 1 deletion tests/srcWithManifest/modules/OtherPSModule.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Get-OtherPSModule {
function Get-OtherPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down
2 changes: 1 addition & 1 deletion tests/srcWithManifest/private/Get-InternalPSModule.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Get-InternalPSModule {
function Get-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down
2 changes: 1 addition & 1 deletion tests/srcWithManifest/private/Set-InternalPSModule.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Set-InternalPSModule {
function Set-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down

0 comments on commit 5778b22

Please sign in to comment.