Skip to content

Commit a1bc06f

Browse files
committed
Bugfix: ref: and path: versions are not read correctly.
Before this patch, with a `.tool-versions` file like: ``` lfe ref:master ``` `get_preset_version_for` would return `ref` instead of `ref:master`. Same was happening for `path:` versions. Actually there was PR asdf-vm#95 on which I based my changes but instead of using space as delimiter I went for using `|` which would be a lot more weird if present as part of a file path, this also allows to specify paths which have spaces which are much more frequent. Closes asdf-vm#94 asdf-vm#95
1 parent 786d8f3 commit a1bc06f

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

lib/commands/current.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ current_command() {
55

66
local search_path=$(pwd)
77
local version_and_path=$(find_version "$plugin_name" "$search_path")
8-
local version=$(cut -d ':' -f 1 <<< "$version_and_path");
9-
local version_file_path=$(cut -d ':' -f 2 <<< "$version_and_path");
8+
local version=$(cut -d '|' -f 1 <<< "$version_and_path");
9+
local version_file_path=$(cut -d '|' -f 2 <<< "$version_and_path");
1010

1111
check_if_version_exists $plugin_name $version
1212
check_for_deprecated_plugin $plugin_name

lib/utils.sh

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ get_version_in_dir() {
6464
local asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" $plugin_name)
6565

6666
if [ -n "$asdf_version" ]; then
67-
echo "$asdf_version:$search_path/.tool-versions"
67+
echo "$asdf_version|$search_path/.tool-versions"
6868
return 0
6969
fi
7070

7171
for filename in $legacy_filenames; do
7272
local legacy_version=$(parse_legacy_version_file "$search_path/$filename" $plugin_name)
7373

7474
if [ -n "$legacy_version" ]; then
75-
echo "$legacy_version:$search_path/$filename"
75+
echo "$legacy_version|$search_path/$filename"
7676
return 0
7777
fi
7878
done
@@ -108,14 +108,11 @@ parse_asdf_version_file() {
108108
local plugin_name=$2
109109

110110
if [ -f "$file_path" ]; then
111-
cat $file_path | while read -r line || [[ -n "$line" ]]; do
112-
local line_parts=($line)
113-
114-
if [ "${line_parts[0]}" = "$plugin_name" ]; then
115-
echo ${line_parts[1]}
116-
return 0
117-
fi
118-
done
111+
local version=$(grep "${plugin_name} " $file_path | sed -e "s/^${plugin_name} //")
112+
if [ -n "$version" ]; then
113+
echo $version
114+
return 0
115+
fi
119116
fi
120117
}
121118

@@ -139,7 +136,7 @@ get_preset_version_for() {
139136
local plugin_name=$1
140137
local search_path=$(pwd)
141138
local version_and_path=$(find_version "$plugin_name" "$search_path")
142-
local version=$(cut -d ':' -f 1 <<< "$version_and_path");
139+
local version=$(cut -d '|' -f 1 <<< "$version_and_path");
143140

144141
echo "$version"
145142
}

test/utils.bats

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ teardown() {
5252

5353
run find_version "dummy" $PROJECT_DIR
5454
[ "$status" -eq 0 ]
55-
[ "$output" = "0.1.0:$PROJECT_DIR/.tool-versions" ]
55+
[ "$output" = "0.1.0|$PROJECT_DIR/.tool-versions" ]
5656
}
5757

5858
@test "find_version should return the legacy file if supported" {
@@ -62,7 +62,7 @@ teardown() {
6262

6363
run find_version "dummy" $PROJECT_DIR
6464
[ "$status" -eq 0 ]
65-
[ "$output" = "0.2.0:$PROJECT_DIR/.dummy-version" ]
65+
[ "$output" = "0.2.0|$PROJECT_DIR/.dummy-version" ]
6666
}
6767

6868
@test "find_version skips .tool-version file that don't list the plugin" {
@@ -71,7 +71,7 @@ teardown() {
7171

7272
run find_version "dummy" $PROJECT_DIR
7373
[ "$status" -eq 0 ]
74-
[ "$output" = "0.1.0:$HOME/.tool-versions" ]
74+
[ "$output" = "0.1.0|$HOME/.tool-versions" ]
7575
}
7676

7777
@test "find_version should return .tool-versions if unsupported" {
@@ -82,7 +82,7 @@ teardown() {
8282

8383
run find_version "dummy" $PROJECT_DIR
8484
[ "$status" -eq 0 ]
85-
[ "$output" = "0.1.0:$HOME/.tool-versions" ]
85+
[ "$output" = "0.1.0|$HOME/.tool-versions" ]
8686
}
8787

8888
@test "get_preset_version_for returns the current version" {
@@ -101,3 +101,19 @@ teardown() {
101101
[ "$status" -eq 0 ]
102102
[ "$output" = "0.1.0" ]
103103
}
104+
105+
@test "get_preset_version_for should return branch reference version" {
106+
cd $PROJECT_DIR
107+
echo "dummy ref:master" > $PROJECT_DIR/.tool-versions
108+
run get_preset_version_for "dummy"
109+
[ "$status" -eq 0 ]
110+
[ "$output" = "ref:master" ]
111+
}
112+
113+
@test "get_preset_version_for should return path version" {
114+
cd $PROJECT_DIR
115+
echo "dummy path:/some/place with spaces" > $PROJECT_DIR/.tool-versions
116+
run get_preset_version_for "dummy"
117+
[ "$status" -eq 0 ]
118+
[ "$output" = "path:/some/place with spaces" ]
119+
}

0 commit comments

Comments
 (0)