Skip to content

Commit

Permalink
fix: support latest with filter on local and global (#633)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas B Homburg <thomas@homburg.dk>
Co-authored-by: James Hegedus <jthegedus@hey.com>
  • Loading branch information
3 people committed Jul 6, 2021
1 parent 04948a3 commit 5cf8f89
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 11 deletions.
11 changes: 11 additions & 0 deletions docs/core-manage-versions.md
Expand Up @@ -28,6 +28,13 @@ asdf list <name>
# asdf list erlang
```

Limit versions to those that begin with a given string.

```shell
asdf list <name> <version>
# asdf list erlang 17
```

## List All Available Versions

```shell
Expand Down Expand Up @@ -63,6 +70,10 @@ asdf global <name> <version> [<version>...]
asdf shell <name> <version> [<version>...]
asdf local <name> <version> [<version>...]
# asdf global elixir 1.2.4

asdf global <name> latest[:<version>]
asdf local <name> latest[:<version>]
# asdf global elixir latest
```

`global` writes the version to `$HOME/.tool-versions`.
Expand Down
11 changes: 9 additions & 2 deletions lib/commands/command-latest.bash
Expand Up @@ -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 "$@"
5 changes: 5 additions & 0 deletions lib/commands/command-list-all.bash
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lib/commands/command-list.bash
Expand Up @@ -2,6 +2,7 @@

list_command() {
local plugin_name=$1
local query=$2

if [ -z "$plugin_name" ]; then
local plugins_path
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions lib/commands/version_commands.bash
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions test/latest_command.bats
Expand Up @@ -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 ]
}
24 changes: 23 additions & 1 deletion test/list_command.bats
Expand Up @@ -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" ]
Expand All @@ -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
Expand All @@ -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"* ]]
Expand Down
37 changes: 37 additions & 0 deletions test/version_commands.bats
Expand Up @@ -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 ]
Expand Down Expand Up @@ -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" ]
Expand Down Expand Up @@ -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 ]
Expand Down

0 comments on commit 5cf8f89

Please sign in to comment.