MCPcopy
hub / github.com/cli/cli / TestNewCmdSendTelemetry

Function TestNewCmdSendTelemetry

pkg/cmd/send-telemetry/send_telemetry_test.go:31–107  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestNewCmdSendTelemetry(t *testing.T) {
32 tests := []struct {
33 name string
34 stdin string
35 env map[string]string
36 wantOpts SendTelemetryOptions
37 wantErr string
38 }{
39 {
40 name: "reads payload from stdin",
41 stdin: `{"events":[{"type":"usage","dimensions":{"command":"gh pr list"}}]}`,
42 wantOpts: SendTelemetryOptions{
43 TelemetryEndpointURL: defaultTelemetryEndpointURL,
44 PayloadJSON: `{"events":[{"type":"usage","dimensions":{"command":"gh pr list"}}]}`,
45 },
46 },
47 {
48 name: "uses GH_TELEMETRY_ENDPOINT_URL env var",
49 stdin: `{"events":[]}`,
50 env: map[string]string{"GH_TELEMETRY_ENDPOINT_URL": "https://custom.endpoint"},
51 wantOpts: SendTelemetryOptions{
52 TelemetryEndpointURL: "https://custom.endpoint",
53 PayloadJSON: `{"events":[]}`,
54 },
55 },
56 {
57 name: "defaults endpoint when env var not set",
58 stdin: `{}`,
59 wantOpts: SendTelemetryOptions{
60 TelemetryEndpointURL: defaultTelemetryEndpointURL,
61 PayloadJSON: `{}`,
62 },
63 },
64 {
65 name: "errors on empty stdin",
66 stdin: "",
67 wantErr: "no payload provided on stdin",
68 },
69 }
70
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 for k, v := range tt.env {
74 t.Setenv(k, v)
75 }
76
77 ios, _, _, _ := iostreams.Test()
78 f := &cmdutil.Factory{
79 IOStreams: ios,
80 Config: func() (gh.Config, error) {
81 return config.NewBlankConfig(), nil
82 },
83 }
84
85 var gotOpts *SendTelemetryOptions
86 cmd := newCmdSendTelemetry(f, func(opts *SendTelemetryOptions) error {
87 gotOpts = opts
88 return nil

Callers

nothing calls this directly

Calls 7

TestFunction · 0.92
NewBlankConfigFunction · 0.92
newCmdSendTelemetryFunction · 0.85
ContainsMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected