@@ -12,16 +12,12 @@ import (
1212func TestRun (t * testing.T ) {
1313 t .Parallel ()
1414
15- t .Run ("parse and run " , func (t * testing.T ) {
15+ t .Run ("print version " , func (t * testing.T ) {
1616 t .Parallel ()
17- var count int
1817
1918 root := & Command {
20- Name : "count" ,
21- Usage : "count [flags] [command]" ,
22- Flags : FlagsFunc (func (fset * flag.FlagSet ) {
23- fset .Bool ("dry-run" , false , "dry run" )
24- }),
19+ Name : "printer" ,
20+ Usage : "printer [flags] [command]" ,
2521 SubCommands : []* Command {
2622 {
2723 Name : "version" ,
@@ -32,35 +28,53 @@ func TestRun(t *testing.T) {
3228 },
3329 },
3430 },
31+ Exec : func (ctx context.Context , s * State ) error { return nil },
32+ }
33+ err := Parse (root , []string {"version" })
34+ require .NoError (t , err )
35+
36+ output := bytes .NewBuffer (nil )
37+ require .NoError (t , err )
38+ err = Run (context .Background (), root , & RunOptions {Stdout : output })
39+ require .NoError (t , err )
40+ require .Equal (t , "1.0.0\n " , output .String ())
41+ })
42+
43+ t .Run ("parse and run" , func (t * testing.T ) {
44+ t .Parallel ()
45+ var count int
46+
47+ root := & Command {
48+ Name : "count" ,
49+ Usage : "count [flags] [command]" ,
50+ Flags : FlagsFunc (func (f * flag.FlagSet ) {
51+ f .Bool ("dry-run" , false , "dry run" )
52+ }),
3553 Exec : func (ctx context.Context , s * State ) error {
3654 if ! GetFlag [bool ](s , "dry-run" ) {
3755 count ++
3856 }
3957 return nil
4058 },
4159 }
42-
43- output := bytes .NewBuffer (nil )
44- err := ParseAndRun (context .Background (), root , []string {"version" }, & RunOptions {
45- Stdout : output ,
46- })
60+ err := Parse (root , nil )
4761 require .NoError (t , err )
48- require .Equal (t , "1.0.0\n " , output .String ())
49- output .Reset ()
50-
5162 // Run the command 3 times
5263 for i := 0 ; i < 3 ; i ++ {
53- err := ParseAndRun (context .Background (), root , nil , nil )
64+ err := Run (context .Background (), root , nil )
5465 require .NoError (t , err )
5566 }
5667 require .Equal (t , 3 , count )
5768 // Run with dry-run flag
58- err = ParseAndRun (context .Background (), root , []string {"--dry-run" }, nil )
69+ err = Parse (root , []string {"--dry-run" })
70+ require .NoError (t , err )
71+ err = Run (context .Background (), root , nil )
5972 require .NoError (t , err )
6073 require .Equal (t , 3 , count )
6174 })
6275 t .Run ("typo suggestion" , func (t * testing.T ) {
6376 t .Parallel ()
77+
6478 root := & Command {
6579 Name : "count" ,
6680 Usage : "count [flags] [command]" ,
@@ -77,7 +91,7 @@ func TestRun(t *testing.T) {
7791 Exec : func (ctx context.Context , s * State ) error { return nil },
7892 }
7993
80- err := ParseAndRun ( context . Background (), root , []string {"verzion" }, nil )
94+ err := Parse ( root , []string {"verzion" })
8195 require .Error (t , err )
8296 require .Contains (t , err .Error (), `unknown command "verzion". Did you mean one of these?` )
8397 require .Contains (t , err .Error (), ` version` )
0 commit comments