From e49c93077140ad1feb0af3a9c0e8569aca8fc051 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 8 Mar 2014 13:32:24 +0000 Subject: [PATCH] Make arguments simpler to deal with in action functions was an optional argument, and the second out of three passed, meaning when only two arguments were given, the action functions would have to know to use the second argument as , instead of the the third. Now the second argument is always , and the argument is always the third, if given. --- src/lib/dotify-action.sh | 17 +++++++++++++---- test/lib/dotify-action-test.sh | 14 +++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/dotify-action.sh b/src/lib/dotify-action.sh index eb5268c..bb65a86 100644 --- a/src/lib/dotify-action.sh +++ b/src/lib/dotify-action.sh @@ -1,17 +1,26 @@ dotify-action() { local action="$1" - local source="$2" - local target="$3" + + 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 - ! valid_action="$(command -v "dotify-action-${action}")" + ! 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 - dotify-action-${action} "$source" "$target" + if [ -n "$source" ]; then + dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source" + else + dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" + fi } diff --git a/test/lib/dotify-action-test.sh b/test/lib/dotify-action-test.sh index 85b7f20..89d716a 100755 --- a/test/lib/dotify-action-test.sh +++ b/test/lib/dotify-action-test.sh @@ -6,7 +6,8 @@ source "../../src/lib/dotify-action.sh" # dotify-action() tests # -# Set required option ENV +# Set required environment variables. +DOTIFY_RUN_MODE="install" DOTIFY_OPT_DEFAULT_ACTION="link" # Simple mock for link action. @@ -16,10 +17,17 @@ dotify-action-link() { # Given a specific action. -assert "dotify-action link ackrc .ackrc" "link stub: ackrc .ackrc" +assert "dotify-action link ackrc .ackrc" "link stub: install .ackrc ackrc" + +# Given a specific action without a . +assert "dotify-action link .ackrc" "link stub: install .ackrc" # Given "default" action, it uses configured default action. -assert "dotify-action default ackrc .ackrc" "link stub: ackrc .ackrc" +assert "dotify-action default ackrc .ackrc" "link stub: install .ackrc ackrc" + +# Given "default" action without a , it uses configured default +# action. +assert "dotify-action default .ackrc" "link stub: install .ackrc" # Given a invalid action. assert_raises "dotify-action foo ackrc .ackrc" 1