Skip to content

Commit

Permalink
Improve windows support by removing carriage return from prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
ptxmac committed Mar 22, 2020
1 parent db03a2b commit 568fb50
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/init.go
Expand Up @@ -177,5 +177,5 @@ func (c *Config) promptString(field string) string {
fmt.Fprintf(c.Stdout, "%s? ", field)
value, err := bufio.NewReader(c.Stdin).ReadString('\n')
panicOnError(err)
return strings.TrimSuffix(value, "\n")
return strings.TrimRight(value, "\r\n")
}
36 changes: 36 additions & 0 deletions cmd/init_test.go
Expand Up @@ -13,6 +13,42 @@ import (
"github.com/twpayne/go-vfs/vfst"
)

func TestPromptStringIgnoreWindowsNewline(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"/home/user/.local/share/chezmoi/.chezmoi.toml.tmpl": strings.Join([]string{
`{{ $env := promptString "environment [work/home]" -}}`,
`[data]`,
` environment = "{{ $env }}"`,
}, "\n"),
})
require.NoError(t, err)
defer cleanup()

c := newTestConfig(
fs,
withStdin(bytes.NewBufferString("home\r\n")),
)

require.NoError(t, c.createConfigFile())

vfst.RunTests(t, fs, "",
vfst.TestPath("/home/user/.config/chezmoi/chezmoi.toml",
vfst.TestModeIsRegular,
vfst.TestModePerm(0600),
vfst.TestContentsString(
strings.Join([]string{
`[data]`,
` environment = "home"`,
}, "\n"),
),
),
)

assert.Equal(t, map[string]interface{}{
"environment": "home",
}, c.Data)
}

func TestCreateConfigFile(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"/home/user/.local/share/chezmoi/.chezmoi.yaml.tmpl": strings.Join([]string{
Expand Down

0 comments on commit 568fb50

Please sign in to comment.