Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9bd0bf

Browse files
committed
added test for UTF-8 characters and simplified the signal interrupt handling
1 parent d8a5830 commit f9bd0bf

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

cli/cliui/prompt.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) {
9191
var line string
9292
var err error
9393

94+
signal.Notify(interrupt, os.Interrupt)
95+
defer signal.Stop(interrupt)
96+
9497
inFile, isInputFile := inv.Stdin.(*os.File)
9598
if opts.Secret && isInputFile && isatty.IsTerminal(inFile.Fd()) {
96-
signal.Notify(interrupt, os.Interrupt)
97-
defer signal.Stop(interrupt)
9899
line, err = readSecretInput(inFile, inv.Stdout)
99100
} else {
100-
signal.Notify(interrupt, os.Interrupt)
101-
defer signal.Stop(interrupt)
102-
103101
line, err = readUntil(inv.Stdin, '\n')
104102

105103
// Check if the first line beings with JSON object or array chars.

cli/cliui/prompt_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ func TestPrompt(t *testing.T) {
202202
resp := testutil.TryReceive(ctx, t, doneChan)
203203
require.Equal(t, "test", resp)
204204
})
205+
206+
t.Run("UTF8Password", func(t *testing.T) {
207+
t.Parallel()
208+
ctx := testutil.Context(t, testutil.WaitShort)
209+
ptty := ptytest.New(t)
210+
doneChan := make(chan string)
211+
go func() {
212+
resp, err := newPrompt(ctx, ptty, cliui.PromptOptions{
213+
Text: "Password:",
214+
Secret: true,
215+
}, nil)
216+
assert.NoError(t, err)
217+
doneChan <- resp
218+
}()
219+
ptty.ExpectMatch("Password: ")
220+
221+
ptty.WriteLine("和製漢字")
222+
223+
resp := testutil.TryReceive(ctx, t, doneChan)
224+
require.Equal(t, "和製漢字", resp)
225+
})
205226
}
206227

207228
func newPrompt(ctx context.Context, ptty *ptytest.PTY, opts cliui.PromptOptions, invOpt func(inv *serpent.Invocation)) (string, error) {

0 commit comments

Comments
 (0)