[Refactor] fix some bugs in `nvm_is_natural_num`, add unit tests.

Jordan Harband 2015-12-27 13:15:53 -08:00
parent 32d184099c
commit f279837d5c
2 changed files with 28 additions and 12 deletions

23
nvm.sh
View File

@ -1240,9 +1240,7 @@ nvm_install_node_source() {
elif [ "_$NVM_OS" = "_sunos" ]; then
NVM_CPU_THREADS="$(psrinfo | wc -l)"
fi
local NVM_CPU_THREAD_VALID
NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $NVM_CPU_THREADS)
if [ -z "$NVM_CPU_THREADS" ] || [ "$NVM_CPU_THREAD_VALID" != "true" ] ; then
if [ ! nvm_is_natural_num "$NVM_CPU_THREADS" ] ; then
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
NVM_MAKE_JOBS="1"
@ -1457,13 +1455,16 @@ nvm_sanitize_path() {
}
nvm_is_natural_num() {
echo $1 | command egrep -q '^[0-9]{1,}$' &> /dev/null
local IS_NATURAL_NUM=$?
if [ "$IS_NATURAL_NUM" = "0" ]; then
echo true
else
echo false
if [ -z "$1" ]; then
return 4
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() {
@ -1586,9 +1587,7 @@ nvm() {
;;
-j)
shift # consume "-j"
local NVM_CPU_THREAD_VALID
NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1)
if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then
if [ nvm_is_natural_num "$1" ]; then
NVM_MAKE_JOBS=$1
echo "number of \`make\` jobs: $NVM_MAKE_JOBS"
else

View File

@ -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'