(t *testing.T)
| 117 | } |
| 118 | |
| 119 | func 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 | } |
nothing calls this directly
no test coverage detected