MCPcopy Create free account
hub / github.com/cli/cli / TestNewCmdView

Function TestNewCmdView

pkg/cmd/event/subscription/view/view_test.go:33–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

31}
32
33func TestNewCmdView(t *testing.T) {
34 tests := []struct {
35 name string
36 args string
37 wantID string
38 wantFields []string
39 wantErr string
40 }{
41 {
42 name: "id",
43 args: "sub_1",
44 wantID: "sub_1",
45 },
46 {
47 name: "id with json fields",
48 args: "sub_1 --json id,expiresAt",
49 wantID: "sub_1",
50 wantFields: []string{"id", "expiresAt"},
51 },
52 {
53 name: "no id",
54 wantErr: "subscription ID required",
55 },
56 {
57 name: "more than one id",
58 args: "sub_1 sub_2",
59 wantErr: "too many arguments",
60 },
61 }
62
63 for _, tt := range tests {
64 t.Run(tt.name, func(t *testing.T) {
65 ios, _, _, _ := iostreams.Test()
66 var gotOpts *ViewOptions
67 cmd := NewCmdView(&cmdutil.Factory{IOStreams: ios}, func(opts *ViewOptions) error {
68 gotOpts = opts
69 return nil
70 })
71 args, err := shlex.Split(tt.args)
72 require.NoError(t, err)
73 cmd.SetArgs(args)
74 cmd.SetOut(&bytes.Buffer{})
75 cmd.SetErr(&bytes.Buffer{})
76
77 _, err = cmd.ExecuteC()
78
79 if tt.wantErr != "" {
80 require.ErrorContains(t, err, tt.wantErr)
81 assert.Nil(t, gotOpts)
82 return
83 }
84 require.NoError(t, err)
85 assert.Equal(t, tt.wantID, gotOpts.ID)
86 if tt.wantFields == nil {
87 assert.Nil(t, gotOpts.Exporter)
88 } else {
89 assert.Equal(t, tt.wantFields, gotOpts.Exporter.Fields())
90 }

Callers

nothing calls this directly

Calls 5

TestFunction · 0.92
EqualMethod · 0.80
NewCmdViewFunction · 0.70
RunMethod · 0.65
FieldsMethod · 0.65

Tested by

no test coverage detected