|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/stretchr/testify/assert" |
5 | 4 | "testing" |
| 5 | + |
| 6 | + "github.com/AlecAivazis/survey/v2/terminal" |
| 7 | + expect "github.com/Netflix/go-expect" |
| 8 | + pseudotty "github.com/creack/pty" |
| 9 | + "github.com/hinshun/vt10x" |
| 10 | + "github.com/spf13/afero" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "github.com/stretchr/testify/assert" |
6 | 13 | ) |
7 | 14 |
|
8 | 15 | func Test_newSetupCommand(t *testing.T) { |
9 | | - cmd := newSetupCommand() |
10 | | - assert.Equal(t, "setup", cmd.Name()) |
| 16 | + t.Run("normal", func(t *testing.T) { |
| 17 | + RunTest(t, func(c expectConsole) { |
| 18 | + c.ExpectString("Select proxy-github") |
| 19 | + c.Send("99988866") |
| 20 | + c.SendLine("") |
| 21 | + |
| 22 | + c.ExpectString("Select provider") |
| 23 | + c.Send("gitee") |
| 24 | + c.SendLine("") |
| 25 | + c.ExpectEOF() |
| 26 | + }, func(tr terminal.Stdio) error { |
| 27 | + fs := afero.NewMemMapFs() |
| 28 | + v := viper.New() |
| 29 | + v.SetFs(fs) |
| 30 | + v.Set("provider", "github") |
| 31 | + |
| 32 | + cmd := newSetupCommand(v, tr) |
| 33 | + assert.Equal(t, "setup", cmd.Name()) |
| 34 | + |
| 35 | + err := cmd.Execute() |
| 36 | + assert.Nil(t, err) |
| 37 | + assert.Equal(t, "gh.api.99988866.xyz", v.GetString("proxy-github")) |
| 38 | + assert.Equal(t, "gitee", v.GetString("provider")) |
| 39 | + return err |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("test the default value", func(t *testing.T) { |
| 44 | + RunTest(t, func(c expectConsole) { |
| 45 | + c.ExpectString("Select proxy-github") |
| 46 | + c.SendLine("") |
| 47 | + |
| 48 | + c.ExpectString("Select provider") |
| 49 | + c.SendLine("") |
| 50 | + c.ExpectEOF() |
| 51 | + }, func(tr terminal.Stdio) error { |
| 52 | + fs := afero.NewMemMapFs() |
| 53 | + v := viper.New() |
| 54 | + v.SetFs(fs) |
| 55 | + v.Set("provider", "gitee") |
| 56 | + v.Set("proxy-github", "gh.api.99988866.xyz") |
| 57 | + |
| 58 | + cmd := newSetupCommand(v, tr) |
| 59 | + assert.Equal(t, "setup", cmd.Name()) |
| 60 | + |
| 61 | + err := cmd.Execute() |
| 62 | + assert.Nil(t, err) |
| 63 | + assert.Equal(t, "gh.api.99988866.xyz", v.GetString("proxy-github")) |
| 64 | + assert.Equal(t, "gitee", v.GetString("provider")) |
| 65 | + return err |
| 66 | + }) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestSelectFromList(t *testing.T) { |
| 71 | + RunTest(t, func(c expectConsole) { |
| 72 | + c.ExpectString("title") |
| 73 | + c.SendLine(string(terminal.KeyArrowDown)) |
| 74 | + c.SendLine("") |
| 75 | + c.ExpectEOF() |
| 76 | + }, func(tr terminal.Stdio) error { |
| 77 | + val, err := selectFromList([]string{"one", "two", "three"}, "", "title", tr) |
| 78 | + assert.Equal(t, "two", val) |
| 79 | + return err |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +type expectConsole interface { |
| 84 | + ExpectString(string) |
| 85 | + ExpectEOF() |
| 86 | + SendLine(string) |
| 87 | + Send(string) |
| 88 | +} |
| 89 | + |
| 90 | +func RunTest(t *testing.T, procedure func(expectConsole), test func(terminal.Stdio) error) { |
| 91 | + t.Helper() |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + pty, tty, err := pseudotty.Open() |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("failed to open pseudotty: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + term := vt10x.New(vt10x.WithWriter(tty)) |
| 100 | + c, err := expect.NewConsole(expect.WithStdin(pty), expect.WithStdout(term), expect.WithCloser(pty, tty)) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("failed to create console: %v", err) |
| 103 | + } |
| 104 | + defer c.Close() |
| 105 | + |
| 106 | + donec := make(chan struct{}) |
| 107 | + go func() { |
| 108 | + defer close(donec) |
| 109 | + procedure(&consoleWithErrorHandling{console: c, t: t}) |
| 110 | + }() |
| 111 | + |
| 112 | + stdio := terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()} |
| 113 | + if err := test(stdio); err != nil { |
| 114 | + t.Error(err) |
| 115 | + } |
| 116 | + |
| 117 | + if err := c.Tty().Close(); err != nil { |
| 118 | + t.Errorf("error closing Tty: %v", err) |
| 119 | + } |
| 120 | + <-donec |
| 121 | +} |
| 122 | + |
| 123 | +type consoleWithErrorHandling struct { |
| 124 | + console *expect.Console |
| 125 | + t *testing.T |
| 126 | +} |
| 127 | + |
| 128 | +func (c *consoleWithErrorHandling) ExpectString(s string) { |
| 129 | + if _, err := c.console.ExpectString(s); err != nil { |
| 130 | + c.t.Helper() |
| 131 | + c.t.Fatalf("ExpectString(%q) = %v", s, err) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func (c *consoleWithErrorHandling) SendLine(s string) { |
| 136 | + if _, err := c.console.SendLine(s); err != nil { |
| 137 | + c.t.Helper() |
| 138 | + c.t.Fatalf("SendLine(%q) = %v", s, err) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (c *consoleWithErrorHandling) Send(s string) { |
| 143 | + if _, err := c.console.Send(s); err != nil { |
| 144 | + c.t.Helper() |
| 145 | + c.t.Fatalf("Send(%q) = %v", s, err) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func (c *consoleWithErrorHandling) ExpectEOF() { |
| 150 | + if _, err := c.console.ExpectEOF(); err != nil { |
| 151 | + c.t.Helper() |
| 152 | + c.t.Fatalf("ExpectEOF() = %v", err) |
| 153 | + } |
11 | 154 | } |
0 commit comments