💅 Clean up version sorting and add test

This commit is contained in:
Mislav Marohnić 2021-05-06 13:25:06 +02:00
parent d3d4606d2f
commit 28cd6f123e
3 changed files with 39 additions and 50 deletions

View File

@ -63,62 +63,47 @@ if [ -d "$versions_dir" ]; then
versions_dir="$(realpath "$versions_dir")" versions_dir="$(realpath "$versions_dir")"
fi fi
if [ -n "$bare" ]; then list_versions() {
hit_prefix="" shopt -s nullglob
miss_prefix="" for path in "$versions_dir"/*; do
current_version="" if [ -d "$path" ]; then
include_system="" if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
else target="$(realpath "$path")"
hit_prefix="* " [ "${target%/*}" != "$versions_dir" ] || continue
miss_prefix=" " fi
current_version="$(rbenv-version-name || true)" echo "${path##*/}"
include_system="1" fi
fi done
shopt -u nullglob
num_versions=0
print_version() {
if [ "$1" == "$current_version" ]; then
echo "${hit_prefix}$(rbenv-version 2>/dev/null)"
else
echo "${miss_prefix}$1"
fi
num_versions=$((num_versions + 1))
} }
if [ -n "$bare" ]; then
list_versions
exit 0
fi
sort_versions() { sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \ sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
} }
# Include "system" in the non-bare output, if it exists versions="$(
if [ -n "$include_system" ] && RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then if RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then
print_version system echo system
fi
shopt -s nullglob
versions=($(
for path in "$versions_dir"/*; do
if [ -d "$path" ]; then
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
target="$(realpath "$path")"
[ "${target%/*}" != "$versions_dir" ] || continue
fi
echo "${path##*/}"
fi fi
done list_versions | sort_versions
)) )"
sorted_versions=($(printf "%s\n" ${versions[@]} | sort_versions)) if [ -z "$versions" ]; then
for version in ${sorted_versions[@]}; do
print_version $version
done
shopt -u nullglob
if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then
echo "Warning: no Ruby detected on the system" >&2 echo "Warning: no Ruby detected on the system" >&2
exit 1 exit 1
fi fi
current_version="$(rbenv-version-name || true)"
while read -r version; do
if [ "$version" == "$current_version" ]; then
echo "* $(rbenv-version 2>/dev/null)"
else
echo " $version"
fi
done <<<"$versions"

View File

@ -119,7 +119,7 @@ path_without() {
if [ "$found" != "${RBENV_ROOT}/shims" ]; then if [ "$found" != "${RBENV_ROOT}/shims" ]; then
alt="${RBENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')" alt="${RBENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')"
mkdir -p "$alt" mkdir -p "$alt"
for util in bash head cut readlink greadlink; do for util in bash head cut readlink greadlink sed sort awk; do
if [ -x "${found}/$util" ]; then if [ -x "${found}/$util" ]; then
ln -s "${found}/$util" "${alt}/$util" ln -s "${found}/$util" "${alt}/$util"
fi fi

View File

@ -56,15 +56,19 @@ OUT
@test "multiple versions" { @test "multiple versions" {
stub_system_ruby stub_system_ruby
create_version "1.8.7" create_version "1.8.7"
create_version "1.9.3" create_version "1.9.3-p13"
create_version "1.9.3-p2"
create_version "2.2.10" create_version "2.2.10"
create_version "2.2.3" create_version "2.2.3"
create_version "2.2.3-pre.2"
run rbenv-versions run rbenv-versions
assert_success assert_success
assert_output <<OUT assert_output <<OUT
* system * system
1.8.7 1.8.7
1.9.3 1.9.3-p2
1.9.3-p13
2.2.3-pre.2
2.2.3 2.2.3
2.2.10 2.2.10
OUT OUT