Add capability to generate tasks

This commit is contained in:
The Magician 2023-11-21 13:18:39 +00:00
parent dffaa3a901
commit 0a3f02198f
2 changed files with 34 additions and 1 deletions

View File

@ -12,7 +12,7 @@ taskl() {
} }
check_active_task() { check_active_task() {
taskcount="$(taskl | wc -l)" taskcount="$(taskl +ACTIVE | wc -l)"
if test "$taskcount" -eq 0; then if test "$taskcount" -eq 0; then
return 1 return 1
@ -23,3 +23,17 @@ pick_random_task_id() {
task_id="$(taskl | shuf | head -n 1)" task_id="$(taskl | shuf | head -n 1)"
echo "$task_id" echo "$task_id"
} }
generate_task() {
check_active_task &&
echo "Cannot generate task. You have active tasks." 1>&2 &&
return 1
task_id="$(pick_random_task_id)"
task rc.verbose:nothing "$task_id" start
}
name="$(basename "$0")"
if test "$name" = "generatetask"; then
generate_task
fi

View File

@ -58,3 +58,22 @@ test_pick_random_task_id_picks_random_id() {
assert_equals "$expected" "$result" assert_equals "$expected" "$result"
} }
test_generate_task_exits_1_if_task_active() {
fake check_active_task 'return 0'
generate_task
assert_equals "1" "$?"
}
test_generate_task_starts_random_task_if_no_task_active() {
fake check_active_task 'return 1'
fake pick_random_task_id 'printf 28'
fake task 'echo "task ${FAKE_PARAMS[@]}"'
expected="task rc.verbose:nothing 28 start"
result="$(generate_task)"
assert_equals "$expected" "$result"
}