Skip to content

Commit

Permalink
Add CmdletBinding attribute to all cmdlets (#55)
Browse files Browse the repository at this point in the history
This will allow, among other things, propagation of the ErrorAction
parameter, and correctly inhibit output when needed.

This behavior is tested in a new test for Get-FeatureFlagConfigFromFile.

Fixes Lack of CmdletBinding prevents ErrorAction from propagating #53
  • Loading branch information
lupino3 authored May 11, 2023
1 parent 21a73d8 commit bc7b139
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 13 additions & 2 deletions FeatureFlags.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ Path to the JSON file containing the configuration.
The output of ConvertFrom-Json (PSCustomObject) if the file contains a valid JSON object
that matches the feature flags JSON schema, $null otherwise.
#>
function Get-FeatureFlagConfigFromFile([string]$jsonConfigPath) {
function Get-FeatureFlagConfigFromFile {
[CmdletBinding()]
param(
[string]$jsonConfigPath
)
$configJson = Get-Content $jsonConfigPath | Out-String
if (-not (Confirm-FeatureFlagConfig $configJson)) {
return $null
Expand Down Expand Up @@ -131,6 +135,7 @@ $false in case of such invalid configuration rather than throwing exceptions tha
to be handled.
#>
function Confirm-FeatureFlagConfig {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[AllowNull()]
Expand Down Expand Up @@ -177,7 +182,8 @@ function Confirm-FeatureFlagConfig {
# Unfortunately it's impossible to express this concept with the current
# JSON schema standard.
function Confirm-StagesPointers {
param(
[CmdletBinding()]
param(
[string] $serializedJson
)

Expand Down Expand Up @@ -264,6 +270,7 @@ function Test-FeatureFlag {

function Test-FeatureConditions
{
[CmdletBinding()]
param(
[PSCustomObject] $conditions,
[string] $predicate,
Expand Down Expand Up @@ -320,6 +327,7 @@ Array of the supported features by name.
#>
function Get-SupportedFeatures
{
[CmdletBinding()]
param(
[PSCustomObject] $config
)
Expand Down Expand Up @@ -348,6 +356,7 @@ Returns the environment variables collection associated with a specific feature
#>
function Get-FeatureEnvironmentVariables
{
[CmdletBinding()]
param(
[PSCustomObject] $Config,
[string] $FeatureName
Expand All @@ -372,6 +381,7 @@ Returns an array of the evaluated feature flags given the specified predicate.
#>
function Get-EvaluatedFeatureFlags
{
[CmdletBinding()]
param(
[string] $predicate,
[PSCustomObject] $config
Expand Down Expand Up @@ -411,6 +421,7 @@ Outputs multiple file formats expressing the evaluated feature flags
#>
function Out-EvaluatedFeaturesFiles
{
[CmdletBinding()]
param(
[PSCustomObject] $Config,
[PSCustomObject] $EvaluatedFeatures,
Expand Down
11 changes: 10 additions & 1 deletion test/FeatureFlags.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Describe 'Confirm-FeatureFlagConfig' {
}
}

Context 'Error output' {
Context 'Error output of Confirm-FeatureFlagConfig' {
It 'Outputs correct error messages in case of failure' {
# Write-Error will add errors to the $error variable and output them to standard error.
# When run with -EA 0 (ErrorAction SilentlyContinue), the errors will be added to $error
Expand All @@ -59,6 +59,15 @@ Describe 'Confirm-FeatureFlagConfig' {
}
}

Context 'Error output of Get-FeatureFlagConfigFromFile' {
It 'Outputs correct error messages in case of failure' {
# Write-Error will add errors to the $error variable and output them to standard error.
# When run with -EA 0 (ErrorAction SilentlyContinue), the errors will be added to $error
# but not printed to stderr, which is desirable to not litter the unit tests output.
Get-FeatureFlagConfigFromFile -jsonConfigPath "this/file/does/not/exist" -EA 0 | Should -Be $null
}
}

Context 'Validation of configs with typos in sections or properties' {
It 'Fails if "stages" is misspelled' {
$cfg = @"
Expand Down

0 comments on commit bc7b139

Please sign in to comment.