From 5cf8f8962fbd5fe2bc86856bc4676f88e1aa8885 Mon Sep 17 00:00:00 2001 From: Kevin Lane Date: Tue, 6 Jul 2021 04:17:00 -0700 Subject: [PATCH] fix: support latest with filter on local and global (#633) Co-authored-by: Thomas B Homburg Co-authored-by: James Hegedus --- docs/core-manage-versions.md | 11 +++++++++ lib/commands/command-latest.bash | 11 +++++++-- lib/commands/command-list-all.bash | 5 ++++ lib/commands/command-list.bash | 18 ++++++++++++--- lib/commands/version_commands.bash | 19 ++++++++++++--- test/latest_command.bats | 10 ++++++-- test/list_command.bats | 24 ++++++++++++++++++- test/version_commands.bats | 37 ++++++++++++++++++++++++++++++ 8 files changed, 124 insertions(+), 11 deletions(-) diff --git a/docs/core-manage-versions.md b/docs/core-manage-versions.md index ea69b2af6..848b13134 100644 --- a/docs/core-manage-versions.md +++ b/docs/core-manage-versions.md @@ -28,6 +28,13 @@ asdf list # asdf list erlang ``` +Limit versions to those that begin with a given string. + +```shell +asdf list +# asdf list erlang 17 +``` + ## List All Available Versions ```shell @@ -63,6 +70,10 @@ asdf global [...] asdf shell [...] asdf local [...] # asdf global elixir 1.2.4 + +asdf global latest[:] +asdf local latest[:] +# asdf global elixir latest ``` `global` writes the version to `$HOME/.tool-versions`. diff --git a/lib/commands/command-latest.bash b/lib/commands/command-latest.bash index c73f9f350..efcac8fe0 100644 --- a/lib/commands/command-latest.bash +++ b/lib/commands/command-latest.bash @@ -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 "$@" diff --git a/lib/commands/command-list-all.bash b/lib/commands/command-list-all.bash index d16602972..e3e23d185 100644 --- a/lib/commands/command-list-all.bash +++ b/lib/commands/command-list-all.bash @@ -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 diff --git a/lib/commands/command-list.bash b/lib/commands/command-list.bash index 1d0655c02..b0cbd1610 100644 --- a/lib/commands/command-list.bash +++ b/lib/commands/command-list.bash @@ -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 diff --git a/lib/commands/version_commands.bash b/lib/commands/version_commands.bash index 7b5564e06..a80eff1a7 100644 --- a/lib/commands/version_commands.bash +++ b/lib/commands/version_commands.bash @@ -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 diff --git a/test/latest_command.bats b/test/latest_command.bats index 8dd06fccb..589f9e8dd 100644 --- a/test/latest_command.bats +++ b/test/latest_command.bats @@ -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 ] +} diff --git a/test/list_command.bats b/test/list_command.bats index 56836946b..d660f5f5e 100644 --- a/test/list_command.bats +++ b/test/list_command.bats @@ -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"* ]] diff --git a/test/version_commands.bats b/test/version_commands.bats index 94b537709..097734a09 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -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 ]