diff --git a/CHANGELOG.md b/CHANGELOG.md index 130e9c5..9d3f265 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.3.1] 2026-01-23 + +### Changed + +- Refactored logging execution to use dedicated `Invoke-Logging` function for + improved maintainability and consistency across feature flag evaluation. +- Fixed parameter name in `Test-FeatureFlag` from `Properties` to `PropertySet` + for consistency with function implementation. + +### Fixed + +- Fixed variable name reference in `Test-FeatureFlag` from `$Feature` to + `$FeatureFlag` to correctly process feature flag rules. + ## [0.3.0] 2026-01-23 ### Added diff --git a/Gatekeeper/Gatekeeper.psd1 b/Gatekeeper/Gatekeeper.psd1 index d924a21..68af32e 100644 --- a/Gatekeeper/Gatekeeper.psd1 +++ b/Gatekeeper/Gatekeeper.psd1 @@ -12,7 +12,7 @@ RootModule = 'Gatekeeper.psm1' # Version number of this module. - ModuleVersion = '0.3.0' + ModuleVersion = '0.3.1' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/Gatekeeper/Private/Invoke-Logging.ps1 b/Gatekeeper/Private/Invoke-Logging.ps1 new file mode 100644 index 0000000..f55c40f --- /dev/null +++ b/Gatekeeper/Private/Invoke-Logging.ps1 @@ -0,0 +1,22 @@ +function Invoke-Logging { + param ( + [Parameter(Mandatory)] + [Effect] + $Effect, + [Parameter(Mandatory)] + [Rule] + $Rule + ) + begin { + $config = Import-GatekeeperConfig + } + process { + $logSettings = $config.Logging.$Effect + + if ($logSettings.Enabled) { + $sb = [scriptblock]::Create($logSettings.Script) + & $sb -Rule $Rule + } + } + +} diff --git a/Gatekeeper/Public/Test-FeatureFlag.ps1 b/Gatekeeper/Public/Test-FeatureFlag.ps1 index 6b0c1af..06f1327 100644 --- a/Gatekeeper/Public/Test-FeatureFlag.ps1 +++ b/Gatekeeper/Public/Test-FeatureFlag.ps1 @@ -10,8 +10,8 @@ .PARAMETER FeatureFlag The name of the feature flag to test. - .PARAMETER Properties - A hashtable of properties that define the different values in the context. + .PARAMETER PropertySet + A PropertySet that defines the different values in the context. .PARAMETER Context The context to use to test against. @@ -30,7 +30,7 @@ $FeatureFlag, [PropertySet] [PropertySetTransformAttribute()] - $Properties, + $PropertySet, [Parameter(Mandatory)] [hashtable] $Context @@ -44,11 +44,11 @@ process { # Process each feature Write-Verbose "Processing Feature $($FeatureFlag.Name) with ($($FeatureFlag.Rules.Count)) rules" - foreach ($rule in $Feature.Rules) { + foreach ($rule in $FeatureFlag.Rules) { Write-Verbose "Processing Rule $($rule.Name)" $testConditionSplat = @{ Context = $Context - Properties = $Properties + PropertySet = $PropertySet Condition = $rule.Conditions } if (Test-Condition @testConditionSplat) { @@ -56,20 +56,20 @@ # Check effect switch ($rule.Effect) { 'Allow' { - . $script:GatekeeperLogging['Allow'] -Rule $rule + Invoke-Logging -Effect 'Allow' -Rule $rule $finalResult = $true break } 'Deny' { - . $script:GatekeeperLogging['Deny'] -Rule $rule + Invoke-Logging -Effect 'Deny' -Rule $rule $finalResult = $false break } 'Audit' { - . $script:GatekeeperLogging['Audit'] -Rule $rule + Invoke-Logging -Effect 'Audit' -Rule $rule } 'Warn' { - . $script:GatekeeperLogging['Warning'] -Rule $rule + Invoke-Logging -Effect 'Warn' -Rule $rule } default { throw 'Unknown effect' diff --git a/docs/en-US/Test-FeatureFlag.md b/docs/en-US/Test-FeatureFlag.md index e589dfc..2fdc1ea 100644 --- a/docs/en-US/Test-FeatureFlag.md +++ b/docs/en-US/Test-FeatureFlag.md @@ -13,7 +13,7 @@ Checks if the current machine's context will pass the feature flag rules. ## SYNTAX ``` -Test-FeatureFlag [-FeatureFlag] [[-Properties] ] [-Context] +Test-FeatureFlag [-FeatureFlag] [[-PropertySet] ] [-Context] [-ProgressAction ] [] ``` @@ -49,8 +49,8 @@ Accept pipeline input: True (ByValue) Accept wildcard characters: False ``` -### -Properties -A hashtable of properties that define the different values in the context. +### -PropertySet +A PropertySet that defines the different values in the context. ```yaml Type: PropertySet diff --git a/tests/Test-FeatureFlag.tests.ps1 b/tests/Test-FeatureFlag.tests.ps1 index c129fe8..c686a5a 100644 --- a/tests/Test-FeatureFlag.tests.ps1 +++ b/tests/Test-FeatureFlag.tests.ps1 @@ -1,13 +1,12 @@ BeforeDiscovery { + if ($env:BHPSModuleManifest -eq $null -or $env:BHProjectPath -eq $null -or $env:BHProjectName -eq $null) { + . $PSscriptRoot\..\build.ps1 -Task Build + } $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest $outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output' $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion $outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1" - - # Get module commands - # Remove all versions of the module from the session. Pester can't handle multiple versions. - Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop } Describe 'Test-FeatureFlag' { @@ -15,10 +14,21 @@ Describe 'Test-FeatureFlag' { $script:propertySet = Read-PropertySet -File "$PSScriptRoot\fixtures\Properties.json" $script:context = @{ Percentage = 30 - Environment = 'Production' + Environment = 'Staging' IsCompliant = $true } # load feature flag - $json = Get-Content -Path "$PSScriptRoot\fixtures\Updawg.json" -Raw | ConvertFrom-JsonToHashtable + $script:featureFlag = Read-FeatureFlag -FilePath "$PSScriptRoot\fixtures\FeatureFlag.json" + + Mock -CommandName Invoke-Logging -ModuleName $env:BHProjectName -Verifiable + } + + It 'Allows when rule matches with Allow effect' { + $result = Test-FeatureFlag -FeatureFlag $script:featureFlag -PropertySet $script:propertySet -Context $script:context + $result | Should -BeTrue + + Assert-MockCalled -CommandName Invoke-Logging -ModuleName $env:BHProjectName -ParameterFilter { + $Effect -eq 'Allow' + } -Times 1 } }