Skip to content

Commit eb19ecd

Browse files
FabienTregannywilken
authored andcommitted
Remove trailing carriage returns from answers (#559)
`comms.Question` removes the tailing '\n' (added when pressing enter), hence callers expect a result where the result of pressing "enter" when answering is removed. However, on Windows, pressing enter adds not only `\n` but `\r\n`, causing the code that compare the output with a given string work only on unix and ios. (causing, e.g. #558)
1 parent 73a9898 commit eb19ecd

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

comms/question.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (q Question) Read(r io.Reader) (string, error) {
2222
if err != nil {
2323
return "", err
2424
}
25-
s = strings.Trim(s, "\n")
25+
s = strings.TrimSpace(s)
2626
if s == "" {
2727
return q.DefaultValue, nil
2828
}

comms/question_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func TestQuestion(t *testing.T) {
1717
}{
1818
{"records interactive response", "hello\n", "", "hello"},
1919
{"responds with default if response is empty", "\n", "Fine.", "Fine."},
20+
{"removes trailing \\r in addition to trailing \\", "hello\r\n", "Fine.", "hello"},
21+
{"removes trailing white spaces", "hello \n", "Fine.", "hello"},
22+
{"falls back to default value", " \n", "Default", "Default"},
2023
}
2124
for _, test := range tests {
2225
q := &Question{

0 commit comments

Comments
 (0)