From 5c7f08843d60333c50b8f68b488916834096af35 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 21 Aug 2014 17:15:53 -0700 Subject: [PATCH] Add error checking to nvm_tree_contains_path --- nvm.sh | 6 ++++++ test/fast/Unit tests/nvm_tree_contains_path | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/nvm.sh b/nvm.sh index 858b0cc..d588e1a 100644 --- a/nvm.sh +++ b/nvm.sh @@ -58,6 +58,12 @@ nvm_tree_contains_path() { tree="$1" local node_path node_path="$2" + + if [ "@$tree@" = "@@" ] || [ "@$node_path@" = "@@" ]; then + >&2 echo "both the tree and the node path are required" + return 2 + fi + local pathdir pathdir=$(dirname "$node_path") while [ "$pathdir" != "" ] && [ "$pathdir" != "." ] && [ "$pathdir" != "/" ] && [ "$pathdir" != "$tree" ]; do diff --git a/test/fast/Unit tests/nvm_tree_contains_path b/test/fast/Unit tests/nvm_tree_contains_path index 278612a..a4ca09e 100755 --- a/test/fast/Unit tests/nvm_tree_contains_path +++ b/test/fast/Unit tests/nvm_tree_contains_path @@ -15,6 +15,11 @@ touch tmp/node mkdir -p tmp2 touch tmp2/node +[ "$(nvm_tree_contains_path 2>&1)" = "both the tree and the node path are required" ] || die 'incorrect error message with no args' +[ "$(nvm_tree_contains_path > /dev/null 2>&1 ; echo $?)" = "2" ] || die 'incorrect error code with no args' +[ "$(nvm_tree_contains_path tmp 2>&1)" = "both the tree and the node path are required" ] || die 'incorrect error message with one arg' +[ "$(nvm_tree_contains_path > /dev/null 2>&1 ; echo $?)" = "2" ] || die 'incorrect error code with one arg' + nvm_tree_contains_path tmp tmp/node || die '"tmp" should contain "tmp/node"' nvm_tree_contains_path tmp tmp2/node && die '"tmp" should not contain "tmp2/node"'