Skip to content

Commit 205960f

Browse files
🩹 [Patch]: Script files should only contain one function (#58)
## Description - Add test to check that a function file only contains one function/filter. ## 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 dfc63d0 commit 205960f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

scripts/tests/PSModule/SourceCode.Tests.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ BeforeAll {
2020

2121
Describe 'PSModule - SourceCode tests' {
2222
Context 'function/filter' {
23+
It 'Should contain one function or filter' {
24+
$issues = @('')
25+
$functionFiles | ForEach-Object {
26+
$path = $_.FullName
27+
$Ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$null, [ref]$null)
28+
$tokens = $Ast.FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } , $true )
29+
if ($tokens.count -ne 1) {
30+
$issues += " - $path"
31+
}
32+
}
33+
$issues -join [Environment]::NewLine |
34+
Should -BeNullOrEmpty -Because 'the script should contain one function or filter'
35+
}
36+
2337
It 'Script filename and function/filter name should match' {
2438
$scriptFiles = @()
2539
Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File | ForEach-Object {
@@ -44,7 +58,7 @@ Describe 'PSModule - SourceCode tests' {
4458
Should -BeNullOrEmpty -Because 'the script files should be called the same as the function they contain'
4559
}
4660

47-
# It 'Script file should only contain one function or filter' {}
61+
4862

4963
# It 'All script files have tests' {} # Look for the folder name in tests called the same as section/folder name of functions
5064

0 commit comments

Comments
 (0)