Skip to content

Commit 7b253c8

Browse files
PeterDaveHelloljharb
authored andcommitted
[New] Supercharge nvm debug output
Try to get shell version, OS and its version, curl/wget/git version.
1 parent f344d06 commit 7b253c8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

nvm.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ nvm_is_alias() {
4949
\alias "${1-}" > /dev/null 2>&1
5050
}
5151

52+
nvm_command_info() {
53+
local COMMAND
54+
local INFO
55+
COMMAND="${1}"
56+
if type "${COMMAND}" | command grep -q hashed; then
57+
INFO="$(type "${COMMAND}" | command sed -E 's/\(|)//g' | command awk '{print $4}')"
58+
elif type "${COMMAND}" | command grep -q aliased; then
59+
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g" ))"
60+
elif type "${COMMAND}" | command grep -q "^${COMMAND} is an alias for"; then
61+
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
62+
elif type "${COMMAND}" | command grep -q "^${COMMAND} is \/"; then
63+
INFO="$(type "${COMMAND}" | command awk '{print $3}')"
64+
else
65+
INFO="$(type "${COMMAND}")"
66+
fi
67+
nvm_echo "${INFO}"
68+
}
69+
5270
nvm_has_colors() {
5371
local NVM_COLORS
5472
if nvm_has tput; then
@@ -2249,6 +2267,28 @@ nvm() {
22492267
nvm_err "\$NPM_CONFIG_PREFIX: '$(nvm_sanitize_path "$NPM_CONFIG_PREFIX")'"
22502268
nvm_err "\$NVM_NODEJS_ORG_MIRROR: '${NVM_NODEJS_ORG_MIRROR}'"
22512269
nvm_err "\$NVM_IOJS_ORG_MIRROR: '${NVM_IOJS_ORG_MIRROR}'"
2270+
nvm_err "shell version: '$(${SHELL} --version | command head -n 1)'"
2271+
nvm_err "uname -a: '$(uname -a | awk '{$2=""; print}' | xargs)'"
2272+
if [ "$(nvm_get_os)" = "darwin" ] && nvm_has sw_vers; then
2273+
nvm_err "OS version: $(sw_vers | command awk '{print $2}' | command xargs)"
2274+
elif [ -r "/etc/issue" ]; then
2275+
nvm_err "OS version: $(command head -n 1 /etc/issue | command sed 's/\\.//g')"
2276+
fi
2277+
if nvm_has "curl"; then
2278+
nvm_err "curl: $(nvm_command_info curl), $(command curl -V | command head -n 1)"
2279+
else
2280+
nvm_err "curl: not found"
2281+
fi
2282+
if nvm_has "wget"; then
2283+
nvm_err "wget: $(nvm_command_info wget), $(command wget -V | command head -n 1)"
2284+
else
2285+
nvm_err "wget: not found"
2286+
fi
2287+
if nvm_has "git"; then
2288+
nvm_err "git: $(nvm_command_info git), $(command git --version)"
2289+
else
2290+
nvm_err "git: not found"
2291+
fi
22522292
local NVM_DEBUG_OUTPUT
22532293
for NVM_DEBUG_COMMAND in 'nvm current' 'which node' 'which iojs' 'which npm' 'npm config get prefix' 'npm root -g'
22542294
do
@@ -3204,7 +3244,7 @@ nvm() {
32043244
nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \
32053245
nvm_sanitize_path nvm_has_colors nvm_process_parameters \
32063246
node_version_has_solaris_binary iojs_version_has_solaris_binary \
3207-
nvm_curl_libz_support \
3247+
nvm_curl_libz_support nvm_command_info \
32083248
> /dev/null 2>&1
32093249
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
32103250
NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \

test/fast/Unit tests/nvm_command_info

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
cleanup() {
4+
unalias wget
5+
unset -f wget
6+
unset WGET_EXPECTED_INFO WGET_COMMAND_INFO
7+
}
8+
9+
die() { echo "$@" ; cleanup ; exit 1; }
10+
11+
\. ../../../nvm.sh
12+
13+
14+
# 1. test wget command
15+
WGET_COMMAND_INFO="$(nvm_command_info wget)"
16+
WGET_EXPECTED_INFO="$(which wget)"
17+
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 1), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"
18+
19+
cleanup
20+
21+
# 2. test aliased wget
22+
shopt -s expand_aliases
23+
# enable expand_aliases to make alias working in interactive shell
24+
alias wget="wget -V"
25+
WGET_COMMAND_INFO="$(nvm_command_info wget)"
26+
WGET_EXPECTED_INFO="$(which wget) (wget -V)"
27+
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 2), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"
28+
29+
cleanup
30+
31+
# 3. test wget function
32+
wget() {
33+
echo "wget function"
34+
}
35+
36+
WGET_COMMAND_INFO="$(nvm_command_info wget)"
37+
WGET_EXPECTED_INFO="$(type wget)"
38+
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 3), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"
39+
40+
cleanup

0 commit comments

Comments
 (0)