Reorganize internal functions

This commit is contained in:
2013-10-03 22:59:38 +01:00
parent ffdfc5f889
commit 4efde780d4
11 changed files with 188 additions and 146 deletions

View File

@@ -0,0 +1,50 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals.sh"
#
# create-symlink() tests
#
# Create temp files/folders used for create-symlink() tests.
mkdir -p "tmp/source" "tmp/target"
touch "tmp/source/profile"
# Creates a normal symlink.
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 0
assert 'readlink tmp/target/.dotfiles' "../source"
rm "tmp/target/.dotfiles"
# When target exists an is a symlink to the same source.
ln -s "../source" "tmp/target/.dotfiles"
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 0
assert 'create-symlink ../source tmp/target/.dotfiles 2>&1' ""
rm "tmp/target/.dotfiles"
# When target exists and is a symlink to a different source.
ln -s "../other" "tmp/target/.dotfiles"
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 1
assert 'create-symlink ../source tmp/target/.dotfiles' ""
assert 'create-symlink ../source tmp/target/.dotfiles 2>&1' \
"ERROR: \"tmp/target/.dotfiles\" exists, is a symlink to: ../other"
rm "tmp/target/.dotfiles"
# When target exists and is a file.
touch "tmp/target/.profile"
assert_raises 'create-symlink ../source/profile tmp/target/.profile' 1
assert 'create-symlink ../source/profile tmp/target/.profile' ""
assert 'create-symlink ../source/profile tmp/target/.profile 2>&1' \
"ERROR: \"tmp/target/.profile\" exists"
rm "tmp/target/.profile"
# When target exists and is a directory.
assert_raises 'create-symlink ../source tmp/target' 1
assert 'create-symlink ../source tmp/target' ""
assert 'create-symlink ../source tmp/target 2>&1' \
"ERROR: \"tmp/target\" exists"
# Remove temp files/folders used for locate-dotfile() tests.
rm "tmp/source/profile"
rmdir "tmp/source" "tmp/target" "tmp"
assert_end 'create-symlink()'

View File

@@ -0,0 +1,51 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals.sh"
#
# locate-dotfile() tests
#
# Create temp files/folders used for locate-dotfile() tests.
mkdir -p "test-tmp/with" "test-tmp/without"
touch "test-tmp/with/Dotfile"
# When $DOTFILE is empty and current path has a Dotfile.
cd "test-tmp/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 "test-tmp/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="test-tmp/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="test-tmp/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 "test-tmp/with/Dotfile"
rmdir "test-tmp/with" "test-tmp/without" "test-tmp"
# Ensure temp files/folder were cleaned up.
assert_raises "test -d test-tmp" 1
# End of locate-dotfile() tests.
assert_end 'locate-dotfile()'

View File

@@ -0,0 +1,36 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals.sh"
#
# locate-target() tests
#
# 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"
# End of locate-target() tests.
assert_end 'locate-target()'

View File

@@ -0,0 +1,89 @@
#! /usr/bin/env bash
source "../test-helper.sh"
source "../../src/lib/parse-dotfile-options.sh"
source "../../src/lib/trim.sh"
#
# parse-dotfile-options() tests
#
stub "parse-dotfile-root_link-option"
stub "parse-dotfile-default_action-option"
parse-dotfile-options
assert 'echo "$OPT_ROOT_LINK"' \
"parse-dotfile-root_link-option stub: "
assert 'echo "$OPT_DEFAULT_ACTION"' \
"parse-dotfile-default_action-option stub: "
restore "parse-dotfile-root_link-option"
restore "parse-dotfile-default_action-option"
assert_end "parse-dotfile-options()"
#
# parse-dotfile-option() tests
#
# Create temp files/folders used for locate-dotfile() tests.
mkdir -p "test-tmp/with" "test-tmp/without"
echo -e "foo_bar .things\nlink: foo -> .foo" > "test-tmp/with/Dotfile"
echo -e "link: foo -> .foo" > "test-tmp/without/Dotfile"
# When $DOTFILE contains root_link option.
DOTFILE="test-tmp/with/Dotfile"
assert 'parse-dotfile-option foo_bar .none' ".things"
unset DOTFILE
# When $DOTFILE does not contain root_link option.
DOTFILE="test-tmp/without/Dotfile"
assert 'parse-dotfile-option foo_bar .none' ".none"
unset DOTFILE
# When Dotfile to parse is specified as argument.
assert 'parse-dotfile-option foo_bar .none test-tmp/with/Dotfile' ".things"
assert 'parse-dotfile-option foo_bar .none test-tmp/without/Dotfile' ".none"
# When option is wrapped across multiple lines with a backslash.
echo 'foo_bar \' > "test-tmp/with/Dotfile"
echo ' .things' >> "test-tmp/with/Dotfile"
echo 'link: foo -> bar' >> "test-tmp/with/Dotfile"
DOTFILE="test-tmp/with/Dotfile"
assert 'parse-dotfile-option foo_bar .none' ".things"
unset DOTFILE
# Remove temp files/folders used for locate-dotfile() tests.
rm "test-tmp/with/Dotfile" "test-tmp/without/Dotfile"
rmdir "test-tmp/with" "test-tmp/without" "test-tmp"
assert_end "parse-dotfile-option()"
#
# parse-dotfile-root_link-option() tests
#
stub "parse-dotfile-option"
assert 'parse-dotfile-root_link-option' \
'parse-dotfile-option stub: root_link .dotfiles '
assert 'parse-dotfile-root_link-option "test-tmp/with/Dotfile"' \
'parse-dotfile-option stub: root_link .dotfiles test-tmp/with/Dotfile'
restore "parse-dotfile-option"
assert_end "parse-dotfile-root_link-option()"
#
# parse-dotfile-default_action-option() tests
#
stub "parse-dotfile-option"
assert 'parse-dotfile-default_action-option' \
'parse-dotfile-option stub: default_action link '
assert 'parse-dotfile-default_action-option "test-tmp/with/Dotfile"' \
'parse-dotfile-option stub: default_action link test-tmp/with/Dotfile'
restore "parse-dotfile-option"
assert_end "parse-dotfile-default_action-option()"