Skip to content

Commit 22eb2b6

Browse files
authored
Merge pull request #49 from rulasg/featureFlag
feat(featureFlag): Enhance feature flag management and configuration
2 parents bd55a4f + 36c6915 commit 22eb2b6

13 files changed

Lines changed: 434 additions & 86 deletions

Test/include/callPrivateContext.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ if(-not $MODULE_PATH){ throw "Missing MODULE_PATH variable initialization. Check
1414
function Invoke-PrivateContext {
1515
param (
1616
[Parameter(Mandatory, Position = 0)]
17-
[scriptblock]$ScriptBlock
17+
[scriptblock]$ScriptBlock,
18+
[string]$ModulePath
1819
)
1920

20-
$modulePath = $MODULE_PATH | Split-Path -Parent
21+
if ([string]::IsNullOrEmpty($ModulePath)) {
22+
$modulePath = $MODULE_PATH | Split-Path -Parent
23+
}
24+
2125
$module = Import-Module -Name $modulePath -PassThru
2226

2327
if ($null -eq $module) {

Test/include/config.mock.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,40 @@
99
if(-not $MODULE_NAME){ throw "Missing MODULE_NAME varaible initialization. Check for module.helerp.ps1 file." }
1010

1111
$MOCK_CONFIG_PATH = "test_config_path"
12-
$CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-$($MODULE_NAME)GetConfigRootPath"
12+
$CONFIG_INVOKE_GET_ROOT_PATH_CMD = "Invoke-{modulename}GetConfigRootPath"
1313

1414
function Mock_Config{
1515
param(
1616
[Parameter(Position=0)][string] $key = "config",
17-
[Parameter(Position=1)][object] $Config
17+
[Parameter(Position=1)][object] $Config,
18+
[Parameter(Position=2)][string] $ModuleName,
19+
[Parameter(Position=3)][string] $MockPath = $MOCK_CONFIG_PATH
1820
)
1921

2022
# Remove mock config path if exists
21-
if(Test-Path $MOCK_CONFIG_PATH){
22-
Remove-Item -Path $MOCK_CONFIG_PATH -ErrorAction SilentlyContinue -Recurse -Force
23+
if(Test-Path $MockPath){
24+
Remove-Item -Path $fullpath -ErrorAction SilentlyContinue -Recurse -Force
2325
}
2426

2527
# create mock config path
26-
New-Item -Path $MOCK_CONFIG_PATH -ItemType Directory -Force
28+
New-Item -Path $MockPath -ItemType Directory -Force
29+
30+
# make full and not relative path
31+
$fullpath = $MockPath | Resolve-Path
2732

2833
# if $config is not null save it to a file
2934
if($null -ne $Config){
30-
$configfile = Join-Path -Path $MOCK_CONFIG_PATH -ChildPath "$key.json"
35+
$configfile = Join-Path -Path $fullpath -ChildPath "$key.json"
3136
$Config | ConvertTo-Json -Depth 10 | Set-Content $configfile
3237
}
3338

39+
if([string]::IsNullOrWhiteSpace($ModuleName)){
40+
$moduleName = $MODULE_NAME
41+
}
42+
43+
$invokefunction = $CONFIG_INVOKE_GET_ROOT_PATH_CMD -replace "{modulename}", $moduleName
44+
3445
# Mock invoke call
35-
MockCallToString $CONFIG_INVOKE_GET_ROOT_PATH_CMD -OutString $MOCK_CONFIG_PATH
46+
MockCallToString $invokefunction -OutString $fullpath
3647

3748
}

Test/public/addIncludeToWorkspace.test.ps1

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -233,37 +233,44 @@ $targetPS1 = $moduleRootPath | Join-Path -ChildPath "public\addIncludeToWorkspac
233233

234234
function Test_ResolveSourceDestinationPath{
235235

236-
$DestinationModuleName = "TargetModule"
237-
$SourceModuleName = "SourceModule"
238-
$LocalModuleName = "LocalModule"
239-
240-
$IncludeHelperModulePath = (Get-Module -name Includehelper).Path | Split-Path -parent
241-
242-
New-ModuleV3 -Name $DestinationModuleName ; $DestinationModulePath = $DestinationModuleName | Convert-Path
243-
New-ModuleV3 -Name $SourceModuleName ; $SourceModulePath = $SourceModuleName | Convert-Path
244-
New-ModuleV3 -Name $LocalModuleName ; $LocalModulePath = $LocalModuleName | Convert-Path
245-
246-
$LocalModuleName | Set-Location
247-
248-
# Act Null / Null - Soruce:Include to Destination:local
249-
$resultsource,$resultdestination = Resolve-SourceDestinationPath
250-
Assert-AreEqualPath -Presented $resultsource -Expected $IncludeHelperModulePath
251-
Assert-AreEqualPath -Presented $resultdestination -Expected $LocalModulePath
252-
253-
# Act Source / Null - Source: Path to Destination: local
254-
$resultsource,$resultdestination = Resolve-SourceDestinationPath -SourceModulePath $SourceModulePath
255-
Assert-AreEqualPath -Presented $resultsource -Expected $SourceModulePath
256-
Assert-AreEqualPath -Presented $resultdestination -Expected $LocalModulePath
257-
258-
# Act Null / Destination sourceI:Include to Destination: Path
259-
$resultsource,$resultdestination = Resolve-SourceDestinationPath -DestinationModulePath $DestinationModulePath
260-
Assert-AreEqualPath -Presented $resultsource -Expected $IncludeHelperModulePath
261-
Assert-AreEqualPath -Presented $resultdestination -Expected $DestinationModulePath
262-
263-
# Act Sorce / Destination - Source: Path to Destination: Path
264-
$resultsource,$resultdestination = Resolve-SourceDestinationPath -SourceModulePath $SourceModulePath -DestinationModulePath $DestinationModulePath
265-
Assert-AreEqualPath -Presented $resultsource -Expected $SourceModulePath
266-
Assert-AreEqualPath -Presented $resultdestination -Expected $DestinationModulePath
236+
Invoke-PrivateContext {
237+
238+
$DestinationModuleName = "TargetModule"
239+
$SourceModuleName = "SourceModule"
240+
$LocalModuleName = "LocalModule"
241+
242+
# Includehelper root folder
243+
$local = $PSScriptRoot
244+
$IncludeHelperModulePath = $local | Split-Path -Parent | Split-Path -Parent
245+
246+
# TEsting modules
247+
New-ModuleV3 -Name $DestinationModuleName ; $DestinationModulePath = $DestinationModuleName | Convert-Path
248+
New-ModuleV3 -Name $SourceModuleName ; $SourceModulePath = $SourceModuleName | Convert-Path
249+
New-ModuleV3 -Name $LocalModuleName ; $LocalModulePath = $LocalModuleName | Convert-Path
250+
251+
# Set location to test module
252+
$LocalModuleName | Set-Location
253+
254+
# Act Null / Null - Soruce:Include to Destination:local
255+
$resultsource,$resultdestination = Resolve-SourceDestinationPath
256+
Assert-AreEqualPath -Presented $resultsource -Expected $IncludeHelperModulePath
257+
Assert-AreEqualPath -Presented $resultdestination -Expected $LocalModulePath
258+
259+
# Act Source / Null - Source: Path to Destination: local
260+
$resultsource,$resultdestination = Resolve-SourceDestinationPath -SourceModulePath $SourceModulePath
261+
Assert-AreEqualPath -Presented $resultsource -Expected $SourceModulePath
262+
Assert-AreEqualPath -Presented $resultdestination -Expected $LocalModulePath
263+
264+
# Act Null / Destination sourceI:Include to Destination: Path
265+
$resultsource,$resultdestination = Resolve-SourceDestinationPath -DestinationModulePath $DestinationModulePath
266+
Assert-AreEqualPath -Presented $resultsource -Expected $IncludeHelperModulePath
267+
Assert-AreEqualPath -Presented $resultdestination -Expected $DestinationModulePath
268+
269+
# Act Sorce / Destination - Source: Path to Destination: Path
270+
$resultsource,$resultdestination = Resolve-SourceDestinationPath -SourceModulePath $SourceModulePath -DestinationModulePath $DestinationModulePath
271+
Assert-AreEqualPath -Presented $resultsource -Expected $SourceModulePath
272+
Assert-AreEqualPath -Presented $resultdestination -Expected $DestinationModulePath
273+
}
267274

268275
}
269276

Test/public/config.test.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ function Test_ConfigInclude{
44

55
Set-IncludeHelperConfigValue -Name "config_name" -Value "test_config_value"
66

7+
# Add Value String
8+
$value = "Some value"
9+
Set-IncludeHelperConfigValue -Name "TestKey" -Value $value
710
$result = Get-IncludeHelperConfig
11+
Assert-AreEqual -Expected $value -Presented $result.TestKey
812

9-
Assert-AreEqual -Expected "test_config_value" -Presented $result.config_name
13+
# Add Value Hashtable
14+
$htable = @{ Key1 = "Value1"; Key2 = "Value2" }
15+
Set-IncludeHelperConfigValue -Name "TestHashtable" -Value $htable
16+
$result = Get-IncludeHelperConfig
17+
Assert-AreEqual -Expected $htable.Key1 -Presented $result.TestHashtable.Key1
18+
Assert-AreEqual -Expected $htable.Key2 -Presented $result.TestHashtable.Key2
19+
20+
# Previuse value still there
21+
Assert-AreEqual -Expected $value -Presented $result.TestKey
1022

1123
}
1224

Test/public/dependencies.test.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ function Test_ImportDepepency_Already_loaded{
1313
Mock_ImportModule -Name $name -Folder $modulesFolder
1414

1515
#Act
16-
Set-IncludeHelperVerbose
16+
Enable-IncludeHelperVerbose
1717
Start-MyTranscript
1818
$result = Import-Dependency -Name $name -Verbose -Confirm:$false
1919
$tt = Stop-MyTranscript
20-
Clear-IncludeHelperVerbose
20+
Disable-IncludeHelperVerbose
2121

2222
#Assert verbose message
2323
Assert-AreEqual -Expected $name -Presented $result.Name
@@ -42,11 +42,11 @@ function Test_ImportDepepency_SideBySide{
4242
Mock_ImportModule -Name $name -Folder $modulesFolder
4343

4444
# Act
45-
Set-IncludeHelperVerbose
45+
Enable-IncludeHelperVerbose
4646
Start-MyTranscript
4747
$result = Import-Dependency -Name $name -Verbose -Confirm:$false
4848
$tt = Stop-MyTranscript
49-
Clear-IncludeHelperVerbose
49+
Disable-IncludeHelperVerbose
5050

5151
# Assert module output
5252
Assert-AreEqual -Expected $name -Presented $result.Name
@@ -77,11 +77,11 @@ function Test_ImportDepepency_Import_From_Module_Manager{
7777

7878

7979
#Act
80-
Set-IncludeHelperVerbose
80+
Enable-IncludeHelperVerbose
8181
Start-MyTranscript
8282
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
8383
$result = Stop-MyTranscript
84-
Clear-IncludeHelperVerbose
84+
Disable-IncludeHelperVerbose
8585

8686
#Assert verbose message
8787
Assert-AreEqual -Expected $name -Presented $output.Name
@@ -119,11 +119,11 @@ function Test_ImportDepepency_Install_From_Gallery{
119119
MockCallExpression -Command "Install-Module -Name $name -AllowPrerelease -Force" -Expression $expression
120120

121121
#Act
122-
Set-IncludeHelperVerbose
122+
Enable-IncludeHelperVerbose
123123
Start-MyTranscript
124124
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
125125
$result = Stop-MyTranscript
126-
Clear-IncludeHelperVerbose
126+
Disable-IncludeHelperVerbose
127127

128128
#Assert verbose message
129129
Assert-AreEqual -Expected $name -Presented $output.Name
@@ -154,11 +154,11 @@ function Test_ImportDependency_Clone_From_GitHub{
154154
Mock_ImportModule -Name $name -Folder $modulesFolder
155155

156156

157-
Set-IncludeHelperVerbose
157+
Enable-IncludeHelperVerbose
158158
Start-MyTranscript
159159
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
160160
$result = Stop-MyTranscript
161-
Clear-IncludeHelperVerbose
161+
Disable-IncludeHelperVerbose
162162

163163
#Assert verbose message
164164
Assert-AreEqual -Expected $name -Presented $output.Name

Test/public/featureflag.test.ps1

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
function Test_FeatureFlag_Success{
2+
Mock_Config
3+
4+
Invoke-PrivateContext{
5+
6+
$ffName = "ff1"
7+
$configFilePath = Invoke-MyCommand -Command "Invoke-IncludeHelperGetConfigRootPath" | Join-Path -ChildPath "config.json"
8+
9+
$result = Test-FeatureFlag -Key $ffName
10+
Assert-IsFalse -Condition $result
11+
12+
# Calling Test-FeatureFlag will create entry on config.FeatureFlags
13+
$config = Get-Content $configFilePath | ConvertFrom-Json -AsHashtable
14+
Assert-IsFalse -Condition $config.FeatureFlags.$ffName
15+
16+
Set-FeatureFlag $ffName
17+
$result = Test-FeatureFlag -Key $ffName
18+
Assert-IsTrue -Condition $result
19+
20+
# Set flag adds $true to the config.FeatureFlags
21+
$config = Get-Content $configFilePath| ConvertFrom-Json -AsHashtable
22+
Assert-IsTrue -Condition $config.FeatureFlags.$ffName
23+
24+
Clear-FeatureFlag $ffName
25+
$result = tff $ffName
26+
Assert-IsFalse -Condition $result
27+
28+
# Clear flag sets the flag to $false in config.FeatureFlags
29+
$config = Get-Content $configFilePath| ConvertFrom-Json -AsHashtable
30+
Assert-IsFalse -Condition $config.FeatureFlags.$ffName
31+
}
32+
}
33+
34+
function Test_RegisteredFeatureFlags_Success{
35+
36+
$modulename = "kk"
37+
38+
# Create Module
39+
New-ModuleV3 -Name $modulename
40+
$fullpath = $modulename | Resolve-Path
41+
42+
# update module with required code to run featureflags in it
43+
Sync-IncludeWithModule -DestinationModulePath $fullpath
44+
Get-IncludeFile invokeCommand.helper.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
45+
Get-IncludeFile MyWrite.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
46+
Get-IncludeFile config.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
47+
Get-IncludeFile featureflag.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
48+
Get-IncludeFile module.helper.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
49+
50+
# Add a fake Registered featureflags configuration
51+
$reg = @{
52+
deprecated = @(
53+
'ff1'
54+
'ff2'
55+
)
56+
}
57+
$reg | ConvertTo-Json | Out-File -FilePath $fullPath/featureflags.json
58+
59+
# Import module
60+
Import-Module -Name $fullpath
61+
# Mock config for module kk
62+
Mock_Config -ModuleName $modulename -MockPath "kk_config"
63+
64+
# Set feature flags in the kk config
65+
Set-kkFeatureFlag ff1
66+
Set-kkFeatureFlag ff2 -Value $false
67+
Set-kkFeatureFlag ff3
68+
69+
# Assert confirm that all 3 flags are set
70+
$result = Get-kkFeatureFlags
71+
Assert-Count -Expected 3 -Presented $result.Keys
72+
Assert-Contains -Expected "ff1" -Presented $result.Keys
73+
Assert-Contains -Expected "ff2" -Presented $result.Keys
74+
Assert-Contains -Expected "ff3" -Presented $result.Keys
75+
76+
# Act - Clear registered deprecated featureflags from config
77+
Invoke-PrivateContext -ModulePath $fullpath {
78+
Clear-FeatureFlagsRegistered
79+
}
80+
81+
# Assert confirm that deprecated flags are removed and ff3 remains
82+
$result = Get-kkFeatureFlags
83+
84+
Assert-Count -Expected 1 -Presented $result.Keys
85+
Assert-Contains -Expected "ff3" -Presented $result.Keys
86+
87+
# Not sure why but if we leave kk loaded it may interfere with other tests
88+
# In particular when calling Resolve-SourceDestinationPath, that calls Get-ModuleRootPath, it will resolve to kk module.helper.ps1 code and not IncludeHelper module one
89+
# resolving to the kk module path and not IncludeHelper module path.
90+
Remove-Module -Name $modulename -Force
91+
}
92+
93+
function Test_RegisteredFeatureFlags__FFFunction_Success{
94+
95+
$modulename = "kk"
96+
97+
# Mock config for module kk
98+
Mock_Config -ModuleName $modulename -MockPath "kk_config"
99+
100+
# Create Module
101+
New-ModuleV3 -Name $modulename
102+
$fullpath = $modulename | Resolve-Path
103+
104+
# update module with required code to run featureflags in it
105+
Sync-IncludeWithModule -DestinationModulePath $fullpath
106+
Get-IncludeFile invokeCommand.helper.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
107+
Get-IncludeFile MyWrite.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
108+
Get-IncludeFile config.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
109+
Get-IncludeFile featureflag.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
110+
Get-IncludeFile module.helper.ps1 | Add-IncludeToWorkspace -DestinationModulePath $fullpath
111+
112+
$functioncode = @"
113+
114+
function Get-kkString {
115+
if(TFF ff){
116+
return "ff"
117+
} else {
118+
return "kk"
119+
}
120+
} Export-ModuleMember -Function Get-kkString
121+
122+
"@
123+
124+
$functioncode | Out-File -FilePath ./kk/public/getString.ps1
125+
126+
# Import module
127+
Import-Module -Name $fullpath
128+
129+
Set-kkFeatureFlag ff
130+
$result = Get-kkFeatureFlags
131+
Assert-IsTrue -Condition $result.ff
132+
133+
$result = Get-kkString
134+
Assert-AreEqual -Expected "ff" -Presented $result
135+
136+
# Not sure why but if we leave kk loaded it may interfere with other tests
137+
# In particular when calling Resolve-SourceDestinationPath, that calls Get-ModuleRootPath, it will resolve to kk module.helper.ps1 code and not IncludeHelper module one
138+
# resolving to the kk module path and not IncludeHelper module path.
139+
Remove-Module -Name $modulename -Force
140+
}

0 commit comments

Comments
 (0)