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

Function TestRecordTelemetry

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

Source from the content-addressed store, hash-verified

14)
15
16func TestRecordTelemetry(t *testing.T) {
17 t.Run("records commands that fail argument validation", func(t *testing.T) {
18 t.Parallel()
19
20 // Given a command that requires a positional argument
21 recorder := &telemetry.EventRecorderSpy{}
22 cmd := &cobra.Command{
23 Use: "list",
24 Args: cobra.ExactArgs(1),
25 SilenceErrors: true,
26 SilenceUsage: true,
27 RunE: func(cmd *cobra.Command, args []string) error { return nil },
28 }
29 cmd.Flags().Bool("web", false, "")
30 cmd.SetArgs([]string{"--web"})
31 cmdutil.RecordTelemetry(cmd, recorder)
32
33 // When Cobra rejects its arguments
34 _, err := cmd.ExecuteC()
35
36 // Then the failure is preserved and the command is still recorded
37 require.EqualError(t, err, "accepts 1 arg(s), received 0")
38 events := recorder.Events()
39 require.Len(t, events, 1)
40 assert.Equal(t, "command_invocation", events[0].Type)
41 assert.Equal(t, "list", events[0].Dimensions["command"])
42 assert.Equal(t, "web", events[0].Dimensions["flags"])
43 })
44
45 tests := []struct {
46 name string
47 args []string
48 flags string
49 }{
50 {
51 name: "records sorted flag names without argument or flag values",
52 args: []string{"--web", "--repo", "private/repository", "private argument"},
53 flags: "repo,web",
54 },
55 {
56 name: "accepts positional arguments with nil Args and records empty flags",
57 args: []string{"private argument"},
58 flags: "",
59 },
60 }
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 t.Parallel()
64
65 // Given a command without argument validation
66 recorder := &telemetry.EventRecorderSpy{}
67 cmd := &cobra.Command{
68 Use: "list",
69 RunE: func(cmd *cobra.Command, args []string) error { return nil },
70 }
71 cmd.Flags().Bool("web", false, "")
72 cmd.Flags().String("repo", "", "")
73 cmd.Flags().Bool("unused", false, "")

Callers

nothing calls this directly

Calls 9

EventsMethod · 0.95
RecordTelemetryFunction · 0.92
DisableTelemetryFunction · 0.92
EqualMethod · 0.80
EmptyMethod · 0.80
RunMethod · 0.65
LenMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected