Massive restructuring code-base

This commit is contained in:
2014-03-19 21:03:31 +00:00
parent 4974654e75
commit 5567a98bb3
47 changed files with 610 additions and 395 deletions

View File

@@ -117,9 +117,38 @@ trim() {
# Internal functions # Internal functions
# #
compile-dotfile() { dotify-action() {
local action="$1"
if [ $# -lt 3 ]; then
local target="$2"
else
local source="$2"
local target="$3"
fi
dotify-setup-root-link
if [ "$action" == "default" ]; then
action="$(dotify-get-default-action)"
fi
! local valid_action="$(command -v "dotify-action-${action}")"
if [ -z "$valid_action" ]; then
echo "ERROR: \"$action\" is not a valid action." >&2
return 1
fi
if [ -n "$source" ]; then
dotify-action-${action} "$(dotify-get-run-mode)" "$target" "$source"
else
dotify-action-${action} "$(dotify-get-run-mode)" "$target"
fi
}
dotify-compile-dotfile() {
local dotfile="$1" local dotfile="$1"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi
if [ ! -f "$dotfile" ]; then if [ ! -f "$dotfile" ]; then
echo "ERROR: \"$dotfile\" does not exist." >&2 echo "ERROR: \"$dotfile\" does not exist." >&2
@@ -164,15 +193,19 @@ compile-dotfile() {
echo -e "$output" echo -e "$output"
} }
create-symlink() { dotify-create-symlink() {
local source="$1" local source="$1"
local target="$2" local target="$2"
if [ ! -e "$target" ] && [ ! -h "$target" ]; then if [ ! -e "$target" ] && [ ! -h "$target" ]; then
ln -s "$source" "$target" ln -s "$source" "$target"
echo "created"
return 0 return 0
elif [ -h "$target" ]; then elif [ -h "$target" ]; then
if [ "$(readlink "$target")" != "$source" ]; then if [ "$(readlink "$target")" == "$source" ]; then
echo "exists"
return 0
else
echo "ERROR: \"$target\" exists, is a symlink to:" \ echo "ERROR: \"$target\" exists, is a symlink to:" \
"$(readlink "$target")" >&2 "$(readlink "$target")" >&2
return 1 return 1
@@ -183,137 +216,213 @@ create-symlink() {
fi fi
} }
execute-dotfile() { dotify-execute-dotfile() {
local dotfile_source="$(dotify-compile)" local dotfile_source="$(dotify-command-compile)"
locate-target dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
ROOT_DIR="$(dirname "$DOTFILE")"
eval "$dotfile_source" eval "$dotfile_source"
return $? return $?
} }
locate-dotfile() { dotify-has-action() {
if [ -n "$DOTFILE" ]; then if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then
if [ ! -f "$DOTFILE" ]; then return 1
echo "ERROR: \"$DOTFILE\" does not exist." >&2 fi
}
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then
DOTIFY_ACTIONS=()
fi
if [[ " ${DOTIFY_ACTIONS[@]} " == *" $1 "* ]]; then
return 1
fi
DOTIFY_ACTIONS+=("$1")
}
dotify-setup-root-link() {
return 0
}
#
# Dotify attributes
#
dotify-set-default-action() {
DOTIFY_ATTR_DEFAULT_ACTION="$1"
}
dotify-get-default-action() {
if [ -z "$DOTIFY_ATTR_DEFAULT_ACTION" ]; then
DOTIFY_ATTR_DEFAULT_ACTION="link" # Default value.
fi
echo "$DOTIFY_ATTR_DEFAULT_ACTION"
}
dotify-get-dotfile-path() {
if [ -n "$DOTIFY_ATTR_DOTFILE_PATH" ]; then
echo "$DOTIFY_ATTR_DOTFILE_PATH"
return 0
fi
if [ -n "$ARG_DOTFILE" ]; then
if [ -f "$ARG_DOTFILE" ]; then
DOTIFY_ATTR_DOTFILE_PATH="$ARG_DOTFILE"
else
echo "ERROR: \"$ARG_DOTFILE\" does not exist." >&2
return 1 return 1
fi fi
elif [ -f "$(pwd)/Dotfile" ]; then elif [ -f "$(pwd)/Dotfile" ]; then
DOTFILE="$(pwd)/Dotfile" DOTIFY_ATTR_DOTFILE_PATH="$(pwd)/Dotfile"
else else
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2 echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
return 1 return 1
fi fi
echo "$DOTIFY_ATTR_DOTFILE_PATH"
} }
locate-target() { dotify-valid-dotfile-path() {
if [ -n "$TARGET" ]; then dotify-get-dotfile-path >/dev/null 2>&1
if [ ! -d "$TARGET" ]; then return "$?"
echo "ERROR: Target \"$TARGET\" is not a directory." >&2 }
dotify-get-dry-run() {
if [ -n "$DOTIFY_ATTR_DRY_RUN" ]; then
DOTIFY_ATTR_DRY_RUN="$ARG_DRY_RUN"
fi
echo "$DOTIFY_ATTR_DRY_RUN"
}
dotify-set-root-link() {
DOTIFY_ATTR_ROOT_LINK="$1"
}
dotify-get-root-link() {
if [ -z "$DOTIFY_ATTR_ROOT_LINK" ]; then
DOTIFY_ATTR_ROOT_LINK=".dotfiles" # Default value.
fi
echo "$DOTIFY_ATTR_ROOT_LINK"
}
dotify-set-run-mode() {
DOTIFY_ATTR_RUN_MODE="$1"
}
dotify-get-run-mode() {
echo "$DOTIFY_ATTR_RUN_MODE"
}
dotify-get-source-path() {
local dotfile="$(dotify-get-dotfile-path)"
if [ "$?" != "0" ]; then return 1; fi
echo "$(dirname "$dotfile")"
}
dotify-valid-source-path() {
dotify-get-source-path >/dev/null 2>&1
return "$?"
}
dotify-get-target-path() {
if [ -n "$DOTIFY_ATTR_TARGET" ]; then
echo "$DOTIFY_ATTR_TARGET"
return 0
fi
if [ -n "$ARG_TARGET" ]; then
if [ -d "$ARG_TARGET" ]; then
DOTIFY_ATTR_TARGET="$ARG_TARGET"
else
echo "ERROR: Target \"$ARG_TARGET\" is not a directory." >&2
return 1 return 1
fi fi
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
TARGET="$HOME" DOTIFY_ATTR_TARGET="$HOME"
elif [ -d ~ ]; then elif [ -d ~ ]; then
TARGET=~ DOTIFY_ATTR_TARGET=~
else else
echo "ERROR: Your \$HOME folder could not be found." >&2 echo "ERROR: Your \$HOME folder could not be found." >&2
return 1 return 1
fi fi
echo "$DOTIFY_ATTR_TARGET"
}
dotify-valid-target-path() {
dotify-get-target-path >/dev/null 2>&1
return "$?"
} }
# #
# Main functions # Dotify commands
# #
dotify-action() { dotify-command-clean() {
local action="$1" dotify-set-run-mode "clean"
dotify-execute-dotfile
if [ $# -lt 3 ]; then
local target="$2"
else
local source="$2"
local target="$3"
fi
if [ "$action" == "default" ]; then
action="$DOTIFY_OPT_DEFAULT_ACTION"
fi
! local valid_action="$(command -v "dotify-action-${action}")"
if [ -z "$valid_action" ]; then
echo "ERROR: \"$action\" is not a valid action." >&2
return 1
fi
if [ -n "$source" ]; then
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source"
else
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target"
fi
}
dotify-clean() {
DOTIFY_RUN_MODE="clean"
execute-dotfile
return $? return $?
} }
dotify-compile() { dotify-command-compile() {
locate-dotfile dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
compile-dotfile "$DOTFILE" dotify-compile-dotfile "$DOTFILE"
return $? return $?
} }
dotify-help() { dotify-command-help() {
echo "$(dotify-print-version)" echo "$(dotify-command-print-version)"
echo "usage: dotify <command> [<args>]" echo "usage: dotify <command> [<args>]"
} }
dotify-info() { dotify-command-info() {
locate-dotfile dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
locate-target dotify-valid-source-path
if [ "$?" != "0" ]; then return 1; fi
dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
echo "$(dotify-print-version)" echo "$(dotify-print-version)"
echo " Dotfile: $DOTFILE" echo " Dotfile: $(dotify-get-dotfile-path)"
echo " Root: $(dirname "$DOTFILE")" echo " Source: $(dotify-get-source-path)"
echo " Target: $TARGET" echo " Target: $(dotify-get-target-path)"
} }
dotify-install() { dotify-command-install() {
DOTIFY_RUN_MODE="install" dotify-set-run-mode "install"
execute-dotfile dotify-execute-dotfile
return $? return $?
} }
dotify-register-action() { dotify-command-print-version() {
if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi echo "dotify $(dotify-command-version)"
DOTIFY_ACTIONS+=("$1")
} }
dotify-uninstall() { dotify-command-uninstall() {
DOTIFY_RUN_MODE="uninstall" dotify-set-run-mode "uninstall"
execute-dotfile dotify-execute-dotfile
return $? return $?
} }
dotify-version() { dotify-command-version() {
echo "0.0.1" echo "0.0.1"
} }
dotify-print-version() {
echo "dotify $(dotify-version)"
}
# #
# Built-in action plugins # Built-in action plugins
@@ -325,30 +434,30 @@ dotify-register-action "link"
# Link action. # Link action.
dotify-action-link() { dotify-action-link() {
local mode="$1" local mode="$1"
! local valid_mode="$(command -v "dotify-action-link-do-${mode}")" ! local valid_mode="$(command -v "dotify-action-link-${mode}")"
if [ -n "$valid_mode" ]; then if [ -n "$valid_mode" ]; then
shift 1 shift 1
dotify-action-link-do-${mode} $@ dotify-action-link-${mode} $@
fi fi
} }
dotify-action-link-do-install() { dotify-action-link-install() {
echo "link install: $@" echo "link install: $@"
} }
dotify-action-link-do-uninstall() { dotify-action-link-uninstall() {
echo "link uninstall: $@" echo "link uninstall: $@"
} }
dotify-action-link-do-cleanup() { dotify-action-link-cleanup() {
echo "link cleanup: $@" echo "link cleanup: $@"
} }
dotify-action-link-post-run() { dotify-action-link-post-run() {
if [ "$1" == "cleanup" ]; then if [ "$1" == "cleanup" ]; then
shift 1 shift 1
dotify-action-link-do-cleanup $@ dotify-action-link-cleanup $@
fi fi
} }
@@ -358,30 +467,30 @@ dotify-register-action "git"
# Git action. # Git action.
dotify-action-git() { dotify-action-git() {
local mode="$1" local mode="$1"
! local valid_mode="$(command -v "dotify-action-git-do-${mode}")" ! local valid_mode="$(command -v "dotify-action-git-${mode}")"
if [ -n "$valid_mode" ]; then if [ -n "$valid_mode" ]; then
shift 1 shift 1
dotify-action-git-do-${mode} $@ dotify-action-git-${mode} $@
fi fi
} }
dotify-action-git-do-install() { dotify-action-git-install() {
echo "git install: $@" echo "git install: $@"
} }
dotify-action-git-do-uninstall() { dotify-action-git-uninstall() {
echo "git uninstall: $@" echo "git uninstall: $@"
} }
dotify-action-git-do-cleanup() { dotify-action-git-cleanup() {
echo "git cleanup: $@" echo "git cleanup: $@"
} }
dotify-action-git-post-run() { dotify-action-git-post-run() {
if [ "$1" == "cleanup" ]; then if [ "$1" == "cleanup" ]; then
shift 1 shift 1
dotify-action-link-do-cleanup $@ dotify-action-git-cleanup $@
fi fi
} }
@@ -391,11 +500,11 @@ dotify-action-git-post-run() {
# #
root_link () { root_link () {
DOTIFY_OPT_ROOT_LINK="$@" dotify-set-root-link "$@"
} }
default_action() { default_action() {
DOTIFY_OPT_DEFAULT_ACTION="$@" dotify-set-default-action "$@"
} }
include() { include() {
@@ -403,41 +512,34 @@ include() {
} }
#
# Default Options
#
DOTIFY_OPT_ROOT_LINK=".dotfiles"
DOTIFY_OPT_DEFAULT_ACTION="link"
# #
# Argument Parsing # Argument Parsing
# #
DOTFILE="" # --dotfile / -f ARG_DOTFILE="" # --dotfile / -f
TARGET="" # --target / -t ARG_TARGET="" # --target / -t
DRY_RUN="" # --dry-run / -d ARG_DRY_RUN="" # --dry-run / -d
HELP="" # --help / -h ARG_HELP="" # --help / -h
VERSION="" # --version / -v ARG_VERSION="" # --version / -v
if has-argument dotfile f "$@"; then if has-argument dotfile f "$@"; then
DOTFILE="$(parse-argument dotfile f "$@")" ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi fi
if has-argument target t "$@"; then if has-argument target t "$@"; then
TARGET="$(parse-argument target t "$@")" ARG_TARGET="$(parse-argument target t "$@")"
fi fi
if has-argument dry-run d "$@"; then if has-argument dry-run d "$@"; then
DRY_RUN="1" ARG_DRY_RUN="1"
fi fi
if has-argument help h "$@"; then if has-argument help h "$@"; then
HELP="1" ARG_HELP="1"
fi fi
if has-argument version v "$@"; then if has-argument version v "$@"; then
VERSION="1" ARG_VERSION="1"
fi fi
@@ -458,33 +560,33 @@ for arg in "$@"; do
done done
# Show help and exit if help arguments or command are given. # Show help and exit if help arguments or command are given.
if [ -n "$HELP" ] || [ "$command" == "help" ]; then if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then
dotify-help dotify-command-help
exit exit
fi fi
# Show version info and exit if version arguments or command are given. # Show version info and exit if version arguments or command are given.
if [ -n "$VERSION" ] || [ "$command" == "version" ]; then if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then
dotify-help | head -1 dotify-command-help | head -1
exit exit
fi fi
# Deal with the commands. # Deal with the commands.
case "$command" in case "$command" in
"info" ) "info" )
dotify-info dotify-command-info
;; ;;
"compile" ) "compile" )
dotify-compile dotify-command-compile
;; ;;
"" | "install" ) "" | "install" )
dotify-install dotify-command-install
;; ;;
"uninstall" | "remove" ) "uninstall" )
dotify-uninstall dotify-command-uninstall
;; ;;
"clean" ) "clean" )
dotify-clean dotify-command-clean
;; ;;
esac esac

View File

@@ -1,5 +1,5 @@
#! /usr/bin/env bash #! /usr/bin/env bash
source "src/lib/dotify-version.sh" source "src/lib/commands/version.sh"
source "src/lib/helpers/trim.sh" source "src/lib/helpers/trim.sh"
resolve_link() { resolve_link() {
@@ -46,7 +46,7 @@ output=""
while IFS= read line; do while IFS= read line; do
if [[ "$line" == "#"* ]]; then if [[ "$line" == "#"* ]]; then
# Replace {{DOTIFY_VERSION}} placeholder in comments. # Replace {{DOTIFY_VERSION}} placeholder in comments.
line="${line/\{\{DOTIFY_VERSION\}\}/$(dotify-version)}" line="${line/\{\{DOTIFY_VERSION\}\}/$(dotify-command-version)}"
line="${line/\{\{COPYRIGHT_YEAR\}\}/$(date +"%Y")}" line="${line/\{\{COPYRIGHT_YEAR\}\}/$(date +"%Y")}"
fi fi

View File

@@ -4,29 +4,29 @@ dotify-register-action "git"
# Git action. # Git action.
dotify-action-git() { dotify-action-git() {
local mode="$1" local mode="$1"
! local valid_mode="$(command -v "dotify-action-git-do-${mode}")" ! local valid_mode="$(command -v "dotify-action-git-${mode}")"
if [ -n "$valid_mode" ]; then if [ -n "$valid_mode" ]; then
shift 1 shift 1
dotify-action-git-do-${mode} $@ dotify-action-git-${mode} $@
fi fi
} }
dotify-action-git-do-install() { dotify-action-git-install() {
echo "git install: $@" echo "git install: $@"
} }
dotify-action-git-do-uninstall() { dotify-action-git-uninstall() {
echo "git uninstall: $@" echo "git uninstall: $@"
} }
dotify-action-git-do-cleanup() { dotify-action-git-cleanup() {
echo "git cleanup: $@" echo "git cleanup: $@"
} }
dotify-action-git-post-run() { dotify-action-git-post-run() {
if [ "$1" == "cleanup" ]; then if [ "$1" == "cleanup" ]; then
shift 1 shift 1
dotify-action-link-do-cleanup $@ dotify-action-git-cleanup $@
fi fi
} }

View File

@@ -4,29 +4,29 @@ dotify-register-action "link"
# Link action. # Link action.
dotify-action-link() { dotify-action-link() {
local mode="$1" local mode="$1"
! local valid_mode="$(command -v "dotify-action-link-do-${mode}")" ! local valid_mode="$(command -v "dotify-action-link-${mode}")"
if [ -n "$valid_mode" ]; then if [ -n "$valid_mode" ]; then
shift 1 shift 1
dotify-action-link-do-${mode} $@ dotify-action-link-${mode} $@
fi fi
} }
dotify-action-link-do-install() { dotify-action-link-install() {
echo "link install: $@" echo "link install: $@"
} }
dotify-action-link-do-uninstall() { dotify-action-link-uninstall() {
echo "link uninstall: $@" echo "link uninstall: $@"
} }
dotify-action-link-do-cleanup() { dotify-action-link-cleanup() {
echo "link cleanup: $@" echo "link cleanup: $@"
} }
dotify-action-link-post-run() { dotify-action-link-post-run() {
if [ "$1" == "cleanup" ]; then if [ "$1" == "cleanup" ]; then
shift 1 shift 1
dotify-action-link-do-cleanup $@ dotify-action-link-cleanup $@
fi fi
} }

View File

@@ -38,25 +38,38 @@ source "../lib/helpers/trim.sh"
# Internal functions # Internal functions
# #
source "../lib/internals/action.sh"
source "../lib/internals/compile-dotfile.sh" source "../lib/internals/compile-dotfile.sh"
source "../lib/internals/create-symlink.sh" source "../lib/internals/create-symlink.sh"
source "../lib/internals/execute-dotfile.sh" source "../lib/internals/execute-dotfile.sh"
source "../lib/internals/locate-dotfile.sh" source "../lib/internals/has-action.sh"
source "../lib/internals/locate-target.sh" source "../lib/internals/register-action.sh"
source "../lib/internals/setup-root-link.sh"
# #
# Main functions # Dotify attributes
# #
source "../lib/dotify-action.sh" source "../lib/attributes/default-action.sh"
source "../lib/dotify-clean.sh" source "../lib/attributes/dotfile-path.sh"
source "../lib/dotify-compile.sh" source "../lib/attributes/dry-run.sh"
source "../lib/dotify-help.sh" source "../lib/attributes/root-link.sh"
source "../lib/dotify-info.sh" source "../lib/attributes/run-mode.sh"
source "../lib/dotify-install.sh" source "../lib/attributes/source-path.sh"
source "../lib/dotify-register-action.sh" source "../lib/attributes/target-path.sh"
source "../lib/dotify-uninstall.sh"
source "../lib/dotify-version.sh" #
# Dotify commands
#
source "../lib/commands/clean.sh"
source "../lib/commands/compile.sh"
source "../lib/commands/help.sh"
source "../lib/commands/info.sh"
source "../lib/commands/install.sh"
source "../lib/commands/print-version.sh"
source "../lib/commands/uninstall.sh"
source "../lib/commands/version.sh"
# #
# Built-in action plugins # Built-in action plugins
@@ -73,41 +86,34 @@ source "../lib/dotfile-commands/root_link.sh"
source "../lib/dotfile-commands/default_action.sh" source "../lib/dotfile-commands/default_action.sh"
source "../lib/dotfile-commands/include.sh" source "../lib/dotfile-commands/include.sh"
#
# Default Options
#
DOTIFY_OPT_ROOT_LINK=".dotfiles"
DOTIFY_OPT_DEFAULT_ACTION="link"
# #
# Argument Parsing # Argument Parsing
# #
DOTFILE="" # --dotfile / -f ARG_DOTFILE="" # --dotfile / -f
TARGET="" # --target / -t ARG_TARGET="" # --target / -t
DRY_RUN="" # --dry-run / -d ARG_DRY_RUN="" # --dry-run / -d
HELP="" # --help / -h ARG_HELP="" # --help / -h
VERSION="" # --version / -v ARG_VERSION="" # --version / -v
if has-argument dotfile f "$@"; then if has-argument dotfile f "$@"; then
DOTFILE="$(parse-argument dotfile f "$@")" ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi fi
if has-argument target t "$@"; then if has-argument target t "$@"; then
TARGET="$(parse-argument target t "$@")" ARG_TARGET="$(parse-argument target t "$@")"
fi fi
if has-argument dry-run d "$@"; then if has-argument dry-run d "$@"; then
DRY_RUN="1" ARG_DRY_RUN="1"
fi fi
if has-argument help h "$@"; then if has-argument help h "$@"; then
HELP="1" ARG_HELP="1"
fi fi
if has-argument version v "$@"; then if has-argument version v "$@"; then
VERSION="1" ARG_VERSION="1"
fi fi
@@ -128,33 +134,33 @@ for arg in "$@"; do
done done
# Show help and exit if help arguments or command are given. # Show help and exit if help arguments or command are given.
if [ -n "$HELP" ] || [ "$command" == "help" ]; then if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then
dotify-help dotify-command-help
exit exit
fi fi
# Show version info and exit if version arguments or command are given. # Show version info and exit if version arguments or command are given.
if [ -n "$VERSION" ] || [ "$command" == "version" ]; then if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then
dotify-help | head -1 dotify-command-help | head -1
exit exit
fi fi
# Deal with the commands. # Deal with the commands.
case "$command" in case "$command" in
"info" ) "info" )
dotify-info dotify-command-info
;; ;;
"compile" ) "compile" )
dotify-compile dotify-command-compile
;; ;;
"" | "install" ) "" | "install" )
dotify-install dotify-command-install
;; ;;
"uninstall" | "remove" ) "uninstall" )
dotify-uninstall dotify-command-uninstall
;; ;;
"clean" ) "clean" )
dotify-clean dotify-command-clean
;; ;;
esac esac

View File

@@ -0,0 +1,11 @@
dotify-set-default-action() {
DOTIFY_ATTR_DEFAULT_ACTION="$1"
}
dotify-get-default-action() {
if [ -z "$DOTIFY_ATTR_DEFAULT_ACTION" ]; then
DOTIFY_ATTR_DEFAULT_ACTION="link" # Default value.
fi
echo "$DOTIFY_ATTR_DEFAULT_ACTION"
}

View File

@@ -0,0 +1,27 @@
dotify-get-dotfile-path() {
if [ -n "$DOTIFY_ATTR_DOTFILE_PATH" ]; then
echo "$DOTIFY_ATTR_DOTFILE_PATH"
return 0
fi
if [ -n "$ARG_DOTFILE" ]; then
if [ -f "$ARG_DOTFILE" ]; then
DOTIFY_ATTR_DOTFILE_PATH="$ARG_DOTFILE"
else
echo "ERROR: \"$ARG_DOTFILE\" does not exist." >&2
return 1
fi
elif [ -f "$(pwd)/Dotfile" ]; then
DOTIFY_ATTR_DOTFILE_PATH="$(pwd)/Dotfile"
else
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
return 1
fi
echo "$DOTIFY_ATTR_DOTFILE_PATH"
}
dotify-valid-dotfile-path() {
dotify-get-dotfile-path >/dev/null 2>&1
return "$?"
}

View File

@@ -0,0 +1,7 @@
dotify-get-dry-run() {
if [ -n "$DOTIFY_ATTR_DRY_RUN" ]; then
DOTIFY_ATTR_DRY_RUN="$ARG_DRY_RUN"
fi
echo "$DOTIFY_ATTR_DRY_RUN"
}

View File

@@ -0,0 +1,11 @@
dotify-set-root-link() {
DOTIFY_ATTR_ROOT_LINK="$1"
}
dotify-get-root-link() {
if [ -z "$DOTIFY_ATTR_ROOT_LINK" ]; then
DOTIFY_ATTR_ROOT_LINK=".dotfiles" # Default value.
fi
echo "$DOTIFY_ATTR_ROOT_LINK"
}

View File

@@ -0,0 +1,7 @@
dotify-set-run-mode() {
DOTIFY_ATTR_RUN_MODE="$1"
}
dotify-get-run-mode() {
echo "$DOTIFY_ATTR_RUN_MODE"
}

View File

@@ -0,0 +1,11 @@
dotify-get-source-path() {
local dotfile="$(dotify-get-dotfile-path)"
if [ "$?" != "0" ]; then return 1; fi
echo "$(dirname "$dotfile")"
}
dotify-valid-source-path() {
dotify-get-source-path >/dev/null 2>&1
return "$?"
}

View File

@@ -0,0 +1,29 @@
dotify-get-target-path() {
if [ -n "$DOTIFY_ATTR_TARGET" ]; then
echo "$DOTIFY_ATTR_TARGET"
return 0
fi
if [ -n "$ARG_TARGET" ]; then
if [ -d "$ARG_TARGET" ]; then
DOTIFY_ATTR_TARGET="$ARG_TARGET"
else
echo "ERROR: Target \"$ARG_TARGET\" is not a directory." >&2
return 1
fi
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
DOTIFY_ATTR_TARGET="$HOME"
elif [ -d ~ ]; then
DOTIFY_ATTR_TARGET=~
else
echo "ERROR: Your \$HOME folder could not be found." >&2
return 1
fi
echo "$DOTIFY_ATTR_TARGET"
}
dotify-valid-target-path() {
dotify-get-target-path >/dev/null 2>&1
return "$?"
}

View File

@@ -0,0 +1,5 @@
dotify-command-clean() {
dotify-set-run-mode "clean"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,7 @@
dotify-command-compile() {
dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi
dotify-compile-dotfile "$DOTFILE"
return $?
}

4
src/lib/commands/help.sh Normal file
View File

@@ -0,0 +1,4 @@
dotify-command-help() {
echo "$(dotify-command-print-version)"
echo "usage: dotify <command> [<args>]"
}

15
src/lib/commands/info.sh Normal file
View File

@@ -0,0 +1,15 @@
dotify-command-info() {
dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi
dotify-valid-source-path
if [ "$?" != "0" ]; then return 1; fi
dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi
echo "$(dotify-print-version)"
echo " Dotfile: $(dotify-get-dotfile-path)"
echo " Source: $(dotify-get-source-path)"
echo " Target: $(dotify-get-target-path)"
}

View File

@@ -0,0 +1,5 @@
dotify-command-install() {
dotify-set-run-mode "install"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,3 @@
dotify-command-print-version() {
echo "dotify $(dotify-command-version)"
}

View File

@@ -0,0 +1,5 @@
dotify-command-uninstall() {
dotify-set-run-mode "uninstall"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,3 @@
dotify-command-version() {
echo "0.0.1"
}

View File

@@ -1,3 +1,3 @@
default_action() { default_action() {
DOTIFY_OPT_DEFAULT_ACTION="$@" dotify-set-default-action "$@"
} }

View File

@@ -1,3 +1,3 @@
root_link () { root_link () {
DOTIFY_OPT_ROOT_LINK="$@" dotify-set-root-link "$@"
} }

View File

@@ -1,5 +0,0 @@
dotify-clean() {
DOTIFY_RUN_MODE="clean"
execute-dotfile
return $?
}

View File

@@ -1,7 +0,0 @@
dotify-compile() {
locate-dotfile
if [ "$?" != "0" ]; then return 1; fi
compile-dotfile "$DOTFILE"
return $?
}

View File

@@ -1,4 +0,0 @@
dotify-help() {
echo "$(dotify-print-version)"
echo "usage: dotify <command> [<args>]"
}

View File

@@ -1,12 +0,0 @@
dotify-info() {
locate-dotfile
if [ "$?" != "0" ]; then return 1; fi
locate-target
if [ "$?" != "0" ]; then return 1; fi
echo "$(dotify-print-version)"
echo " Dotfile: $DOTFILE"
echo " Root: $(dirname "$DOTFILE")"
echo " Target: $TARGET"
}

View File

@@ -1,5 +0,0 @@
dotify-install() {
DOTIFY_RUN_MODE="install"
execute-dotfile
return $?
}

View File

@@ -1,4 +0,0 @@
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi
DOTIFY_ACTIONS+=("$1")
}

View File

@@ -1,5 +0,0 @@
dotify-uninstall() {
DOTIFY_RUN_MODE="uninstall"
execute-dotfile
return $?
}

View File

@@ -1,7 +0,0 @@
dotify-version() {
echo "0.0.1"
}
dotify-print-version() {
echo "dotify $(dotify-version)"
}

View File

@@ -8,8 +8,10 @@ dotify-action() {
local target="$3" local target="$3"
fi fi
dotify-setup-root-link
if [ "$action" == "default" ]; then if [ "$action" == "default" ]; then
action="$DOTIFY_OPT_DEFAULT_ACTION" action="$(dotify-get-default-action)"
fi fi
! local valid_action="$(command -v "dotify-action-${action}")" ! local valid_action="$(command -v "dotify-action-${action}")"
@@ -19,8 +21,8 @@ dotify-action() {
fi fi
if [ -n "$source" ]; then if [ -n "$source" ]; then
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source" dotify-action-${action} "$(dotify-get-run-mode)" "$target" "$source"
else else
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" dotify-action-${action} "$(dotify-get-run-mode)" "$target"
fi fi
} }

View File

@@ -1,6 +1,6 @@
compile-dotfile() { dotify-compile-dotfile() {
local dotfile="$1" local dotfile="$1"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi
if [ ! -f "$dotfile" ]; then if [ ! -f "$dotfile" ]; then
echo "ERROR: \"$dotfile\" does not exist." >&2 echo "ERROR: \"$dotfile\" does not exist." >&2

View File

@@ -1,12 +1,16 @@
create-symlink() { dotify-create-symlink() {
local source="$1" local source="$1"
local target="$2" local target="$2"
if [ ! -e "$target" ] && [ ! -h "$target" ]; then if [ ! -e "$target" ] && [ ! -h "$target" ]; then
ln -s "$source" "$target" ln -s "$source" "$target"
echo "created"
return 0 return 0
elif [ -h "$target" ]; then elif [ -h "$target" ]; then
if [ "$(readlink "$target")" != "$source" ]; then if [ "$(readlink "$target")" == "$source" ]; then
echo "exists"
return 0
else
echo "ERROR: \"$target\" exists, is a symlink to:" \ echo "ERROR: \"$target\" exists, is a symlink to:" \
"$(readlink "$target")" >&2 "$(readlink "$target")" >&2
return 1 return 1

View File

@@ -1,11 +1,9 @@
execute-dotfile() { dotify-execute-dotfile() {
local dotfile_source="$(dotify-compile)" local dotfile_source="$(dotify-command-compile)"
locate-target dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
ROOT_DIR="$(dirname "$DOTFILE")"
eval "$dotfile_source" eval "$dotfile_source"
return $? return $?
} }

View File

@@ -0,0 +1,5 @@
dotify-has-action() {
if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then
return 1
fi
}

View File

@@ -1,13 +0,0 @@
locate-dotfile() {
if [ -n "$DOTFILE" ]; then
if [ ! -f "$DOTFILE" ]; then
echo "ERROR: \"$DOTFILE\" does not exist." >&2
return 1
fi
elif [ -f "$(pwd)/Dotfile" ]; then
DOTFILE="$(pwd)/Dotfile"
else
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
return 1
fi
}

View File

@@ -1,15 +0,0 @@
locate-target() {
if [ -n "$TARGET" ]; then
if [ ! -d "$TARGET" ]; then
echo "ERROR: Target \"$TARGET\" is not a directory." >&2
return 1
fi
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
TARGET="$HOME"
elif [ -d ~ ]; then
TARGET=~
else
echo "ERROR: Your \$HOME folder could not be found." >&2
return 1
fi
}

View File

@@ -0,0 +1,11 @@
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then
DOTIFY_ACTIONS=()
fi
if [[ " ${DOTIFY_ACTIONS[@]} " == *" $1 "* ]]; then
return 1
fi
DOTIFY_ACTIONS+=("$1")
}

View File

@@ -0,0 +1,3 @@
dotify-setup-root-link() {
return 0
}

View File

@@ -0,0 +1,33 @@
#! /usr/bin/env bash
source "../test-helper.sh"
source "../../src/lib/dotify-action.sh"
#
# Integration test: link action
#
# Create temp files/folders used for tests.
TEST_SOURCE="tmp/source"
TEST_TARGET="tmp/target"
MY_DOTFILE="$TEST_SOURCE/Dotfile"
mkdir -p "$TEST_SOURCE" "$TEST_TARGET"
PROFILE_TXT="# I am a .profile file"
echo "$PROFILE_TXT" > "tmp/source/profile"
# Basic of basics.
echo -e "profile -> .profile" > "$MY_DOTFILE"
assert_raises "../../bin/dotify -f '$MY_DOTFILE' -t '$TEST_TARGET'" 0
assert "../../bin/dotify -f '$MY_DOTFILE' -t '$TEST_TARGET'" \
" Create symlink: $TEST_TARGET/.profile -> .dotfiles/profile"
assert "readlink '$TEST_TARGET/.profile'" ".dotfiles/profile"
assert "cat '$TEST_TARGET/.profile'" "$PROFILE_TXT"
rm "TEST_TARGET/.profile"
rm "$MY_DOTFILE"
# Remove temp files/folders used for locate-dotfile() tests.
rm "tmp/source/profile"
rmdir "tmp/source" "tmp/target" "tmp"
assert_end 'Integration: link action'

View File

@@ -1,20 +1,15 @@
#! /usr/bin/env bash #! /usr/bin/env bash
source "../test-helper.sh" source "../../test-helper.sh"
source "../../src/lib/dotify-action.sh" source "../../../src/lib/internals/action.sh"
# #
# dotify-action() tests # dotify-action() tests
# #
# Set required environment variables. stub "dotify-setup-root-link"
DOTIFY_RUN_MODE="install" stub_and_echo "dotify-get-run-mode" "install"
DOTIFY_OPT_DEFAULT_ACTION="link" stub_and_echo "dotify-get-default-action" "link"
stub_and_echo "dotify-action-link" "link stub: \$@"
# Simple mock for link action.
dotify-action-link() {
echo "link stub: $@"
}
# Given a specific action. # Given a specific action.
assert "dotify-action link ackrc .ackrc" "link stub: install .ackrc ackrc" assert "dotify-action link ackrc .ackrc" "link stub: install .ackrc ackrc"
@@ -34,4 +29,8 @@ assert_raises "dotify-action foo ackrc .ackrc" 1
assert "dotify-action foo ackrc .ackrc" "" assert "dotify-action foo ackrc .ackrc" ""
assert "dotify-action foo ackrc .ackrc 2>&1" "ERROR: \"foo\" is not a valid action." assert "dotify-action foo ackrc .ackrc 2>&1" "ERROR: \"foo\" is not a valid action."
restore "dotify-setup-root-link"
restore "dotify-get-run-mode"
restore "dotify-action-link"
assert_end "dotify-action()" assert_end "dotify-action()"

View File

@@ -4,7 +4,7 @@ source "../../../src/lib/helpers/trim.sh"
source "../../../src/lib/internals/compile-dotfile.sh" source "../../../src/lib/internals/compile-dotfile.sh"
# #
# compile-dotfile() tests # dotify-compile-dotfile() tests
# #
# Create temp files/folders used for locate-dotfile() tests. # Create temp files/folders used for locate-dotfile() tests.
@@ -14,27 +14,28 @@ echo "hostname='air'" > $dotfile
# Compiles file given as first argument. # Compiles file given as first argument.
assert "compile-dotfile $dotfile" "hostname='air'" assert "dotify-compile-dotfile $dotfile" "hostname='air'"
assert_raises "compile-dotfile tmp/fake" 1 assert_raises "dotify-compile-dotfile tmp/fake" 1
assert "compile-dotfile tmp/fake" "" assert "dotify-compile-dotfile tmp/fake" ""
assert "compile-dotfile tmp/fake 2>&1" "ERROR: \"tmp/fake\" does not exist." assert "dotify-compile-dotfile tmp/fake 2>&1" \
"ERROR: \"tmp/fake\" does not exist."
# Compiles file from global $DOTFILE variable. # Compiles file from dotify-get-dotfile-path attribute helper.
DOTFILE=$dotfile stub_and_echo "dotify-get-dotfile-path" "$dotfile"
assert "compile-dotfile" "hostname='air'" assert "dotify-compile-dotfile" "hostname='air'"
DOTFILE="tmp/fake" stub_and_echo "dotify-get-dotfile-path" "tmp/fake"
assert_raises "compile-dotfile" 1 assert_raises "dotify-compile-dotfile" 1
assert "compile-dotfile" "" assert "dotify-compile-dotfile" ""
assert "compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist." assert "dotify-compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist."
unset DOTFILE restore "dotify-get-dotfile-path"
# Compiles standard actions # Compiles standard actions
echo -e "root_link .dotfiles echo -e "root_link .dotfiles
link: ackrc -> .ackrc link: ackrc -> .ackrc
link: gitconfig -> \".gitconfig\"" > $dotfile link: gitconfig -> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link ackrc .ackrc dotify-action link ackrc .ackrc
dotify-action link gitconfig \".gitconfig\"" dotify-action link gitconfig \".gitconfig\""
@@ -43,7 +44,7 @@ dotify-action link gitconfig \".gitconfig\""
echo -e "root_link .dotfiles echo -e "root_link .dotfiles
link: -> .ackrc link: -> .ackrc
link:-> \".gitconfig\"" > $dotfile link:-> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link .ackrc dotify-action link .ackrc
dotify-action link \".gitconfig\"" dotify-action link \".gitconfig\""
@@ -52,7 +53,7 @@ dotify-action link \".gitconfig\""
echo -e "root_link .dotfiles echo -e "root_link .dotfiles
link: ackrc -> .ackrc link: ackrc -> .ackrc
gitconfig -> \".gitconfig\"" > $dotfile gitconfig -> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link ackrc .ackrc dotify-action link ackrc .ackrc
dotify-action default gitconfig \".gitconfig\"" dotify-action default gitconfig \".gitconfig\""
@@ -61,7 +62,7 @@ dotify-action default gitconfig \".gitconfig\""
echo -e "root_link .dotfiles echo -e "root_link .dotfiles
-> .ackrc -> .ackrc
-> \".gitconfig\"" > $dotfile -> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action default .ackrc dotify-action default .ackrc
dotify-action default \".gitconfig\"" dotify-action default \".gitconfig\""
@@ -71,7 +72,7 @@ echo -e "root_link .dotfiles
if [ true ]; then if [ true ]; then
ackrc -> .ackrc ackrc -> .ackrc
fi" > $dotfile fi" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
if [ true ]; then if [ true ]; then
dotify-action default ackrc .ackrc dotify-action default ackrc .ackrc
fi" fi"
@@ -87,7 +88,7 @@ profile -> .profile
# Apps # Apps
ackrc -> .ackrc ackrc -> .ackrc
gitconfig -> .gitconfig" > $dotfile gitconfig -> .gitconfig" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action default profile .profile dotify-action default profile .profile
dotify-action default ackrc .ackrc dotify-action default ackrc .ackrc
dotify-action default gitconfig .gitconfig" dotify-action default gitconfig .gitconfig"
@@ -98,4 +99,4 @@ rm "tmp/Dotfile"
rmdir "tmp" rmdir "tmp"
assert_raises "test -d tmp" 1 assert_raises "test -d tmp" 1
assert_end 'compile-dotfile()' assert_end 'dotify-compile-dotfile()'

View File

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

View File

@@ -0,0 +1,32 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/register-action.sh"
source "../../../src/lib/internals/has-action.sh"
#
# dotify-register-action() tests
#
# When no actions are registered.
assert_raises 'dotify-has-action "link"' 1
# When checking an action that is registered.
dotify-register-action "link"
assert_raises 'dotify-has-action "link"' 0
unset DOTIFY_ACTIONS
# When checking an action that is not registered.
dotify-register-action "link"
assert_raises 'dotify-has-action "git"' 1
unset DOTIFY_ACTIONS
# When registering and checking multiple actions
dotify-register-action "link"
dotify-register-action "git"
assert_raises 'dotify-has-action "link"' 0
assert_raises 'dotify-has-action "git"' 0
assert_raises 'dotify-has-action "copy"' 1
unset DOTIFY_ACTIONS
assert_end "dotify-register-action() tests"

View File

@@ -1,51 +0,0 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/locate-dotfile.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

@@ -1,36 +0,0 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/locate-target.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,28 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/register-action.sh"
#
# dotify-register-action() tests
#
# Registering an action.
dotify-register-action "link"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link"
unset DOTIFY_ACTIONS
# Registering multiple actions.
dotify-register-action "link"
dotify-register-action "git"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link git"
unset DOTIFY_ACTIONS
# Registering the same action multiple times.
dotify-register-action "link"
dotify-register-action "link"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link"
assert_raises 'dotify-register-action "link"' 1
unset DOTIFY_ACTIONS
assert_end "dotify-register-action() tests"