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

Function Test_addRun

pkg/cmd/repo/deploy-key/add/add_test.go:16–89  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func Test_addRun(t *testing.T) {
17 tests := []struct {
18 name string
19 opts AddOptions
20 isTTY bool
21 stdin string
22 httpStubs func(t *testing.T, reg *httpmock.Registry)
23 wantStdout string
24 wantStderr string
25 wantErr bool
26 }{
27 {
28 name: "add from stdin",
29 isTTY: true,
30 opts: AddOptions{
31 KeyFile: "-",
32 Title: "my sacred key",
33 AllowWrite: false,
34 },
35 stdin: "PUBKEY\n",
36 httpStubs: func(t *testing.T, reg *httpmock.Registry) {
37 reg.Register(
38 httpmock.REST("POST", "repos/OWNER/REPO/keys"),
39 httpmock.RESTPayload(200, `{}`, func(payload map[string]any) {
40 if title := payload["title"].(string); title != "my sacred key" {
41 t.Errorf("POST title %q, want %q", title, "my sacred key")
42 }
43 if key := payload["key"].(string); key != "PUBKEY\n" {
44 t.Errorf("POST key %q, want %q", key, "PUBKEY\n")
45 }
46 if isReadOnly := payload["read_only"].(bool); !isReadOnly {
47 t.Errorf("POST read_only %v, want %v", isReadOnly, true)
48 }
49 }))
50 },
51 wantStdout: "✓ Deploy key added to OWNER/REPO\n",
52 wantStderr: "",
53 wantErr: false,
54 },
55 }
56
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
59 ios, stdin, stdout, stderr := iostreams.Test()
60 stdin.WriteString(tt.stdin)
61 ios.SetStdinTTY(tt.isTTY)
62 ios.SetStdoutTTY(tt.isTTY)
63 ios.SetStderrTTY(tt.isTTY)
64
65 reg := &httpmock.Registry{}
66 if tt.httpStubs != nil {
67 tt.httpStubs(t, reg)
68 }
69
70 opts := tt.opts
71 opts.IO = ios
72 opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }
73 opts.HTTPClient = func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }

Callers

nothing calls this directly

Calls 12

RegisterMethod · 0.95
RESTFunction · 0.92
RESTPayloadFunction · 0.92
TestFunction · 0.92
NewFunction · 0.92
addRunFunction · 0.85
SetStdinTTYMethod · 0.80
SetStdoutTTYMethod · 0.80
SetStderrTTYMethod · 0.80
ErrorfMethod · 0.65
RunMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected