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