Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allowing find the closest version if the exact version is not found #1767

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/version_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
bsalesc marked this conversation as resolved.
Show resolved Hide resolved
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 ]
Expand Down
Loading