MCPcopy Index your code
hub / github.com/cli/cli / TestNewCmdSwitch

Function TestNewCmdSwitch

pkg/cmd/auth/switch/switch_test.go:20–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestNewCmdSwitch(t *testing.T) {
21 tests := []struct {
22 name string
23 input string
24 expectedOpts SwitchOptions
25 expectedErrMsg string
26 }{
27 {
28 name: "no flags",
29 input: "",
30 expectedOpts: SwitchOptions{},
31 },
32 {
33 name: "hostname flag",
34 input: "--hostname github.com",
35 expectedOpts: SwitchOptions{
36 Hostname: "github.com",
37 },
38 },
39 {
40 name: "user flag",
41 input: "--user monalisa",
42 expectedOpts: SwitchOptions{
43 Username: "monalisa",
44 },
45 },
46 {
47 name: "positional args is an error",
48 input: "some-positional-arg",
49 expectedErrMsg: "accepts 0 arg(s), received 1",
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 f := &cmdutil.Factory{}
56 argv, err := shlex.Split(tt.input)
57 require.NoError(t, err)
58
59 var gotOpts *SwitchOptions
60 cmd := NewCmdSwitch(f, func(opts *SwitchOptions) error {
61 gotOpts = opts
62 return nil
63 })
64 // Override the help flag as happens in production to allow -h flag
65 // to be used for hostname.
66 cmd.Flags().BoolP("help", "x", false, "")
67
68 cmd.SetArgs(argv)
69 cmd.SetIn(&bytes.Buffer{})
70 cmd.SetOut(&bytes.Buffer{})
71 cmd.SetErr(&bytes.Buffer{})
72
73 _, err = cmd.ExecuteC()
74 if tt.expectedErrMsg != "" {
75 require.ErrorContains(t, err, tt.expectedErrMsg)
76 return
77 }

Callers

nothing calls this directly

Calls 3

NewCmdSwitchFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected