From fad6369ba7ebcfffc237e6b47f374caecedb93ec Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 21 Oct 2013 01:30:42 +0100 Subject: [PATCH] Make dotify-action error out when given invalid actions --- src/lib/dotify-action.sh | 6 ++++++ test/lib/dotify-action-test.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 test/lib/dotify-action-test.sh diff --git a/src/lib/dotify-action.sh b/src/lib/dotify-action.sh index cc68398..eb5268c 100644 --- a/src/lib/dotify-action.sh +++ b/src/lib/dotify-action.sh @@ -7,5 +7,11 @@ dotify-action() { action="$DOTIFY_OPT_DEFAULT_ACTION" fi + ! 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" } diff --git a/test/lib/dotify-action-test.sh b/test/lib/dotify-action-test.sh new file mode 100755 index 0000000..85b7f20 --- /dev/null +++ b/test/lib/dotify-action-test.sh @@ -0,0 +1,29 @@ +#! /usr/bin/env bash +source "../test-helper.sh" +source "../../src/lib/dotify-action.sh" + +# +# dotify-action() tests +# + +# Set required option ENV +DOTIFY_OPT_DEFAULT_ACTION="link" + +# Simple mock for link action. +dotify-action-link() { + echo "link stub: $@" +} + + +# Given a specific action. +assert "dotify-action link ackrc .ackrc" "link stub: ackrc .ackrc" + +# Given "default" action, it uses configured default action. +assert "dotify-action default ackrc .ackrc" "link stub: ackrc .ackrc" + +# Given a invalid action. +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." + +assert_end "dotify-action()"