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