From 2fd3b18d39bc319a5022ca3e62e51d474ff33a4c Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Thu, 20 Mar 2014 11:30:27 +0100 Subject: [PATCH 1/5] Remove carriage return characters in version file When created on Windows, .rbenv-version or .ruby-version files may have CR characters that will prevent rbenv from correctly parsing the Ruby version. Discard those characters when reading the file. --- libexec/rbenv-version-file-read | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read index cde0ca75..9385ab1e 100755 --- a/libexec/rbenv-version-file-read +++ b/libexec/rbenv-version-file-read @@ -9,7 +9,7 @@ if [ -e "$VERSION_FILE" ]; then # Read the first non-whitespace word from the specified version file. # Be careful not to load it whole in case there's something crazy in it. words=( $(cut -b 1-1024 "$VERSION_FILE") ) - version="${words[0]}" + version="${words[0]//[$'\r']}" if [ -n "$version" ]; then echo "$version" From 3be9773c4f8180c9902403af4331e3ef64b94539 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Thu, 20 Mar 2014 16:27:13 +0100 Subject: [PATCH 2/5] Add test for carriage return in ruby-version file --- test/version.bats | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/version.bats b/test/version.bats index 886d0efa..1fb97c70 100644 --- a/test/version.bats +++ b/test/version.bats @@ -30,6 +30,13 @@ setup() { assert_success "1.9.3 (set by ${PWD}/.ruby-version)" } +@test "set by local file containing CR" { + create_version "1.9.3" + cat > ".ruby-version" <<< $'1.9.3\r' + run rbenv-version + assert_success "1.9.3 (set by ${PWD}/.ruby-version)" +} + @test "set by global file" { create_version "1.9.3" cat > "${RBENV_ROOT}/version" <<<"1.9.3" From f50cee2ac7cc1adbe6849a72865b99f9cc0a2da3 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Thu, 20 Mar 2014 16:52:45 +0100 Subject: [PATCH 3/5] Simplify bash expression --- libexec/rbenv-version-file-read | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read index 9385ab1e..521a6cf9 100755 --- a/libexec/rbenv-version-file-read +++ b/libexec/rbenv-version-file-read @@ -9,7 +9,7 @@ if [ -e "$VERSION_FILE" ]; then # Read the first non-whitespace word from the specified version file. # Be careful not to load it whole in case there's something crazy in it. words=( $(cut -b 1-1024 "$VERSION_FILE") ) - version="${words[0]//[$'\r']}" + version="${words[0]//$'\r'}" if [ -n "$version" ]; then echo "$version" From f205ec83592aa5a6f61a720ba5ee5cb950790b97 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Fri, 21 Mar 2014 01:32:29 +0100 Subject: [PATCH 4/5] Move carriage return test to version-file-read --- test/version-file-read.bats | 6 ++++++ test/version.bats | 7 ------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/version-file-read.bats b/test/version-file-read.bats index 66bd46fa..bf7bf910 100644 --- a/test/version-file-read.bats +++ b/test/version-file-read.bats @@ -64,3 +64,9 @@ IN run rbenv-version-file-read my-version assert_success "1.8.7" } + +@test "ignores carriage returns" { + cat > my-version <<< $'1.9.3\r' + run rbenv-version-file-read my-version + assert_success "1.9.3" +} diff --git a/test/version.bats b/test/version.bats index 1fb97c70..886d0efa 100644 --- a/test/version.bats +++ b/test/version.bats @@ -30,13 +30,6 @@ setup() { assert_success "1.9.3 (set by ${PWD}/.ruby-version)" } -@test "set by local file containing CR" { - create_version "1.9.3" - cat > ".ruby-version" <<< $'1.9.3\r' - run rbenv-version - assert_success "1.9.3 (set by ${PWD}/.ruby-version)" -} - @test "set by global file" { create_version "1.9.3" cat > "${RBENV_ROOT}/version" <<<"1.9.3" From b025dfcf585abc2528028c0d0c360048eeb13b21 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Fri, 21 Mar 2014 01:36:39 +0100 Subject: [PATCH 5/5] Add \r to IFS instead of removing it manually --- libexec/rbenv-version-file-read | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read index 521a6cf9..03d2db0c 100755 --- a/libexec/rbenv-version-file-read +++ b/libexec/rbenv-version-file-read @@ -8,8 +8,9 @@ VERSION_FILE="$1" if [ -e "$VERSION_FILE" ]; then # Read the first non-whitespace word from the specified version file. # Be careful not to load it whole in case there's something crazy in it. + IFS="${IFS}"$'\r' words=( $(cut -b 1-1024 "$VERSION_FILE") ) - version="${words[0]//$'\r'}" + version="${words[0]}" if [ -n "$version" ]; then echo "$version"