Update dotfile option parsing and complete tests

This commit is contained in:
2013-07-05 00:57:53 +02:00
parent d8ef9d824d
commit 7594f1bc59
2 changed files with 90 additions and 24 deletions

View File

@@ -1,38 +1,36 @@
# Parse Dotfile options and set relevant global variables.
parse-dotfile-options() { parse-dotfile-options() {
OPT_ROOT_LINK="$(parse-dotfile-root_link-option)" OPT_ROOT_LINK="$(parse-dotfile-root_link-option)"
OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)" OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)"
} }
parse-dotfile-root_link-option() { # Extract a specific option from Dotfile.
local dotfile="$DOTFILE" #
if [ -n "$1" ]; then dotfile="$1"; fi # Arguments:
# - $1: Name of option to extract.
# Set default. # - $2: (optional) Default value of option if not present in Dotfile.
local root_link=".dotfiles" # - $3: (optional) Specific Dotfile to read. Uses $DOTFILE if empty.
#
parse-dotfile-option() {
local name="$1"
local value="$2"
local dotfile="$3"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi
while read line; do while read line; do
if [[ "$line" == "root_link "* ]]; then if [[ "$line" == "$name "* ]]; then
root_link="$(trim "${line/#root_link /}")" value="$(trim "${line/#$name }")"
break break
fi fi
done < "$dotfile" done < "$dotfile"
echo "$root_link" echo "$value"
}
parse-dotfile-root_link-option() {
echo "$(parse-dotfile-option "root_link" ".dotfiles" "$1")"
} }
parse-dotfile-default_action-option() { parse-dotfile-default_action-option() {
local dotfile="$DOTFILE" echo "$(parse-dotfile-option "default_action" "link" "$1")"
if [ -n "$1" ]; then dotfile="$1"; fi
# Set default value.
default_action="link"
while read line; do
if [[ "$line" == "default_action "* ]]; then
default_action="$(trim "${line/#default_action /}")"
break
fi
done < "$dotfile"
echo "$default_action"
} }

View File

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