|
| 1 | +package surveyext |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/AlecAivazis/survey/v2" |
| 14 | + "github.com/AlecAivazis/survey/v2/terminal" |
| 15 | + pseudotty "github.com/creack/pty" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func Test_GhEditor_Prompt(t *testing.T) { |
| 21 | + e := &GhEditor{ |
| 22 | + BlankAllowed: true, |
| 23 | + EditorCommand: "false", |
| 24 | + Editor: &survey.Editor{ |
| 25 | + Message: "Body", |
| 26 | + FileName: "*.md", |
| 27 | + Default: "initial value", |
| 28 | + HideDefault: true, |
| 29 | + AppendDefault: true, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + pty, tty, err := pseudotty.Open() |
| 34 | + if errors.Is(err, pseudotty.ErrUnsupported) { |
| 35 | + return |
| 36 | + } |
| 37 | + require.NoError(t, err) |
| 38 | + defer pty.Close() |
| 39 | + defer tty.Close() |
| 40 | + |
| 41 | + err = pseudotty.Setsize(tty, &pseudotty.Winsize{Cols: 72, Rows: 30}) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + out := teeWriter{File: tty} |
| 45 | + e.WithStdio(terminal.Stdio{ |
| 46 | + In: tty, |
| 47 | + Out: &out, |
| 48 | + Err: tty, |
| 49 | + }) |
| 50 | + |
| 51 | + var res string |
| 52 | + errc := make(chan error) |
| 53 | + |
| 54 | + go func() { |
| 55 | + r, err := e.Prompt(defaultPromptConfig()) |
| 56 | + if r != nil { |
| 57 | + res = r.(string) |
| 58 | + } |
| 59 | + errc <- err |
| 60 | + }() |
| 61 | + |
| 62 | + time.Sleep(5 * time.Millisecond) |
| 63 | + assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch false, enter to skip] \x1b[0m\x1b[?25l", out.String()) |
| 64 | + fmt.Fprint(pty, "\n") // send Enter key |
| 65 | + |
| 66 | + err = <-errc |
| 67 | + assert.NoError(t, err) |
| 68 | + assert.Equal(t, "initial value", res) |
| 69 | + assert.Equal(t, "\x1b[?25h", out.String()) |
| 70 | +} |
| 71 | + |
| 72 | +// survey doesn't expose this |
| 73 | +func defaultPromptConfig() *survey.PromptConfig { |
| 74 | + return &survey.PromptConfig{ |
| 75 | + PageSize: 7, |
| 76 | + HelpInput: "?", |
| 77 | + SuggestInput: "tab", |
| 78 | + Icons: survey.IconSet{ |
| 79 | + Error: survey.Icon{ |
| 80 | + Text: "X", |
| 81 | + Format: "red", |
| 82 | + }, |
| 83 | + Help: survey.Icon{ |
| 84 | + Text: "?", |
| 85 | + Format: "cyan", |
| 86 | + }, |
| 87 | + Question: survey.Icon{ |
| 88 | + Text: "?", |
| 89 | + Format: "green+hb", |
| 90 | + }, |
| 91 | + MarkedOption: survey.Icon{ |
| 92 | + Text: "[x]", |
| 93 | + Format: "green", |
| 94 | + }, |
| 95 | + UnmarkedOption: survey.Icon{ |
| 96 | + Text: "[ ]", |
| 97 | + Format: "default+hb", |
| 98 | + }, |
| 99 | + SelectFocus: survey.Icon{ |
| 100 | + Text: ">", |
| 101 | + Format: "cyan+b", |
| 102 | + }, |
| 103 | + }, |
| 104 | + Filter: func(filter string, value string, index int) (include bool) { |
| 105 | + filter = strings.ToLower(filter) |
| 106 | + return strings.Contains(strings.ToLower(value), filter) |
| 107 | + }, |
| 108 | + KeepFilter: false, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// teeWriter is a writer that duplicates all writes to a file into a buffer |
| 113 | +type teeWriter struct { |
| 114 | + *os.File |
| 115 | + buf bytes.Buffer |
| 116 | + mu sync.Mutex |
| 117 | +} |
| 118 | + |
| 119 | +func (f *teeWriter) Write(p []byte) (n int, err error) { |
| 120 | + f.mu.Lock() |
| 121 | + defer f.mu.Unlock() |
| 122 | + _, _ = f.buf.Write(p) |
| 123 | + return f.File.Write(p) |
| 124 | +} |
| 125 | + |
| 126 | +func (f *teeWriter) String() string { |
| 127 | + f.mu.Lock() |
| 128 | + s := f.buf.String() |
| 129 | + f.buf.Reset() |
| 130 | + f.mu.Unlock() |
| 131 | + return s |
| 132 | +} |
0 commit comments