fix: support latest with filter on local and global (#633)

Co-authored-by: Thomas B Homburg <thomas@homburg.dk>
Co-authored-by: James Hegedus <jthegedus@hey.com>
This commit is contained in:
Kevin Lane 2021-07-06 04:17:00 -07:00 committed by GitHub
parent 04948a3286
commit 5cf8f8962f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 124 additions and 11 deletions

View File

@ -28,6 +28,13 @@ asdf list <name>
# asdf list erlang
```
Limit versions to those that begin with a given string.
```shell
asdf list <name> <version>
# asdf list erlang 17
```
## List All Available Versions
```shell
@ -63,6 +70,10 @@ asdf global <name> <version> [<version>...]
asdf shell <name> <version> [<version>...]
asdf local <name> <version> [<version>...]
# asdf global elixir 1.2.4
asdf global <name> latest[:<version>]
asdf local <name> latest[:<version>]
# asdf global elixir latest
```
`global` writes the version to `$HOME/.tool-versions`.

View File

@ -8,11 +8,18 @@ latest_command() {
[[ -z $query ]] && query="$DEFAULT_QUERY"
local versions
# pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest)
asdf list-all "$plugin_name" "$query" |
versions=$(asdf list-all "$plugin_name" "$query" |
grep -vE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" |
sed 's/^\s\+//' |
tail -1
tail -1)
if [ -z "${versions}" ]; then
exit 1
fi
printf "%s\n" "$versions"
}
latest_command "$@"

View File

@ -32,6 +32,11 @@ list_all_command() {
output=$(cat "$std_out_file")
fi
if [ -z "$output" ]; then
display_error "No compatible versions available ($plugin_name $query)"
exit 1
fi
IFS=' ' read -r -a versions_list <<<"$output"
for version in "${versions_list[@]}"; do

View File

@ -2,6 +2,7 @@
list_command() {
local plugin_name=$1
local query=$2
if [ -z "$plugin_name" ]; then
local plugins_path
@ -11,20 +12,31 @@ list_command() {
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
printf "%s\\n" "$plugin_name"
display_installed_versions "$plugin_name"
display_installed_versions "$plugin_name" "$query"
done
else
printf "%s\\n" 'No plugins installed'
fi
else
check_if_plugin_exists "$plugin_name"
display_installed_versions "$plugin_name"
display_installed_versions "$plugin_name" "$query"
fi
}
display_installed_versions() {
local plugin_name=$1
local query=$2
local versions
versions=$(list_installed_versions "$1")
versions=$(list_installed_versions "$plugin_name")
if [[ $query ]]; then
versions=$(printf "%s\n" "$versions" | grep -E "^\s*$query")
if [ -z "${versions}" ]; then
display_error "No compatible versions installed ($plugin_name $query)"
exit 1
fi
fi
if [ -n "${versions}" ]; then
for version in $versions; do

View File

@ -33,15 +33,28 @@ version_command() {
check_if_plugin_exists "$plugin_name"
declare -a resolved_versions
local version
for version in "${versions[@]}"; do
if [ "$version" = "latest" ]; then
local item
for item in "${!versions[@]}"; do
IFS=':' read -r -a version_info <<<"${versions[$item]}"
if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then
version=$(asdf latest "$plugin_name" "${version_info[1]}")
elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then
version=$(asdf latest "$plugin_name")
else
# if branch handles ref: || path: || normal versions
version="${versions[$item]}"
fi
# check_if_version_exists should probably handle if either param is empty string
if [ -z "$version" ]; then
exit 1
fi
if ! (check_if_version_exists "$plugin_name" "$version"); then
version_not_installed_text "$plugin_name" "$version" 1>&2
exit 1
fi
resolved_versions+=("$version")
done

View File

@ -13,12 +13,18 @@ teardown() {
@test "latest_command shows latest stable version" {
run asdf latest dummy
[ "$(echo -e "2.0.0")" == "$output" ]
[ "$(echo "2.0.0")" == "$output" ]
[ "$status" -eq 0 ]
}
@test "latest_command with version shows latest stable version that matches the given string" {
run asdf latest dummy 1
[ "$(echo -e "1.1.0")" == "$output" ]
[ "$(echo "1.1.0")" == "$output" ]
[ "$status" -eq 0 ]
}
@test "latest_command with an invalid version should return an error" {
run asdf latest dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}

View File

@ -43,6 +43,23 @@ teardown() {
[ "$status" -eq 0 ]
}
@test "list_command with version filters installed versions" {
run asdf install dummy 1.0
run asdf install dummy 1.1
run asdf install dummy 2.0
run asdf list dummy 1
[ "$(echo -e " 1.0\n 1.1")" == "$output" ]
[ "$status" -eq 0 ]
}
@test "list_command with an invalid version should return an error" {
run asdf install dummy 1.0
run asdf install dummy 1.1
run asdf list dummy 2
[ "$(echo "No compatible versions installed (dummy 2)")" == "$output" ]
[ "$status" -eq 1 ]
}
@test "list_all_command lists available versions" {
run asdf list-all dummy
[ "$(echo -e "1.0.0\n1.1.0\n2.0.0")" == "$output" ]
@ -55,6 +72,12 @@ teardown() {
[ "$status" -eq 0 ]
}
@test "list_all_command with an invalid version should return an error" {
run asdf list-all dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}
@test "list_all_command fails when list-all script exits with non-zero code" {
run asdf list-all dummy-broken
echo $output
@ -69,7 +92,6 @@ teardown() {
[[ "$output" == *"Attempting to list versions" ]]
}
@test "list_all_command ignores stderr when completing successfully" {
run asdf list-all dummy
[[ "$output" != *"ignore this error"* ]]

View File

@ -47,6 +47,24 @@ teardown() {
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
}
@test "local with latest should use the latest installed version" {
run asdf local "dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 2.0.0" ]
}
@test "local with latest:version should use the latest valid installed version" {
run asdf local "dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.0.0" ]
}
@test "local with latest:version should return an error for invalid versions" {
run asdf local "dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (dummy 99)")" ]
}
@test "local should allow multiple versions" {
run asdf local "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
@ -75,6 +93,7 @@ teardown() {
@test "local should overwrite the existing version if it's set" {
echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions
run asdf local "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
@ -141,6 +160,24 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}
@test "global with latest should use the latest installed version" {
run asdf global "dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 2.0.0" ]
}
@test "global with latest:version should use the latest valid installed version" {
run asdf global "dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 1.0.0" ]
}
@test "global with latest:version should return an error for invalid versions" {
run asdf global "dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (dummy 99)")" ]
}
@test "global should accept multiple versions" {
run asdf global "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]