(t *testing.T)
| 236 | } |
| 237 | |
| 238 | func TestRecordTelemetryForSubcommands(t *testing.T) { |
| 239 | t.Parallel() |
| 240 | |
| 241 | // Given a command tree instrumented from its root |
| 242 | recorder := &telemetry.EventRecorderSpy{} |
| 243 | root := &cobra.Command{Use: "gh"} |
| 244 | parent := &cobra.Command{Use: "pr"} |
| 245 | child := &cobra.Command{ |
| 246 | Use: "list", |
| 247 | RunE: func(cmd *cobra.Command, args []string) error { return nil }, |
| 248 | } |
| 249 | root.AddCommand(parent) |
| 250 | root.AddCommand(&cobra.Command{ |
| 251 | Use: "version", |
| 252 | RunE: func(cmd *cobra.Command, args []string) error { return nil }, |
| 253 | }) |
| 254 | parent.AddCommand(child) |
| 255 | root.SetArgs([]string{"pr", "list"}) |
| 256 | cmdutil.RecordTelemetryForSubcommands(root, recorder) |
| 257 | |
| 258 | // When Cobra executes a nested command |
| 259 | _, err := root.ExecuteC() |
| 260 | |
| 261 | // Then only the invoked descendant is recorded |
| 262 | require.NoError(t, err) |
| 263 | events := recorder.Events() |
| 264 | require.Len(t, events, 1) |
| 265 | assert.Equal(t, "command_invocation", events[0].Type) |
| 266 | assert.Equal(t, "gh pr list", events[0].Dimensions["command"]) |
| 267 | } |
nothing calls this directly
no test coverage detected