From 3adab3933c76b7602df8ab5c676aec05db929dd6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:26:04 +0200 Subject: [PATCH 1/6] use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index b7ab8b20..4f72685d 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -8,6 +8,14 @@ Param( [string] $Path ) +BeforeAll { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseDeclaredVarsMoreThanAssignments', 'scriptFiles', + Justification = 'scriptFiles is used in the test.' + )] + $scriptFiles = Get-ChildItem -Path $Path -Filter '*.ps1' -Recurse -File +} + Describe 'PSModule - SourceCode tests' { Context 'function/filter' { @@ -67,4 +75,23 @@ Describe 'PSModule - SourceCode tests' { # It 'datatype for parameters and parameter name are separated by a single space' {} # It 'parameters are separated by a blank line' {} } + + Context 'Compatability checks' { + It "Should use '[System.Environment]::ProcessorCount' instead of '$env:NUMBER_OF_PROCESSORS'" { + $issues = @('') + $scriptFiles | ForEach-Object { + $fileContent = Get-Content -Path $_.FullName -Raw + if ($fileContent -match '\$env:NUMBER_OF_PROCESSORS') { + $issues += " - $($_.FullName): Use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS" + } + } + $issues -join [Environment]::NewLine | + Should -BeNullOrEmpty -Because 'the script should use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS' + } + } + + Context 'Module manifest' { + # It 'Module Manifest exists (maifest.psd1 or modulename.psd1)' {} + # It 'Module Manifest is valid' {} + } } From 46bf4f851de0d2a2db32f85b5894c20ce8687a28 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:27:30 +0200 Subject: [PATCH 2/6] Add a test --- tests/src/public/New-PSModuleTest.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/public/New-PSModuleTest.ps1 b/tests/src/public/New-PSModuleTest.ps1 index d97e8f33..b06d22fc 100644 --- a/tests/src/public/New-PSModuleTest.ps1 +++ b/tests/src/public/New-PSModuleTest.ps1 @@ -26,4 +26,5 @@ function New-PSModuleTest { [string] $Name ) Write-Output "Hello, $Name!" + Write-Output "Processor count: $env:NUMBER_OF_PROCESSORS" } From 8d5833c5162f529caa6523f9927419541e02bed1 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:35:17 +0200 Subject: [PATCH 3/6] test --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 4f72685d..25e0e8be 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -82,7 +82,9 @@ Describe 'PSModule - SourceCode tests' { $scriptFiles | ForEach-Object { $fileContent = Get-Content -Path $_.FullName -Raw if ($fileContent -match '\$env:NUMBER_OF_PROCESSORS') { - $issues += " - $($_.FullName): Use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS" + $issues += " - $($_.FullName)" + #Get linenumber of the match + Write-Verbose ($matches | Out-String) -Verbose } } $issues -join [Environment]::NewLine | From 09e79efd20af76730434c52e99eb93eb661f9a39 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:39:50 +0200 Subject: [PATCH 4/6] test --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 25e0e8be..9a81bf25 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -80,12 +80,16 @@ Describe 'PSModule - SourceCode tests' { It "Should use '[System.Environment]::ProcessorCount' instead of '$env:NUMBER_OF_PROCESSORS'" { $issues = @('') $scriptFiles | ForEach-Object { - $fileContent = Get-Content -Path $_.FullName -Raw - if ($fileContent -match '\$env:NUMBER_OF_PROCESSORS') { - $issues += " - $($_.FullName)" - #Get linenumber of the match - Write-Verbose ($matches | Out-String) -Verbose + Select-String -Path $_.FullName -Pattern '\$env:NUMBER_OF_PROCESSORS' -AllMatches | ForEach-Object { + $issues += " - $($_.Path):$($_.LineNumber):$($_.Line)" + Write-Verbose ($_ | Out-String) -Verbose } + # $fileContent = Get-Content -Path $_.FullName -Raw + # if ($fileContent -match '\$env:NUMBER_OF_PROCESSORS') { + # $issues += " - $($_.FullName)" + # #Get linenumber of the match + # Write-Verbose ($matches | Out-String) -Verbose + # } } $issues -join [Environment]::NewLine | Should -BeNullOrEmpty -Because 'the script should use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS' From 646dbc2b72435385aa4262fba90dc66126587807 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:43:42 +0200 Subject: [PATCH 5/6] FIx --- scripts/tests/PSModule/SourceCode.Tests.ps1 | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/scripts/tests/PSModule/SourceCode.Tests.ps1 b/scripts/tests/PSModule/SourceCode.Tests.ps1 index 9a81bf25..86783876 100644 --- a/scripts/tests/PSModule/SourceCode.Tests.ps1 +++ b/scripts/tests/PSModule/SourceCode.Tests.ps1 @@ -42,7 +42,7 @@ Describe 'PSModule - SourceCode tests' { " - $($_.filePath): Function/filter name [$($_.functionName)]. Change file name or function/filter name so they match." } $issues -join [Environment]::NewLine | - Should -BeNullOrEmpty -Because 'the script files should be called the same as the function they contain' + 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' {} @@ -81,18 +81,11 @@ Describe 'PSModule - SourceCode tests' { $issues = @('') $scriptFiles | ForEach-Object { Select-String -Path $_.FullName -Pattern '\$env:NUMBER_OF_PROCESSORS' -AllMatches | ForEach-Object { - $issues += " - $($_.Path):$($_.LineNumber):$($_.Line)" - Write-Verbose ($_ | Out-String) -Verbose + $issues += " - $($_.Path):L$($_.LineNumber)" } - # $fileContent = Get-Content -Path $_.FullName -Raw - # if ($fileContent -match '\$env:NUMBER_OF_PROCESSORS') { - # $issues += " - $($_.FullName)" - # #Get linenumber of the match - # Write-Verbose ($matches | Out-String) -Verbose - # } } $issues -join [Environment]::NewLine | - Should -BeNullOrEmpty -Because 'the script should use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS' + Should -BeNullOrEmpty -Because 'the script should use [System.Environment]::ProcessorCount instead of $env:NUMBER_OF_PROCESSORS' } } From 49606abcd994303c39f96e5dd6696db62d10b15f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 13 Apr 2024 19:44:27 +0200 Subject: [PATCH 6/6] Refactor New-PSModuleTest function to remove unused code --- tests/src/public/New-PSModuleTest.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/src/public/New-PSModuleTest.ps1 b/tests/src/public/New-PSModuleTest.ps1 index b06d22fc..d97e8f33 100644 --- a/tests/src/public/New-PSModuleTest.ps1 +++ b/tests/src/public/New-PSModuleTest.ps1 @@ -26,5 +26,4 @@ function New-PSModuleTest { [string] $Name ) Write-Output "Hello, $Name!" - Write-Output "Processor count: $env:NUMBER_OF_PROCESSORS" }