From 5c69d8d852043156881b99cc5a5bccc8bcdd222c Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Date: Sat, 20 Jul 2024 11:06:57 +0100 Subject: [PATCH 1/2] Allowing find the closest version if the exact version is not found --- lib/functions/versions.bash | 8 ++++++-- lib/utils.bash | 20 ++++++++++++++++++++ test/version_commands.bats | 12 ++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 7d1d941d..c02f05c5 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -53,8 +53,12 @@ version_command() { fi if ! (check_if_version_exists "$plugin_name" "$version"); then - version_not_installed_text "$plugin_name" "$version" 1>&2 - exit 1 + closest_version=$(try_get_closest_version "$plugin_name" "$version") + if [[ ! $closest_version ]]; then + version_not_installed_text "$plugin_name" "$version" 1>&2 + exit 1 + fi + version="$closest_version" fi resolved_versions+=("$version") diff --git a/lib/utils.bash b/lib/utils.bash index 21978a92..3d66fd57 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -137,6 +137,26 @@ check_if_version_exists() { fi } +try_get_closest_version() { + local plugin_name=$1 + local partial_version=$2 + local versions + + versions=$(list_installed_versions "$plugin_name") + + if [ -n "${versions}" ]; then + for version in $versions; do + if [[ $version == $partial_version* ]]; then + matched_versions+=("$version") + fi + done + fi + + if [ ${#matched_versions[@]} -eq 1 ]; then + printf "%s" "${matched_versions[0]}" + fi +} + version_not_installed_text() { local plugin_name=$1 local version=$2 diff --git a/test/version_commands.bats b/test/version_commands.bats index 3c9fd0f7..7eae0617 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -204,6 +204,18 @@ teardown() { [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ] } +@test "global should create a global .tool-versions file when a close version is found" { + run asdf global "dummy" "1.1" + [ "$status" -eq 0 ] + [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ] +} + +@test "global should emit an error when a single close plugin version is found" { + run asdf global "dummy" "1" + [ "$status" -eq 1 ] + [ "$output" = "version 1 is not installed for dummy" ] +} + @test "[global - dummy_plugin] with latest should use the latest installed version" { run asdf global "dummy" "latest" [ "$status" -eq 0 ] From 5c30032f4402dcc506b3547910ce3458477f3af1 Mon Sep 17 00:00:00 2001 From: Bruno Sales Cardoso Date: Mon, 22 Jul 2024 09:10:09 +0100 Subject: [PATCH 2/2] Update test/version_commands.bats --- test/version_commands.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/version_commands.bats b/test/version_commands.bats index 7eae0617..b6ddbee8 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -210,7 +210,7 @@ teardown() { [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ] } -@test "global should emit an error when a single close plugin version is found" { +@test "global should emit an error when a single close plugin version is not found" { run asdf global "dummy" "1" [ "$status" -eq 1 ] [ "$output" = "version 1 is not installed for dummy" ]