@@ -2343,3 +2343,90 @@ func TestSetContextPersistentPreRun(t *testing.T) {
2343
2343
t .Error (err )
2344
2344
}
2345
2345
}
2346
+
2347
+ const VersionFlag = "--version"
2348
+ const HelpFlag = "--help"
2349
+
2350
+ func TestNoRootRunCommandExecutedWithVersionSet (t * testing.T ) {
2351
+ rootCmd := & Command {Use : "root" , Version : "1.0.0" , Long : "Long description" }
2352
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2353
+
2354
+ output , err := executeCommand (rootCmd )
2355
+ if err != nil {
2356
+ t .Errorf ("Unexpected error: %v" , err )
2357
+ }
2358
+
2359
+ checkStringContains (t , output , rootCmd .Long )
2360
+ checkStringContains (t , output , HelpFlag )
2361
+ checkStringContains (t , output , VersionFlag )
2362
+ }
2363
+
2364
+ func TestNoRootRunCommandExecutedWithoutVersionSet (t * testing.T ) {
2365
+ rootCmd := & Command {Use : "root" , Long : "Long description" }
2366
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2367
+
2368
+ output , err := executeCommand (rootCmd )
2369
+ if err != nil {
2370
+ t .Errorf ("Unexpected error: %v" , err )
2371
+ }
2372
+
2373
+ checkStringContains (t , output , rootCmd .Long )
2374
+ checkStringContains (t , output , HelpFlag )
2375
+ checkStringOmits (t , output , VersionFlag )
2376
+ }
2377
+
2378
+ func TestHelpCommandExecutedWithVersionSet (t * testing.T ) {
2379
+ rootCmd := & Command {Use : "root" , Version : "1.0.0" , Long : "Long description" , Run : emptyRun }
2380
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2381
+
2382
+ output , err := executeCommand (rootCmd , "help" )
2383
+ if err != nil {
2384
+ t .Errorf ("Unexpected error: %v" , err )
2385
+ }
2386
+
2387
+ checkStringContains (t , output , rootCmd .Long )
2388
+ checkStringContains (t , output , HelpFlag )
2389
+ checkStringContains (t , output , VersionFlag )
2390
+ }
2391
+
2392
+ func TestHelpCommandExecutedWithoutVersionSet (t * testing.T ) {
2393
+ rootCmd := & Command {Use : "root" , Long : "Long description" , Run : emptyRun }
2394
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2395
+
2396
+ output , err := executeCommand (rootCmd , "help" )
2397
+ if err != nil {
2398
+ t .Errorf ("Unexpected error: %v" , err )
2399
+ }
2400
+
2401
+ checkStringContains (t , output , rootCmd .Long )
2402
+ checkStringContains (t , output , HelpFlag )
2403
+ checkStringOmits (t , output , VersionFlag )
2404
+ }
2405
+
2406
+ func TestHelpflagCommandExecutedWithVersionSet (t * testing.T ) {
2407
+ rootCmd := & Command {Use : "root" , Version : "1.0.0" , Long : "Long description" , Run : emptyRun }
2408
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2409
+
2410
+ output , err := executeCommand (rootCmd , HelpFlag )
2411
+ if err != nil {
2412
+ t .Errorf ("Unexpected error: %v" , err )
2413
+ }
2414
+
2415
+ checkStringContains (t , output , rootCmd .Long )
2416
+ checkStringContains (t , output , HelpFlag )
2417
+ checkStringContains (t , output , VersionFlag )
2418
+ }
2419
+
2420
+ func TestHelpflagCommandExecutedWithoutVersionSet (t * testing.T ) {
2421
+ rootCmd := & Command {Use : "root" , Long : "Long description" , Run : emptyRun }
2422
+ rootCmd .AddCommand (& Command {Use : "child" , Run : emptyRun })
2423
+
2424
+ output , err := executeCommand (rootCmd , HelpFlag )
2425
+ if err != nil {
2426
+ t .Errorf ("Unexpected error: %v" , err )
2427
+ }
2428
+
2429
+ checkStringContains (t , output , rootCmd .Long )
2430
+ checkStringContains (t , output , HelpFlag )
2431
+ checkStringOmits (t , output , VersionFlag )
2432
+ }
0 commit comments