From b70e444ea700ce208da99e5023c88f926d423af9 Mon Sep 17 00:00:00 2001 From: Jos Koelewijn Date: Mon, 17 Feb 2020 17:08:54 +0100 Subject: [PATCH 1/3] fix modulehelp false positives When looking up module version, if the module is not installed, also check if it is loaded and get module version from loaded module Add 'AttachAndDebug' to paramBlackList, so it does not throw false positives for DEBUG builds. Small bugfix for the actual blacklist check, the property 'name' of the $parameter variable needs to be used to check against the blacklist. --- Tests/Engine/ModuleHelp.Tests.ps1 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Tests/Engine/ModuleHelp.Tests.ps1 b/Tests/Engine/ModuleHelp.Tests.ps1 index a38c6b37b..147addbb9 100644 --- a/Tests/Engine/ModuleHelp.Tests.ps1 +++ b/Tests/Engine/ModuleHelp.Tests.ps1 @@ -205,6 +205,10 @@ function Get-CommandVersion { if (!$RequiredVersion) { $RequiredVersion = (Get-Module $ModuleName -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1).Version + if ($null -eq $RequiredVersion) { + # Look for loaded modules instead of installed modules, if we don't find any installed modules. + $RequiredVersion = (Get-Module $ModuleName | Sort-Object -Property Version -Descending | Select-Object -First 1).Version + } } # Remove all versions of the module from the session. Pester can't handle multiple versions. @@ -214,7 +218,9 @@ Get-Module $ModuleName | Remove-Module Import-Module $ModuleName -RequiredVersion $RequiredVersion -ErrorAction Stop $ms = $null $commands =$null -$paramBlackList = @() +$paramBlackList = @( + 'AttachAndDebug' # Reason: When building with DEGUG configuration, an additional parameter 'AttachAndDebug' will be added to Invoke-ScriptAnalyzer and Invoke-Formatter, but there is no Help for those, as they are not intended for production usage. +) if ($PSVersionTable.PSVersion -lt [Version]'5.0.0') { $ms = New-Object -TypeName 'Microsoft.PowerShell.Commands.ModuleSpecification' -ArgumentList $ModuleName $commands = Get-Command -Module $ms.Name @@ -222,7 +228,9 @@ if ($PSVersionTable.PSVersion -lt [Version]'5.0.0') { } else { $ms = [Microsoft.PowerShell.Commands.ModuleSpecification]@{ ModuleName = $ModuleName; RequiredVersion = $RequiredVersion } - $commands = Get-Command -FullyQualifiedModule $ms -CommandType Cmdlet,Function,Workflow # Not alias + # Because on Windows Powershell we have workflow, we need to include it there, but in pwsh, we can't. This makes sure this works on both. + $commandTypes = ([System.Enum]::GetNames("System.Management.Automation.CommandTypes") -match "^Cmdlet$|^Function$|^Workflow$") + $commands = Get-Command -FullyQualifiedModule $ms -CommandType $commandTypes } ## When testing help, remember that help is cached at the beginning of each session. @@ -276,7 +284,7 @@ foreach ($command in $commands) { $HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique foreach ($parameter in $parameters) { - if ($parameter -in $paramBlackList) { + if ($parameter.Name -in $paramBlackList) { continue } $parameterName = $parameter.Name From 938696fe548ea411824853764d0dd5b5e63c6732 Mon Sep 17 00:00:00 2001 From: Jos Koelewijn Date: Mon, 17 Feb 2020 19:52:56 +0100 Subject: [PATCH 2/3] undo check for loaded module, because $env:psmodulepath is set to .\out\ module during testing, the built module will always appear 'installed'. --- Tests/Engine/ModuleHelp.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Tests/Engine/ModuleHelp.Tests.ps1 b/Tests/Engine/ModuleHelp.Tests.ps1 index 147addbb9..48d45a2e9 100644 --- a/Tests/Engine/ModuleHelp.Tests.ps1 +++ b/Tests/Engine/ModuleHelp.Tests.ps1 @@ -205,10 +205,6 @@ function Get-CommandVersion { if (!$RequiredVersion) { $RequiredVersion = (Get-Module $ModuleName -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1).Version - if ($null -eq $RequiredVersion) { - # Look for loaded modules instead of installed modules, if we don't find any installed modules. - $RequiredVersion = (Get-Module $ModuleName | Sort-Object -Property Version -Descending | Select-Object -First 1).Version - } } # Remove all versions of the module from the session. Pester can't handle multiple versions. From 58fb6c6d5dad70c38828654d81079363be849ea2 Mon Sep 17 00:00:00 2001 From: Jos Koelewijn Date: Thu, 20 Feb 2020 12:10:51 +0100 Subject: [PATCH 3/3] simplify command discovery --- Tests/Engine/ModuleHelp.Tests.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Tests/Engine/ModuleHelp.Tests.ps1 b/Tests/Engine/ModuleHelp.Tests.ps1 index 48d45a2e9..c84b19cb4 100644 --- a/Tests/Engine/ModuleHelp.Tests.ps1 +++ b/Tests/Engine/ModuleHelp.Tests.ps1 @@ -224,9 +224,7 @@ if ($PSVersionTable.PSVersion -lt [Version]'5.0.0') { } else { $ms = [Microsoft.PowerShell.Commands.ModuleSpecification]@{ ModuleName = $ModuleName; RequiredVersion = $RequiredVersion } - # Because on Windows Powershell we have workflow, we need to include it there, but in pwsh, we can't. This makes sure this works on both. - $commandTypes = ([System.Enum]::GetNames("System.Management.Automation.CommandTypes") -match "^Cmdlet$|^Function$|^Workflow$") - $commands = Get-Command -FullyQualifiedModule $ms -CommandType $commandTypes + $commands = Get-Command -FullyQualifiedModule $ms } ## When testing help, remember that help is cached at the beginning of each session.