@@ -16,136 +16,239 @@ package switches
1616import (
1717 "flag"
1818
19- "github.com/onmetal/controller-utils/set"
2019 . "github.com/onsi/ginkgo/v2"
2120 . "github.com/onsi/gomega"
2221 "github.com/spf13/pflag"
22+ "k8s.io/apimachinery/pkg/util/sets"
2323)
2424
2525var _ = Describe ("CMD Switches" , func () {
2626 Context ("Testing Switches interface" , func () {
27- It ("should disable runner" , func () {
28- s := New ("runner-a" , "runner-b" )
29- Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
30- Expect (s .Enabled ("runner-a" )).To (BeTrue ())
31- Expect (s .Enabled ("runner-b" )).To (BeFalse ())
27+ Describe ("Enabled" , func () {
28+ It ("should return whether an item is enabled or not" , func () {
29+ s := New ("runner-a" , "runner-b" )
30+ Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
31+
32+ Expect (s .Enabled ("runner-a" )).To (BeTrue ())
33+ Expect (s .Enabled ("runner-b" )).To (BeFalse ())
34+ })
3235 })
33- It ("should return all items" , func () {
34- s := New ("runner-a" , "runner-b" , Disable ("runner-c" ))
35- Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
3636
37- expected := set .New ("runner-a" , "runner-b" , "runner-c" )
38- Expect (s .All ()).To (Equal (expected ))
37+ Describe ("AllEnabled" , func () {
38+ It ("should return true when all given switches are enabled" , func () {
39+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
40+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
41+
42+ Expect (s .AllEnabled ("runner-a" , "runner-c" )).To (BeTrue ())
43+ })
44+
45+ It ("should return false if any of the given switches is not enabled" , func () {
46+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
47+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
48+
49+ Expect (s .AllEnabled ("runner-a" , "runner-b" )).To (BeFalse ())
50+ })
51+
52+ It ("should return true on empty arguments" , func () {
53+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
54+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
55+
56+ Expect (s .AllEnabled ()).To (BeTrue ())
57+ })
3958 })
40- It ("should return only active items" , func () {
41- s := New ("runner-a" , "runner-b" , Disable ("runner-c" ))
42- Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
4359
44- expected := set .New ("runner-a" )
45- Expect (s .Active ()).To (Equal (expected ))
60+ Describe ("AnyEnabled" , func () {
61+ It ("should return true when any of the given switches is enabled" , func () {
62+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
63+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
64+
65+ Expect (s .AnyEnabled ("runner-a" , "runner-b" )).To (BeTrue ())
66+ })
67+
68+ It ("should return false if all of the given switches are not enabled" , func () {
69+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
70+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
71+
72+ Expect (s .AnyEnabled ("runner-b" )).To (BeFalse ())
73+ })
74+
75+ It ("should return false on empty arguments" , func () {
76+ s := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
77+ Expect (s .Set ("*" )).NotTo (HaveOccurred ())
78+
79+ Expect (s .AnyEnabled ()).To (BeFalse ())
80+ })
4681 })
47- It ("should return all enabled items" , func () {
48- s := New ("runner-a" , Disable ("runner-b" ))
4982
50- expected := set .New ("runner-a" )
51- Expect (s .EnabledByDefault ()).To (Equal (expected ))
83+ Describe ("Values" , func () {
84+ It ("should return the switches with their values" , func () {
85+ s := New ("runner-a" , "runner-b" )
86+ Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
87+
88+ Expect (s .Values ()).To (Equal (map [string ]bool {
89+ "runner-a" : true ,
90+ "runner-b" : false ,
91+ }))
92+ })
5293 })
53- It ("should return all disabled items" , func () {
54- s := New ("runner-a" , Disable ("runner-b" ))
5594
56- expected := set .New ("runner-b" )
57- Expect (s .DisabledByDefault ()).To (Equal (expected ))
95+ Describe ("All" , func () {
96+ It ("should return all items" , func () {
97+ s := New ("runner-a" , "runner-b" , Disable ("runner-c" ))
98+ Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
99+
100+ Expect (s .All ()).To (Equal (sets .New ("runner-a" , "runner-b" , "runner-c" )))
101+ })
58102 })
59- It ("should return string" , func () {
60- s := New ("runner-a" , "runner-b" )
61- Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
62- Expect (s .String ()).To (Equal ("runner-a,-runner-b" ))
103+
104+ Describe ("Active" , func () {
105+ It ("should return all enabled items" , func () {
106+ s := New ("runner-a" , "runner-b" , Disable ("runner-c" ))
107+ Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
108+
109+ Expect (s .Active ()).To (Equal (sets .New ("runner-a" )))
110+ })
111+ })
112+
113+ Describe ("EnabledByDefault" , func () {
114+ It ("should return all items that are enabled by default" , func () {
115+ s := New ("runner-a" , Disable ("runner-b" ))
116+
117+ Expect (s .EnabledByDefault ()).To (Equal (sets .New ("runner-a" )))
118+ })
119+ })
120+
121+ Describe ("DisabledByDefault" , func () {
122+ It ("should return all items disabled by default" , func () {
123+ s := New ("runner-a" , Disable ("runner-b" ))
124+
125+ Expect (s .DisabledByDefault ()).To (Equal (sets .New ("runner-b" )))
126+ })
127+ })
128+
129+ Describe ("String" , func () {
130+ It ("should return a string repressentation of the switches" , func () {
131+ s := New ("runner-a" , "runner-b" )
132+ Expect (s .Set ("*,-runner-b" )).ToNot (HaveOccurred ())
133+
134+ Expect (s .String ()).To (Equal ("runner-a,-runner-b" ))
135+ })
63136 })
64137 })
65138
66- Context ( "Testing flag package behavior " , func () {
139+ Describe ( "goflag.Parse " , func () {
67140 It ("should disable all controllers when no flag is passed" , func () {
68141 fs := flag .NewFlagSet ("" , flag .ExitOnError )
69142 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
70143 fs .Var (controllers , "controllers" , "" )
71144
72145 Expect (fs .Parse ([]string {})).NotTo (HaveOccurred ())
73- Expect (controllers .Enabled ("runner-a" )).To (BeFalse ())
74- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
75- Expect (controllers .Enabled ("runner-c" )).To (BeFalse ())
146+
147+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
148+ "runner-a" : false ,
149+ "runner-b" : false ,
150+ "runner-c" : false ,
151+ }))
76152 })
153+
77154 It ("should keep default settings when * is passed" , func () {
78155 fs := flag .NewFlagSet ("" , flag .ExitOnError )
79156 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
80157 fs .Var (controllers , "controllers" , "" )
81158
82159 Expect (fs .Parse ([]string {"--controllers=*" })).NotTo (HaveOccurred ())
83- Expect (controllers .Enabled ("runner-a" )).To (BeTrue ())
84- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
85- Expect (controllers .Enabled ("runner-c" )).To (BeTrue ())
160+
161+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
162+ "runner-a" : true ,
163+ "runner-b" : false ,
164+ "runner-c" : true ,
165+ }))
86166 })
167+
87168 It ("should override default settings" , func () {
88169 fs := flag .NewFlagSet ("" , flag .ExitOnError )
89170 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
90171 fs .Var (controllers , "controllers" , "" )
91172
92173 Expect (fs .Parse ([]string {"--controllers=runner-a,-runner-c" })).NotTo (HaveOccurred ())
93- Expect (controllers .Enabled ("runner-a" )).To (BeTrue ())
94- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
95- Expect (controllers .Enabled ("runner-c" )).To (BeFalse ())
174+
175+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
176+ "runner-a" : true ,
177+ "runner-b" : false ,
178+ "runner-c" : false ,
179+ }))
96180 })
181+
97182 It ("should override some of default settings" , func () {
98183 fs := flag .NewFlagSet ("" , flag .ExitOnError )
99184 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
100185 fs .Var (controllers , "controllers" , "" )
101186
102187 Expect (fs .Parse ([]string {"--controllers=*,-runner-a" })).NotTo (HaveOccurred ())
103- Expect (controllers .Enabled ("runner-a" )).To (BeFalse ())
104- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
105- Expect (controllers .Enabled ("runner-c" )).To (BeTrue ())
188+
189+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
190+ "runner-a" : false ,
191+ "runner-b" : false ,
192+ "runner-c" : true ,
193+ }))
106194 })
107195 })
108196
109- Context ( "Testing pflag package behavior " , func () {
197+ Describe ( " pflag.parse " , func () {
110198 It ("should disable all controllers when no flag is passed" , func () {
111199 fs := pflag .NewFlagSet ("" , pflag .ExitOnError )
112200 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
113201 fs .Var (controllers , "controllers" , "" )
114202
115203 Expect (fs .Parse ([]string {})).NotTo (HaveOccurred ())
116- Expect (controllers .Enabled ("runner-a" )).To (BeFalse ())
117- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
118- Expect (controllers .Enabled ("runner-c" )).To (BeFalse ())
204+
205+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
206+ "runner-a" : false ,
207+ "runner-b" : false ,
208+ "runner-c" : false ,
209+ }))
119210 })
211+
120212 It ("should keep default settings when * is passed" , func () {
121213 fs := pflag .NewFlagSet ("" , pflag .ExitOnError )
122214 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
123215 fs .Var (controllers , "controllers" , "" )
124216
125217 Expect (fs .Parse ([]string {"--controllers=*" })).NotTo (HaveOccurred ())
126- Expect (controllers .Enabled ("runner-a" )).To (BeTrue ())
127- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
128- Expect (controllers .Enabled ("runner-c" )).To (BeTrue ())
218+
219+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
220+ "runner-a" : true ,
221+ "runner-b" : false ,
222+ "runner-c" : true ,
223+ }))
129224 })
225+
130226 It ("should override default settings" , func () {
131227 fs := pflag .NewFlagSet ("" , pflag .ExitOnError )
132228 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
133229 fs .Var (controllers , "controllers" , "" )
134230
135231 Expect (fs .Parse ([]string {"--controllers=runner-a,-runner-c" })).NotTo (HaveOccurred ())
136- Expect (controllers .Enabled ("runner-a" )).To (BeTrue ())
137- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
138- Expect (controllers .Enabled ("runner-c" )).To (BeFalse ())
232+
233+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
234+ "runner-a" : true ,
235+ "runner-b" : false ,
236+ "runner-c" : false ,
237+ }))
139238 })
239+
140240 It ("should override some of default settings" , func () {
141241 fs := pflag .NewFlagSet ("" , pflag .ExitOnError )
142242 controllers := New ("runner-a" , Disable ("runner-b" ), "runner-c" )
143243 fs .Var (controllers , "controllers" , "" )
144244
145245 Expect (fs .Parse ([]string {"--controllers=*,-runner-a" })).NotTo (HaveOccurred ())
146- Expect (controllers .Enabled ("runner-a" )).To (BeFalse ())
147- Expect (controllers .Enabled ("runner-b" )).To (BeFalse ())
148- Expect (controllers .Enabled ("runner-c" )).To (BeTrue ())
246+
247+ Expect (controllers .Values ()).To (Equal (map [string ]bool {
248+ "runner-a" : false ,
249+ "runner-b" : false ,
250+ "runner-c" : true ,
251+ }))
149252 })
150253 })
151254})
0 commit comments