Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jordan Harband · committed by Jordan Harband · parent 32d1840 · Browse files · 2 files+28 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−12 removed

  • tests:+17 added−0 removed
  • other:+11 added−12 removed

2 files+28 added−12 removed

added:test/fast/Unit tests/nvm_is_natural_num+17 added
1#!/bin/sh23die () { echo $@ ; exit 1; }45. ../../../nvm.sh67! nvm_is_natural_num || die 'no args is not false'8! nvm_is_natural_num '' || die 'empty string is not false'9! nvm_is_natural_num a || die 'a is not false'10! nvm_is_natural_num -1 || 'negative number is not false'11! nvm_is_natural_num --1 || 'double negative number is not false'12! nvm_is_natural_num 1.2 || 'decimal number is not false'13! nvm_is_natural_num 0 || die 'zero is not false'1415nvm_is_natural_num 1 || die '1 is not true'16nvm_is_natural_num 2 || die '2 is not true'17nvm_is_natural_num 1234 || die '1234 is not true'
modified:nvm.sh+11 added-12 removed
1238 unmodified lines
1239 NVM_CPU_THREADS="$(sysctl -n hw.ncpu)"1240 elif [ "_$NVM_OS" = "_sunos" ]; then1241 NVM_CPU_THREADS="$(psrinfo | wc -l)"1242 fi1243 local NVM_CPU_THREAD_VALID1244 NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $NVM_CPU_THREADS)1245 if [ -z "$NVM_CPU_THREADS" ] || [ "$NVM_CPU_THREAD_VALID" != "true" ] ; then1243 if [ ! nvm_is_natural_num "$NVM_CPU_THREADS" ] ; then1244 echo "Can not determine how many thread(s) we can use, set to only 1 now." 1>&21245 echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" 1>&21246 NVM_MAKE_JOBS="1"1247 else
206 unmodified lines
1454 echo "$SANITIZED_PATH" | command sed "s#$HOME#\$HOME#g"1455}14561457nvm_is_natural_num() {1460 echo $1 | command egrep -q '^[0-9]{1,}$' &> /dev/null1461 local IS_NATURAL_NUM=$?1462 if [ "$IS_NATURAL_NUM" = "0" ]; then1463 echo true1464 else1465 echo false1458 if [ -z "$1" ]; then1459 return 41460 fi1461 case "$1" in1462 0) return 1 ;;1463 -*) return 3 ;; # some BSDs return false positives for double-negated args1464 *)1465 [ $1 -eq $1 2> /dev/null ] # returns 2 if it doesn't match1466 ;;1467 esac1468}14691470nvm() {1471 if [ $# -lt 1 ]; then
114 unmodified lines
1586 nobinary=11587 ;;1588 -j)1589 shift # consume "-j"1589 local NVM_CPU_THREAD_VALID1590 NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1)1591 if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then1590 if [ nvm_is_natural_num "$1" ]; then1591 NVM_MAKE_JOBS=$11592 echo "number of \`make\` jobs: $NVM_MAKE_JOBS"1593 else1594 unset NVM_MAKE_JOBS
744 unmodified lines