From c233bc3dc8db9dfe0b8fd4fd77ce5c2decb5e48c Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 28 Jun 2013 13:55:37 +0200 Subject: [PATCH] Add tests for locate-* functions --- test/lib/internals-test.sh | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 test/lib/internals-test.sh diff --git a/test/lib/internals-test.sh b/test/lib/internals-test.sh new file mode 100755 index 0000000..a8464c0 --- /dev/null +++ b/test/lib/internals-test.sh @@ -0,0 +1,89 @@ +#! /usr/bin/env bash +source "../assert.sh" + +source "../../src/lib/internals.sh" + +# silent shortcut to "cd -" +cd-back() { + cd - 1>/dev/null +} + +# +# locate-dotfile() +# + +# Create temp files/folders used for locate-dotfile() tests. +mkdir -p "locate-dotfile-tests/without" +mkdir -p "locate-dotfile-tests/with" +touch "locate-dotfile-tests/with/Dotfile" + +# When $DOTFILE is empty and current path has a Dotfile. +cd "locate-dotfile-tests/with" +DOTFILE="" +assert_raises 'locate-dotfile' 0 +assert 'locate-dotfile; echo "$DOTFILE"' "$(pwd)/Dotfile" +unset DOTFILE +cd-back + +# When $DOTFILE is empty and current path does not have a Dotfile. +cd "locate-dotfile-tests/without" +DOTFILE="" +assert_raises 'locate-dotfile' 1 +assert 'locate-dotfile 2>&1' "ERROR: \"$(pwd)\" does not have a Dotfile." +assert 'locate-dotfile; echo "$DOTFILE"' "" +unset DOTFILE +cd-back + +# When $DOTFILE is not empty and points at a existing Dotfile. +DOTFILE="locate-dotfile-tests/with/Dotfile" +assert_raises 'locate-dotfile' 0 +assert 'locate-dotfile; echo "$DOTFILE"' "$DOTFILE" +unset DOTFILE + +# When $DOTFILE is not empty and points at a non-existing Dotfile. +DOTFILE="locate-dotfile-tests/without/Dotfile" +assert_raises 'locate-dotfile' 1 +assert 'locate-dotfile 2>&1' "ERROR: \"$DOTFILE\" does not exist." +assert 'locate-dotfile; echo "$DOTFILE"' "$DOTFILE" +unset DOTFILE + +# Remove temp files/folders used for locate-dotfile() tests. +rm "locate-dotfile-tests/with/Dotfile" +rmdir "locate-dotfile-tests/with" +rmdir "locate-dotfile-tests/without" +rmdir "locate-dotfile-tests" + +assert_end 'locate-dotfile()' + + +# +# locate-target() +# + +# When $TARGET is empty. +TARGET="" +assert_raises 'locate-target' 0 +assert 'locate-target; echo "$TARGET"' "$HOME" +unset TARGET + +# When $TARGET is not empty and is a directory. +TARGET="$(pwd)" +assert_raises 'locate-target' 0 +assert 'locate-target; echo "$TARGET"' "$(pwd)" +unset TARGET + +# When $TARGET is not empty and is not a directory. +TARGET="/tmp/this/does/not/exist" +assert_raises 'locate-target' 1 +assert 'locate-target 2>&1' "ERROR: Target \"$TARGET\" is not a directory." +assert 'locate-target; echo "$TARGET"' "$TARGET" +unset TARGET + +# If neither $TARGET or $HOME is set, ~ is expanded to find home folder +original_home="$HOME" +unset HOME +assert_raises 'locate-target' 0 +assert 'locate-target; echo "$TARGET"' "$original_home" +HOME="$original_home" + +assert_end 'locate-target()'