From 4e6765c1939a178497a13791deb12b8a10bf92a0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:30:11 +0200 Subject: [PATCH 1/8] test for cmdletbinding --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 18c2d29f..84c4fd81 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -68,7 +68,7 @@ Describe 'PSModule - SourceCode tests' { Should -BeNullOrEmpty -Because "the script should use '`$null = ' instead of ' | Out-Null'" } - It "Should not use ternary operations for compatability reasons" { + It 'Should not use ternary operations for compatability reasons' { $issues = @('') $scriptFiles | ForEach-Object { Select-String -Path $_.FullName -Pattern '(? Date: Sun, 14 Apr 2024 17:32:05 +0200 Subject: [PATCH 2/8] test --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 5 ++++- tests/src/public/Get-PSModuleTest.ps1 | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 84c4fd81..4e59dfb6 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -95,9 +95,12 @@ Describe 'PSModule - SourceCode tests' { $tokens = $scriptAst.FindAll({ $true }, $true) foreach ($token in $tokens) { if ($token.TypeName.Name -eq 'CmdletBinding') { - $issues += " - $($_.Extent.File)" + $found = $true } } + if (-not $found) { + $issues += " - $scriptFilePath" + } } $issues -join [Environment]::NewLine | Should -BeNullOrEmpty -Because 'the script should have [CmdletBinding()] attribute' diff --git a/tests/src/public/Get-PSModuleTest.ps1 b/tests/src/public/Get-PSModuleTest.ps1 index 0e9aacfe..4728e8a9 100644 --- a/tests/src/public/Get-PSModuleTest.ps1 +++ b/tests/src/public/Get-PSModuleTest.ps1 @@ -10,7 +10,6 @@ function Get-PSModuleTest { "Hello, World!" #> - [CmdletBinding()] param ( # Name of the person to greet. [Parameter(Mandatory)] From 3108545a2a0b1c0c21c3c58413e327515f647f09 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:43:47 +0200 Subject: [PATCH 3/8] test --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 4e59dfb6..b3d2e1eb 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -10,10 +10,13 @@ Param( BeforeAll { [Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSUseDeclaredVarsMoreThanAssignments', 'scriptFiles', + 'PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'scriptFiles is used in the test.' )] $scriptFiles = Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File + $functionFiles = Get-ChildItem -Directory -Path $Path | + Where-Object { $_.Name -in 'public', 'private' } | + Get-ChildItem -Filter '*.ps1' -File } Describe 'PSModule - SourceCode tests' { @@ -86,12 +89,11 @@ Describe 'PSModule - SourceCode tests' { # It 'has synopsis for all functions' {} # It 'has description for all functions' {} # It 'has examples for all functions' {} - # It 'has output documentation for all functions' {} It 'should have [CmdletBinding()] attribute' { $issues = @('') - $scriptFiles | ForEach-Object { - $scriptFilePath = $_.FullName - $scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($scriptFilePath, [ref]$null, [ref]$null) + $functionFiles | ForEach-Object { + $filePath = $_.FullName + $scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null) $tokens = $scriptAst.FindAll({ $true }, $true) foreach ($token in $tokens) { if ($token.TypeName.Name -eq 'CmdletBinding') { @@ -99,7 +101,7 @@ Describe 'PSModule - SourceCode tests' { } } if (-not $found) { - $issues += " - $scriptFilePath" + $issues += " - $filePath" } } $issues -join [Environment]::NewLine | From 1947228dd487d6d7a2950c0cc93b0ce1f0f259d2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:48:13 +0200 Subject: [PATCH 4/8] test --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index b3d2e1eb..d9db4ebc 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -92,11 +92,15 @@ Describe 'PSModule - SourceCode tests' { It 'should have [CmdletBinding()] attribute' { $issues = @('') $functionFiles | ForEach-Object { + $found = $false $filePath = $_.FullName + Write-Verbose "Processing [$filePath]" $scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null) $tokens = $scriptAst.FindAll({ $true }, $true) foreach ($token in $tokens) { + Write-Verbose "Looking at: [$($token.TypeName.Name)]" if ($token.TypeName.Name -eq 'CmdletBinding') { + Write-Verbose "--- Found CmdletBinding attribute" $found = $true } } From 2c280b299e26a0f18f3f3099ad9bb13589c71970 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:50:39 +0200 Subject: [PATCH 5/8] Fix --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index d9db4ebc..f0ef2525 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -94,13 +94,10 @@ Describe 'PSModule - SourceCode tests' { $functionFiles | ForEach-Object { $found = $false $filePath = $_.FullName - Write-Verbose "Processing [$filePath]" $scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null) $tokens = $scriptAst.FindAll({ $true }, $true) foreach ($token in $tokens) { - Write-Verbose "Looking at: [$($token.TypeName.Name)]" if ($token.TypeName.Name -eq 'CmdletBinding') { - Write-Verbose "--- Found CmdletBinding attribute" $found = $true } } From 104dd2ec553b055eea0efc7f682f430d90014651 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:53:42 +0200 Subject: [PATCH 6/8] Fix --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index f0ef2525..82946840 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -89,6 +89,7 @@ Describe 'PSModule - SourceCode tests' { # It 'has synopsis for all functions' {} # It 'has description for all functions' {} # It 'has examples for all functions' {} + It 'should have [CmdletBinding()] attribute' { $issues = @('') $functionFiles | ForEach-Object { From 68214bfa5f28e371d2c9690a416b54f2c4092d6f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:54:08 +0200 Subject: [PATCH 7/8] Fix test --- tests/src/public/Get-PSModuleTest.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/public/Get-PSModuleTest.ps1 b/tests/src/public/Get-PSModuleTest.ps1 index 4728e8a9..0e9aacfe 100644 --- a/tests/src/public/Get-PSModuleTest.ps1 +++ b/tests/src/public/Get-PSModuleTest.ps1 @@ -10,6 +10,7 @@ function Get-PSModuleTest { "Hello, World!" #> + [CmdletBinding()] param ( # Name of the person to greet. [Parameter(Mandatory)] From e6a8be109f0b2ec90a04a0bf033fc0397d04fcbc Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 14 Apr 2024 17:58:31 +0200 Subject: [PATCH 8/8] fix linting issues --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 82946840..0d189f99 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -9,14 +9,13 @@ Param( ) BeforeAll { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSUseDeclaredVarsMoreThanAssignments', '', - Justification = 'scriptFiles is used in the test.' - )] $scriptFiles = Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File $functionFiles = Get-ChildItem -Directory -Path $Path | Where-Object { $_.Name -in 'public', 'private' } | Get-ChildItem -Filter '*.ps1' -File + + Write-Verbose "Found $($scriptFiles.Count) script files in $Path" + Write-Verbose "Found $($functionFiles.Count) function files in $Path" } Describe 'PSModule - SourceCode tests' { @@ -89,7 +88,7 @@ Describe 'PSModule - SourceCode tests' { # It 'has synopsis for all functions' {} # It 'has description for all functions' {} # It 'has examples for all functions' {} - + It 'should have [CmdletBinding()] attribute' { $issues = @('') $functionFiles | ForEach-Object {