Skip to content

Commit c300650

Browse files
🩹 [Patch]: Add a test to check that each function has a test (#61)
## Description - Add a test to check that each function has a test. - Fixes #34 ## 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 4b7b9fd commit c300650

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

scripts/helpers/Test-PSModule.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ function Test-PSModule {
9797
$containerParams = @{
9898
Path = Join-Path -Path $env:GITHUB_ACTION_PATH -ChildPath 'scripts\tests\PSModule\SourceCode.Tests.ps1'
9999
Data = @{
100-
Path = $Path
101-
Verbose = $true
100+
Path = $Path
101+
TestsPath = $moduleTestsPath
102+
Verbose = $true
102103
}
103104
}
104105
Write-Verbose 'ContainerParams:'

scripts/tests/PSModule/SourceCode.Tests.ps1

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
2-
'PSReviewUnusedParameter', 'Path',
3-
Justification = 'Path is used to specify the path to the module to test.'
2+
'PSReviewUnusedParameter', '',
3+
Justification = 'Parameters are used in the test.'
44
)]
55
[CmdLetBinding()]
66
Param(
77
[Parameter(Mandatory)]
8-
[string] $Path
8+
[string] $Path,
9+
10+
[Parameter(Mandatory)]
11+
[string] $TestsPath
912
)
1013

1114
BeforeAll {
@@ -14,8 +17,11 @@ BeforeAll {
1417
Where-Object { $_.Name -in 'public', 'private' } |
1518
Get-ChildItem -Filter '*.ps1' -File
1619

20+
$publicFunctionFiles = Get-ChildItem -Directory -Path (Join-Path -Path $Path -ChildPath 'public') -File -Filter '*.ps1'
21+
1722
Write-Verbose "Found $($scriptFiles.Count) script files in $Path"
1823
Write-Verbose "Found $($functionFiles.Count) function files in $Path"
24+
Write-Verbose "Found $($publicFunctionFiles.Count) public function files in $Path"
1925
}
2026

2127
Describe 'PSModule - SourceCode tests' {
@@ -51,7 +57,37 @@ Describe 'PSModule - SourceCode tests' {
5157
Should -BeNullOrEmpty -Because 'the script files should be called the same as the function they contain'
5258
}
5359

54-
# It 'All script files have tests' {} # Look for the folder name in tests called the same as section/folder name of functions
60+
It 'All public functions/filters have tests' {
61+
$issues = @('')
62+
63+
$testFiles = Get-ChildItem -Path $TestsPath -Recurse -File -Filter '*.ps1'
64+
$functionsInTestFiles = $testFiles | ForEach-Object {
65+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($_.FullName, [ref]$null, [ref]$null)
66+
$ast.FindAll(
67+
{
68+
param($node)
69+
$node -is [System.Management.Automation.Language.CommandAst] -and
70+
$node.GetCommandName() -ne $null
71+
},
72+
$true
73+
) | ForEach-Object {
74+
$_.GetCommandName()
75+
} | Sort-Object -Unique
76+
}
77+
78+
$publicFunctionFiles | ForEach-Object {
79+
$filePath = $_.FullName
80+
$relativePath = $filePath.Replace($Path, '').Trim('\').Trim('/')
81+
$Ast = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null)
82+
$tokens = $Ast.FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } , $true )
83+
$functionName = $tokens.Name
84+
if ($functionsInTestFiles -notcontains $functionName) {
85+
$issues += " - $relativePath - $functionName"
86+
}
87+
}
88+
$issues -join [Environment]::NewLine |
89+
Should -BeNullOrEmpty -Because 'a test should exist for each of the functions in the module'
90+
}
5591

5692
It "Should not contain '-Verbose' unless it is disabled using ':`$false' qualifier after it" {
5793
$issues = @('')
@@ -143,13 +179,11 @@ Describe 'PSModule - SourceCode tests' {
143179
# It 'boolean parameters in CmdletBinding() attribute are written without assignments' {}
144180
# I.e. [CmdletBinding(ShouldProcess)] instead of [CmdletBinding(ShouldProcess = $true)]
145181
# It 'has [OutputType()] attribute' {}
146-
# It 'has verb 'New','Set','Disable','Enable' etc. and uses "ShoudProcess" in the [CmdletBinding()] attribute' {}
147182
}
148183

149184
Context 'Parameter design' {
150185
# It 'has parameter description for all functions' {}
151-
# It 'has parameter validation for all functions' {}
152-
# It 'parameters have [Parameters()] attribute' {}
186+
# It 'parameters have [Parameter()] attribute' {}
153187
# It 'boolean parameters to the [Parameter()] attribute are written without assignments' {}
154188
# I.e. [Parameter(Mandatory)] instead of [Parameter(Mandatory = $true)]
155189
# It 'datatype for parameters are written on the same line as the parameter name' {}

tests/tests/PSModuleTest.Tests.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
Describe 'PSModuleTest.Tests.ps1' {
2-
It 'Should be able to import the module' {
3-
Import-Module -Name 'PSModuleTest' -Verbose
4-
Get-Module -Name 'PSModuleTest' | Should -Not -BeNullOrEmpty
5-
Write-Verbose (Get-Module -Name 'PSModuleTest' | Out-String) -Verbose
1+
Describe 'Module' {
2+
It 'Function: Get-PSModuleTest' {
3+
Get-PSModuleTest -Name 'World' | Should -Be 'Hello, World!'
64
}
7-
It 'Should be able to call the function' {
5+
It 'Function: New-PSModuleTest' {
6+
New-PSModuleTest -Name 'World' | Should -Be 'Hello, World!'
7+
}
8+
It 'Function: Set-PSModuleTest' {
9+
Set-PSModuleTest -Name 'World' | Should -Be 'Hello, World!'
10+
}
11+
It 'Function: Test-PSModuleTest' {
812
Test-PSModuleTest -Name 'World' | Should -Be 'Hello, World!'
9-
Write-Verbose (Test-PSModuleTest -Name 'World' | Out-String) -Verbose
1013
}
1114
}

0 commit comments

Comments
 (0)