|
1 | | -package cobrakai |
| 1 | +package cobrakai_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
4 | 7 | "testing" |
5 | 8 |
|
| 9 | + ck "github.com/bep/cobrakai" |
6 | 10 | qt "github.com/frankban/quicktest" |
| 11 | + "github.com/spf13/pflag" |
7 | 12 | ) |
8 | 13 |
|
9 | | -func TestFoo(t *testing.T) { |
| 14 | +func TestCobraKai(t *testing.T) { |
| 15 | + |
| 16 | + var ( |
| 17 | + fooCommand = &testComand1{name: "foo"} |
| 18 | + barCommand = &testComand1{name: "bar"} |
| 19 | + fooBazCommand = &testComand2{name: "foo_baz"} |
| 20 | + ) |
| 21 | + |
| 22 | + os.Args = []string{"hugo", "foo", "--localFlagName", "foo_local", "--persistentFlagName", "foo_persistent"} |
10 | 23 | c := qt.New(t) |
11 | | - c.Assert(Foo(), qt.Equals, "foo") |
| 24 | + r, err := ck.R( |
| 25 | + &testComand1{name: "hugo"}, |
| 26 | + ck.C(fooCommand, |
| 27 | + ck.C(fooBazCommand), |
| 28 | + ), |
| 29 | + ck.C(barCommand), |
| 30 | + ) |
| 31 | + c.Assert(err, qt.IsNil) |
| 32 | + |
| 33 | + // This can be anything, just used to make sure the same context is passed all the way. |
| 34 | + type key string |
| 35 | + ctx := context.WithValue(context.Background(), key("foo"), "bar") |
| 36 | + cdeer, err := r.Execute(ctx) |
| 37 | + c.Assert(err, qt.IsNil) |
| 38 | + c.Assert(cdeer.Command.Name(), qt.Equals, "foo") |
| 39 | + tc := cdeer.Command.(*testComand1) |
| 40 | + c.Assert(tc.ctx, qt.Equals, ctx) |
| 41 | + c.Assert(tc.localFlagName, qt.Equals, "foo_local") |
| 42 | + c.Assert(tc.persistentFlagName, qt.Equals, "foo_persistent") |
| 43 | + |
| 44 | + os.Args = []string{"hugo", "foo", "foo_baz", "--localFlagName", "foo_local2", "--persistentFlagName", "foo_persistent2"} |
| 45 | + ctx = context.WithValue(context.Background(), key("bar"), "baz") |
| 46 | + cdeer2, err := r.Execute(ctx) |
| 47 | + c.Assert(err, qt.IsNil) |
| 48 | + c.Assert(cdeer2.Command.Name(), qt.Equals, "foo_baz") |
| 49 | + tc2 := cdeer2.Command.(*testComand2) |
| 50 | + c.Assert(tc2.ctx, qt.Equals, ctx) |
| 51 | + c.Assert(tc2.localFlagName, qt.Equals, "foo_local2") |
| 52 | + c.Assert(tc.persistentFlagName, qt.Equals, "foo_persistent2") |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +type testComand1 struct { |
| 57 | + persistentFlagName string |
| 58 | + localFlagName string |
| 59 | + |
| 60 | + ctx context.Context |
| 61 | + name string |
| 62 | +} |
| 63 | + |
| 64 | +func (c *testComand1) Run(ctx context.Context, args []string) error { |
| 65 | + c.ctx = ctx |
| 66 | + fmt.Println("testComand.Run", c.name, args) |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *testComand1) Name() string { |
| 71 | + return c.name |
| 72 | +} |
| 73 | + |
| 74 | +func (c *testComand1) AddFlagsLocal(flags *pflag.FlagSet) { |
| 75 | + flags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName") |
| 76 | +} |
| 77 | + |
| 78 | +func (c *testComand1) AddFlagsPersistent(flags *pflag.FlagSet) { |
| 79 | + flags.StringVar(&c.persistentFlagName, "persistentFlagName", "", "set persistentFlagName") |
| 80 | +} |
| 81 | + |
| 82 | +type testComand2 struct { |
| 83 | + localFlagName string |
| 84 | + |
| 85 | + ctx context.Context |
| 86 | + name string |
| 87 | +} |
| 88 | + |
| 89 | +func (c *testComand2) Run(ctx context.Context, args []string) error { |
| 90 | + c.ctx = ctx |
| 91 | + fmt.Println("testComand2.Run", c.name, args) |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (c *testComand2) Name() string { |
| 96 | + return c.name |
| 97 | +} |
| 98 | + |
| 99 | +func (c *testComand2) AddFlagsLocal(flags *pflag.FlagSet) { |
| 100 | + flags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for testCommand2") |
| 101 | +} |
| 102 | + |
| 103 | +func (c *testComand2) AddFlagsPersistent(flags *pflag.FlagSet) { |
| 104 | + |
12 | 105 | } |
0 commit comments