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: Enable use of latest: in .tool-versions files #1793

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion lib/commands/command-current.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- sh -*-
# shellcheck source=lib/functions/plugins.bash
. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash"
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

# shellcheck disable=SC2059
plugin_current_command() {
Expand All @@ -21,7 +23,8 @@ plugin_current_command() {
local description=""

IFS=' ' read -r -a versions <<<"$full_version"
for version in "${versions[@]}"; do
for version_spec in "${versions[@]}"; do
version="$(resolve_version_spec "$version_spec")"
if ! (check_if_version_exists "$plugin_name" "$version"); then
version_not_installed="$version"
fi
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/command-env.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

shim_env_command() {
local shim_name="$1"
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/command-exec.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

shim_exec_command() {
local shim_name
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/command-which.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

which_command() {
local shim_name
Expand Down
26 changes: 17 additions & 9 deletions lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ version_command() {
declare -a resolved_versions
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=$(latest_command "$plugin_name" "${version_info[1]}")
elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then
version=$(latest_command "$plugin_name")
else
# if branch handles ref: || path: || normal versions
version="${versions[$item]}"
fi
version="$(resolve_version_spec "${versions[$item]}")"

# check_if_version_exists should probably handle if either param is empty string
if [ -z "$version" ]; then
Expand Down Expand Up @@ -79,6 +71,22 @@ version_command() {
fi
}

resolve_version_spec() {
local version_spec=$1

IFS=':' read -r -a version_info <<<"$version_spec"
if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then
version=$(latest_command "$plugin_name" "${version_info[1]}")
elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then
version=$(latest_command "$plugin_name")
else
# if branch handles ref: || path: || normal versions
version="$version_spec"
fi

printf "%s\n" "$version"
}

list_all_command() {
local plugin_name=$1
local query=$2
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ select_version() {
version_and_path=$(find_versions "$plugin_name" "$search_path")
IFS='|' read -r version_string _path <<<"$version_and_path"
IFS=' ' read -r -a usable_plugin_versions <<<"$version_string"
for plugin_version in "${usable_plugin_versions[@]}"; do
for version_spec in "${usable_plugin_versions[@]}"; do
plugin_version="$(resolve_version_spec "$version_spec")"
Comment on lines -739 to +740
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key change to enable the desired feature of this PR - now resolving each version spec in the .tool-versions file to a precise version number, rather than it having to already be a precise version number.

for plugin_and_version in "${shim_versions[@]}"; do
local plugin_shim_name
local plugin_shim_version
Expand Down
10 changes: 10 additions & 0 deletions test/current_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ teardown() {
[ "$output" = "$expected" ]
}

@test "current should not error on version specifications like 'latest:'" {
cd "$PROJECT_DIR"
echo "dummy 1.2.0 latest:1.1" >>"$PROJECT_DIR/.tool-versions"
expected="dummy 1.2.0 latest:1.1 $PROJECT_DIR/.tool-versions"

run asdf current "dummy"
[ "$status" -eq 0 ]
[ "$output" = "$expected" ]
}

@test "current should derive from the legacy file if enabled" {
cd "$PROJECT_DIR"
echo 'legacy_version_file = yes' >"$HOME/.asdfrc"
Expand Down
8 changes: 8 additions & 0 deletions test/install_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ teardown() {
[ "$(cat "$ASDF_DIR/installs/dummy/1.2.0/version")" = "1.2.0" ]
}

@test "install_command installs the 'latest:' version in .tool-versions" {
cd "$PROJECT_DIR"
echo -n 'dummy latest:1.1' >".tool-versions"
run asdf install dummy
[ "$status" -eq 0 ]
[ "$(cat "$ASDF_DIR/installs/dummy/1.1.0/version")" = "1.1.0" ]
}

@test "install_command set ASDF_CONCURRENCY" {
run asdf install dummy 1.0.0
[ "$status" -eq 0 ]
Expand Down
13 changes: 13 additions & 0 deletions test/shim_exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ teardown() {
echo "$output" | grep -q "This is Dummy 3.0! hello world" 2>/dev/null
}

@test "shim exec should respect 'latest:' in .tool-versions" {
run asdf install dummy 1.0.0
run asdf install dummy 1.1.0
run asdf install dummy 2.0.0

echo "dummy latest:1.1" >"$PROJECT_DIR/.tool-versions"

run "$ASDF_DIR/shims/dummy" world hello
[ "$status" -eq 0 ]

echo "$output" | grep -q "This is Dummy 1.1.0! hello world" 2>/dev/null
}

@test "shim exec should only use the first version found for a plugin" {
run asdf install dummy 3.0

Expand Down
2 changes: 2 additions & 0 deletions test/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ bats_require_minimum_version 1.7.0

# shellcheck source=lib/utils.bash
. "$(dirname "$BATS_TEST_DIRNAME")"/lib/utils.bash
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$BATS_TEST_DIRNAME")"/lib/functions/versions.bash
Comment on lines +7 to +8
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required because utils.bats has a test that invokes with_shim_executable() (which itself now calls resolve_version_spec() in versions.bash).

Without this change, the test fails:

 ✗ with_shim_executable doesn't crash when executable names contain dashes
   (in test file test/utils.bats, line 460)
     `[ "$status" -eq 0 ]' failed


setup_asdf_dir() {
if [ "$BATS_TEST_NAME" = 'test_shim_exec_should_use_path_executable_when_specified_version_path-3a-3cpath-3e' ]; then
Expand Down