[Refactor] fix some bugs in `nvm_is_natural_num`, add unit tests.
parent
32d184099c
commit
f279837d5c
23
nvm.sh
23
nvm.sh
|
@ -1240,9 +1240,7 @@ nvm_install_node_source() {
|
||||||
elif [ "_$NVM_OS" = "_sunos" ]; then
|
elif [ "_$NVM_OS" = "_sunos" ]; then
|
||||||
NVM_CPU_THREADS="$(psrinfo | wc -l)"
|
NVM_CPU_THREADS="$(psrinfo | wc -l)"
|
||||||
fi
|
fi
|
||||||
local NVM_CPU_THREAD_VALID
|
if [ ! nvm_is_natural_num "$NVM_CPU_THREADS" ] ; then
|
||||||
NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $NVM_CPU_THREADS)
|
|
||||||
if [ -z "$NVM_CPU_THREADS" ] || [ "$NVM_CPU_THREAD_VALID" != "true" ] ; then
|
|
||||||
echo "Can not determine how many thread(s) we can use, set to only 1 now." 1>&2
|
echo "Can not determine how many thread(s) we can use, set to only 1 now." 1>&2
|
||||||
echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" 1>&2
|
echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" 1>&2
|
||||||
NVM_MAKE_JOBS="1"
|
NVM_MAKE_JOBS="1"
|
||||||
|
@ -1457,13 +1455,16 @@ nvm_sanitize_path() {
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm_is_natural_num() {
|
nvm_is_natural_num() {
|
||||||
echo $1 | command egrep -q '^[0-9]{1,}$' &> /dev/null
|
if [ -z "$1" ]; then
|
||||||
local IS_NATURAL_NUM=$?
|
return 4
|
||||||
if [ "$IS_NATURAL_NUM" = "0" ]; then
|
|
||||||
echo true
|
|
||||||
else
|
|
||||||
echo false
|
|
||||||
fi
|
fi
|
||||||
|
case "$1" in
|
||||||
|
0) return 1 ;;
|
||||||
|
-*) return 3 ;; # some BSDs return false positives for double-negated args
|
||||||
|
*)
|
||||||
|
[ $1 -eq $1 2> /dev/null ] # returns 2 if it doesn't match
|
||||||
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm() {
|
nvm() {
|
||||||
|
@ -1586,9 +1587,7 @@ nvm() {
|
||||||
;;
|
;;
|
||||||
-j)
|
-j)
|
||||||
shift # consume "-j"
|
shift # consume "-j"
|
||||||
local NVM_CPU_THREAD_VALID
|
if [ nvm_is_natural_num "$1" ]; then
|
||||||
NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1)
|
|
||||||
if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then
|
|
||||||
NVM_MAKE_JOBS=$1
|
NVM_MAKE_JOBS=$1
|
||||||
echo "number of \`make\` jobs: $NVM_MAKE_JOBS"
|
echo "number of \`make\` jobs: $NVM_MAKE_JOBS"
|
||||||
else
|
else
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
die () { echo $@ ; exit 1; }
|
||||||
|
|
||||||
|
. ../../../nvm.sh
|
||||||
|
|
||||||
|
! nvm_is_natural_num || die 'no args is not false'
|
||||||
|
! nvm_is_natural_num '' || die 'empty string is not false'
|
||||||
|
! nvm_is_natural_num a || die 'a is not false'
|
||||||
|
! nvm_is_natural_num -1 || 'negative number is not false'
|
||||||
|
! nvm_is_natural_num --1 || 'double negative number is not false'
|
||||||
|
! nvm_is_natural_num 1.2 || 'decimal number is not false'
|
||||||
|
! nvm_is_natural_num 0 || die 'zero is not false'
|
||||||
|
|
||||||
|
nvm_is_natural_num 1 || die '1 is not true'
|
||||||
|
nvm_is_natural_num 2 || die '2 is not true'
|
||||||
|
nvm_is_natural_num 1234 || die '1234 is not true'
|
Loading…
Reference in New Issue