From 5afade1d61717239206539cfc048ef8637cd23f3 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 18:29:54 +0200 Subject: [PATCH 1/2] File should only contain one function --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 16 +++++++++++++++- tests/src/public/Get-PSModuleTest.ps1 | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 0d189f99..a1a3455b 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -20,6 +20,20 @@ BeforeAll { Describe 'PSModule - SourceCode tests' { Context 'function/filter' { + It 'Should contain one function or filter' { + $issues = @('') + $functionFiles | ForEach-Object { + $path = $_.FullName + $Ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$null, [ref]$null) + $tokens = $Ast.FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } , $true ) + if ($tokens.count -ne 1) { + $issues += " - $path" + } + } + $issues -join [Environment]::NewLine | + Should -BeNullOrEmpty -Because 'the script should contain one function or filter' + } + It 'Script filename and function/filter name should match' { $scriptFiles = @() Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File | ForEach-Object { @@ -44,7 +58,7 @@ Describe 'PSModule - SourceCode tests' { Should -BeNullOrEmpty -Because 'the script files should be called the same as the function they contain' } - # It 'Script file should only contain one function or filter' {} + # It 'All script files have tests' {} # Look for the folder name in tests called the same as section/folder name of functions diff --git a/tests/src/public/Get-PSModuleTest.ps1 b/tests/src/public/Get-PSModuleTest.ps1 index 0e9aacfe..36745fc7 100644 --- a/tests/src/public/Get-PSModuleTest.ps1 +++ b/tests/src/public/Get-PSModuleTest.ps1 @@ -18,3 +18,22 @@ function Get-PSModuleTest { ) Write-Output "Hello, $Name!" } + +filter Get-PSModuleTestfilter { + <# + .SYNOPSIS + Performs tests on a module. + + .EXAMPLE + Test-PSModule -Name 'World' + + "Hello, World!" + #> + [CmdletBinding()] + param ( + # Name of the person to greet. + [Parameter(Mandatory)] + [string] $Name + ) + Write-Output "Hello, $Name!" +} From f9c1c36596327f795b4b2de227306fdaac7427e1 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 18:32:41 +0200 Subject: [PATCH 2/2] Correct test data to see if it passes --- tests/src/public/Get-PSModuleTest.ps1 | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/src/public/Get-PSModuleTest.ps1 b/tests/src/public/Get-PSModuleTest.ps1 index 36745fc7..0e9aacfe 100644 --- a/tests/src/public/Get-PSModuleTest.ps1 +++ b/tests/src/public/Get-PSModuleTest.ps1 @@ -18,22 +18,3 @@ function Get-PSModuleTest { ) Write-Output "Hello, $Name!" } - -filter Get-PSModuleTestfilter { - <# - .SYNOPSIS - Performs tests on a module. - - .EXAMPLE - Test-PSModule -Name 'World' - - "Hello, World!" - #> - [CmdletBinding()] - param ( - # Name of the person to greet. - [Parameter(Mandatory)] - [string] $Name - ) - Write-Output "Hello, $Name!" -}