Skip to content

Commit

Permalink
Allow for placing addons in subfolders.
Browse files Browse the repository at this point in the history
Addons can be placed in subfolders with the same name as the action,
in order to be able to e.g. clone git repos into the TODO_ACTIONS_DIR
rather than having to download addons and manage updates manually.

Closes #120
  • Loading branch information
nthorne authored and ginatrapani committed Dec 6, 2013
1 parent 07b50a0 commit 7c92f46
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tests/actions-test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ echo "custom action $1"
EOF
chmod +x ".todo.actions.d/$1"
}

make_action_in_folder()
{
unset TODO_ACTIONS_DIR
[ -d .todo.actions.d ] || mkdir .todo.actions.d
mkdir .todo.actions.d/$1
cat > ".todo.actions.d/$1/$1" <<EOF
#!/bin/bash
[ "\$1" = "usage" ] && {
echo " $1 ITEM#[, ITEM#, ...] [TERM...]"
echo " This custom action does $1."
echo ""
exit
}
echo "custom action $1 in folder $1"
EOF
chmod +x ".todo.actions.d/$1/$1"
}
26 changes: 22 additions & 4 deletions tests/t6050-completion-addons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ readonly ACTIONS='add a addto addm append app archive command del rm depri dp do
readonly OPTIONS='-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x'

readonly ADDONS='bar baz foobar'

readonly CONTAINED='xeno zoolander'
makeCustomActions()
{
set -e
Expand All @@ -31,12 +33,28 @@ makeCustomActions()
chmod -x "$datafile"
[ -x "$datafile" ] && rm "$datafile" # Some file systems may always make files executable; then, skip this check.

# Add an executable file in a folder with the same name as the file,
# in order to ensure completion
for contained in $CONTAINED
do
mkdir "${1}/$contained"
> "${1}/$contained/$contained"
chmod u+x "${1}/$contained/$contained"
done

set +e
}
removeCustomActions()
{
set -e
rmdir "${1}/subdir"

for contained in $CONTAINED
do
rm "${1}/$contained/$contained"
rmdir "${1}/$contained"
done

rm "${1:?}/"*
rmdir "$1"
set +e
Expand All @@ -46,8 +64,8 @@ removeCustomActions()
# Test resolution of the default TODO_ACTIONS_DIR.
#
makeCustomActions "$HOME/.todo.actions.d"
test_todo_completion 'all arguments' 'todo.sh ' "$ACTIONS $ADDONS $OPTIONS"
test_todo_completion 'all arguments after option' 'todo.sh -a ' "$ACTIONS $ADDONS $OPTIONS"
test_todo_completion 'all arguments' 'todo.sh ' "$ACTIONS $ADDONS $CONTAINED $OPTIONS"
test_todo_completion 'all arguments after option' 'todo.sh -a ' "$ACTIONS $ADDONS $CONTAINED $OPTIONS"
test_todo_completion 'all arguments beginning with b' 'todo.sh b' 'bar baz'
test_todo_completion 'all arguments beginning with f after options' 'todo.sh -a -v f' 'foobar'
test_todo_completion 'nothing after addon action' 'todo.sh foobar ' ''
Expand All @@ -58,7 +76,7 @@ removeCustomActions "$HOME/.todo.actions.d"
#
mkdir "$HOME/.todo"
makeCustomActions "$HOME/.todo/actions"
test_todo_completion 'all arguments with actions from .todo/actions/' 'todo.sh ' "$ACTIONS $ADDONS $OPTIONS"
test_todo_completion 'all arguments with actions from .todo/actions/' 'todo.sh ' "$ACTIONS $ADDONS $CONTAINED $OPTIONS"
removeCustomActions "$HOME/.todo/actions"

#
Expand All @@ -68,7 +86,7 @@ makeCustomActions "$HOME/addons"
cat >> todo.cfg <<'EOF'
export TODO_ACTIONS_DIR="$HOME/addons"
EOF
test_todo_completion 'all arguments with actions from addons/' 'todo.sh ' "$ACTIONS $ADDONS $OPTIONS"
test_todo_completion 'all arguments with actions from addons/' 'todo.sh ' "$ACTIONS $ADDONS $CONTAINED $OPTIONS"
removeCustomActions "$HOME/addons"

test_done
40 changes: 40 additions & 0 deletions tests/t8010-listaddons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,44 @@ ls
quux
EOF

make_action_in_folder "chuck"
# Add a bit of cruft in the action folders in order to ensure that we only
# care about the executables with the same name as the folder in which they
# reside.
touch .todo.actions.d/chuck/mc_hammer # can't touch this
chmod u+x .todo.actions.d/chuck/mc_hammer # better run, better run run
touch .todo.actions.d/chuck/README

make_action_in_folder "norris"

test_todo_session 'custom actions in subfolders' <<EOF
>>> test -f .todo.actions.d/chuck/README
=== 0
>>> test -x .todo.actions.d/chuck/mc_hammer
=== 0
>>> todo.sh listaddons
bar
chuck
ls
norris
quux
EOF

# nthorne: shamelessly stolen from above..
chmod -x .todo.actions.d/norris/norris
# On Cygwin, clearing the executable flag may have no effect, as the Windows ACL
# may still grant execution rights. In this case, we skip the test.
if [ -x .todo.actions.d/norris/norris ]; then
SKIP_TESTS="${SKIP_TESTS}${SKIP_TESTS+ }t8010.8"
fi
test_todo_session 'nonexecutable action in subfolder' <<EOF
>>> todo.sh listaddons
bar
chuck
ls
quux
EOF

test_done
16 changes: 16 additions & 0 deletions todo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ addonHelp()
didPrintAddonActionsHeader=1
fi
"$action" usage
elif [ -d "$action" -a -x "$action/$(basename $action)" ]; then
if [ ! "$didPrintAddonActionsHeader" ]; then
cat <<-EndAddonActionsHeader
Add-on Actions:
EndAddonActionsHeader
didPrintAddonActionsHeader=1
fi
"$action/$(basename $action)" usage
fi
done
fi
Expand All @@ -314,6 +322,8 @@ actionUsage()
action="${TODO_ACTIONS_DIR}/${actionName}"
if [ -f "$action" -a -x "$action" ]; then
"$action" usage
elif [ -d "$action" -a -x "$action/$(basename $action)" ]; then
"$action/$(basename $action)" usage
else
builtinActionUsage=$(actionsHelp | sed -n -e "/^ ${actionName//\//\\/} /,/^\$/p" -e "/^ ${actionName//\//\\/}$/,/^\$/p")
if [ "$builtinActionUsage" ]; then
Expand Down Expand Up @@ -964,6 +974,10 @@ then
shift
## Reset action to new first argument
action=$( printf "%s\n" "$1" | tr 'A-Z' 'a-z' )
elif [ -d "$TODO_ACTIONS_DIR/$action" -a -x "$TODO_ACTIONS_DIR/$action/$action" ]
then
"$TODO_ACTIONS_DIR/$action/$action" "$@"
exit $?
elif [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ]
then
"$TODO_ACTIONS_DIR/$action" "$@"
Expand Down Expand Up @@ -1405,6 +1419,8 @@ note: PRIORITY must be anywhere from A to Z."
do
if [ -f "$action" -a -x "$action" ]; then
echo "$action"
elif [ -d "$action" -a -x "$action/$action" ]; then
echo "$action"
fi
done
fi
Expand Down

0 comments on commit 7c92f46

Please sign in to comment.