diff --git a/Build/Build.Properties.json b/Build/Build.Properties.json index 7e4c623..812205e 100644 --- a/Build/Build.Properties.json +++ b/Build/Build.Properties.json @@ -20,7 +20,7 @@ "FileList": { "PSQualityCheck": null, "PSQualityCheck.Functions": [ - "Checks\\HelpElementRules.psd1" + "Checks\\HelpRules.psd1" ] }, "GUID": { @@ -46,8 +46,8 @@ "pester-tests" ], "Version": { - "PSQualityCheck": "1.2.1", - "PSQualityCheck.Functions": "1.2.1" + "PSQualityCheck": "1.3.0", + "PSQualityCheck.Functions": "1.3.0" } } } diff --git a/Checks/HelpElementRules.psd1 b/Checks/HelpRules.psd1 similarity index 100% rename from Checks/HelpElementRules.psd1 rename to Checks/HelpRules.psd1 diff --git a/Checks/Script.Tests.ps1 b/Checks/Script.Tests.ps1 index 20e9272..5515f4c 100644 --- a/Checks/Script.Tests.ps1 +++ b/Checks/Script.Tests.ps1 @@ -3,7 +3,11 @@ param( [string[]]$Source, [parameter(Mandatory = $false)] - [string[]]$ScriptAnalyzerRulesPath + [string[]]$ScriptAnalyzerRulesPath, + + [parameter(Mandatory = $false)] + [string]$HelpRulesPath + ) BeforeDiscovery { @@ -40,7 +44,7 @@ BeforeDiscovery { Describe "Script Tests" -Tag "Script" { - Context "Script: at " -Foreach $scriptFiles { + Context "Script: at " -ForEach $scriptFiles { BeforeAll { @@ -79,7 +83,7 @@ Describe "Script Tests" -Tag "Script" { throw "No help block found" } $helpTokens = Convert-Help -Help $helpComments.Content - Test-RequiredToken -HelpTokens $helpTokens + Test-RequiredToken -HelpTokens $helpTokens -HelpRulesPath $HelpRulesPath } | Should -Not -Throw @@ -95,7 +99,7 @@ Describe "Script Tests" -Tag "Script" { throw "No help block found" } $helpTokens = Convert-Help -Help $helpComments.Content - Test-UnspecifiedToken -HelpTokens $helpTokens + Test-UnspecifiedToken -HelpTokens $helpTokens -HelpRulesPath $HelpRulesPath } | Should -Not -Throw @@ -126,7 +130,7 @@ Describe "Script Tests" -Tag "Script" { throw "No help block found" } $helpTokens = Convert-Help -Help $helpComments.Content - Test-HelpTokensCountIsValid -HelpTokens $helpTokens + Test-HelpTokensCountIsValid -HelpTokens $helpTokens -HelpRulesPath $HelpRulesPath } | Should -Not -Throw diff --git a/PSQualityCheck.Functions.psd1 b/PSQualityCheck.Functions.psd1 index a8fc6e3..8aaff59 100644 --- a/PSQualityCheck.Functions.psd1 +++ b/PSQualityCheck.Functions.psd1 @@ -3,7 +3,7 @@ # # Generated by: Andrew Davidson # -# Generated on: 23/01/2021 +# Generated on: 27/01/2021 # @{ @@ -12,7 +12,7 @@ RootModule = 'PSQualityCheck.Functions.psm1' # Version number of this module. -ModuleVersion = '1.2.1' +ModuleVersion = '1.3.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -94,7 +94,7 @@ AliasesToExport = @() # ModuleList = @() # List of all files packaged with this module -FileList = 'Checks\HelpElementRules.psd1' +FileList = 'Checks\HelpRules.psd1' # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. PrivateData = @{ diff --git a/PSQualityCheck.Functions.psm1 b/PSQualityCheck.Functions.psm1 index b818b74..cfd4790 100644 --- a/PSQualityCheck.Functions.psm1 +++ b/PSQualityCheck.Functions.psm1 @@ -907,30 +907,20 @@ function Test-HelpTokensCountIsValid { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath # create a HashTable for tracking whether the element has been found $tokenFound = @{} - for ($order = 1; $order -le $helpElementRules.Count; $order++) { - $token = $helpElementRules."$order".Key + for ($order = 1; $order -le $HelpRules.Count; $order++) { + $token = $HelpRules."$order".Key $tokenFound[$token] = $false } @@ -940,9 +930,9 @@ function Test-HelpTokensCountIsValid { foreach ($key in $HelpTokens.Keys) { # loop through all the help element rules - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $HelpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $HelpRules."$order" # if the found token matches against a rule if ( $token.Key -eq $key ) { @@ -1281,31 +1271,21 @@ function Test-RequiredToken { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath $tokenErrors = @() - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $HelpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $HelpRules."$order" if ($token.Key -notin $HelpTokens.Keys ) { @@ -1350,40 +1330,30 @@ function Test-UnspecifiedToken { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath $tokenErrors = @() $helpTokensKeys = @() # Create an array of the help element rules elements - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $helpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $helpRules."$order" $helpTokensKeys += $token.key } # search through the found tokens and match them against the rules - foreach ($key in $HelpTokens.Keys) { + foreach ($key in $helpTokens.Keys) { if ( $key -notin $helpTokensKeys ) { diff --git a/PSQualityCheck.psd1 b/PSQualityCheck.psd1 index 978d783..0bfab6d 100644 --- a/PSQualityCheck.psd1 +++ b/PSQualityCheck.psd1 @@ -3,7 +3,7 @@ # # Generated by: Andrew Davidson # -# Generated on: 23/01/2021 +# Generated on: 27/01/2021 # @{ @@ -12,7 +12,7 @@ RootModule = 'PSQualityCheck.psm1' # Version number of this module. -ModuleVersion = '1.2.1' +ModuleVersion = '1.3.0' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/PSQualityCheck.psm1 b/PSQualityCheck.psm1 index 47da082..ac25e35 100644 --- a/PSQualityCheck.psm1 +++ b/PSQualityCheck.psm1 @@ -41,6 +41,9 @@ function Invoke-PSQualityCheck { .PARAMETER ProjectPath A path to the root of a Project + .PARAMETER HelpRulesPath + A path to the HelpRules parameter file + .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' @@ -130,7 +133,10 @@ function Invoke-PSQualityCheck { [String[]]$Include, [Parameter(Mandatory = $false)] - [String[]]$Exclude + [String[]]$Exclude, + + [Parameter(Mandatory = $false)] + [String]$HelpRulesPath ) @@ -159,6 +165,22 @@ function Invoke-PSQualityCheck { $extractedScriptResults = $null $scriptResults = $null + if ($PSBoundParameters.ContainsKey('HelpRulesPath')) { + + if ( -not (Test-Path -Path $HelpRulesPath)) { + + Write-Error "-HelpRulesPath does not exist" + break + + } + + } + else { + + $helpRulesPath = (Join-Path -Path $modulePath -ChildPath "Checks\HelpRules.psd1") + + } + if ($PSBoundParameters.ContainsKey('PesterConfiguration') -and $PesterConfiguration -is [PesterConfiguration]) { # left here so that we can over-ride passed in object with values we require @@ -364,7 +386,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; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath; HelpRulesPath = $HelpRulesPath } $PesterConfiguration.Run.Container = $container3 $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration } diff --git a/README.md b/README.md index e9d38d9..13ebbea 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ For more information please see the wiki here [Wiki](https://github.com/andrewrd #### 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.2.1](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.2.1/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.2.1) +[![devtag](https://img.shields.io/badge/branch-1.4.0-blue)](https://github.com/andrewrdavidson/PSQualityCheck/tree/release-1.4.0) +[![commits since 1.3.0](https://img.shields.io/github/commits-since/andrewrdavidson/psqualitycheck/1.3.0/main?include_prereleases)](https://github.com/andrewrdavidson/PSQualityCheck/releases/1.3.0) ## Plans diff --git a/Source/PSQualityCheck.Functions/Public/Test-HelpTokensCountIsValid.ps1 b/Source/PSQualityCheck.Functions/Public/Test-HelpTokensCountIsValid.ps1 index 86db588..793b5cc 100644 --- a/Source/PSQualityCheck.Functions/Public/Test-HelpTokensCountIsValid.ps1 +++ b/Source/PSQualityCheck.Functions/Public/Test-HelpTokensCountIsValid.ps1 @@ -19,30 +19,20 @@ function Test-HelpTokensCountIsValid { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath # create a HashTable for tracking whether the element has been found $tokenFound = @{} - for ($order = 1; $order -le $helpElementRules.Count; $order++) { - $token = $helpElementRules."$order".Key + for ($order = 1; $order -le $HelpRules.Count; $order++) { + $token = $HelpRules."$order".Key $tokenFound[$token] = $false } @@ -52,9 +42,9 @@ function Test-HelpTokensCountIsValid { foreach ($key in $HelpTokens.Keys) { # loop through all the help element rules - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $HelpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $HelpRules."$order" # if the found token matches against a rule if ( $token.Key -eq $key ) { diff --git a/Source/PSQualityCheck.Functions/Public/Test-RequiredToken.ps1 b/Source/PSQualityCheck.Functions/Public/Test-RequiredToken.ps1 index 5005a30..9a3792a 100644 --- a/Source/PSQualityCheck.Functions/Public/Test-RequiredToken.ps1 +++ b/Source/PSQualityCheck.Functions/Public/Test-RequiredToken.ps1 @@ -16,31 +16,21 @@ function Test-RequiredToken { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath $tokenErrors = @() - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $HelpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $HelpRules."$order" if ($token.Key -notin $HelpTokens.Keys ) { diff --git a/Source/PSQualityCheck.Functions/Public/Test-UnspecifiedToken.ps1 b/Source/PSQualityCheck.Functions/Public/Test-UnspecifiedToken.ps1 index 5e9927c..c8d0827 100644 --- a/Source/PSQualityCheck.Functions/Public/Test-UnspecifiedToken.ps1 +++ b/Source/PSQualityCheck.Functions/Public/Test-UnspecifiedToken.ps1 @@ -16,40 +16,30 @@ function Test-UnspecifiedToken { [OutputType([System.Exception], [System.Void])] param ( [parameter(Mandatory = $true)] - [HashTable]$HelpTokens + [HashTable]$HelpTokens, + + [parameter(Mandatory = $true)] + [string]$HelpRulesPath ) try { - $module = Get-Module -Name PSQualityCheck - - $helpElementRulesPath = (Join-Path -Path $module.ModuleBase -ChildPath "Checks\HelpElementRules.psd1") - - if (Test-Path -Path $helpElementRulesPath) { - - $helpElementRules = Import-PowerShellDataFile -Path $helpElementRulesPath - - } - else { - - throw "Unable to load Checks\HelpElementRules.psd1" - - } + $helpRules = Import-PowerShellDataFile -Path $HelpRulesPath $tokenErrors = @() $helpTokensKeys = @() # Create an array of the help element rules elements - for ($order = 1; $order -le $helpElementRules.Count; $order++) { + for ($order = 1; $order -le $helpRules.Count; $order++) { - $token = $helpElementRules."$order" + $token = $helpRules."$order" $helpTokensKeys += $token.key } # search through the found tokens and match them against the rules - foreach ($key in $HelpTokens.Keys) { + foreach ($key in $helpTokens.Keys) { if ( $key -notin $helpTokensKeys ) { diff --git a/Source/PSQualityCheck/Public/Invoke-PSQualityCheck.ps1 b/Source/PSQualityCheck/Public/Invoke-PSQualityCheck.ps1 index ad13e30..f986fb5 100644 --- a/Source/PSQualityCheck/Public/Invoke-PSQualityCheck.ps1 +++ b/Source/PSQualityCheck/Public/Invoke-PSQualityCheck.ps1 @@ -41,6 +41,9 @@ function Invoke-PSQualityCheck { .PARAMETER ProjectPath A path to the root of a Project + .PARAMETER HelpRulesPath + A path to the HelpRules parameter file + .EXAMPLE Invoke-PSQualityCheck -Path 'C:\Scripts' @@ -130,7 +133,10 @@ function Invoke-PSQualityCheck { [String[]]$Include, [Parameter(Mandatory = $false)] - [String[]]$Exclude + [String[]]$Exclude, + + [Parameter(Mandatory = $false)] + [String]$HelpRulesPath ) @@ -159,6 +165,22 @@ function Invoke-PSQualityCheck { $extractedScriptResults = $null $scriptResults = $null + if ($PSBoundParameters.ContainsKey('HelpRulesPath')) { + + if ( -not (Test-Path -Path $HelpRulesPath)) { + + Write-Error "-HelpRulesPath does not exist" + break + + } + + } + else { + + $helpRulesPath = (Join-Path -Path $modulePath -ChildPath "Checks\HelpRules.psd1") + + } + if ($PSBoundParameters.ContainsKey('PesterConfiguration') -and $PesterConfiguration -is [PesterConfiguration]) { # left here so that we can over-ride passed in object with values we require @@ -364,7 +386,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; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath } + $container3 = New-PesterContainer -Path (Join-Path -Path $modulePath -ChildPath 'Checks\Script.Tests.ps1') -Data @{ Source = $extractedScriptsToTest; ScriptAnalyzerRulesPath = $ScriptAnalyzerRulesPath; HelpRulesPath = $HelpRulesPath } $PesterConfiguration.Run.Container = $container3 $extractedScriptResults = Invoke-Pester -Configuration $PesterConfiguration }