Skip to content

Commit bc7b139

Browse files
authored
Add CmdletBinding attribute to all cmdlets (#55)
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
1 parent 21a73d8 commit bc7b139

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

FeatureFlags.psm1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ Path to the JSON file containing the configuration.
1010
The output of ConvertFrom-Json (PSCustomObject) if the file contains a valid JSON object
1111
that matches the feature flags JSON schema, $null otherwise.
1212
#>
13-
function Get-FeatureFlagConfigFromFile([string]$jsonConfigPath) {
13+
function Get-FeatureFlagConfigFromFile {
14+
[CmdletBinding()]
15+
param(
16+
[string]$jsonConfigPath
17+
)
1418
$configJson = Get-Content $jsonConfigPath | Out-String
1519
if (-not (Confirm-FeatureFlagConfig $configJson)) {
1620
return $null
@@ -131,6 +135,7 @@ $false in case of such invalid configuration rather than throwing exceptions tha
131135
to be handled.
132136
#>
133137
function Confirm-FeatureFlagConfig {
138+
[CmdletBinding()]
134139
param (
135140
[Parameter(Mandatory=$true)]
136141
[AllowNull()]
@@ -177,7 +182,8 @@ function Confirm-FeatureFlagConfig {
177182
# Unfortunately it's impossible to express this concept with the current
178183
# JSON schema standard.
179184
function Confirm-StagesPointers {
180-
param(
185+
[CmdletBinding()]
186+
param(
181187
[string] $serializedJson
182188
)
183189

@@ -264,6 +270,7 @@ function Test-FeatureFlag {
264270

265271
function Test-FeatureConditions
266272
{
273+
[CmdletBinding()]
267274
param(
268275
[PSCustomObject] $conditions,
269276
[string] $predicate,
@@ -320,6 +327,7 @@ Array of the supported features by name.
320327
#>
321328
function Get-SupportedFeatures
322329
{
330+
[CmdletBinding()]
323331
param(
324332
[PSCustomObject] $config
325333
)
@@ -348,6 +356,7 @@ Returns the environment variables collection associated with a specific feature
348356
#>
349357
function Get-FeatureEnvironmentVariables
350358
{
359+
[CmdletBinding()]
351360
param(
352361
[PSCustomObject] $Config,
353362
[string] $FeatureName
@@ -372,6 +381,7 @@ Returns an array of the evaluated feature flags given the specified predicate.
372381
#>
373382
function Get-EvaluatedFeatureFlags
374383
{
384+
[CmdletBinding()]
375385
param(
376386
[string] $predicate,
377387
[PSCustomObject] $config
@@ -411,6 +421,7 @@ Outputs multiple file formats expressing the evaluated feature flags
411421
#>
412422
function Out-EvaluatedFeaturesFiles
413423
{
424+
[CmdletBinding()]
414425
param(
415426
[PSCustomObject] $Config,
416427
[PSCustomObject] $EvaluatedFeatures,

test/FeatureFlags.Tests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Describe 'Confirm-FeatureFlagConfig' {
4343
}
4444
}
4545

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

62+
Context 'Error output of Get-FeatureFlagConfigFromFile' {
63+
It 'Outputs correct error messages in case of failure' {
64+
# Write-Error will add errors to the $error variable and output them to standard error.
65+
# When run with -EA 0 (ErrorAction SilentlyContinue), the errors will be added to $error
66+
# but not printed to stderr, which is desirable to not litter the unit tests output.
67+
Get-FeatureFlagConfigFromFile -jsonConfigPath "this/file/does/not/exist" -EA 0 | Should -Be $null
68+
}
69+
}
70+
6271
Context 'Validation of configs with typos in sections or properties' {
6372
It 'Fails if "stages" is misspelled' {
6473
$cfg = @"

0 commit comments

Comments
 (0)