Skip to content

Commit

Permalink
feat: asdf latest defer to plugin to determine the latest version (#938)
Browse files Browse the repository at this point in the history
Co-authored-by: James Hegedus <jthegedus@hey.com>
  • Loading branch information
threkk and jthegedus committed Jul 7, 2021
1 parent 5cf8f89 commit 664d82e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 21 deletions.
29 changes: 21 additions & 8 deletions lib/commands/command-latest.bash
Expand Up @@ -5,18 +5,31 @@ latest_command() {

local plugin_name=$1
local query=$2
local plugin_path

[[ -z $query ]] && query="$DEFAULT_QUERY"

plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

local versions
# pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest)
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)

if [ -z "${versions}" ]; then
exit 1

if [ -f "${plugin_path}/bin/latest-stable" ]; then
versions=$(bash "${plugin_path}"/bin/latest-stable "$query")
if [ -z "${versions}" ]; then
# this branch requires this print to mimic the error from the list-all branch
printf "No compatible versions available (%s %s)\n" "$plugin_name" "$query" >&2
exit 1
fi
else
# pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest)
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)
if [ -z "${versions}" ]; then
exit 1
fi
fi

printf "%s\n" "$versions"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/dummy_legacy_plugin/bin/list-all
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

versions_list=(1.0 1.1 2.0)
versions_list=(1.0.0 1.1.0 2.0.0)
echo "${versions_list[@]}"
10 changes: 10 additions & 0 deletions test/fixtures/dummy_plugin/bin/latest-stable
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

get_latest_stable() {
query=$1

version_list=(1.0.0 1.1.0 2.0.0)
printf "%s\n" "${version_list[@]}" | grep -E "^\\s*$query" | tail -1
}

get_latest_stable "$1"
37 changes: 34 additions & 3 deletions test/latest_command.bats
Expand Up @@ -5,26 +5,57 @@ load test_helpers
setup() {
setup_asdf_dir
install_dummy_plugin
install_dummy_legacy_plugin
}

teardown() {
clean_asdf_dir
}

@test "latest_command shows latest stable version" {
####################################################
#### plugin with bin/latest-stable ####
####################################################
@test "[latest_command - dummy_plugin] shows latest stable version" {
run asdf latest dummy
[ "$(echo "2.0.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "latest_command with version shows latest stable version that matches the given string" {
@test "[latest_command - dummy_plugin] shows latest stable version that matches the given string" {
run asdf latest dummy 1
[ "$(echo "1.1.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "latest_command with an invalid version should return an error" {
@test "[latest_command - dummy_plugin] an invalid version should return an error" {
run asdf latest dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}

####################################################
#### plugin without bin/latest-stable ####
####################################################
@test "[latest_command - dummy_legacy_plugin] shows latest stable version" {
run asdf latest legacy-dummy
echo "status: $status"
echo "output: $output"
[ "$(echo "2.0.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_legacy_plugin] shows latest stable version that matches the given string" {
run asdf latest legacy-dummy 1
echo "status: $status"
echo "output: $output"
[ "$(echo "1.1.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_legacy_plugin] an invalid version should return an error" {
run asdf latest legacy-dummy 3
echo "status: $status"
echo "output: $output"
[ "$(echo "No compatible versions available (legacy-dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}
4 changes: 4 additions & 0 deletions test/test_helpers.bash
Expand Up @@ -73,6 +73,10 @@ install_dummy_version() {
install_mock_plugin_version "dummy" "$1"
}

install_dummy_legacy_version() {
install_mock_plugin_version "legacy-dummy" "$1"
}

install_dummy_exec_path_script() {
local name=$1
local exec_path="$ASDF_DIR/plugins/dummy/bin/exec-path"
Expand Down
80 changes: 71 additions & 9 deletions test/version_commands.bats
Expand Up @@ -9,6 +9,11 @@ setup() {
install_dummy_version "1.1.0"
install_dummy_version "2.0.0"

install_dummy_legacy_plugin
install_dummy_legacy_version "1.0.0"
install_dummy_legacy_version "1.1.0"
install_dummy_legacy_version "2.0.0"

PROJECT_DIR=$HOME/project
mkdir -p $PROJECT_DIR

Expand Down Expand Up @@ -47,24 +52,42 @@ teardown() {
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
}

@test "local with latest should use the latest installed version" {
@test "[local - dummy_plugin] 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" {
@test "[local - dummy_plugin] 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" {
@test "[local - dummy_plugin] 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 - dummy_legacy_plugin] with latest should use the latest installed version" {
run asdf local "legacy-dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "legacy-dummy 2.0.0" ]
}

@test "[local - dummy_legacy_plugin] with latest:version should use the latest valid installed version" {
run asdf local "legacy-dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "legacy-dummy 1.0.0" ]
}

@test "[local - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
run asdf local "legacy-dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (legacy-dummy 99)")" ]
}

@test "local should allow multiple versions" {
run asdf local "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -160,24 +183,42 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}

@test "global with latest should use the latest installed version" {
@test "[global - dummy_plugin] 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" {
@test "[global - dummy_plugin] 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" {
@test "[global - dummy_plugin] 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 - dummy_legacy_plugin] with latest should use the latest installed version" {
run asdf global "legacy-dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "legacy-dummy 2.0.0" ]
}

@test "[global - dummy_legacy_plugin] with latest:version should use the latest valid installed version" {
run asdf global "legacy-dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "legacy-dummy 1.0.0" ]
}

@test "[global - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
run asdf global "legacy-dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (legacy-dummy 99)")" ]
}

@test "global should accept multiple versions" {
run asdf global "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -342,23 +383,44 @@ false"
[ "$output" = "set -e ASDF_DUMMY_VERSION" ]
}

@test "shell wrapper function should support latest" {
@test "[shell - dummy_plugin] wrapper function should support latest" {
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "dummy" "latest"
[ $(echo $ASDF_DUMMY_VERSION) = "2.0.0" ]
unset ASDF_DUMMY_VERSION
}

@test "global should support latest" {
@test "[shell - dummy_legacy_plugin] wrapper function should support latest" {
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "legacy-dummy" "latest"
[ $(echo $ASDF_LEGACY_DUMMY_VERSION) = "2.0.0" ]
unset ASDF_LEGACY_DUMMY_VERSION
}

@test "[global - dummy_plugin] should support latest" {
echo 'dummy 1.0.0' >> $HOME/.tool-versions
run asdf global "dummy" "1.0.0" "latest"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 1.0.0 2.0.0" ]
}

@test "local should support latest" {
@test "[global - dummy_legcay_plugin] should support latest" {
echo 'legacy-dummy 1.0.0' >> $HOME/.tool-versions
run asdf global "legacy-dummy" "1.0.0" "latest"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "legacy-dummy 1.0.0 2.0.0" ]
}

@test "[local - dummy_plugin] should support latest" {
echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions
run asdf local "dummy" "1.0.0" "latest"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.0.0 2.0.0" ]
}

@test "[local - dummy_legacy_plugin] should support latest" {
echo 'legacy-dummy 1.0.0' >> $PROJECT_DIR/.tool-versions
run asdf local "legacy-dummy" "1.0.0" "latest"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "legacy-dummy 1.0.0 2.0.0" ]
}

0 comments on commit 664d82e

Please sign in to comment.