Compare commits

..

15 Commits

Author SHA1 Message Date
ryenus
d655614c69 show a warning about skipped versions if any
due the use of --min or NVM_MIN, which would show only the versions
higher than the specified min version, and/or the available minor/patch
updates for the installed versions.
2024-11-06 17:48:34 +08:00
ryenus
cd30a37384 set NVM_NO_COLORS to --no-colors to disable color 2024-11-06 17:48:34 +08:00
ryenus
c13e155709 rename min_ver to min for consistency 2024-11-06 17:48:34 +08:00
ryenus
90de98bbfc [test] avoid leaking temp variable 2024-11-06 17:48:34 +08:00
ryenus
4613313498 ls-remote: introduce temp var for env inheritance
This is to inherit $NVM_MIN from env when defined, meanwhile avoiding
inline local variable initialization for ksh compatibility.

Co-authored-by: Jordan Harband <ljharb@gmail.com>
2024-11-06 17:48:34 +08:00
ryenus
e493440b3c avoid inline initialization for ksh compatibility
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2024-11-06 17:48:34 +08:00
ryenus
b9304f18dc ls-remote: add CLI option --min=<version>
When omitted, fallback to the environment variable "NVM_MIN" if set.
And the CLI option --min=<version> takes precedence over the environment
variable "NVM_MIN" if both are present.
2024-11-06 17:48:34 +08:00
ryenus
e215a57abd always show updates for installed versions
even if they're older than $NVM_MIN
2024-11-06 17:48:34 +08:00
ryenus
e7df2f2110 remove function ref duplication 2024-11-06 17:48:23 +08:00
ryenus
5fc1ebc91e prefixed versions like v18 also work 2024-11-06 17:48:23 +08:00
ryenus
2a893e5b5d rename NVM_MIN_VER to NVM_MIN
since nvm is all about versions, so no need for the explicit suffix.
2024-11-06 17:48:23 +08:00
ryenus
8ec7a3b5fc trim leading space in mock output due to eclint errors 2024-11-06 17:48:23 +08:00
ryenus
02bb9338e1 add test for NVM_MIN_VER support
show only versions newer than NVM_MIN_VER if set
2024-11-06 17:48:23 +08:00
ryenus
307ad6d118 always list installed versions 2024-11-06 17:48:22 +08:00
ryenus
6ebe56fb11 show only versions newer than NVM_MIN_VER if set 2024-11-06 17:48:22 +08:00
5 changed files with 92 additions and 124 deletions

View File

@ -1,6 +1,6 @@
name: urchin tests
on: [push, pull_request]
on: [push]
permissions:
contents: read

View File

@ -113,11 +113,3 @@ Co-authored-by: Name Here <email@here>
# Where can I ask for help?
If you have any questions, please contact [@LJHarb](mailto:ljharb@gmail.com).
# Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
- The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or
- The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or
- The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
- I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

View File

@ -296,17 +296,17 @@ nvm_detect_profile() {
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "${ZDOTDIR:-${HOME}}/.zshrc" ]; then
DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zshrc"
elif [ -f "${ZDOTDIR:-${HOME}}/.zprofile" ]; then
DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zprofile"
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.zprofile" ]; then
DETECTED_PROFILE="$HOME/.zprofile"
fi
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zprofile" ".zshrc"
do
if DETECTED_PROFILE="$(nvm_try_profile "${ZDOTDIR:-${HOME}}/${EACH_PROFILE}")"; then
if DETECTED_PROFILE="$(nvm_try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done

127
nvm.sh
View File

@ -1862,79 +1862,80 @@ nvm_print_versions() {
-v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \
-v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \
-v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '
function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }
function v2a(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); }
function v2m(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); return a[1]; }
function vcmp(v1,v2,a1,a2,i,d) { v2a(v1,a1); v2a(v2,a2); for(i=1;i<4;i++) { d = a1[i] - a2[i]; if(d!=0) return d; } return 0; }
BEGIN {
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *";
function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }
function v2a(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); }
function v2m(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); return a[1]; }
function vcmp(v1,v2,a1,a2,i,d) { v2a(v1,a1); v2a(v2,a2); for(i=1;i<4;i++) { d = a1[i] - a2[i]; if(d!=0) return d; } return 0; }
BEGIN {
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *";
latest_lts_color = current_color;
sub(/0;/, "1;", latest_lts_color);
latest_lts_color = current_color;
sub(/0;/, "1;", latest_lts_color);
fmt_latest_lts = has_colors && latest_lts_color ? ("\033[" latest_lts_color " (Latest LTS: %s)\033[0m") : " (Latest LTS: %s)";
fmt_old_lts = has_colors && old_lts_color ? ("\033[" old_lts_color " (LTS: %s)\033[0m") : " (LTS: %s)";
fmt_latest_lts = has_colors && latest_lts_color ? ("\033[" latest_lts_color " (Latest LTS: %s)\033[0m") : " (Latest LTS: %s)";
fmt_old_lts = has_colors && old_lts_color ? ("\033[" old_lts_color " (LTS: %s)\033[0m") : " (LTS: %s)";
split(remote_versions, lines, "|");
split(installed_versions, installed, "|");
rows = alen(lines);
filter_on = (vcmp("v0.0.0", min) != 0);
current_major = -1;
for (m = n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+");
cols = alen(fields);
version = fields[1];
is_installed = 0;
for (i in installed) {
if (version == installed[i]) {
is_installed = 1;
break;
}
split(remote_versions, lines, "|");
split(installed_versions, installed, "|");
rows = alen(lines);
filter_on = (vcmp("v0.0.0", min) != 0);
current_major = -1;
for (m = n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+");
cols = alen(fields);
version = fields[1];
is_installed = 0;
for (i in installed) {
if (version == installed[i]) {
is_installed = 1;
break;
}
if (filter_on != 0) {
if (is_installed) {
current_major = v2m(version);
} else if (vcmp(version, min) >= 0) {
filter_on = 0;
} else if (v2m(version) != current_major) {
continue;
}
}
fmt_version = "%15s";
if (version == current) {
fmt_version = fmt_current;
} else if (version == "system") {
fmt_version = fmt_system;
} else if (is_installed) {
fmt_version = fmt_installed;
}
padding = (is_installed && !has_colors) ? "" : " ";
if (cols == 1) {
formatted = sprintf(fmt_version, version);
} else if (cols == 2) {
formatted = sprintf((fmt_version padding fmt_old_lts), version, fields[2]);
} else if (cols == 3 && fields[3] == "*") {
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);
}
output[m++] = formatted;
}
for (n = 1; n < m; n++) {
print output[n]
if (filter_on != 0) {
if (is_installed) {
current_major = v2m(version);
} else if (vcmp(version, min) >= 0) {
filter_on = 0;
} else if (v2m(version) != current_major) {
continue;
}
}
if (rows > --m) {
printf("[INFO] showing %d (of %d) versions.\n", m, rows) > "/dev/stderr"
fmt_version = "%15s";
if (version == current) {
fmt_version = fmt_current;
} else if (version == "system") {
fmt_version = fmt_system;
} else if (is_installed) {
fmt_version = fmt_installed;
}
exit
}'
padding = (is_installed && !has_colors) ? "" : " ";
if (cols == 1) {
formatted = sprintf(fmt_version, version);
} else if (cols == 2) {
formatted = sprintf((fmt_version padding fmt_old_lts), version, fields[2]);
} else if (cols == 3 && fields[3] == "*") {
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);
}
output[m++] = formatted;
}
skipped = rows - m + 1
if (skipped > 0) {
printf("[WARN] %d version(s) skipped.\n", skipped) > "/dev/stderr"
}
for (n = 1; n < m; n++) {
print output[n]
}
exit
}'
}
nvm_validate_implicit_alias() {

View File

@ -3,16 +3,12 @@
setup () {
HOME="."
NVM_ENV=testing \. ../../install.sh
ZDOTDIR="$HOME/zdotdir"
mkdir -p zdotdir
touch ".bashrc"
touch ".bash_profile"
touch ".zprofile"
touch ".zshrc"
touch ".profile"
touch "test_profile"
touch "zdotdir/.zshrc"
touch "zdotdir/.zprofile"
}
cleanup () {
@ -21,9 +17,7 @@ cleanup () {
unset NVM_DETECT_PROFILE
unset SHELL
unset -f setup cleanup die
unset ZDOTDIR
rm -f ".bashrc" ".bash_profile" ".zprofile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
rm -rf zdotdir 2>&1
}
die () { echo "$@" '$NVM_DETECT_PROFILE:' "$NVM_DETECT_PROFILE"; cleanup; exit 1; }
@ -35,54 +29,49 @@ setup
#
# setting $PROFILE to /dev/null should return no detected profile
NVM_DETECT_PROFILE="$(PROFILE='/dev/null' nvm_detect_profile)"
NVM_DETECT_PROFILE="$(PROFILE='/dev/null'; nvm_detect_profile)"
if [ -n "$NVM_DETECT_PROFILE" ]; then
die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null"
fi
# .bashrc should be detected for bash
NVM_DETECT_PROFILE="$(SHELL="/bin/bash" PROFILE= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
fi
# $PROFILE should override .bashrc profile detection
NVM_DETECT_PROFILE="$(SHELL="/bin/bash" PROFILE="test_profile" nvm_detect_profile)"
NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
die "nvm_detect_profile ignored \$PROFILE"
fi
# zdotdir/.zshrc should be detected for zsh
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh" PROFILE= nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zshrc" ]; then
die "nvm_detect_profile didn't pick \$ZDOTDIR/.zshrc for zsh"
fi
# .zshrc should be detected for zsh
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh" PROFILE= ZDOTDIR= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
fi
# $PROFILE should override .zshrc profile detection
NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh" PROFILE="test_profile" nvm_detect_profile)"
NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; PROFILE="test_profile"; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
die "nvm_detect_profile ignored \$PROFILE"
fi
#
# Confirm $PROFILE is only returned when it points to a valid file
#
# $PROFILE is a valid file
NVM_DETECT_PROFILE="$(PROFILE="test_profile" SHELL= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
fi
# $PROFILE is not a valid file
rm "test_profile"
NVM_DETECT_PROFILE="$(PROFILE="test_profile" nvm_detect_profile)"
NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then
die "nvm_detect_profile picked \$PROFILE when it was an invalid file"
fi
@ -94,58 +83,44 @@ fi
#
# It should favor .profile if file exists
NVM_DETECT_PROFILE="$(SHELL= ZDOTDIR= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
die "nvm_detect_profile should have selected .profile; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have selected .profile"
fi
# Otherwise, it should favor .bashrc if file exists
rm ".profile"
NVM_DETECT_PROFILE="$(SHELL= ZDOTDIR= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile should have selected .bashrc; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have selected .bashrc"
fi
# Otherwise, it should favor .bash_profile if file exists
rm ".bashrc"
NVM_DETECT_PROFILE="$(SHELL= ZDOTDIR= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
die "nvm_detect_profile should have selected .bash_profile; got $NVM_DETECT_PROFILE"
fi
# Otherwise, it should favor zdotdir/.zprofile if file exists
rm ".bash_profile"
NVM_DETECT_PROFILE="$(SHELL= nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zprofile" ]; then
die "nvm_detect_profile should have selected zdotdir/.zprofile; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have selected .bash_profile"
fi
# Otherwise, it should favor .zprofile if file exists
rm "zdotdir/.zprofile"
NVM_DETECT_PROFILE="$(SHELL= ZDOTDIR= nvm_detect_profile)"
rm ".bash_profile"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zprofile" ]; then
die "nvm_detect_profile should have selected .zprofile; got $NVM_DETECT_PROFILE"
fi
# Otherwise, it should favor zdotdir/.zshrc if file exists
rm ".zprofile"
NVM_DETECT_PROFILE="$(SHELL= nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zshrc" ]; then
die "nvm_detect_profile should have selected zdotdir/.zshrc; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have selected .zprofile"
fi
# Otherwise, it should favor .zshrc if file exists
rm "zdotdir/.zshrc"
NVM_DETECT_PROFILE="$(SHELL= ZDOTDIR= nvm_detect_profile)"
rm ".zprofile"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile should have selected .zshrc; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have selected .zshrc"
fi
# It should be empty if none is found
rm ".zshrc"
NVM_DETECT_PROFILE="$(SHELL= nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ ! -z "$NVM_DETECT_PROFILE" ]; then
die "nvm_detect_profile should have returned an empty value; got $NVM_DETECT_PROFILE"
die "nvm_detect_profile should have returned an empty value"
fi
cleanup