MCPcopy
hub / github.com/cli/cli / TestRecordTelemetryForSubcommands

Function TestRecordTelemetryForSubcommands

pkg/cmdutil/telemetry_test.go:119–168  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

117}
118
119func TestRecordTelemetryForSubcommands(t *testing.T) {
120 t.Run("instruments nested subcommands", func(t *testing.T) {
121 recorder := &telemetry.EventRecorderSpy{}
122
123 root := &cobra.Command{Use: "gh"}
124 parent := &cobra.Command{Use: "pr"}
125 child := &cobra.Command{
126 Use: "list",
127 RunE: func(cmd *cobra.Command, args []string) error { return nil },
128 }
129 root.AddCommand(parent)
130 parent.AddCommand(child)
131
132 cmdutil.RecordTelemetryForSubcommands(root, recorder)
133 require.NoError(t, child.RunE(child, nil))
134
135 require.Len(t, recorder.Events, 1)
136 assert.Equal(t, "command_invocation", recorder.Events[0].Type)
137 assert.Equal(t, "gh pr list", recorder.Events[0].Dimensions["command"])
138 })
139
140 t.Run("skips subcommands with nil RunE", func(t *testing.T) {
141 recorder := &telemetry.EventRecorderSpy{}
142
143 root := &cobra.Command{Use: "gh"}
144 child := &cobra.Command{Use: "help"} // no RunE
145 root.AddCommand(child)
146
147 cmdutil.RecordTelemetryForSubcommands(root, recorder)
148
149 assert.Nil(t, child.RunE, "nil RunE should remain nil")
150 })
151
152 t.Run("skips subcommands with telemetry disabled", func(t *testing.T) {
153 recorder := &telemetry.EventRecorderSpy{}
154
155 root := &cobra.Command{Use: "gh"}
156 child := &cobra.Command{
157 Use: "send-telemetry",
158 RunE: func(cmd *cobra.Command, args []string) error { return nil },
159 }
160 cmdutil.DisableTelemetry(child)
161 root.AddCommand(child)
162
163 cmdutil.RecordTelemetryForSubcommands(root, recorder)
164 require.NoError(t, child.RunE(child, nil))
165
166 assert.Empty(t, recorder.Events, "disabled commands should not record telemetry")
167 })
168}

Callers

nothing calls this directly

Calls 5

DisableTelemetryFunction · 0.92
EqualMethod · 0.80
RunMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected