@@ -46,7 +46,7 @@ func TestCleanupCmd(t *testing.T) {
4646 testCleanupCmd := & cobra.Command {
4747 Use : "cleanup" ,
4848 Short : "cleanup removes all services with the tag prefix" ,
49- Run : cleanupCmd .Run ,
49+ RunE : cleanupCmd .RunE ,
5050 }
5151 cmd .AddCommand (testCleanupCmd )
5252
@@ -124,18 +124,120 @@ func TestCleanupCmdHelp(t *testing.T) {
124124 testCleanupCmd := & cobra.Command {
125125 Use : "cleanup" ,
126126 Short : "cleanup removes all services with the tag prefix from a given consul service" ,
127- Run : cleanupCmd .Run ,
127+ RunE : cleanupCmd .RunE ,
128128 }
129129 cmd .AddCommand (testCleanupCmd )
130130
131131 buf := new (bytes.Buffer )
132132 cmd .SetOut (buf )
133133 cmd .SetArgs ([]string {"cleanup" , "--help" })
134-
134+
135135 err := cmd .Execute ()
136136 assert .NoError (t , err )
137137
138138 output := buf .String ()
139139 assert .Contains (t , output , "cleanup removes all services with the tag prefix" )
140140 assert .Contains (t , output , "Usage:" )
141- }
141+ }
142+
143+ func TestCleanupCmdExecution (t * testing.T ) {
144+ tests := []struct {
145+ name string
146+ consulAddr string
147+ expectError bool
148+ errorContains string
149+ }{
150+ {
151+ name : "Invalid consul address" ,
152+ consulAddr : "invalid-consul-address" ,
153+ expectError : true ,
154+ errorContains : "failed to clean up tags" ,
155+ },
156+ }
157+
158+ for _ , tt := range tests {
159+ t .Run (tt .name , func (t * testing.T ) {
160+ cmd := & cobra.Command {Use : "tagit" }
161+ cmd .PersistentFlags ().StringP ("consul-addr" , "c" , "127.0.0.1:8500" , "consul address" )
162+ cmd .PersistentFlags ().StringP ("service-id" , "s" , "" , "consul service id" )
163+ cmd .PersistentFlags ().StringP ("script" , "x" , "" , "path to script used to generate tags" )
164+ cmd .PersistentFlags ().StringP ("tag-prefix" , "p" , "tagged" , "prefix to be added to tags" )
165+ cmd .PersistentFlags ().StringP ("token" , "t" , "" , "consul token" )
166+
167+ testCleanupCmd := & cobra.Command {
168+ Use : "cleanup" ,
169+ Short : "cleanup removes all services with the tag prefix from a given consul service" ,
170+ RunE : cleanupCmd .RunE ,
171+ }
172+ cmd .AddCommand (testCleanupCmd )
173+
174+ var stderr bytes.Buffer
175+ cmd .SetErr (& stderr )
176+ cmd .SetArgs ([]string {
177+ "cleanup" ,
178+ "--service-id=test-service" ,
179+ "--script=/tmp/test.sh" ,
180+ "--consul-addr=" + tt .consulAddr ,
181+ "--tag-prefix=test" ,
182+ })
183+
184+ err := cmd .Execute ()
185+
186+ if tt .expectError {
187+ assert .Error (t , err )
188+ if tt .errorContains != "" {
189+ assert .Contains (t , err .Error (), tt .errorContains )
190+ }
191+ } else {
192+ assert .NoError (t , err )
193+ }
194+ })
195+ }
196+ }
197+
198+ func TestCleanupCmdFlagRetrieval (t * testing.T ) {
199+ // Test that all flag retrievals work correctly within the RunE function
200+ cmd := & cobra.Command {Use : "tagit" }
201+ cmd .PersistentFlags ().StringP ("consul-addr" , "c" , "127.0.0.1:8500" , "consul address" )
202+ cmd .PersistentFlags ().StringP ("service-id" , "s" , "" , "consul service id" )
203+ cmd .PersistentFlags ().StringP ("script" , "x" , "" , "path to script used to generate tags" )
204+ cmd .PersistentFlags ().StringP ("tag-prefix" , "p" , "tagged" , "prefix to be added to tags" )
205+ cmd .PersistentFlags ().StringP ("token" , "t" , "" , "consul token" )
206+
207+ var capturedValues map [string ]string
208+
209+ testCleanupCmd := & cobra.Command {
210+ Use : "cleanup" ,
211+ Short : "cleanup removes all services with the tag prefix from a given consul service" ,
212+ RunE : func (cmd * cobra.Command , args []string ) error {
213+ // Test the same flag access pattern used in the actual cleanup command
214+ capturedValues = make (map [string ]string )
215+ capturedValues ["consul-addr" ] = cmd .InheritedFlags ().Lookup ("consul-addr" ).Value .String ()
216+ capturedValues ["token" ] = cmd .InheritedFlags ().Lookup ("token" ).Value .String ()
217+ capturedValues ["service-id" ] = cmd .InheritedFlags ().Lookup ("service-id" ).Value .String ()
218+ capturedValues ["tag-prefix" ] = cmd .InheritedFlags ().Lookup ("tag-prefix" ).Value .String ()
219+
220+ // Don't actually try to connect to consul - just test flag access
221+ return nil
222+ },
223+ }
224+ cmd .AddCommand (testCleanupCmd )
225+
226+ cmd .SetArgs ([]string {
227+ "cleanup" ,
228+ "--service-id=test-service" ,
229+ "--script=/tmp/test.sh" ,
230+ "--consul-addr=localhost:9500" ,
231+ "--tag-prefix=test-prefix" ,
232+ "--token=test-token" ,
233+ })
234+
235+ err := cmd .Execute ()
236+ assert .NoError (t , err )
237+
238+ // Verify all values were captured correctly
239+ assert .Equal (t , "localhost:9500" , capturedValues ["consul-addr" ])
240+ assert .Equal (t , "test-token" , capturedValues ["token" ])
241+ assert .Equal (t , "test-service" , capturedValues ["service-id" ])
242+ assert .Equal (t , "test-prefix" , capturedValues ["tag-prefix" ])
243+ }
0 commit comments