From 5567a98bb3fb3cf398f45520458271cf05144a3f Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 19 Mar 2014 21:03:31 +0000 Subject: [PATCH] Massive restructuring code-base --- bin/dotify | 336 ++++++++++++------ build.sh | 4 +- src/actions/git.sh | 12 +- src/actions/link.sh | 12 +- src/bin/dotify | 84 +++-- src/lib/attributes/default-action.sh | 11 + src/lib/attributes/dotfile-path.sh | 27 ++ src/lib/attributes/dry-run.sh | 7 + src/lib/attributes/root-link.sh | 11 + src/lib/attributes/run-mode.sh | 7 + src/lib/attributes/source-path.sh | 11 + src/lib/attributes/target-path.sh | 29 ++ src/lib/commands/clean.sh | 5 + src/lib/commands/compile.sh | 7 + src/lib/commands/help.sh | 4 + src/lib/commands/info.sh | 15 + src/lib/commands/install.sh | 5 + src/lib/commands/print-version.sh | 3 + src/lib/commands/uninstall.sh | 5 + src/lib/commands/version.sh | 3 + src/lib/dotfile-commands/default_action.sh | 2 +- src/lib/dotfile-commands/root_link.sh | 2 +- src/lib/dotify-clean.sh | 5 - src/lib/dotify-compile.sh | 7 - src/lib/dotify-help.sh | 4 - src/lib/dotify-info.sh | 12 - src/lib/dotify-install.sh | 5 - src/lib/dotify-register-action.sh | 4 - src/lib/dotify-uninstall.sh | 5 - src/lib/dotify-version.sh | 7 - .../{dotify-action.sh => internals/action.sh} | 8 +- src/lib/internals/compile-dotfile.sh | 4 +- src/lib/internals/create-symlink.sh | 8 +- src/lib/internals/execute-dotfile.sh | 8 +- src/lib/internals/has-action.sh | 5 + src/lib/internals/locate-dotfile.sh | 13 - src/lib/internals/locate-target.sh | 15 - src/lib/internals/register-action.sh | 11 + src/lib/internals/setup-root-link.sh | 3 + test/integration/link-action-test.sh | 33 ++ .../action-test.sh} | 21 +- test/lib/internals/compile-dotfile-test.sh | 41 +-- test/lib/internals/create-symlink-test.sh | 37 +- test/lib/internals/has-action-test.sh | 32 ++ test/lib/internals/locate-dotfile-test.sh | 51 --- test/lib/internals/locate-target-test.sh | 36 -- test/lib/internals/register-action-test.sh | 28 ++ 47 files changed, 610 insertions(+), 395 deletions(-) create mode 100644 src/lib/attributes/default-action.sh create mode 100644 src/lib/attributes/dotfile-path.sh create mode 100644 src/lib/attributes/dry-run.sh create mode 100644 src/lib/attributes/root-link.sh create mode 100644 src/lib/attributes/run-mode.sh create mode 100644 src/lib/attributes/source-path.sh create mode 100644 src/lib/attributes/target-path.sh create mode 100644 src/lib/commands/clean.sh create mode 100644 src/lib/commands/compile.sh create mode 100644 src/lib/commands/help.sh create mode 100644 src/lib/commands/info.sh create mode 100644 src/lib/commands/install.sh create mode 100644 src/lib/commands/print-version.sh create mode 100644 src/lib/commands/uninstall.sh create mode 100644 src/lib/commands/version.sh delete mode 100644 src/lib/dotify-clean.sh delete mode 100644 src/lib/dotify-compile.sh delete mode 100644 src/lib/dotify-help.sh delete mode 100644 src/lib/dotify-info.sh delete mode 100644 src/lib/dotify-install.sh delete mode 100644 src/lib/dotify-register-action.sh delete mode 100644 src/lib/dotify-uninstall.sh delete mode 100644 src/lib/dotify-version.sh rename src/lib/{dotify-action.sh => internals/action.sh} (66%) create mode 100644 src/lib/internals/has-action.sh delete mode 100644 src/lib/internals/locate-dotfile.sh delete mode 100644 src/lib/internals/locate-target.sh create mode 100644 src/lib/internals/register-action.sh create mode 100644 src/lib/internals/setup-root-link.sh create mode 100755 test/integration/link-action-test.sh rename test/lib/{dotify-action-test.sh => internals/action-test.sh} (69%) create mode 100755 test/lib/internals/has-action-test.sh delete mode 100755 test/lib/internals/locate-dotfile-test.sh delete mode 100755 test/lib/internals/locate-target-test.sh create mode 100755 test/lib/internals/register-action-test.sh diff --git a/bin/dotify b/bin/dotify index 044263c..cc59ce3 100755 --- a/bin/dotify +++ b/bin/dotify @@ -117,9 +117,38 @@ trim() { # 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" - if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi + if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi if [ ! -f "$dotfile" ]; then echo "ERROR: \"$dotfile\" does not exist." >&2 @@ -164,15 +193,19 @@ compile-dotfile() { echo -e "$output" } -create-symlink() { +dotify-create-symlink() { local source="$1" local target="$2" if [ ! -e "$target" ] && [ ! -h "$target" ]; then ln -s "$source" "$target" + echo "created" return 0 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:" \ "$(readlink "$target")" >&2 return 1 @@ -183,137 +216,213 @@ create-symlink() { fi } -execute-dotfile() { - local dotfile_source="$(dotify-compile)" +dotify-execute-dotfile() { + local dotfile_source="$(dotify-command-compile)" - locate-target + dotify-valid-target-path if [ "$?" != "0" ]; then return 1; fi - ROOT_DIR="$(dirname "$DOTFILE")" - eval "$dotfile_source" return $? } -locate-dotfile() { - if [ -n "$DOTFILE" ]; then - if [ ! -f "$DOTFILE" ]; then - echo "ERROR: \"$DOTFILE\" does not exist." >&2 +dotify-has-action() { + if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then + return 1 + 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 fi elif [ -f "$(pwd)/Dotfile" ]; then - DOTFILE="$(pwd)/Dotfile" + DOTIFY_ATTR_DOTFILE_PATH="$(pwd)/Dotfile" else echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2 return 1 fi + + echo "$DOTIFY_ATTR_DOTFILE_PATH" } -locate-target() { - if [ -n "$TARGET" ]; then - if [ ! -d "$TARGET" ]; then - echo "ERROR: Target \"$TARGET\" is not a directory." >&2 +dotify-valid-dotfile-path() { + dotify-get-dotfile-path >/dev/null 2>&1 + return "$?" +} + +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 fi elif [ -n "$HOME" ] && [ -d "$HOME" ]; then - TARGET="$HOME" + DOTIFY_ATTR_TARGET="$HOME" elif [ -d ~ ]; then - TARGET=~ + 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 "$?" } # -# Main functions +# Dotify commands # -dotify-action() { - local action="$1" - - 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 +dotify-command-clean() { + dotify-set-run-mode "clean" + dotify-execute-dotfile return $? } -dotify-compile() { - locate-dotfile +dotify-command-compile() { + dotify-valid-dotfile-path if [ "$?" != "0" ]; then return 1; fi - compile-dotfile "$DOTFILE" + dotify-compile-dotfile "$DOTFILE" return $? } -dotify-help() { - echo "$(dotify-print-version)" +dotify-command-help() { + echo "$(dotify-command-print-version)" echo "usage: dotify []" } -dotify-info() { - locate-dotfile +dotify-command-info() { + dotify-valid-dotfile-path 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 echo "$(dotify-print-version)" - echo " Dotfile: $DOTFILE" - echo " Root: $(dirname "$DOTFILE")" - echo " Target: $TARGET" + echo " Dotfile: $(dotify-get-dotfile-path)" + echo " Source: $(dotify-get-source-path)" + echo " Target: $(dotify-get-target-path)" } -dotify-install() { - DOTIFY_RUN_MODE="install" - execute-dotfile +dotify-command-install() { + dotify-set-run-mode "install" + dotify-execute-dotfile return $? } -dotify-register-action() { - if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi - DOTIFY_ACTIONS+=("$1") +dotify-command-print-version() { + echo "dotify $(dotify-command-version)" } -dotify-uninstall() { - DOTIFY_RUN_MODE="uninstall" - execute-dotfile +dotify-command-uninstall() { + dotify-set-run-mode "uninstall" + dotify-execute-dotfile return $? } -dotify-version() { +dotify-command-version() { echo "0.0.1" } -dotify-print-version() { - echo "dotify $(dotify-version)" -} - # # Built-in action plugins @@ -325,30 +434,30 @@ dotify-register-action "link" # Link action. dotify-action-link() { 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 shift 1 - dotify-action-link-do-${mode} $@ + dotify-action-link-${mode} $@ fi } -dotify-action-link-do-install() { +dotify-action-link-install() { echo "link install: $@" } -dotify-action-link-do-uninstall() { +dotify-action-link-uninstall() { echo "link uninstall: $@" } -dotify-action-link-do-cleanup() { +dotify-action-link-cleanup() { echo "link cleanup: $@" } dotify-action-link-post-run() { if [ "$1" == "cleanup" ]; then shift 1 - dotify-action-link-do-cleanup $@ + dotify-action-link-cleanup $@ fi } @@ -358,30 +467,30 @@ dotify-register-action "git" # Git action. dotify-action-git() { 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 shift 1 - dotify-action-git-do-${mode} $@ + dotify-action-git-${mode} $@ fi } -dotify-action-git-do-install() { +dotify-action-git-install() { echo "git install: $@" } -dotify-action-git-do-uninstall() { +dotify-action-git-uninstall() { echo "git uninstall: $@" } -dotify-action-git-do-cleanup() { +dotify-action-git-cleanup() { echo "git cleanup: $@" } dotify-action-git-post-run() { if [ "$1" == "cleanup" ]; then shift 1 - dotify-action-link-do-cleanup $@ + dotify-action-git-cleanup $@ fi } @@ -391,11 +500,11 @@ dotify-action-git-post-run() { # root_link () { - DOTIFY_OPT_ROOT_LINK="$@" + dotify-set-root-link "$@" } default_action() { - DOTIFY_OPT_DEFAULT_ACTION="$@" + dotify-set-default-action "$@" } include() { @@ -403,41 +512,34 @@ include() { } -# -# Default Options -# - -DOTIFY_OPT_ROOT_LINK=".dotfiles" -DOTIFY_OPT_DEFAULT_ACTION="link" - # # Argument Parsing # -DOTFILE="" # --dotfile / -f -TARGET="" # --target / -t -DRY_RUN="" # --dry-run / -d -HELP="" # --help / -h -VERSION="" # --version / -v +ARG_DOTFILE="" # --dotfile / -f +ARG_TARGET="" # --target / -t +ARG_DRY_RUN="" # --dry-run / -d +ARG_HELP="" # --help / -h +ARG_VERSION="" # --version / -v if has-argument dotfile f "$@"; then - DOTFILE="$(parse-argument dotfile f "$@")" + ARG_DOTFILE="$(parse-argument dotfile f "$@")" fi if has-argument target t "$@"; then - TARGET="$(parse-argument target t "$@")" + ARG_TARGET="$(parse-argument target t "$@")" fi if has-argument dry-run d "$@"; then - DRY_RUN="1" + ARG_DRY_RUN="1" fi if has-argument help h "$@"; then - HELP="1" + ARG_HELP="1" fi if has-argument version v "$@"; then - VERSION="1" + ARG_VERSION="1" fi @@ -458,33 +560,33 @@ for arg in "$@"; do done # Show help and exit if help arguments or command are given. -if [ -n "$HELP" ] || [ "$command" == "help" ]; then - dotify-help +if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then + dotify-command-help exit fi # Show version info and exit if version arguments or command are given. -if [ -n "$VERSION" ] || [ "$command" == "version" ]; then - dotify-help | head -1 +if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then + dotify-command-help | head -1 exit fi # Deal with the commands. case "$command" in "info" ) - dotify-info + dotify-command-info ;; "compile" ) - dotify-compile + dotify-command-compile ;; "" | "install" ) - dotify-install + dotify-command-install ;; - "uninstall" | "remove" ) - dotify-uninstall + "uninstall" ) + dotify-command-uninstall ;; "clean" ) - dotify-clean + dotify-command-clean ;; esac diff --git a/build.sh b/build.sh index 428aef5..df17093 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash -source "src/lib/dotify-version.sh" +source "src/lib/commands/version.sh" source "src/lib/helpers/trim.sh" resolve_link() { @@ -46,7 +46,7 @@ output="" while IFS= read line; do if [[ "$line" == "#"* ]]; then # 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")}" fi diff --git a/src/actions/git.sh b/src/actions/git.sh index 9a5dc4f..f5d152b 100644 --- a/src/actions/git.sh +++ b/src/actions/git.sh @@ -4,29 +4,29 @@ dotify-register-action "git" # Git action. dotify-action-git() { 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 shift 1 - dotify-action-git-do-${mode} $@ + dotify-action-git-${mode} $@ fi } -dotify-action-git-do-install() { +dotify-action-git-install() { echo "git install: $@" } -dotify-action-git-do-uninstall() { +dotify-action-git-uninstall() { echo "git uninstall: $@" } -dotify-action-git-do-cleanup() { +dotify-action-git-cleanup() { echo "git cleanup: $@" } dotify-action-git-post-run() { if [ "$1" == "cleanup" ]; then shift 1 - dotify-action-link-do-cleanup $@ + dotify-action-git-cleanup $@ fi } diff --git a/src/actions/link.sh b/src/actions/link.sh index fe2f458..73c834b 100644 --- a/src/actions/link.sh +++ b/src/actions/link.sh @@ -4,29 +4,29 @@ dotify-register-action "link" # Link action. dotify-action-link() { 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 shift 1 - dotify-action-link-do-${mode} $@ + dotify-action-link-${mode} $@ fi } -dotify-action-link-do-install() { +dotify-action-link-install() { echo "link install: $@" } -dotify-action-link-do-uninstall() { +dotify-action-link-uninstall() { echo "link uninstall: $@" } -dotify-action-link-do-cleanup() { +dotify-action-link-cleanup() { echo "link cleanup: $@" } dotify-action-link-post-run() { if [ "$1" == "cleanup" ]; then shift 1 - dotify-action-link-do-cleanup $@ + dotify-action-link-cleanup $@ fi } diff --git a/src/bin/dotify b/src/bin/dotify index 8f5de8b..e36485f 100755 --- a/src/bin/dotify +++ b/src/bin/dotify @@ -38,25 +38,38 @@ source "../lib/helpers/trim.sh" # Internal functions # +source "../lib/internals/action.sh" source "../lib/internals/compile-dotfile.sh" source "../lib/internals/create-symlink.sh" source "../lib/internals/execute-dotfile.sh" -source "../lib/internals/locate-dotfile.sh" -source "../lib/internals/locate-target.sh" +source "../lib/internals/has-action.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/dotify-clean.sh" -source "../lib/dotify-compile.sh" -source "../lib/dotify-help.sh" -source "../lib/dotify-info.sh" -source "../lib/dotify-install.sh" -source "../lib/dotify-register-action.sh" -source "../lib/dotify-uninstall.sh" -source "../lib/dotify-version.sh" +source "../lib/attributes/default-action.sh" +source "../lib/attributes/dotfile-path.sh" +source "../lib/attributes/dry-run.sh" +source "../lib/attributes/root-link.sh" +source "../lib/attributes/run-mode.sh" +source "../lib/attributes/source-path.sh" +source "../lib/attributes/target-path.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 @@ -73,41 +86,34 @@ source "../lib/dotfile-commands/root_link.sh" source "../lib/dotfile-commands/default_action.sh" source "../lib/dotfile-commands/include.sh" -# -# Default Options -# - -DOTIFY_OPT_ROOT_LINK=".dotfiles" -DOTIFY_OPT_DEFAULT_ACTION="link" - # # Argument Parsing # -DOTFILE="" # --dotfile / -f -TARGET="" # --target / -t -DRY_RUN="" # --dry-run / -d -HELP="" # --help / -h -VERSION="" # --version / -v +ARG_DOTFILE="" # --dotfile / -f +ARG_TARGET="" # --target / -t +ARG_DRY_RUN="" # --dry-run / -d +ARG_HELP="" # --help / -h +ARG_VERSION="" # --version / -v if has-argument dotfile f "$@"; then - DOTFILE="$(parse-argument dotfile f "$@")" + ARG_DOTFILE="$(parse-argument dotfile f "$@")" fi if has-argument target t "$@"; then - TARGET="$(parse-argument target t "$@")" + ARG_TARGET="$(parse-argument target t "$@")" fi if has-argument dry-run d "$@"; then - DRY_RUN="1" + ARG_DRY_RUN="1" fi if has-argument help h "$@"; then - HELP="1" + ARG_HELP="1" fi if has-argument version v "$@"; then - VERSION="1" + ARG_VERSION="1" fi @@ -128,33 +134,33 @@ for arg in "$@"; do done # Show help and exit if help arguments or command are given. -if [ -n "$HELP" ] || [ "$command" == "help" ]; then - dotify-help +if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then + dotify-command-help exit fi # Show version info and exit if version arguments or command are given. -if [ -n "$VERSION" ] || [ "$command" == "version" ]; then - dotify-help | head -1 +if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then + dotify-command-help | head -1 exit fi # Deal with the commands. case "$command" in "info" ) - dotify-info + dotify-command-info ;; "compile" ) - dotify-compile + dotify-command-compile ;; "" | "install" ) - dotify-install + dotify-command-install ;; - "uninstall" | "remove" ) - dotify-uninstall + "uninstall" ) + dotify-command-uninstall ;; "clean" ) - dotify-clean + dotify-command-clean ;; esac diff --git a/src/lib/attributes/default-action.sh b/src/lib/attributes/default-action.sh new file mode 100644 index 0000000..12ba4bb --- /dev/null +++ b/src/lib/attributes/default-action.sh @@ -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" +} diff --git a/src/lib/attributes/dotfile-path.sh b/src/lib/attributes/dotfile-path.sh new file mode 100644 index 0000000..46f1dbe --- /dev/null +++ b/src/lib/attributes/dotfile-path.sh @@ -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 "$?" +} diff --git a/src/lib/attributes/dry-run.sh b/src/lib/attributes/dry-run.sh new file mode 100644 index 0000000..b923122 --- /dev/null +++ b/src/lib/attributes/dry-run.sh @@ -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" +} diff --git a/src/lib/attributes/root-link.sh b/src/lib/attributes/root-link.sh new file mode 100644 index 0000000..f9571cd --- /dev/null +++ b/src/lib/attributes/root-link.sh @@ -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" +} diff --git a/src/lib/attributes/run-mode.sh b/src/lib/attributes/run-mode.sh new file mode 100644 index 0000000..bfe493a --- /dev/null +++ b/src/lib/attributes/run-mode.sh @@ -0,0 +1,7 @@ +dotify-set-run-mode() { + DOTIFY_ATTR_RUN_MODE="$1" +} + +dotify-get-run-mode() { + echo "$DOTIFY_ATTR_RUN_MODE" +} diff --git a/src/lib/attributes/source-path.sh b/src/lib/attributes/source-path.sh new file mode 100644 index 0000000..ee4c310 --- /dev/null +++ b/src/lib/attributes/source-path.sh @@ -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 "$?" +} diff --git a/src/lib/attributes/target-path.sh b/src/lib/attributes/target-path.sh new file mode 100644 index 0000000..8561e65 --- /dev/null +++ b/src/lib/attributes/target-path.sh @@ -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 "$?" +} diff --git a/src/lib/commands/clean.sh b/src/lib/commands/clean.sh new file mode 100644 index 0000000..acd9dbe --- /dev/null +++ b/src/lib/commands/clean.sh @@ -0,0 +1,5 @@ +dotify-command-clean() { + dotify-set-run-mode "clean" + dotify-execute-dotfile + return $? +} diff --git a/src/lib/commands/compile.sh b/src/lib/commands/compile.sh new file mode 100644 index 0000000..8d1d6dc --- /dev/null +++ b/src/lib/commands/compile.sh @@ -0,0 +1,7 @@ +dotify-command-compile() { + dotify-valid-dotfile-path + if [ "$?" != "0" ]; then return 1; fi + + dotify-compile-dotfile "$DOTFILE" + return $? +} diff --git a/src/lib/commands/help.sh b/src/lib/commands/help.sh new file mode 100644 index 0000000..131eb7d --- /dev/null +++ b/src/lib/commands/help.sh @@ -0,0 +1,4 @@ +dotify-command-help() { + echo "$(dotify-command-print-version)" + echo "usage: dotify []" +} diff --git a/src/lib/commands/info.sh b/src/lib/commands/info.sh new file mode 100644 index 0000000..a228d5d --- /dev/null +++ b/src/lib/commands/info.sh @@ -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)" +} diff --git a/src/lib/commands/install.sh b/src/lib/commands/install.sh new file mode 100644 index 0000000..d0e821b --- /dev/null +++ b/src/lib/commands/install.sh @@ -0,0 +1,5 @@ +dotify-command-install() { + dotify-set-run-mode "install" + dotify-execute-dotfile + return $? +} diff --git a/src/lib/commands/print-version.sh b/src/lib/commands/print-version.sh new file mode 100644 index 0000000..bfefe96 --- /dev/null +++ b/src/lib/commands/print-version.sh @@ -0,0 +1,3 @@ +dotify-command-print-version() { + echo "dotify $(dotify-command-version)" +} diff --git a/src/lib/commands/uninstall.sh b/src/lib/commands/uninstall.sh new file mode 100644 index 0000000..877426f --- /dev/null +++ b/src/lib/commands/uninstall.sh @@ -0,0 +1,5 @@ +dotify-command-uninstall() { + dotify-set-run-mode "uninstall" + dotify-execute-dotfile + return $? +} diff --git a/src/lib/commands/version.sh b/src/lib/commands/version.sh new file mode 100644 index 0000000..d210eb2 --- /dev/null +++ b/src/lib/commands/version.sh @@ -0,0 +1,3 @@ +dotify-command-version() { + echo "0.0.1" +} diff --git a/src/lib/dotfile-commands/default_action.sh b/src/lib/dotfile-commands/default_action.sh index 29dd19a..a4fb83a 100644 --- a/src/lib/dotfile-commands/default_action.sh +++ b/src/lib/dotfile-commands/default_action.sh @@ -1,3 +1,3 @@ default_action() { - DOTIFY_OPT_DEFAULT_ACTION="$@" + dotify-set-default-action "$@" } diff --git a/src/lib/dotfile-commands/root_link.sh b/src/lib/dotfile-commands/root_link.sh index 8c99358..5b17537 100644 --- a/src/lib/dotfile-commands/root_link.sh +++ b/src/lib/dotfile-commands/root_link.sh @@ -1,3 +1,3 @@ root_link () { - DOTIFY_OPT_ROOT_LINK="$@" + dotify-set-root-link "$@" } diff --git a/src/lib/dotify-clean.sh b/src/lib/dotify-clean.sh deleted file mode 100644 index 23580a2..0000000 --- a/src/lib/dotify-clean.sh +++ /dev/null @@ -1,5 +0,0 @@ -dotify-clean() { - DOTIFY_RUN_MODE="clean" - execute-dotfile - return $? -} diff --git a/src/lib/dotify-compile.sh b/src/lib/dotify-compile.sh deleted file mode 100644 index 57d1283..0000000 --- a/src/lib/dotify-compile.sh +++ /dev/null @@ -1,7 +0,0 @@ -dotify-compile() { - locate-dotfile - if [ "$?" != "0" ]; then return 1; fi - - compile-dotfile "$DOTFILE" - return $? -} diff --git a/src/lib/dotify-help.sh b/src/lib/dotify-help.sh deleted file mode 100644 index 0354990..0000000 --- a/src/lib/dotify-help.sh +++ /dev/null @@ -1,4 +0,0 @@ -dotify-help() { - echo "$(dotify-print-version)" - echo "usage: dotify []" -} diff --git a/src/lib/dotify-info.sh b/src/lib/dotify-info.sh deleted file mode 100644 index 411b20d..0000000 --- a/src/lib/dotify-info.sh +++ /dev/null @@ -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" -} diff --git a/src/lib/dotify-install.sh b/src/lib/dotify-install.sh deleted file mode 100644 index 9ff3b8a..0000000 --- a/src/lib/dotify-install.sh +++ /dev/null @@ -1,5 +0,0 @@ -dotify-install() { - DOTIFY_RUN_MODE="install" - execute-dotfile - return $? -} diff --git a/src/lib/dotify-register-action.sh b/src/lib/dotify-register-action.sh deleted file mode 100644 index 300eeae..0000000 --- a/src/lib/dotify-register-action.sh +++ /dev/null @@ -1,4 +0,0 @@ -dotify-register-action() { - if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi - DOTIFY_ACTIONS+=("$1") -} diff --git a/src/lib/dotify-uninstall.sh b/src/lib/dotify-uninstall.sh deleted file mode 100644 index e845e56..0000000 --- a/src/lib/dotify-uninstall.sh +++ /dev/null @@ -1,5 +0,0 @@ -dotify-uninstall() { - DOTIFY_RUN_MODE="uninstall" - execute-dotfile - return $? -} diff --git a/src/lib/dotify-version.sh b/src/lib/dotify-version.sh deleted file mode 100644 index 0f0479b..0000000 --- a/src/lib/dotify-version.sh +++ /dev/null @@ -1,7 +0,0 @@ -dotify-version() { - echo "0.0.1" -} - -dotify-print-version() { - echo "dotify $(dotify-version)" -} diff --git a/src/lib/dotify-action.sh b/src/lib/internals/action.sh similarity index 66% rename from src/lib/dotify-action.sh rename to src/lib/internals/action.sh index bb65a86..ac2d7d0 100644 --- a/src/lib/dotify-action.sh +++ b/src/lib/internals/action.sh @@ -8,8 +8,10 @@ dotify-action() { local target="$3" fi + dotify-setup-root-link + if [ "$action" == "default" ]; then - action="$DOTIFY_OPT_DEFAULT_ACTION" + action="$(dotify-get-default-action)" fi ! local valid_action="$(command -v "dotify-action-${action}")" @@ -19,8 +21,8 @@ dotify-action() { fi if [ -n "$source" ]; then - dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source" + dotify-action-${action} "$(dotify-get-run-mode)" "$target" "$source" else - dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" + dotify-action-${action} "$(dotify-get-run-mode)" "$target" fi } diff --git a/src/lib/internals/compile-dotfile.sh b/src/lib/internals/compile-dotfile.sh index c1d9298..dc3fe2b 100644 --- a/src/lib/internals/compile-dotfile.sh +++ b/src/lib/internals/compile-dotfile.sh @@ -1,6 +1,6 @@ -compile-dotfile() { +dotify-compile-dotfile() { local dotfile="$1" - if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi + if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi if [ ! -f "$dotfile" ]; then echo "ERROR: \"$dotfile\" does not exist." >&2 diff --git a/src/lib/internals/create-symlink.sh b/src/lib/internals/create-symlink.sh index d16a54a..c22ffb9 100644 --- a/src/lib/internals/create-symlink.sh +++ b/src/lib/internals/create-symlink.sh @@ -1,12 +1,16 @@ -create-symlink() { +dotify-create-symlink() { local source="$1" local target="$2" if [ ! -e "$target" ] && [ ! -h "$target" ]; then ln -s "$source" "$target" + echo "created" return 0 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:" \ "$(readlink "$target")" >&2 return 1 diff --git a/src/lib/internals/execute-dotfile.sh b/src/lib/internals/execute-dotfile.sh index f22559c..3d07881 100644 --- a/src/lib/internals/execute-dotfile.sh +++ b/src/lib/internals/execute-dotfile.sh @@ -1,11 +1,9 @@ -execute-dotfile() { - local dotfile_source="$(dotify-compile)" +dotify-execute-dotfile() { + local dotfile_source="$(dotify-command-compile)" - locate-target + dotify-valid-target-path if [ "$?" != "0" ]; then return 1; fi - ROOT_DIR="$(dirname "$DOTFILE")" - eval "$dotfile_source" return $? } diff --git a/src/lib/internals/has-action.sh b/src/lib/internals/has-action.sh new file mode 100644 index 0000000..7c17236 --- /dev/null +++ b/src/lib/internals/has-action.sh @@ -0,0 +1,5 @@ +dotify-has-action() { + if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then + return 1 + fi +} diff --git a/src/lib/internals/locate-dotfile.sh b/src/lib/internals/locate-dotfile.sh deleted file mode 100644 index b4713b4..0000000 --- a/src/lib/internals/locate-dotfile.sh +++ /dev/null @@ -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 -} diff --git a/src/lib/internals/locate-target.sh b/src/lib/internals/locate-target.sh deleted file mode 100644 index f4a6bd0..0000000 --- a/src/lib/internals/locate-target.sh +++ /dev/null @@ -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 -} diff --git a/src/lib/internals/register-action.sh b/src/lib/internals/register-action.sh new file mode 100644 index 0000000..256bec8 --- /dev/null +++ b/src/lib/internals/register-action.sh @@ -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") +} diff --git a/src/lib/internals/setup-root-link.sh b/src/lib/internals/setup-root-link.sh new file mode 100644 index 0000000..50a5aae --- /dev/null +++ b/src/lib/internals/setup-root-link.sh @@ -0,0 +1,3 @@ +dotify-setup-root-link() { + return 0 +} diff --git a/test/integration/link-action-test.sh b/test/integration/link-action-test.sh new file mode 100755 index 0000000..489ddbf --- /dev/null +++ b/test/integration/link-action-test.sh @@ -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' diff --git a/test/lib/dotify-action-test.sh b/test/lib/internals/action-test.sh similarity index 69% rename from test/lib/dotify-action-test.sh rename to test/lib/internals/action-test.sh index 89d716a..3403856 100755 --- a/test/lib/dotify-action-test.sh +++ b/test/lib/internals/action-test.sh @@ -1,20 +1,15 @@ #! /usr/bin/env bash -source "../test-helper.sh" -source "../../src/lib/dotify-action.sh" +source "../../test-helper.sh" +source "../../../src/lib/internals/action.sh" # # dotify-action() tests # -# Set required environment variables. -DOTIFY_RUN_MODE="install" -DOTIFY_OPT_DEFAULT_ACTION="link" - -# Simple mock for link action. -dotify-action-link() { - echo "link stub: $@" -} - +stub "dotify-setup-root-link" +stub_and_echo "dotify-get-run-mode" "install" +stub_and_echo "dotify-get-default-action" "link" +stub_and_echo "dotify-action-link" "link stub: \$@" # Given a specific action. 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 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()" diff --git a/test/lib/internals/compile-dotfile-test.sh b/test/lib/internals/compile-dotfile-test.sh index 850d8e8..7227a53 100755 --- a/test/lib/internals/compile-dotfile-test.sh +++ b/test/lib/internals/compile-dotfile-test.sh @@ -4,7 +4,7 @@ source "../../../src/lib/helpers/trim.sh" source "../../../src/lib/internals/compile-dotfile.sh" # -# compile-dotfile() tests +# dotify-compile-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. -assert "compile-dotfile $dotfile" "hostname='air'" -assert_raises "compile-dotfile tmp/fake" 1 -assert "compile-dotfile tmp/fake" "" -assert "compile-dotfile tmp/fake 2>&1" "ERROR: \"tmp/fake\" does not exist." +assert "dotify-compile-dotfile $dotfile" "hostname='air'" +assert_raises "dotify-compile-dotfile tmp/fake" 1 +assert "dotify-compile-dotfile tmp/fake" "" +assert "dotify-compile-dotfile tmp/fake 2>&1" \ + "ERROR: \"tmp/fake\" does not exist." -# Compiles file from global $DOTFILE variable. -DOTFILE=$dotfile -assert "compile-dotfile" "hostname='air'" -DOTFILE="tmp/fake" -assert_raises "compile-dotfile" 1 -assert "compile-dotfile" "" -assert "compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist." -unset DOTFILE +# Compiles file from dotify-get-dotfile-path attribute helper. +stub_and_echo "dotify-get-dotfile-path" "$dotfile" +assert "dotify-compile-dotfile" "hostname='air'" +stub_and_echo "dotify-get-dotfile-path" "tmp/fake" +assert_raises "dotify-compile-dotfile" 1 +assert "dotify-compile-dotfile" "" +assert "dotify-compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist." +restore "dotify-get-dotfile-path" # Compiles standard actions echo -e "root_link .dotfiles link: ackrc -> .ackrc 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 gitconfig \".gitconfig\"" @@ -43,7 +44,7 @@ dotify-action link gitconfig \".gitconfig\"" echo -e "root_link .dotfiles link: -> .ackrc 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 \".gitconfig\"" @@ -52,7 +53,7 @@ dotify-action link \".gitconfig\"" echo -e "root_link .dotfiles link: ackrc -> .ackrc 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 default gitconfig \".gitconfig\"" @@ -61,7 +62,7 @@ dotify-action default gitconfig \".gitconfig\"" echo -e "root_link .dotfiles -> .ackrc -> \".gitconfig\"" > $dotfile -assert "compile-dotfile $dotfile" "root_link .dotfiles +assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles dotify-action default .ackrc dotify-action default \".gitconfig\"" @@ -71,7 +72,7 @@ echo -e "root_link .dotfiles if [ true ]; then ackrc -> .ackrc fi" > $dotfile -assert "compile-dotfile $dotfile" "root_link .dotfiles +assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles if [ true ]; then dotify-action default ackrc .ackrc fi" @@ -87,7 +88,7 @@ profile -> .profile # Apps ackrc -> .ackrc 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 ackrc .ackrc dotify-action default gitconfig .gitconfig" @@ -98,4 +99,4 @@ rm "tmp/Dotfile" rmdir "tmp" assert_raises "test -d tmp" 1 -assert_end 'compile-dotfile()' +assert_end 'dotify-compile-dotfile()' diff --git a/test/lib/internals/create-symlink-test.sh b/test/lib/internals/create-symlink-test.sh index 2aabdc7..ec9283e 100755 --- a/test/lib/internals/create-symlink-test.sh +++ b/test/lib/internals/create-symlink-test.sh @@ -3,48 +3,53 @@ source "../../test-helper.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" touch "tmp/source/profile" # 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" 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" -assert_raises 'create-symlink ../source tmp/target/.dotfiles' 0 -assert 'create-symlink ../source tmp/target/.dotfiles 2>&1' "" +assert_raises 'dotify-create-symlink ../source tmp/target/.dotfiles' 0 +assert 'dotify-create-symlink ../source tmp/target/.dotfiles' "exists" +assert 'dotify-create-symlink ../source tmp/target/.dotfiles 2>&1' "exists" 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' \ +assert_raises 'dotify-create-symlink ../source tmp/target/.dotfiles' 1 +assert 'dotify-create-symlink ../source tmp/target/.dotfiles' "" +assert 'dotify-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' \ +assert_raises 'dotify-create-symlink ../source/profile tmp/target/.profile' 1 +assert 'dotify-create-symlink ../source/profile tmp/target/.profile' "" +assert 'dotify-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' \ +assert_raises 'dotify-create-symlink ../source tmp/target' 1 +assert 'dotify-create-symlink ../source tmp/target' "" +assert 'dotify-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()' +assert_end 'dotify-create-symlink()' diff --git a/test/lib/internals/has-action-test.sh b/test/lib/internals/has-action-test.sh new file mode 100755 index 0000000..5565287 --- /dev/null +++ b/test/lib/internals/has-action-test.sh @@ -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" diff --git a/test/lib/internals/locate-dotfile-test.sh b/test/lib/internals/locate-dotfile-test.sh deleted file mode 100755 index 78f0aa6..0000000 --- a/test/lib/internals/locate-dotfile-test.sh +++ /dev/null @@ -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()' diff --git a/test/lib/internals/locate-target-test.sh b/test/lib/internals/locate-target-test.sh deleted file mode 100755 index 18bdfdc..0000000 --- a/test/lib/internals/locate-target-test.sh +++ /dev/null @@ -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()' diff --git a/test/lib/internals/register-action-test.sh b/test/lib/internals/register-action-test.sh new file mode 100755 index 0000000..823ea5b --- /dev/null +++ b/test/lib/internals/register-action-test.sh @@ -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"