@@ -20,6 +20,7 @@ import (
2020 "fmt"
2121 "os"
2222 "path/filepath"
23+ "strings"
2324 "testing"
2425
2526 "github.com/GoogleContainerTools/config-sync/cmd/nomos/flags"
@@ -50,6 +51,7 @@ func resetFlags() {
5051 keepOutput = false
5152 outPath = flags .DefaultHydrationOutput
5253 flags .OutputFormat = flags .OutputYAML
54+ flags .NoAPIServerCheckForGroup = nil
5355}
5456
5557var examplesDir = cmpath .RelativeSlash ("../../../examples" )
@@ -180,6 +182,75 @@ func TestVet_MultiCluster(t *testing.T) {
180182 }
181183}
182184
185+ func TestVet_FlagExclusivity (t * testing.T ) {
186+ testCases := []struct {
187+ name string
188+ flags []string
189+ expectError bool
190+ expectedError string
191+ }{
192+ {
193+ name : "Both flags used" ,
194+ flags : []string {"--no-api-server-check" , "--no-api-server-check-for-group=constraints.gatekeeper.sh" },
195+ expectError : true ,
196+ expectedError : "cannot use --no-api-server-check with --no-api-server-check-for-group" ,
197+ },
198+ {
199+ name : "Only --no-api-server-check used" ,
200+ flags : []string {"--no-api-server-check" },
201+ expectError : false ,
202+ },
203+ {
204+ name : "Only --no-api-server-check-for-group used" ,
205+ flags : []string {"--no-api-server-check-for-group=constraints.gatekeeper.sh" },
206+ expectError : false ,
207+ },
208+ {
209+ name : "No flags used" ,
210+ flags : []string {},
211+ expectError : false ,
212+ },
213+ }
214+
215+ for _ , tc := range testCases {
216+ t .Run (tc .name , func (t * testing.T ) {
217+ resetFlags ()
218+ // We need a valid directory for the command to run, even though we're testing flag validation.
219+ tmpDir , err := os .MkdirTemp ("" , "nomos-vet-test" )
220+ require .NoError (t , err )
221+ t .Cleanup (func () {
222+ err := os .RemoveAll (tmpDir )
223+ require .NoError (t , err )
224+ })
225+
226+ // Reset flags to a clean state for each test case.
227+ flags .SkipAPIServer = false
228+ flags .NoAPIServerCheckForGroup = nil
229+
230+ // Set the flags for the current test case.
231+ var groups []string
232+ for _ , f := range tc .flags {
233+ if f == "--no-api-server-check" {
234+ flags .SkipAPIServer = true
235+ } else if strings .HasPrefix (f , "--no-api-server-check-for-group=" ) {
236+ parts := strings .SplitN (f , "=" , 2 )
237+ groups = append (groups , strings .Split (parts [1 ], "," )... )
238+ }
239+ }
240+ flags .NoAPIServerCheckForGroup = groups
241+
242+ err = Cmd .PreRunE (Cmd , nil )
243+
244+ if tc .expectError {
245+ require .Error (t , err )
246+ require .Contains (t , err .Error (), tc .expectedError )
247+ } else {
248+ require .NoError (t , err )
249+ }
250+ })
251+ }
252+ }
253+
183254func TestVet_Threshold (t * testing.T ) {
184255 Cmd .SilenceUsage = true
185256
0 commit comments