From 42af4012c83b00d4cf59e767d4262eeb5f14c7f6 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Wed, 20 Jan 2021 17:24:15 +0000 Subject: [PATCH 01/12] Change of SonarQubeRulesPath to ScriptAnalyzerRulesPath and allow to accept multiple paths --- Checks/Script.Tests.ps1 | 46 ++++++++++++++----- .../PSQualityCheck/Invoke-PSQualityCheck.ps1 | 14 +++--- .../Unit/Invoke-PSQualityCheck.Tests.ps1 | 4 +- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Checks/Script.Tests.ps1 b/Checks/Script.Tests.ps1 index bab0a53..adbcb61 100644 --- a/Checks/Script.Tests.ps1 +++ b/Checks/Script.Tests.ps1 @@ -2,8 +2,8 @@ param( [parameter(Mandatory = $true)] [string[]]$Source, - [parameter(Mandatory = $false)] - [string]$SonarQubeRules + [parameter(Mandatory = $true)] + [string[]]$ScriptAnalyzerRulesPath ) BeforeDiscovery { @@ -23,17 +23,37 @@ BeforeDiscovery { } + if ( -not ($ScriptAnalyzerRulesPath -is [Array])) { + $ScriptAnalyzerRulesPath = @($ScriptAnalyzerRulesPath) + } + + $rulesPath = @() + + $ScriptAnalyzerRulesPath | ForEach-Object { + + $rulesPath += @{ + 'Path' = $_ + + } + + } + } Describe "Script Tests" { - Context "Script: <_.Name> at <_.Directory>" -ForEach $scriptFiles { + Context "Script: at " -Foreach $scriptFiles { - BeforeEach { + BeforeAll { + + $file = $_ - $scriptFile = $_.FullName + } + + BeforeEach { - $fileContent = Get-FileContent -Path $_.FullName + $scriptFile = $file.FullName + $fileContent = Get-FileContent -Path $file.FullName if (-not([string]::IsNullOrEmpty($fileContent))) { ($ParsedFile, $ErrorCount) = Get-ParsedContent -Content $fileContent @@ -198,21 +218,23 @@ Describe "Script Tests" { } - It "check script contains no PSScriptAnalyser SonarQube rule failures" { + It "check script contains no PSScriptAnalyser rule failures '<_.Path>" -TestCases $rulesPath { + + param($Path) - if ( [string]::IsNullOrEmpty($SonarQubeRules) ) { + if ( [string]::IsNullOrEmpty($Path)) { - Set-ItResult -Inconclusive -Because "No SonarQube PSScriptAnalyzer rules folder specified" + Set-ItResult -Inconclusive -Because "Empty ScriptAnalyzerRulesPath '$Path'" } - if ( -not (Test-Path -Path $SonarQubeRules -ErrorAction SilentlyContinue)) { + if ( -not (Test-Path -Path $Path -ErrorAction SilentlyContinue)) { - Set-ItResult -Inconclusive -Because "SonarQube PSScriptAnalyzer rules not found" + Set-ItResult -Inconclusive -Because "ScriptAnalyzerRulesPath path '$Path' not found" } - $AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile -CustomRulePath $SonarQubeRules) + $AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile -CustomRulePath $Path) $AnalyserFailures | ForEach-Object { $_.Message } | Should -BeNullOrEmpty diff --git a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 index 54c91f0..7a76b0b 100644 --- a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 +++ b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 @@ -15,8 +15,8 @@ function Invoke-PSQualityCheck { .PARAMETER Recurse A switch specifying whether or not to recursively search the path specified - .PARAMETER SonarQubeRulesPath - A path the the external PSScriptAnalyzer rules for SonarQube + .PARAMETER ScriptAnalyzerRulesPath + A path the the external PSScriptAnalyzer rules .PARAMETER ShowCheckResults Show a summary of the Check results at the end of processing @@ -67,9 +67,9 @@ function Invoke-PSQualityCheck { This will call the quality checks with multiple files. Files can be either scripts or modules .EXAMPLE - Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -SonarQubeRulesPath 'C:\SonarQubeRules' + Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -ScriptAnalyzerRulesPath 'C:\ScriptAnalyzerRulesPath' - This will call the quality checks with single file and the extra PSScriptAnalyzer rules used by SonarQube + This will call the quality checks with single file and the extra PSScriptAnalyzer rules .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults @@ -102,7 +102,7 @@ function Invoke-PSQualityCheck { [switch]$Recurse, [Parameter(Mandatory = $false)] - [String]$SonarQubeRulesPath, + [String[]]$ScriptAnalyzerRulesPath, [switch]$ShowCheckResults, @@ -234,7 +234,7 @@ function Invoke-PSQualityCheck { $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse # Run the Script tests against all the extracted functions .ps1 files - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; SonarQubeRules = $SonarQubeRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } $PesterConfiguration.Run.Container = $container3 $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration @@ -243,7 +243,7 @@ function Invoke-PSQualityCheck { if ($scriptsToTest.Count -ge 1) { # Run the Script tests against all the valid script files found - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; SonarQubeRules = $SonarQubeRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } $PesterConfiguration.Run.Container = $container3 $scriptResults = Invoke-Pester -Configuration $PesterConfiguration diff --git a/Source/Tests/Unit/Invoke-PSQualityCheck.Tests.ps1 b/Source/Tests/Unit/Invoke-PSQualityCheck.Tests.ps1 index d130235..dce9b78 100644 --- a/Source/Tests/Unit/Invoke-PSQualityCheck.Tests.ps1 +++ b/Source/Tests/Unit/Invoke-PSQualityCheck.Tests.ps1 @@ -1,9 +1,9 @@ Describe "Invoke-PSQualityCheck.Tests" { - Context "Parameter Tests" -ForEach @( + Context "Parameter Tests" -Foreach @( @{ 'Name' = 'Path'; 'Type' = 'String[]'; 'MandatoryFlag' = $true; 'ParameterSet' = 'Path' } @{ 'Name' = 'File'; 'Type' = 'String[]'; 'MandatoryFlag' = $true; 'ParameterSet' = 'File' } - @{ 'Name' = 'SonarQubeRulesPath'; 'Type' = 'String'; 'MandatoryFlag' = $false; 'ParameterSet' = '__AllParameterSets' } + @{ 'Name' = 'ScriptAnalyzerRulesPath'; 'Type' = 'String[]'; 'MandatoryFlag' = $false; 'ParameterSet' = '__AllParameterSets' } @{ 'Name' = 'ShowCheckResults'; 'Type' = 'SwitchParameter'; 'MandatoryFlag' = $false; 'ParameterSet' = '__AllParameterSets' } @{ 'Name' = 'ExportCheckResults'; 'Type' = 'SwitchParameter'; 'MandatoryFlag' = $false; 'ParameterSet' = '__AllParameterSets' } @{ 'Name' = 'Passthru'; 'Type' = 'SwitchParameter'; 'MandatoryFlag' = $false; 'ParameterSet' = '__AllParameterSets' } From 759fc0a879714826551b6eb9b7ca50526bf48748 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Wed, 20 Jan 2021 17:24:34 +0000 Subject: [PATCH 02/12] Module rebuilt to version 1.2.0 --- PSQualityCheck.Functions.psd1 | 2 +- PSQualityCheck.psd1 | 2 +- PSQualityCheck.psm1 | 14 +++++++------- README.md | 18 +++++++++--------- Source/Build.Properties.json | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/PSQualityCheck.Functions.psd1 b/PSQualityCheck.Functions.psd1 index d73aaf8..57f4abf 100644 --- a/PSQualityCheck.Functions.psd1 +++ b/PSQualityCheck.Functions.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSQualityCheck.Functions.psm1' # Version number of this module. -ModuleVersion = '1.1.1' +ModuleVersion = '1.2.0' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/PSQualityCheck.psd1 b/PSQualityCheck.psd1 index ddcf793..5d5b505 100644 --- a/PSQualityCheck.psd1 +++ b/PSQualityCheck.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSQualityCheck.psm1' # Version number of this module. -ModuleVersion = '1.1.1' +ModuleVersion = '1.2.0' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/PSQualityCheck.psm1 b/PSQualityCheck.psm1 index 3731999..dd25e69 100644 --- a/PSQualityCheck.psm1 +++ b/PSQualityCheck.psm1 @@ -15,8 +15,8 @@ function Invoke-PSQualityCheck { .PARAMETER Recurse A switch specifying whether or not to recursively search the path specified - .PARAMETER SonarQubeRulesPath - A path the the external PSScriptAnalyzer rules for SonarQube + .PARAMETER ScriptAnalyzerRulesPath + A path the the external PSScriptAnalyzer rules .PARAMETER ShowCheckResults Show a summary of the Check results at the end of processing @@ -67,9 +67,9 @@ function Invoke-PSQualityCheck { This will call the quality checks with multiple files. Files can be either scripts or modules .EXAMPLE - Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -SonarQubeRulesPath 'C:\SonarQubeRules' + Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -ScriptAnalyzerRulesPath 'C:\ScriptAnalyzerRulesPath' - This will call the quality checks with single file and the extra PSScriptAnalyzer rules used by SonarQube + This will call the quality checks with single file and the extra PSScriptAnalyzer rules .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults @@ -102,7 +102,7 @@ function Invoke-PSQualityCheck { [switch]$Recurse, [Parameter(Mandatory = $false)] - [String]$SonarQubeRulesPath, + [String[]]$ScriptAnalyzerRulesPath, [switch]$ShowCheckResults, @@ -234,7 +234,7 @@ function Invoke-PSQualityCheck { $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse # Run the Script tests against all the extracted functions .ps1 files - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; SonarQubeRules = $SonarQubeRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } $PesterConfiguration.Run.Container = $container3 $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration @@ -243,7 +243,7 @@ function Invoke-PSQualityCheck { if ($scriptsToTest.Count -ge 1) { # Run the Script tests against all the valid script files found - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; SonarQubeRules = $SonarQubeRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } $PesterConfiguration.Run.Container = $container3 $scriptResults = Invoke-Pester -Configuration $PesterConfiguration diff --git a/README.md b/README.md index 81440b4..35e9fe1 100644 --- a/README.md +++ b/README.md @@ -83,35 +83,35 @@ Copy the files to **one** of the available module folders: then run using the examples below as a guide: -#### Check the folder C:\Scripts: +#### Check the folder C:\Scripts `Invoke-PSQualityCheck -Path 'C:\Scripts'` -#### Check the folder C:\Scripts and all subfolders beneath it: +#### Check the folder C:\Scripts and all subfolders beneath it `Invoke-PSQualityCheck -Path 'C:\Scripts' -Recurse` -#### Check the folders C:\Scripts and C:\MoreScripts': +#### Check the folders C:\Scripts and C:\MoreScripts' `Invoke-PSQualityCheck -Path @('C:\Scripts', 'C:\MoreScripts')` -#### Check the folders C:\Scripts and C:\MoreScripts' and all subfolders beneath both folders: +#### Check the folders C:\Scripts and C:\MoreScripts' and all subfolders beneath both folders `Invoke-PSQualityCheck -Path @('C:\Scripts', 'C:\MoreScripts') -Recurse` -#### Check the file C:\Scripts\Script.ps1: +#### Check the file C:\Scripts\Script.ps1 `Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1'` -#### Check the files C:\Scripts\Script1.ps1, C:\Scripts\Script2.ps1: +#### Check the files C:\Scripts\Script1.ps1, C:\Scripts\Script2.ps1 `Invoke-PSQualityCheck -File @('C:\Scripts\Script.ps1', 'C:\Scripts\Script.ps1')` -#### Check the file C:\Scripts\Script.ps1 including the extra PSScriptAnalyzer rules used by SonarQube: +#### Check the file C:\Scripts\Script.ps1 including the extra PSScriptAnalyzer rules -`Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -SonarQubeRulesPath 'C:\SonarQubeRules'` +`Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -ScriptAnalyzerRulesPath 'C:\ScriptAnalyzerRulesPath'` -#### Check the folder C:\Scripts and all subfolders beneath it and display a summary of the checks performed: +#### Check the folder C:\Scripts and all subfolders beneath it and display a summary of the checks performed `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults` diff --git a/Source/Build.Properties.json b/Source/Build.Properties.json index ed42e7d..5b8d8cb 100644 --- a/Source/Build.Properties.json +++ b/Source/Build.Properties.json @@ -18,7 +18,7 @@ }, "HelpInfoURI": "https://github.com/andrewrdavidson/PSQualityCheck/wiki", "LicenseUri": "https://github.com/andrewrdavidson/PSQualityCheck/blob/main/LICENSE", - "ModuleVersion": "1.1.1", + "ModuleVersion": "1.2.0", "NestedModules": { "PSQualityCheck": "PSQualityCheck.Functions.psd1", "PSQualityCheck.Functions": null From e356f0cdb0bae139a97dac63a7e1509ccdbb4e2d Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:20:55 +0000 Subject: [PATCH 03/12] Additing -Include and -Exclude parameters for including and excluding tests by Tag --- Checks/Module.Tests.ps1 | 18 +- Checks/Script.Tests.ps1 | 42 ++--- .../PSQualityCheck.Functions/Get-TagList.ps1 | 72 ++++++++ .../PSQualityCheck/Invoke-PSQualityCheck.ps1 | 154 ++++++++++++++++-- Source/Tests/Unit/Get-TagList.Tests.ps1 | 7 + 5 files changed, 249 insertions(+), 44 deletions(-) create mode 100644 Source/PSQualityCheck.Functions/Get-TagList.ps1 create mode 100644 Source/Tests/Unit/Get-TagList.Tests.ps1 diff --git a/Checks/Module.Tests.ps1 b/Checks/Module.Tests.ps1 index 74e20c7..8b94fd3 100644 --- a/Checks/Module.Tests.ps1 +++ b/Checks/Module.Tests.ps1 @@ -22,9 +22,9 @@ BeforeDiscovery { } -Describe "Module Tests" { +Describe "Module Tests" -Tag "Module" { - Context "Script: <_.Name> at <_.Directory>" -ForEach $moduleFiles { + Context "Script: <_.Name> at <_.Directory>" -Foreach $moduleFiles { BeforeEach { @@ -35,19 +35,19 @@ Describe "Module Tests" { } - It "Module should exist" { + It "Module should exist" -Tag "ModuleShouldExist" { $moduleFile | Should -Exist } - It "Manifest should exist" { + It "Manifest should exist" -Tag "ManifestShouldExist" { $manifestFile | Should -Exist } - It "Manifest should be valid" { + It "Manifest should be valid" -Tag "ValidManifest" { $manifest = Test-ModuleManifest -Path $manifestFile -ErrorAction SilentlyContinue @@ -55,25 +55,25 @@ Describe "Module Tests" { } - It "Manifest should export Functions" { + It "Manifest should export Functions" -Tag "ModuleShouldExportFunctions" { ($ExportedCommandsCount) | Should -BeGreaterOrEqual 1 } - It "Module should have Functions" { + It "Module should have Functions" -Tag "ModuleShouldHaveFunctions" { ($CommandInModuleCount) | Should -BeGreaterOrEqual 1 } - It "all exported Functions from Manifest should exist in the Module" { + It "all exported Functions from Manifest should exist in the Module" -Tag "FunctionsFromManifestExistInModule" { ($ExportedCommandsCount -eq $CommandFoundInModuleCount -and $ExportedCommandsCount -ge 1) | Should -BeTrue } - It "all Functions in the Module should exist in Manifest " { + It "all Functions in the Module should exist in Manifest " -Tag "FunctionsFromModuleExistInManifest" { ($CommandInModuleCount -eq $CommandFoundInManifestCount -and $CommandFoundInManifestCount -ge 1 ) | Should -BeTrue diff --git a/Checks/Script.Tests.ps1 b/Checks/Script.Tests.ps1 index adbcb61..682d45f 100644 --- a/Checks/Script.Tests.ps1 +++ b/Checks/Script.Tests.ps1 @@ -40,7 +40,7 @@ BeforeDiscovery { } -Describe "Script Tests" { +Describe "Script Tests" -Tag "Script" { Context "Script: at " -Foreach $scriptFiles { @@ -66,13 +66,13 @@ Describe "Script Tests" { } - It "check script has valid PowerShell syntax" { + It "check script has valid PowerShell syntax" -Tag "ValidSyntax" { $ErrorCount | Should -Be 0 } - It "check help must contain required elements" { + It "check help must contain required elements" -Tag "HelpMustContainRequiredElements" { { @@ -87,7 +87,8 @@ Describe "Script Tests" { Should -Not -Throw } - It "check help must not contain unspecified elements" { + + It "check help must not contain unspecified elements" -Tag "HelpMustContainUnspecifiedElements" { { @@ -103,7 +104,7 @@ Describe "Script Tests" { } - It "check help elements text is not empty" { + It "check help elements text is not empty" -Tag "HelpElementsNotEmpty" { { @@ -118,7 +119,7 @@ Describe "Script Tests" { } - It "check help elements Min/Max counts are valid" { + It "check help elements Min/Max counts are valid" -Tag "HelpElementsMinMaxCount" { { @@ -133,7 +134,7 @@ Describe "Script Tests" { } - It "check script contains [CmdletBinding] attribute" { + It "check script contains [CmdletBinding] attribute" -Tag "ContainsCmdletBinding" { $cmdletBindingCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "CmdletBinding")).Count @@ -141,7 +142,7 @@ Describe "Script Tests" { } - It "check script contains [OutputType] attribute" { + It "check script contains [OutputType] attribute" -Tag "ContainsOutputType" { $outputTypeCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "OutputType")).Count @@ -149,7 +150,7 @@ Describe "Script Tests" { } - It "check script [OutputType] attribute is not empty" { + It "check script [OutputType] attribute is not empty" -Tag "OutputTypeNotEmpty" { $outputTypeToken = (Get-Token -ParsedContent $ParsedFile -Type "Attribute" -Content "OutputType") @@ -159,15 +160,16 @@ Describe "Script Tests" { } - It "check script contains param attribute" { + # Note: Disabled because I'm questioning the validity of the rule. So many function haven't got a need for params + # It "check script contains param attribute" -Tag "ContainsParam" { - $paramCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Keyword" -Content "param")).Count + # $paramCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Keyword" -Content "param")).Count - $paramCount | Should -Be 1 + # $paramCount | Should -Be 1 - } + # } - It "check script param block variables have type" { + It "check script param block variables have type" -Tag "ParamVariablesHaveType" { $parameterVariables = Get-ScriptParameter -Content $fileContent @@ -185,7 +187,7 @@ Describe "Script Tests" { } - It "check .PARAMETER help matches variables in param block" { + It "check .PARAMETER help matches variables in param block" -Tag "HelpMatchesParamVariables" { { @@ -202,15 +204,17 @@ Describe "Script Tests" { } - It "check script contains no PSScriptAnalyzer suppressions" { + It "check script contains no PSScriptAnalyzer suppressions" -Tag "NoScriptAnalyzerSuppressions" { $suppressCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "Diagnostics.CodeAnalysis.SuppressMessageAttribute")).Count + $suppressCount | Should -Be 0 + $suppressCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "Diagnostics.CodeAnalysis.SuppressMessage")).Count $suppressCount | Should -Be 0 } - It "check script contains no PSScriptAnalyzer failures" { + It "check script contains no PSScriptAnalyzer failures" -Tag "NoScriptAnalyzerFailures" { $AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile) @@ -218,7 +222,7 @@ Describe "Script Tests" { } - It "check script contains no PSScriptAnalyser rule failures '<_.Path>" -TestCases $rulesPath { + It "check script contains no PSScriptAnalyser rule failures '<_.Path>" -Tag "NoScriptAnalyzerExtraRulesFailures" -TestCases $rulesPath { param($Path) @@ -240,7 +244,7 @@ Describe "Script Tests" { } - It "check Import-Module statements have valid format" { + It "check Import-Module statements have valid format" -Tag "ValidImportModuleStatements" { $importModuleTokens = @($ParsedFile | Where-Object { $_.Type -eq "Command" -and $_.Content -eq "Import-Module" }) diff --git a/Source/PSQualityCheck.Functions/Get-TagList.ps1 b/Source/PSQualityCheck.Functions/Get-TagList.ps1 new file mode 100644 index 0000000..3d6a038 --- /dev/null +++ b/Source/PSQualityCheck.Functions/Get-TagList.ps1 @@ -0,0 +1,72 @@ +function Get-TagList { + <# + .SYNOPSIS + Return a list of test tags + + .DESCRIPTION + Return a list of test tags from the module and script checks file + + .EXAMPLE + ($moduleTags, $scriptTags) = Get-TagList + + #> + [CmdletBinding()] + [OutputType([System.Object[]])] + param ( + ) + + # Get the list of test tags from the checks files + $moduleTags = @() + $scriptTags = @() + + $modulePath = (Get-Module -Name 'PSQualityCheck').ModuleBase + + $checksPath = (Join-Path -Path $modulePath -ChildPath "Checks") + + Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Module.Tests.ps1") -Raw | ForEach-Object { + $ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null) + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "Describe" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $moduleTags += $_.CommandElements[3].Value + } + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "It" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $moduleTags += $_.CommandElements[3].Value + } + } + + Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Script.Tests.ps1") -Raw | ForEach-Object { + $ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null) + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "Describe" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $scriptTags += $_.CommandElements[3].Value + } + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "It" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $scriptTags += $_.CommandElements[3].Value + } + } + + return $moduleTags, $scriptTags + +} diff --git a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 index 7a76b0b..582b19f 100644 --- a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 +++ b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 @@ -30,6 +30,12 @@ function Invoke-PSQualityCheck { .PARAMETER PesterConfiguration A Pester configuration object to allow configuration of Pester + .PARAMETER Include + An array of test tags to run + + .PARAMETER Exclude + An array of test tags to not run + .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' @@ -111,7 +117,14 @@ function Invoke-PSQualityCheck { [switch]$Passthru, [Parameter(Mandatory = $false)] - [System.Object]$PesterConfiguration + [System.Object]$PesterConfiguration, + + [Parameter(Mandatory = $false)] + [String[]]$Include, + + [Parameter(Mandatory = $false)] + [String[]]$Exclude + ) Set-StrictMode -Version Latest @@ -127,8 +140,12 @@ function Invoke-PSQualityCheck { $scriptsToTest = @() $modulesToTest = @() + Write-Verbose "Starting" -Verbose + if ($PSBoundParameters.ContainsKey('Path')) { + Write-Verbose 'Found Path in $PSBoundParameters' -Verbose + if ($Path -isnot [string[]]) { $Path = @($Path) } @@ -161,6 +178,8 @@ function Invoke-PSQualityCheck { if ($PSBoundParameters.ContainsKey('File')) { + Write-Verbose 'Found File in $PSBoundParameters' -Verbose + if ($File -isnot [string[]]) { $File = @($File) } @@ -210,6 +229,102 @@ function Invoke-PSQualityCheck { $PesterConfiguration.Should.ErrorAction = 'Stop' } + # Get the list of test tags from the checks files + if ($PSBoundParameters.ContainsKey('Include') -or + $PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose 'Getting Tags' -Verbose + + ($moduleTags, $scriptTags) = Get-TagList + $moduleTagsToInclude = @() + $moduleTagsToExclude = @() + $scriptTagsToInclude = @() + $scriptTagsToExclude = @() + $runModuleCheck = $false + $runScriptCheck = $false + + Write-Verbose "Got ModuleTags: $ModuleTags" -Verbose + Write-Verbose "Got scriptTags: $scriptTags" -Verbose + + } + else { + $runModuleCheck = $true + $runScriptCheck = $true + } + + if ($PSBoundParameters.ContainsKey('Include')) { + + Write-Verbose 'Processing -Include' -Verbose + if ($Include -eq 'All') { + $moduleTagsToInclude = $moduleTags + $scriptTagsToInclude = $scriptTags + $runModuleCheck = $true + $runScriptCheck = $true + } + else { + # Validate tests to include from $Include + $Include | ForEach-Object { + if ($_ -in $moduleTags) { + $moduleTagsToInclude += $_ + $runModuleCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $Include | ForEach-Object { + if ($_ -in $scriptTags) { + $scriptTagsToInclude += $_ + $runScriptCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + } + $PesterConfiguration.Filter.Tag = $moduleTagsToInclude + $scriptTagsToInclude + + } + + if ($PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose 'Processing -Exclude' -Verbose + # Validate tests to exclude from $Exclude + $Exclude | ForEach-Object { + if ($_ -in $moduleTags) { + $moduleTagsToExclude += $_ + $runModuleCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $Exclude | ForEach-Object { + if ($_ -in $scriptTags) { + $scriptTagsToExclude += $_ + $runScriptCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $PesterConfiguration.Filter.ExcludeTag = $moduleTagsToExclude + $scriptTagsToExclude + + } + + # Need to work out which of the checks files can be run + if ($PSBoundParameters.ContainsKey('Include') -or + $PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose "moduleTagsToInclude $moduleTagsToInclude" -Verbose + Write-Verbose "scriptTagsToInclude $scriptTagsToInclude" -Verbose + Write-Verbose "moduleTagsToExclude $moduleTagsToExclude" -Verbose + Write-Verbose "scriptTagsToExclude $scriptTagsToExclude" -Verbose + + } + Write-Verbose "runModuleCheck $runModuleCheck" -Verbose + Write-Verbose "runScriptCheck $runScriptCheck" -Verbose + $moduleResults = $null $extractionResults = $null $extractedScriptResults = $null @@ -220,27 +335,34 @@ function Invoke-PSQualityCheck { # Location of files extracted from any passed modules $extractPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath (New-Guid).Guid - # Run the Module tests on all the valid module files found - $container1 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Module.Tests.ps1') -Data @{ Source = $modulesToTest } - $PesterConfiguration.Run.Container = $container1 - $moduleResults = Invoke-Pester -Configuration $PesterConfiguration + if ($runModuleCheck -eq $true) { - # Extract all the functions from the modules into individual .ps1 files ready for testing - $container2 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Function-Extraction.Tests.ps1') -Data @{ Source = $modulesToTest; ExtractPath = $extractPath } - $PesterConfiguration.Run.Container = $container2 - $extractionResults = Invoke-Pester -Configuration $PesterConfiguration + # Run the Module tests on all the valid module files found + $container1 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Module.Tests.ps1') -Data @{ Source = $modulesToTest } + $PesterConfiguration.Run.Container = $container1 + $moduleResults = Invoke-Pester -Configuration $PesterConfiguration - # Get a list of the 'extracted' function scripts .ps1 files - $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse + # Extract all the functions from the modules into individual .ps1 files ready for testing + $container2 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Function-Extraction.Tests.ps1') -Data @{ Source = $modulesToTest; ExtractPath = $extractPath } + $PesterConfiguration.Run.Container = $container2 + $extractionResults = Invoke-Pester -Configuration $PesterConfiguration - # Run the Script tests against all the extracted functions .ps1 files - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } - $PesterConfiguration.Run.Container = $container3 - $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration + } + + if ($runScriptCheck -eq $true -and (Test-Path -Path $extractPath -ErrorAction SilentlyContinue)) { + + # Get a list of the 'extracted' function scripts .ps1 files + $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse + + # Run the Script tests against all the extracted functions .ps1 files + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } + $PesterConfiguration.Run.Container = $container3 + $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration + } } - if ($scriptsToTest.Count -ge 1) { + if ($scriptsToTest.Count -ge 1 -and $runScriptCheck -eq $true) { # Run the Script tests against all the valid script files found $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } diff --git a/Source/Tests/Unit/Get-TagList.Tests.ps1 b/Source/Tests/Unit/Get-TagList.Tests.ps1 new file mode 100644 index 0000000..8a4b027 --- /dev/null +++ b/Source/Tests/Unit/Get-TagList.Tests.ps1 @@ -0,0 +1,7 @@ +Describe "Get-TagList.Tests" { + + Context "Function tests" { + + } + +} From 8bff1663dd6d1ec0388777af3eac8cd79f1ae1ba Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:21:35 +0000 Subject: [PATCH 04/12] Rebuilt module v1.2.0 --- PSQualityCheck.Functions.psd1 | 4 +- PSQualityCheck.Functions.psm1 | 73 ++++++++++++++++ PSQualityCheck.psd1 | 2 +- PSQualityCheck.psm1 | 154 ++++++++++++++++++++++++++++++---- 4 files changed, 214 insertions(+), 19 deletions(-) diff --git a/PSQualityCheck.Functions.psd1 b/PSQualityCheck.Functions.psd1 index 57f4abf..5c0fcc2 100644 --- a/PSQualityCheck.Functions.psd1 +++ b/PSQualityCheck.Functions.psd1 @@ -3,7 +3,7 @@ # # Generated by: Andrew Davidson # -# Generated on: 20/01/2021 +# Generated on: 21/01/2021 # @{ @@ -71,7 +71,7 @@ PowerShellVersion = '5.0' # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = 'Convert-Help', 'Export-FunctionsFromModule', 'Get-FileContent', 'Get-FileList', 'Get-FunctionCount', 'Get-ParsedContent', - 'Get-ParsedFile', 'Get-ScriptParameter', 'Get-Token', + 'Get-ParsedFile', 'Get-ScriptParameter', 'Get-TagList', 'Get-Token', 'Get-TokenComponent', 'Get-TokenMarker', 'Test-HelpTokensCountIsValid', 'Test-HelpTokensParamsMatch', 'Test-HelpTokensTextIsValid', 'Test-ImportModuleIsValid', diff --git a/PSQualityCheck.Functions.psm1 b/PSQualityCheck.Functions.psm1 index 50de042..b818b74 100644 --- a/PSQualityCheck.Functions.psm1 +++ b/PSQualityCheck.Functions.psm1 @@ -696,6 +696,79 @@ function Get-ScriptParameter { } +function Get-TagList { + <# + .SYNOPSIS + Return a list of test tags + + .DESCRIPTION + Return a list of test tags from the module and script checks file + + .EXAMPLE + ($moduleTags, $scriptTags) = Get-TagList + + #> + [CmdletBinding()] + [OutputType([System.Object[]])] + param ( + ) + + # Get the list of test tags from the checks files + $moduleTags = @() + $scriptTags = @() + + $modulePath = (Get-Module -Name 'PSQualityCheck').ModuleBase + + $checksPath = (Join-Path -Path $modulePath -ChildPath "Checks") + + Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Module.Tests.ps1") -Raw | ForEach-Object { + $ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null) + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "Describe" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $moduleTags += $_.CommandElements[3].Value + } + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "It" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $moduleTags += $_.CommandElements[3].Value + } + } + + Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Script.Tests.ps1") -Raw | ForEach-Object { + $ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null) + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "Describe" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $scriptTags += $_.CommandElements[3].Value + } + $ast.FindAll( { + param($node) + $node -is [System.Management.Automation.Language.CommandAst] -and + $node.CommandElements[0].Value -eq "It" -and + $node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and + $node.CommandElements[2].ParameterName -eq "Tag" + }, $true) | ForEach-Object { + $scriptTags += $_.CommandElements[3].Value + } + } + + return $moduleTags, $scriptTags + +} + function Get-Token { <# .SYNOPSIS diff --git a/PSQualityCheck.psd1 b/PSQualityCheck.psd1 index 5d5b505..6869e71 100644 --- a/PSQualityCheck.psd1 +++ b/PSQualityCheck.psd1 @@ -3,7 +3,7 @@ # # Generated by: Andrew Davidson # -# Generated on: 20/01/2021 +# Generated on: 21/01/2021 # @{ diff --git a/PSQualityCheck.psm1 b/PSQualityCheck.psm1 index dd25e69..ff28652 100644 --- a/PSQualityCheck.psm1 +++ b/PSQualityCheck.psm1 @@ -30,6 +30,12 @@ function Invoke-PSQualityCheck { .PARAMETER PesterConfiguration A Pester configuration object to allow configuration of Pester + .PARAMETER Include + An array of test tags to run + + .PARAMETER Exclude + An array of test tags to not run + .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' @@ -111,7 +117,14 @@ function Invoke-PSQualityCheck { [switch]$Passthru, [Parameter(Mandatory = $false)] - [System.Object]$PesterConfiguration + [System.Object]$PesterConfiguration, + + [Parameter(Mandatory = $false)] + [String[]]$Include, + + [Parameter(Mandatory = $false)] + [String[]]$Exclude + ) Set-StrictMode -Version Latest @@ -127,8 +140,12 @@ function Invoke-PSQualityCheck { $scriptsToTest = @() $modulesToTest = @() + Write-Verbose "Starting" -Verbose + if ($PSBoundParameters.ContainsKey('Path')) { + Write-Verbose 'Found Path in $PSBoundParameters' -Verbose + if ($Path -isnot [string[]]) { $Path = @($Path) } @@ -161,6 +178,8 @@ function Invoke-PSQualityCheck { if ($PSBoundParameters.ContainsKey('File')) { + Write-Verbose 'Found File in $PSBoundParameters' -Verbose + if ($File -isnot [string[]]) { $File = @($File) } @@ -210,6 +229,102 @@ function Invoke-PSQualityCheck { $PesterConfiguration.Should.ErrorAction = 'Stop' } + # Get the list of test tags from the checks files + if ($PSBoundParameters.ContainsKey('Include') -or + $PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose 'Getting Tags' -Verbose + + ($moduleTags, $scriptTags) = Get-TagList + $moduleTagsToInclude = @() + $moduleTagsToExclude = @() + $scriptTagsToInclude = @() + $scriptTagsToExclude = @() + $runModuleCheck = $false + $runScriptCheck = $false + + Write-Verbose "Got ModuleTags: $ModuleTags" -Verbose + Write-Verbose "Got scriptTags: $scriptTags" -Verbose + + } + else { + $runModuleCheck = $true + $runScriptCheck = $true + } + + if ($PSBoundParameters.ContainsKey('Include')) { + + Write-Verbose 'Processing -Include' -Verbose + if ($Include -eq 'All') { + $moduleTagsToInclude = $moduleTags + $scriptTagsToInclude = $scriptTags + $runModuleCheck = $true + $runScriptCheck = $true + } + else { + # Validate tests to include from $Include + $Include | ForEach-Object { + if ($_ -in $moduleTags) { + $moduleTagsToInclude += $_ + $runModuleCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $Include | ForEach-Object { + if ($_ -in $scriptTags) { + $scriptTagsToInclude += $_ + $runScriptCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + } + $PesterConfiguration.Filter.Tag = $moduleTagsToInclude + $scriptTagsToInclude + + } + + if ($PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose 'Processing -Exclude' -Verbose + # Validate tests to exclude from $Exclude + $Exclude | ForEach-Object { + if ($_ -in $moduleTags) { + $moduleTagsToExclude += $_ + $runModuleCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $Exclude | ForEach-Object { + if ($_ -in $scriptTags) { + $scriptTagsToExclude += $_ + $runScriptCheck = $true + #* To satisfy PSScriptAnalyzer + $runModuleCheck = $runModuleCheck + $runScriptCheck = $runScriptCheck + } + } + $PesterConfiguration.Filter.ExcludeTag = $moduleTagsToExclude + $scriptTagsToExclude + + } + + # Need to work out which of the checks files can be run + if ($PSBoundParameters.ContainsKey('Include') -or + $PSBoundParameters.ContainsKey('Exclude')) { + + Write-Verbose "moduleTagsToInclude $moduleTagsToInclude" -Verbose + Write-Verbose "scriptTagsToInclude $scriptTagsToInclude" -Verbose + Write-Verbose "moduleTagsToExclude $moduleTagsToExclude" -Verbose + Write-Verbose "scriptTagsToExclude $scriptTagsToExclude" -Verbose + + } + Write-Verbose "runModuleCheck $runModuleCheck" -Verbose + Write-Verbose "runScriptCheck $runScriptCheck" -Verbose + $moduleResults = $null $extractionResults = $null $extractedScriptResults = $null @@ -220,27 +335,34 @@ function Invoke-PSQualityCheck { # Location of files extracted from any passed modules $extractPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath (New-Guid).Guid - # Run the Module tests on all the valid module files found - $container1 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Module.Tests.ps1') -Data @{ Source = $modulesToTest } - $PesterConfiguration.Run.Container = $container1 - $moduleResults = Invoke-Pester -Configuration $PesterConfiguration + if ($runModuleCheck -eq $true) { - # Extract all the functions from the modules into individual .ps1 files ready for testing - $container2 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Function-Extraction.Tests.ps1') -Data @{ Source = $modulesToTest; ExtractPath = $extractPath } - $PesterConfiguration.Run.Container = $container2 - $extractionResults = Invoke-Pester -Configuration $PesterConfiguration + # Run the Module tests on all the valid module files found + $container1 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Module.Tests.ps1') -Data @{ Source = $modulesToTest } + $PesterConfiguration.Run.Container = $container1 + $moduleResults = Invoke-Pester -Configuration $PesterConfiguration - # Get a list of the 'extracted' function scripts .ps1 files - $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse + # Extract all the functions from the modules into individual .ps1 files ready for testing + $container2 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Function-Extraction.Tests.ps1') -Data @{ Source = $modulesToTest; ExtractPath = $extractPath } + $PesterConfiguration.Run.Container = $container2 + $extractionResults = Invoke-Pester -Configuration $PesterConfiguration - # Run the Script tests against all the extracted functions .ps1 files - $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } - $PesterConfiguration.Run.Container = $container3 - $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration + } + + if ($runScriptCheck -eq $true -and (Test-Path -Path $extractPath -ErrorAction SilentlyContinue)) { + + # Get a list of the 'extracted' function scripts .ps1 files + $extractedScriptsToTest = Get-ChildItem -Path $extractPath -Include '*.ps1' -Recurse + + # Run the Script tests against all the extracted functions .ps1 files + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } + $PesterConfiguration.Run.Container = $container3 + $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration + } } - if ($scriptsToTest.Count -ge 1) { + if ($scriptsToTest.Count -ge 1 -and $runScriptCheck -eq $true) { # Run the Script tests against all the valid script files found $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $scriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } From b900e46210a27cc0b5ad809572af04238056943d Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:36:29 +0000 Subject: [PATCH 05/12] Fix for empty ScriptAnalyzerRulesPath issue --- Checks/Script.Tests.ps1 | 4 ++-- Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Checks/Script.Tests.ps1 b/Checks/Script.Tests.ps1 index 682d45f..0084cb1 100644 --- a/Checks/Script.Tests.ps1 +++ b/Checks/Script.Tests.ps1 @@ -2,7 +2,7 @@ param( [parameter(Mandatory = $true)] [string[]]$Source, - [parameter(Mandatory = $true)] + [parameter(Mandatory = $false)] [string[]]$ScriptAnalyzerRulesPath ) @@ -42,7 +42,7 @@ BeforeDiscovery { Describe "Script Tests" -Tag "Script" { - Context "Script: at " -Foreach $scriptFiles { + Context "Script: at " -ForEach $scriptFiles { BeforeAll { diff --git a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 index 582b19f..ff28652 100644 --- a/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 +++ b/Source/PSQualityCheck/Invoke-PSQualityCheck.ps1 @@ -491,3 +491,4 @@ function Invoke-PSQualityCheck { } } + From 7575a2feccb5751d0043a0fd3d93f5f6a271d0db Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:36:56 +0000 Subject: [PATCH 06/12] Updated README.md --- README.md | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 35e9fe1..4d93ee4 100644 --- a/README.md +++ b/README.md @@ -59,22 +59,6 @@ From the PSGallery: `Install-Module -Name PSQualityCheck` -### Manual Installation - -Copy the files to **one** of the available module folders: - -#### For PowerShell 5.x - -* `C:\Users\\Documents\WindowsPowerShell\Modules\PSQualityCheck` - -* `C:\Program Files\WindowsPowerShell\Modules\PSQualityCheck` - -#### For PowerShell 7.x - -* `C:\Users\\Documents\PowerShell\Modules\PSQualityCheck` - -* `C:\Program Files\PowerShell\7\Modules\PSQualityCheck` - ## Usage #### Import the module @@ -111,6 +95,14 @@ then run using the examples below as a guide: `Invoke-PSQualityCheck -File 'C:\Scripts\Script.ps1' -ScriptAnalyzerRulesPath 'C:\ScriptAnalyzerRulesPath'` +#### Check the folder C:\Scripts and all subfolders beneath it and run only the "ValidSyntax" test + +`Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults -Include "ValidSyntax"` + +#### Check the folder C:\Scripts and all subfolders beneath it and exclude "ValidSyntax" test + +`Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults -Exclude "ValidSyntax"` + #### Check the folder C:\Scripts and all subfolders beneath it and display a summary of the checks performed `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults` @@ -138,17 +130,20 @@ A quick description of the available Pester tests |PowerShell Version|PSQualityCheck Version|Operating System Result |:---|:---|:---| +|7.1.1|1.2.0|![Windows 10 - Pass](https://img.shields.io/badge/windows%2010-pass-brightgreen)| |7.1.1|1.1.1|![Windows 10 - Pass](https://img.shields.io/badge/windows%2010-pass-brightgreen)| -|7.1.0|1.1.0|

![Windows 10 - Pass](https://img.shields.io/badge/windows%2010-pass-brightgreen) ![Server 2019 - Pass](https://img.shields.io/badge/server%202019-pass-brightgreen) ![Server 2016 - Pass](https://img.shields.io/badge/server%202016-pass-brightgreen) ![Ubuntu 20.04 - Pass](https://img.shields.io/badge/ubuntu%2020.04-pass-brightgreen)

| +|7.1.0|1.1.0|![Windows 10 - Pass](https://img.shields.io/badge/windows%2010-pass-brightgreen) ![Server 2019 - Pass](https://img.shields.io/badge/server%202019-pass-brightgreen) ![Server 2016 - Pass](https://img.shields.io/badge/server%202016-pass-brightgreen) ![Ubuntu 20.04 - Pass](https://img.shields.io/badge/ubuntu%2020.04-pass-brightgreen)| |5.1|1.1.0|![Windows 10 - Fail](https://img.shields.io/badge/windows%2010-pass-brightgreen) ![Server 2019 - Pass](https://img.shields.io/badge/server%202019-pass-brightgreen) ![Server 2016 - Pass](https://img.shields.io/badge/server%202016-pass-brightgreen)|n/a| #### RuleSet/PSQualityCheck/PowerShell version testing matrix |RuleSet|PSQualityCheck Version|PowerShell Result| |:---|:---|:---| +|None|1.2.0|![PowerShell 7.1.1 - Pass](https://img.shields.io/badge/powershell%207.1.1-pass-brightgreen)| |None|1.1.1|![PowerShell 7.1.1 - Pass](https://img.shields.io/badge/powershell%207.1.1-pass-brightgreen)| |None|1.1.0|![PowerShell 7.1.0 - Pass](https://img.shields.io/badge/powershell%207.1.0-pass-brightgreen) ![PowerShell 5.1 - Pass](https://img.shields.io/badge/powershell%205.1-pass-brightgreen)| |None|1.0.10|![PowerShell 7.1.0 - Pass](https://img.shields.io/badge/powershell%207.1.0-pass-brightgreen) ![PowerShell 5.1 - Pass](https://img.shields.io/badge/powershell%205.1-pass-brightgreen)| +|[indented-automation](https://github.com/indented-automation/ScriptAnalyzerRules)
(used by SonarQube)|1.2.0|![PowerShell 7.1.1 - Pass](https://img.shields.io/badge/powershell%207.1.1-pass-brightgreen)| |[indented-automation](https://github.com/indented-automation/ScriptAnalyzerRules)
(used by SonarQube)|1.1.1|![PowerShell 7.1.1 - Pass](https://img.shields.io/badge/powershell%207.1.1-pass-brightgreen)| |[indented-automation](https://github.com/indented-automation/ScriptAnalyzerRules)
(used by SonarQube)|1.0.10|![PowerShell 7.1.0 - Pass](https://img.shields.io/badge/powershell%207.1.0-pass-brightgreen) ![PowerShell 5.1 - Pass](https://img.shields.io/badge/powershell%205.1-pass-brightgreen)| |[PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer/tree/master/Tests/Engine/CommunityAnalyzerRules)
(used by VSCode)|1.0.9|![PowerShell 7.1.0 - Fail](https://img.shields.io/badge/powershell%207.1.0-fail-red) ![PowerShell 5.1 - Further Testing To Be Performed](https://img.shields.io/badge/powershell%205.1-not%20run-lightgrey)| From e087ea0c23169d230b6e1d043c245f267a49a0ea Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:50:26 +0000 Subject: [PATCH 07/12] Updated README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d93ee4..61d96af 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,8 @@ then run using the examples below as a guide: `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults -Include "ValidSyntax"` +Tags are available [For Module Tests](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Module-Tests) and [For Script Tests](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Script-Tests) + #### Check the folder C:\Scripts and all subfolders beneath it and exclude "ValidSyntax" test `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults -Exclude "ValidSyntax"` @@ -119,7 +121,7 @@ output below uses sample data: ## Pester Tests -A quick description of the available Pester tests +A quick description of the available Pester tests with their tags: * [Module Test Details](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Module-Tests) * [Script Test Details](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Script-Tests) From 81867c7ef86bbf734fe19949132ff238a5a8f168 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:52:24 +0000 Subject: [PATCH 08/12] Updated README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 61d96af..b8c865f 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,8 @@ Tags are available [For Module Tests](https://github.com/andrewrdavidson/PSQuali `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults -Exclude "ValidSyntax"` +Tags are available [For Module Tests](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Module-Tests) and [For Script Tests](https://github.com/andrewrdavidson/PSQualityCheck/wiki/Script-Tests) + #### Check the folder C:\Scripts and all subfolders beneath it and display a summary of the checks performed `Invoke-PSQualityCheck -Path 'C:\Scripts' -ShowCheckResults` @@ -128,7 +130,7 @@ A quick description of the available Pester tests with their tags: ## Tests -#### PowerShell version/PSQualityCheck/Operating System testing matrix +### PowerShell version/PSQualityCheck/Operating System testing matrix |PowerShell Version|PSQualityCheck Version|Operating System Result |:---|:---|:---| @@ -137,7 +139,7 @@ A quick description of the available Pester tests with their tags: |7.1.0|1.1.0|![Windows 10 - Pass](https://img.shields.io/badge/windows%2010-pass-brightgreen) ![Server 2019 - Pass](https://img.shields.io/badge/server%202019-pass-brightgreen) ![Server 2016 - Pass](https://img.shields.io/badge/server%202016-pass-brightgreen) ![Ubuntu 20.04 - Pass](https://img.shields.io/badge/ubuntu%2020.04-pass-brightgreen)| |5.1|1.1.0|![Windows 10 - Fail](https://img.shields.io/badge/windows%2010-pass-brightgreen) ![Server 2019 - Pass](https://img.shields.io/badge/server%202019-pass-brightgreen) ![Server 2016 - Pass](https://img.shields.io/badge/server%202016-pass-brightgreen)|n/a| -#### RuleSet/PSQualityCheck/PowerShell version testing matrix +### RuleSet/PSQualityCheck/PowerShell version testing matrix |RuleSet|PSQualityCheck Version|PowerShell Result| |:---|:---|:---| From 71eb25fba507fa1289378f08591b81af2127fa92 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:54:51 +0000 Subject: [PATCH 09/12] Updated README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b8c865f..27c88b4 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,21 @@ The quality standards are summarised here: [Quality Standards Summary](https://g ## Releases -#### PowerShell Gallery +### PowerShell Gallery [![psgallery version](https://img.shields.io/powershellgallery/v/psqualitycheck)](https://www.powershellgallery.com/packages/PSQualityCheck) [![downloads](https://img.shields.io/powershellgallery/dt/PSQualityCheck)](https://www.powershellgallery.com/packages/PSQualityCheck) -#### GitHub +### GitHub -**Release Version** +#### Release Version [![github tag](https://img.shields.io/github/v/tag/andrewrdavidson/psqualitycheck?sort=semver)](https://github.com/andrewrdavidson/PSQualityCheck/releases?sort=semver) [![release date](https://img.shields.io/github/release-date/andrewrdavidson/psqualitycheck)](https://github.com/andrewrdavidson/PSQualityCheck/releases) -**Development/Preview Release** +#### Development/Preview Release [![github pre-release tag](https://img.shields.io/github/v/tag/andrewrdavidson/psqualitycheck?include_prereleases)](https://img.shields.io/github/v/release/andrewrdavidson/PSQualityCheck?include_prereleases) -**Commits** +#### Commits [![commits since 1.1.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.1.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.1.1) From ea9c32369ad812e2e3198cd74a59c1e5c5659aed Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:57:00 +0000 Subject: [PATCH 10/12] Updated README.md --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 27c88b4..a4ea6e2 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,9 @@ The quality standards are summarised here: [Quality Standards Summary](https://g [![github tag](https://img.shields.io/github/v/tag/andrewrdavidson/psqualitycheck?sort=semver)](https://github.com/andrewrdavidson/PSQualityCheck/releases?sort=semver) [![release date](https://img.shields.io/github/release-date/andrewrdavidson/psqualitycheck)](https://github.com/andrewrdavidson/PSQualityCheck/releases) -#### Development/Preview Release - -[![github pre-release tag](https://img.shields.io/github/v/tag/andrewrdavidson/psqualitycheck?include_prereleases)](https://img.shields.io/github/v/release/andrewrdavidson/PSQualityCheck?include_prereleases) - -#### Commits +#### Development +[![devtag](https://img.shields.io/badge/branch-1.3.0-blue)](https://github.com/andrewrdavidson/PSQualityCheck/tree/release-1.3.0) [![commits since 1.1.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.1.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.1.1) #### Issues From 6bd43d25ec07ebbb960f2e03b2e4a8a6f6f4e645 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:57:41 +0000 Subject: [PATCH 11/12] Updated README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4ea6e2..be066fc 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The quality standards are summarised here: [Quality Standards Summary](https://g #### Development [![devtag](https://img.shields.io/badge/branch-1.3.0-blue)](https://github.com/andrewrdavidson/PSQualityCheck/tree/release-1.3.0) -[![commits since 1.1.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.1.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.1.1) +[![commits since 1.1.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.1.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.2.0) #### Issues From 0e1d543972474f494fcd1dc45afc6edc6d78f0d5 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Thu, 21 Jan 2021 17:58:44 +0000 Subject: [PATCH 12/12] Updated README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be066fc..0bb421e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The quality standards are summarised here: [Quality Standards Summary](https://g #### Development [![devtag](https://img.shields.io/badge/branch-1.3.0-blue)](https://github.com/andrewrdavidson/PSQualityCheck/tree/release-1.3.0) -[![commits since 1.1.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.1.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.2.0) +[![commits since 1.2.0](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.2.0/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.2.0) #### Issues