Automatically load rule modules #218 (#245)

This commit is contained in:
Bernie White 2019-07-25 12:58:38 +10:00 коммит произвёл GitHub
Родитель de55ece634
Коммит 3654ac39dc
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 62 добавлений и 5 удалений

Просмотреть файл

@ -2,6 +2,7 @@
## Unreleased
- Add detection for improper keyword use. [#203](https://github.com/BernieWhite/PSRule/issues/203)
- Automatically load rule modules. [#218](https://github.com/BernieWhite/PSRule/issues/218)
## v0.8.0-B190716 (pre-release)

Просмотреть файл

@ -1193,7 +1193,6 @@ function Recommend {
# Get a list of rule script files in the matching paths
function GetRuleScriptPath {
[CmdletBinding()]
[OutputType([PSRule.Rules.RuleSource])]
param (
@ -1250,6 +1249,11 @@ function GetRuleScriptPath {
if ($PSBoundParameters.ContainsKey('Module')) {
$moduleParams['Name'] = $Module;
# Determine if module should be automatically loaded
if (GetAutoloadPreference) {
LoadModule -Name $Module -Verbose:$VerbosePreference;
}
}
if ($PSBoundParameters.ContainsKey('ListAvailable')) {
@ -1277,6 +1281,16 @@ function GetRuleScriptPath {
}
}
function GetAutoloadPreference {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
$v = Get-Variable -Name 'PSModuleAutoLoadingPreference' -ErrorAction SilentlyContinue;
return ($Null -eq $v) -or ($v.Value -eq [System.Management.Automation.PSModuleAutoLoadingPreference]::All);
}
}
function GetRuleModule {
[CmdletBinding()]
[OutputType([System.Management.Automation.PSModuleInfo])]
@ -1285,15 +1299,33 @@ function GetRuleModule {
[String[]]$Name,
[Parameter(Mandatory = $False)]
[Switch]$ListAvailable
[Switch]$ListAvailable = $False
)
process {
Microsoft.PowerShell.Core\Get-Module @PSBoundParameters | Where-Object -FilterScript {
$moduleResults = (Microsoft.PowerShell.Core\Get-Module @PSBoundParameters | Where-Object -FilterScript {
'PSRule' -in $_.Tags
} | Group-Object -Property Name)
if ($Null -ne $moduleResults) {
foreach ($m in $moduleResults) {
@($m.Group | Sort-Object -Descending -Property Version)[0];
}
}
}
}
function LoadModule {
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $True)]
[String[]]$Name
)
process{
$Null = GetRuleModule -Name $Name -ListAvailable | Import-Module -Global;
}
}
function GetFilePath {
[CmdletBinding()]
[OutputType([System.String])]
@ -1301,7 +1333,6 @@ function GetFilePath {
[Parameter(Mandatory = $True)]
[String[]]$Path
)
process {
$builder = New-Object -TypeName 'PSRule.Pipeline.InputPathBuilder';
Write-Verbose -Message "[PSRule][D] -- Scanning for input files: $Path";
@ -1593,7 +1624,7 @@ function InitCompletionServices {
# Complete -Module parameter
Register-ArgumentCompleter -CommandName Get-PSRule,Get-PSRuleHelp,Invoke-PSRule,Test-PSRuleTarget -ParameterName Module -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
GetRuleModule -Name "$wordToComplete*" | ForEach-Object -Process {
GetRuleModule -Name "$wordToComplete*" -ListAvailable | ForEach-Object -Process {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', ([String]::Concat("ModuleName: ", $_.Name, ", ModuleVersion: ", $_.Version.ToString())))
}
}

Просмотреть файл

@ -998,6 +998,31 @@ Describe 'Get-PSRule' -Tag 'Get-PSRule','Common' {
$Null = Remove-Module -Name TestModule;
}
It 'Loads module with preference' {
Mock -CommandName 'LoadModule' -ModuleName 'PSRule';
$currentLoadingPreference = Get-Variable -Name PSModuleAutoLoadingPreference -ErrorAction SilentlyContinue -ValueOnly;
try {
# Test negative case
$Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::None;
$Null = Get-PSRule -Module 'TestModule';
Assert-MockCalled -CommandName 'LoadModule' -ModuleName 'PSRule' -Times 0 -Scope 'It';
# Test positive case
$Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::All;
$Null = Get-PSRule -Module 'TestModule';
Assert-MockCalled -CommandName 'LoadModule' -ModuleName 'PSRule' -Times 1 -Scope 'It';
}
finally {
if ($Null -eq $currentLoadingPreference) {
Remove-Variable -Name PSModuleAutoLoadingPreference -Force -ErrorAction SilentlyContinue;
}
else {
$Global:PSModuleAutoLoadingPreference = $currentLoadingPreference;
}
}
}
It 'Handles path spaces' {
# Copy file
$testParentPath = Join-Path -Path $outputPath -ChildPath 'Program Files\';