From f351e17a63ee7742fd78a845b66001cf092f068b Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 31 Jan 2010 23:29:22 -0500 Subject: [PATCH] Enhancement - Hooks - Implemented all command hooks. - Implemented several field hooks. - Implemented several task hooks. - Reorganized event validation code. - Finalized Hooks -> API::call* mechanism. - Implemented several hook unit tests. - Corrected unit tests that didn't specify rc.hooks=on. - Corrected builds that include Lua. --- src/API.cpp | 67 +++++-- src/API.h | 14 +- src/Hooks.cpp | 252 +++++++++++++++++++++----- src/Hooks.h | 16 +- src/command.cpp | 36 ++-- src/custom.cpp | 30 ++- src/report.cpp | 15 +- src/tests/hook.format-id.t | 84 +++++++++ src/tests/hook.format-priority-long.t | 95 ++++++++++ src/tests/hook.format-priority.t | 93 ++++++++++ src/tests/hook.format-uuid.t | 81 +++++++++ src/tests/hook.post-start.t | 1 + src/tests/hook.pre-completed.t | 86 +++++++++ src/tests/hook.pre-exit.t | 1 + src/tests/rc.t | 8 +- 15 files changed, 782 insertions(+), 97 deletions(-) create mode 100755 src/tests/hook.format-id.t create mode 100755 src/tests/hook.format-priority-long.t create mode 100755 src/tests/hook.format-priority.t create mode 100755 src/tests/hook.format-uuid.t create mode 100755 src/tests/hook.pre-completed.t diff --git a/src/API.cpp b/src/API.cpp index a991e413c..b21d645b8 100644 --- a/src/API.cpp +++ b/src/API.cpp @@ -516,10 +516,12 @@ bool API::callProgramHook ( } //////////////////////////////////////////////////////////////////////////////// +// TODO No intention of implementing this before task 2.0. Why? Because we +// need to implement a Lua iterator, in C++, to iterate over a std::vector. bool API::callListHook ( const std::string& file, - const std::string& function/*, - iterator i*/) + const std::string& function, + std::vector & all) { loadFile (file); @@ -535,10 +537,13 @@ bool API::callListHook ( bool API::callTaskHook ( const std::string& file, const std::string& function, - int id) + Task& task) { loadFile (file); + // Save the task for reference via the API. + current = task; + // Get function. lua_getglobal (L, function.c_str ()); if (!lua_isfunction (L, -1)) @@ -548,7 +553,7 @@ bool API::callTaskHook ( } // Prepare args. - lua_pushnumber (L, id); + lua_pushnumber (L, current.id); // Make call. if (lua_pcall (L, 1, 2, 0) != 0) @@ -583,17 +588,57 @@ bool API::callTaskHook ( bool API::callFieldHook ( const std::string& file, const std::string& function, - const std::string& field, - const std::string& value) + const std::string& name, + std::string& value) { loadFile (file); - // TODO Get function. - // TODO Prepare args. - // TODO Make call. - // TODO Get exit status. + // Get function. + lua_getglobal (L, function.c_str ()); + if (!lua_isfunction (L, -1)) + { + lua_pop (L, 1); + throw std::string ("The Lua function '") + function + "' was not found."; + } - return true; + // Prepare args. + lua_pushstring (L, name.c_str ()); + lua_pushstring (L, value.c_str ()); + + // Make call. + if (lua_pcall (L, 2, 3, 0) != 0) + throw std::string ("Error calling '") + function + "' - " + lua_tostring (L, -1); + + // Call successful - get return values. + if (!lua_isstring (L, -3)) + throw std::string ("Error: '") + function + "' did not return a modified value"; + + if (!lua_isnumber (L, -2)) + throw std::string ("Error: '") + function + "' did not return a success indicator"; + + if (!lua_isstring (L, -1) && !lua_isnil (L, -1)) + throw std::string ("Error: '") + function + "' did not return a message or nil"; + + const char* new_value = lua_tostring (L, -3); + int rc = lua_tointeger (L, -2); + const char* message = lua_tostring (L, -1); + + if (rc == 0) + { + // Overwrite with the modified value. + value = new_value; + + if (message) + context.footnote (std::string ("Warning: ") + message); + } + else + { + if (message) + throw std::string (message); + } + + lua_pop (L, 1); + return rc == 0 ? true : false; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/API.h b/src/API.h index ec808085d..067fa60d2 100644 --- a/src/API.h +++ b/src/API.h @@ -32,6 +32,8 @@ #include #include +#include "Task.h" + extern "C" { #include "lua.h" @@ -49,9 +51,9 @@ public: void initialize (); bool callProgramHook (const std::string&, const std::string&); - bool callListHook (const std::string&, const std::string& /*, iterator */); - bool callTaskHook (const std::string&, const std::string&, int); - bool callFieldHook (const std::string&, const std::string&, const std::string&, const std::string&); + bool callListHook (const std::string&, const std::string&, std::vector &); + bool callTaskHook (const std::string&, const std::string&, Task&); + bool callFieldHook (const std::string&, const std::string&, const std::string&, std::string&); private: void loadFile (const std::string&); @@ -59,6 +61,12 @@ private: public: lua_State* L; std::vector loaded; + + // Context for the API. +// std::vector all; + Task current; +// std::string& name; +// std::string& value; }; #endif diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 91e7212e4..41256639c 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -139,14 +139,7 @@ void Hooks::initialize () } //////////////////////////////////////////////////////////////////////////////// -void Hooks::setTaskId (int id) -{ -#ifdef HAVE_LIBLUA - task_id = id; -#endif -} - -//////////////////////////////////////////////////////////////////////////////// +// Program hooks. bool Hooks::trigger (const std::string& event) { #ifdef HAVE_LIBLUA @@ -157,24 +150,14 @@ bool Hooks::trigger (const std::string& event) { Timer timer (std::string ("Hooks::trigger ") + event); - bool rc = true; - std::string type; - if (eventType (event, type)) + if (validProgramEvent (event)) { context.debug (std::string ("Event ") + event + " triggered"); - - // Figure out where to get the calling-context info from. - if (type == "program") rc = api.callProgramHook (it->file, it->function); - else if (type == "list") rc = api.callListHook (it->file, it->function/*, tasks*/); - else if (type == "task") rc = api.callTaskHook (it->file, it->function, task_id); - else if (type == "field") rc = api.callFieldHook (it->file, it->function, "field", "value"); + if (! api.callProgramHook (it->file, it->function)) + return false; } else throw std::string ("Unrecognized hook event '") + event + "'"; - - // If any hook returns false, stop. - if (!rc) - return false; } } #endif @@ -183,43 +166,212 @@ bool Hooks::trigger (const std::string& event) } //////////////////////////////////////////////////////////////////////////////// -bool Hooks::eventType (const std::string& event, std::string& type) +// List hooks. +bool Hooks::trigger (const std::string& event, std::vector & tasks) { - if (event == "post-start" || - event == "pre-exit" || - event == "pre-debug" || event == "post-debug" || - event == "pre-header" || event == "post-header" || - event == "pre-footnote" || event == "post-footnote" || - event == "pre-output" || event == "post-output" || - event == "pre-dispatch" || event == "post-dispatch" || - event == "pre-gc" || event == "post-gc" || - event == "pre-undo" || event == "post-undo" || - event == "pre-file-lock" || event == "post-file-lock" || - event == "pre-add-command" || event == "post-add-command" || - event == "pre-delete-command" || event == "post-delete-command" || - event == "pre-info-command" || event == "post-info-command") +#ifdef HAVE_LIBLUA + std::vector ::iterator it; + for (it = all.begin (); it != all.end (); ++it) { - type = "program"; - return true; + if (it->event == event) + { + Timer timer (std::string ("Hooks::trigger ") + event); + + if (validListEvent (event)) + { + context.debug (std::string ("Event ") + event + " triggered"); + if (! api.callListHook (it->file, it->function, tasks)) + return false; + } + else + throw std::string ("Unrecognized hook event '") + event + "'"; + } } - else if (event == "?") +#endif + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// Task hooks. +bool Hooks::trigger (const std::string& event, Task& task) +{ +#ifdef HAVE_LIBLUA + std::vector ::iterator it; + for (it = all.begin (); it != all.end (); ++it) { - type = "list"; - return true; + if (it->event == event) + { + Timer timer (std::string ("Hooks::trigger ") + event); + + if (validTaskEvent (event)) + { + context.debug (std::string ("Event ") + event + " triggered"); + if (! api.callTaskHook (it->file, it->function, task)) + return false; + } + else + throw std::string ("Unrecognized hook event '") + event + "'"; + } } - else if (event == "pre-tag" || event == "post-tag" || - event == "pre-detag" || event == "post-detag" || - event == "pre-delete" || event == "post-delete" || - event == "pre-completed" || event == "post-completed") +#endif + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// Field hooks. +bool Hooks::trigger ( + const std::string& event, + const std::string& name, + std::string& value) +{ +#ifdef HAVE_LIBLUA + std::vector ::iterator it; + for (it = all.begin (); it != all.end (); ++it) { - type = "task"; - return true; + if (it->event == event) + { + Timer timer (std::string ("Hooks::trigger ") + event); + + if (validFieldEvent (event)) + { + context.debug (std::string ("Event ") + event + " triggered"); + if (! api.callFieldHook (it->file, it->function, name, value)) + return false; + } + else + throw std::string ("Unrecognized hook event '") + event + "'"; + } } - else if (event == "?") - { - type = "field"; +#endif + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::validProgramEvent (const std::string& event) +{ + if (event == "post-start" || + event == "pre-fatal-error" || + event == "pre-exit" || + event == "pre-command-line" || event == "post-command-line" || + event == "pre-command-line-override" || event == "post-command-line-override" || + event == "pre-config-create" || event == "post-config-create" || + event == "pre-config-read" || event == "post-config-read" || + event == "pre-config-value-read" || event == "post-config-value-read" || + event == "pre-config-value-write" || event == "post-config-value-write" || + event == "pre-file-lock" || event == "post-file-lock" || + event == "pre-file-unlock" || event == "post-file-unlock" || + event == "pre-file-read" || event == "post-file-read" || + event == "pre-file-write" || event == "post-file-write" || + event == "pre-output" || event == "post-output" || + event == "pre-debug" || event == "post-debug" || + event == "pre-header" || event == "post-header" || + event == "pre-footnote" || event == "post-footnote" || + event == "pre-dispatch" || event == "post-dispatch" || + event == "pre-gc" || event == "post-gc" || + event == "pre-archive" || event == "post-archive" || + event == "pre-purge" || event == "post-purge" || + event == "pre-recurrence" || event == "post-recurrence" || + event == "pre-interactive" || event == "post-interactive" || + event == "pre-undo" || event == "post-undo" || + event == "pre-confirm" || event == "post-confirm" || + event == "pre-shell-prompt" || event == "post-shell-prompt" || + event == "pre-add-command" || event == "post-add-command" || + event == "pre-annotate-command" || event == "post-annotate-command" || + event == "pre-append-command" || event == "post-append-command" || + event == "pre-calendar-command" || event == "post-calendar-command" || + event == "pre-color-command" || event == "post-color-command" || + event == "pre-config-command" || event == "post-config-command" || + event == "pre-custom-report-command" || event == "post-custom-report-command" || + event == "pre-default-command" || event == "post-default-command" || + event == "pre-delete-command" || event == "post-delete-command" || + event == "pre-done-command" || event == "post-done-command" || + event == "pre-duplicate-command" || event == "post-duplicate-command" || + event == "pre-edit-command" || event == "post-edit-command" || + event == "pre-export-command" || event == "post-export-command" || + event == "pre-ghistory-command" || event == "post-ghistory-command" || + event == "pre-history-command" || event == "post-history-command" || + event == "pre-import-command" || event == "post-import-command" || + event == "pre-info-command" || event == "post-info-command" || + event == "pre-prepend-command" || event == "post-prepend-command" || + event == "pre-projects-command" || event == "post-projects-command" || + event == "pre-shell-command" || event == "post-shell-command" || + event == "pre-start-command" || event == "post-start-command" || + event == "pre-stats-command" || event == "post-stats-command" || + event == "pre-stop-command" || event == "post-stop-command" || + event == "pre-summary-command" || event == "post-summary-command" || + event == "pre-tags-command" || event == "post-tags-command" || + event == "pre-timesheet-command" || event == "post-timesheet-command" || + event == "pre-undo-command" || event == "post-undo-command" || + event == "pre-usage-command" || event == "post-usage-command" || + event == "pre-version-command" || event == "post-version-command") + return true; + + return false; +} + +bool Hooks::validListEvent (const std::string& event) +{ + if (event == "pre-filter" || event == "post-filter") + return true; + + return false; +} + +bool Hooks::validTaskEvent (const std::string& event) +{ + if (event == "pre-display" || + event == "pre-modification" || event == "post-modification" || + event == "pre-delete" || event == "post-delete" || + event == "pre-add" || event == "post-add" || + event == "pre-undo" || event == "post-undo" || + event == "pre-wait" || event == "post-wait" || + event == "pre-unwait" || event == "post-unwait" || + event == "pre-completed" || event == "post-completed" || + event == "pre-priority-change" || event == "post-priority-change" || + event == "pre-project-change" || event == "post-project-change" || + event == "pre-substitution" || event == "post-substitution" || + event == "pre-annotation" || event == "post-annotation" || + event == "pre-tag" || event == "post-tag" || + event == "pre-detag" || event == "post-detag" || + event == "pre-colorization" || event == "post-colorization") + return true; + + return false; +} + +bool Hooks::validFieldEvent (const std::string& event) +{ + if ( + event == "format-number" || + event == "format-date" || + event == "format-duration" || + event == "format-text" || + event == "format-id" || + event == "format-uuid" || + event == "format-project" || + event == "format-priority" || + event == "format-priority_long" || + event == "format-entry" || + event == "format-entry_time" || + event == "format-start" || + event == "format-start_time" || + event == "format-end" || + event == "format-end_time" || + event == "format-due" || + event == "format-age" || + event == "format-age_compact" || + event == "format-active" || + event == "format-tags" || + event == "format-recur" || + event == "format-recurrence_indicator" || + event == "format-tag_indicator" || + event == "format-description_only" || + event == "format-description" || + event == "format-wait") return true; - } return false; } diff --git a/src/Hooks.h b/src/Hooks.h index 43c809244..02b30e379 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -58,22 +58,22 @@ public: void initialize (); - void setTaskId (int); -// void setField (const std::string&, const std::string&); -// void setTaskList (const std::vector &); - bool trigger (const std::string&); + bool trigger (const std::string&); // Program + bool trigger (const std::string&, std::vector &); // List + bool trigger (const std::string&, Task&); // Task + bool trigger (const std::string&, const std::string&, std::string&); // Field private: - bool eventType (const std::string&, std::string&); + bool validProgramEvent (const std::string&); + bool validListEvent (const std::string&); + bool validTaskEvent (const std::string&); + bool validFieldEvent (const std::string&); private: #ifdef HAVE_LIBLUA API api; #endif std::vector all; // All current hooks. -#ifdef HAVE_LIBLUA - int task_id; -#endif }; #endif diff --git a/src/command.cpp b/src/command.cpp index 90916054d..a56bfefa9 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -545,17 +545,22 @@ int handleConfig (std::string &outs) { std::stringstream out; + // Obtain the arguments from the description. That way, things like '--' + // have already been handled. + std::vector args; + split (args, context.task.get ("description"), ' '); + // Support: // task config name value # set name to value // task config name "" # set name to blank // task config name # remove name - if (context.args.size () >= 2) + if (args.size () > 0) { - std::string name = context.args[1]; + std::string name = args[0]; std::string value = ""; - if (context.args.size () >= 3) - value = context.args[2]; + if (args.size () > 1) + value = args[1]; if (name != "") { @@ -567,7 +572,8 @@ int handleConfig (std::string &outs) // task config name value // task config name "" - if (context.args.size () >= 3) + if (args.size () > 1 || + context.args[context.args.size () - 1] == "") { // Find existing entry & overwrite std::string::size_type pos = contents.find (name + "="); @@ -891,8 +897,7 @@ int handleDelete (std::string &outs) foreach (task, tasks) { - context.hooks.setTaskId (task->id); - if (context.hooks.trigger ("pre-delete")) + if (context.hooks.trigger ("pre-delete", *task)) { std::stringstream question; question << "Permanently delete task " @@ -968,7 +973,7 @@ int handleDelete (std::string &outs) rc = 1; } - context.hooks.trigger ("post-delete"); + context.hooks.trigger ("post-delete", *task); } } @@ -1148,8 +1153,7 @@ int handleDone (std::string &outs) if (taskDiff (before, *task)) { - context.hooks.setTaskId (task->id); - if (context.hooks.trigger ("pre-completed")) + if (context.hooks.trigger ("pre-completed", *task)) { if (permission.confirmed (before, taskDifferences (before, *task) + "Proceed with change?")) { @@ -1164,9 +1168,8 @@ int handleDone (std::string &outs) << std::endl; ++count; + context.hooks.trigger ("post-completed", *task); } - - context.hooks.trigger ("post-completed"); } else continue; @@ -1914,28 +1917,27 @@ int deltaDescription (Task& task) int deltaTags (Task& task) { int changes = 0; - context.hooks.setTaskId (task.id); // Apply or remove tags, if any. std::vector tags; context.task.getTags (tags); foreach (tag, tags) { - if (context.hooks.trigger ("pre-tag")) + if (context.hooks.trigger ("pre-tag", task)) { task.addTag (*tag); ++changes; - context.hooks.trigger ("post-tag"); + context.hooks.trigger ("post-tag", task); } } foreach (tag, context.tagRemovals) { - if (context.hooks.trigger ("pre-detag")) + if (context.hooks.trigger ("pre-detag", task)) { task.removeTag (*tag); ++changes; - context.hooks.trigger ("post-detag"); + context.hooks.trigger ("post-detag", task); } } diff --git a/src/custom.cpp b/src/custom.cpp index 0a7533faa..d0f766620 100644 --- a/src/custom.cpp +++ b/src/custom.cpp @@ -182,12 +182,24 @@ int runCustomReport ( table.setColumnWidth (columnCount, Table::minimum); table.setColumnJustification (columnCount, Table::right); + char s[16]; + std::string value; int row = 0; foreach (task, tasks) + { if (task->id != 0) - table.addCell (row++, columnCount, task->id); + { + sprintf (s, "%d", (int) task->id); + value = s; + } else - table.addCell (row++, columnCount, "-"); + { + value = "-"; + } + + context.hooks.trigger ("format-id", "id", value); + table.addCell (row++, columnCount, value); + } } else if (*col == "uuid") @@ -196,9 +208,14 @@ int runCustomReport ( table.setColumnWidth (columnCount, Table::minimum); table.setColumnJustification (columnCount, Table::left); + std::string value; int row = 0; foreach (task, tasks) - table.addCell (row++, columnCount, task->get ("uuid")); + { + value = task->get ("uuid"); + context.hooks.trigger ("format-uuid", "uuid", value); + table.addCell (row++, columnCount, value); + } } else if (*col == "project") @@ -220,7 +237,11 @@ int runCustomReport ( int row = 0; foreach (task, tasks) - table.addCell (row++, columnCount, task->get ("priority")); + { + std::string value = task->get ("priority"); + context.hooks.trigger ("format-priority", "priority", value); + table.addCell (row++, columnCount, value); + } } else if (*col == "priority_long") @@ -239,6 +260,7 @@ int runCustomReport ( else if (pri == "M") pri = "Medium"; // TODO i18n else if (pri == "L") pri = "Low"; // TODO i18n + context.hooks.trigger ("format-priority_long", "priority", pri); table.addCell (row++, columnCount, pri); } } diff --git a/src/report.cpp b/src/report.cpp index 243b58c99..195f86d8d 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -348,9 +348,14 @@ int handleInfo (std::string &outs) table.setColumnJustification (1, Table::left); Date now; + char svalue[12]; + std::string value; int row = table.addRow (); table.addCell (row, 0, "ID"); - table.addCell (row, 1, task->id); + sprintf (svalue, "%d", (int) task->id); + value = svalue; + context.hooks.trigger ("format-id", "id", value); + table.addCell (row, 1, value); std::string status = ucFirst (Task::statusToText (task->getStatus ())); @@ -373,7 +378,9 @@ int handleInfo (std::string &outs) { row = table.addRow (); table.addCell (row, 0, "Priority"); - table.addCell (row, 1, task->get ("priority")); + value = task->get ("priority"); + context.hooks.trigger ("format-priority", "priority", value); + table.addCell (row, 1, value); } if (task->getStatus () == Task::recurring || @@ -477,7 +484,9 @@ int handleInfo (std::string &outs) // uuid row = table.addRow (); table.addCell (row, 0, "UUID"); - table.addCell (row, 1, task->get ("uuid")); + value = task->get ("uuid"); + context.hooks.trigger ("format-uuid", "uuid", value); + table.addCell (row, 1, value); // entry row = table.addRow (); diff --git a/src/tests/hook.format-id.t b/src/tests/hook.format-id.t new file mode 100755 index 000000000..5a28722fe --- /dev/null +++ b/src/tests/hook.format-id.t @@ -0,0 +1,84 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2010, Paul Beckingham. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 8; + +# Create the rc file. +if (open my $fh, '>', 'hook.rc') +{ + print $fh "data.location=.\n", + "hooks=on\n", + "hook.format-id=" . $ENV{'PWD'} . "/hook:priority\n"; + close $fh; + ok (-r 'hook.rc', 'Created hook.rc'); +} + +# Create the hook functions. +if (open my $fh, '>', 'hook') +{ + print $fh "function priority (name, value)\n", + " value = '(' .. value .. ')'\n", + " return value, 0, nil\n", + "end\n"; + close $fh; + ok (-r 'hook', 'Created hook'); +} + +my $output = qx{../task rc:hook.rc version}; +if ($output =~ /PUC-Rio/) +{ + qx{../task rc:hook.rc add foo}; + qx{../task rc:hook.rc add bar}; + $output = qx{../task rc:hook.rc ls}; + + like ($output, qr/\(1\)\s+foo/, 'format-id hook 1 -> (1)'); + like ($output, qr/\(2\)\s+bar/, 'format-id hook 2 -> (2)'); +} +else +{ + pass ('format-id hook 1 -> (1) - skip: no Lua support'); + pass ('format-id hook 2 -> (2) - skip: no Lua support'); +} + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'hook'; +ok (!-r 'hook', 'Removed hook'); + +unlink 'hook.rc'; +ok (!-r 'hook.rc', 'Removed hook.rc'); + +exit 0; + diff --git a/src/tests/hook.format-priority-long.t b/src/tests/hook.format-priority-long.t new file mode 100755 index 000000000..b22e253a7 --- /dev/null +++ b/src/tests/hook.format-priority-long.t @@ -0,0 +1,95 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2010, Paul Beckingham. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 9; + +# Create the rc file. +if (open my $fh, '>', 'hook.rc') +{ + print $fh "data.location=.\n", + "hooks=on\n", + "hook.format-priority_long=" . $ENV{'PWD'} . "/hook:priority\n", + "report.ls.columns=id,project,priority_long,description\n", + "report.ls.sort=priority_long-,project+\n"; + close $fh; + ok (-r 'hook.rc', 'Created hook.rc'); +} + +# Create the hook functions. +if (open my $fh, '>', 'hook') +{ + print $fh "function priority (name, value)\n", + " if value == 'High' then\n", + " value = '^^^^'\n", + " elseif value == 'Medium' then\n", + " value = '===='\n", + " elseif value == 'Low' then\n", + " value = 'vvvv'\n", + " end\n", + " return value, 0, nil\n", + "end\n"; + close $fh; + ok (-r 'hook', 'Created hook'); +} + +my $output = qx{../task rc:hook.rc version}; +if ($output =~ /PUC-Rio/) +{ + qx{../task rc:hook.rc add foo pri:H}; + qx{../task rc:hook.rc add bar pri:M}; + qx{../task rc:hook.rc add baz pri:L}; + $output = qx{../task rc:hook.rc ls}; + + like ($output, qr/\^\^\^\^\s+foo/, 'format-priority_long hook High -> ^^^^'); + like ($output, qr/====\s+bar/, 'format-priority_long hook Medium -> ===='); + like ($output, qr/vvvv\s+baz/, 'format-priority_long hook Low -> vvvv'); +} +else +{ + pass ('format-priority_long hook High -> ^^^^ - skip: no Lua support'); + pass ('format-priority_long hook Medium -> ==== - skip: no Lua support'); + pass ('format-priority_long hook Low -> vvvv - skip: no Lua support'); +} + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'hook'; +ok (!-r 'hook', 'Removed hook'); + +unlink 'hook.rc'; +ok (!-r 'hook.rc', 'Removed hook.rc'); + +exit 0; + diff --git a/src/tests/hook.format-priority.t b/src/tests/hook.format-priority.t new file mode 100755 index 000000000..37703f70b --- /dev/null +++ b/src/tests/hook.format-priority.t @@ -0,0 +1,93 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2010, Paul Beckingham. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 9; + +# Create the rc file. +if (open my $fh, '>', 'hook.rc') +{ + print $fh "data.location=.\n", + "hooks=on\n", + "hook.format-priority=" . $ENV{'PWD'} . "/hook:priority\n"; + close $fh; + ok (-r 'hook.rc', 'Created hook.rc'); +} + +# Create the hook functions. +if (open my $fh, '>', 'hook') +{ + print $fh "function priority (name, value)\n", + " if value == 'H' then\n", + " value = 'Hi'\n", + " elseif value == 'M' then\n", + " value = 'Me'\n", + " elseif value == 'L' then\n", + " value = 'Lo'\n", + " end\n", + " return value, 0, nil\n", + "end\n"; + close $fh; + ok (-r 'hook', 'Created hook'); +} + +my $output = qx{../task rc:hook.rc version}; +if ($output =~ /PUC-Rio/) +{ + qx{../task rc:hook.rc add foo pri:H}; + qx{../task rc:hook.rc add bar pri:M}; + qx{../task rc:hook.rc add baz pri:L}; + $output = qx{../task rc:hook.rc ls}; + + like ($output, qr/Hi\s+foo/, 'format-priority hook H -> Hi'); + like ($output, qr/Me\s+bar/, 'format-priority hook M -> Me'); + like ($output, qr/Lo\s+baz/, 'format-priority hook L -> Lo'); +} +else +{ + pass ('format-priority hook H -> Hi - skip: no Lua support'); + pass ('format-priority hook M -> Me - skip: no Lua support'); + pass ('format-priority hook L -> Lo - skip: no Lua support'); +} + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'hook'; +ok (!-r 'hook', 'Removed hook'); + +unlink 'hook.rc'; +ok (!-r 'hook.rc', 'Removed hook.rc'); + +exit 0; + diff --git a/src/tests/hook.format-uuid.t b/src/tests/hook.format-uuid.t new file mode 100755 index 000000000..7180ad6df --- /dev/null +++ b/src/tests/hook.format-uuid.t @@ -0,0 +1,81 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2010, Paul Beckingham. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 7; + +# Create the rc file. +if (open my $fh, '>', 'hook.rc') +{ + print $fh "data.location=.\n", + "hooks=on\n", + "hook.format-uuid=" . $ENV{'PWD'} . "/hook:uuid\n"; + close $fh; + ok (-r 'hook.rc', 'Created hook.rc'); +} + +# Create the hook functions. +if (open my $fh, '>', 'hook') +{ + print $fh "function uuid (name, value)\n", + " value = '<' .. value .. '>'\n", + " return value, 0, nil\n", + "end\n"; + close $fh; + ok (-r 'hook', 'Created hook'); +} + +my $output = qx{../task rc:hook.rc version}; +if ($output =~ /PUC-Rio/) +{ + qx{../task rc:hook.rc add foo}; + $output = qx{../task rc:hook.rc info 1}; + + like ($output, qr/UUID\s+<[0-9a-f-]+>/, 'format-uuid hook uuid -> '); +} +else +{ + pass ('format-uuid hook uuid -> - skip: no Lua support'); +} + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'hook'; +ok (!-r 'hook', 'Removed hook'); + +unlink 'hook.rc'; +ok (!-r 'hook.rc', 'Removed hook.rc'); + +exit 0; + diff --git a/src/tests/hook.post-start.t b/src/tests/hook.post-start.t index cdd30516b..5c4427473 100755 --- a/src/tests/hook.post-start.t +++ b/src/tests/hook.post-start.t @@ -34,6 +34,7 @@ use Test::More tests => 7; if (open my $fh, '>', 'hook.rc') { print $fh "data.location=.\n", + "hooks=on\n", "hook.post-start=" . $ENV{'PWD'} . "/hook:test\n"; close $fh; ok (-r 'hook.rc', 'Created hook.rc'); diff --git a/src/tests/hook.pre-completed.t b/src/tests/hook.pre-completed.t new file mode 100755 index 000000000..214849160 --- /dev/null +++ b/src/tests/hook.pre-completed.t @@ -0,0 +1,86 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2010, Paul Beckingham. +## All rights reserved. +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, write to the +## +## Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, +## Boston, MA +## 02110-1301 +## USA +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 8; + +# Create the rc file. +if (open my $fh, '>', 'hook.rc') +{ + print $fh "data.location=.\n", + "hooks=on\n"; + close $fh; + ok (-r 'hook.rc', 'Created hook.rc'); +} + +# Create the hook functions. +if (open my $fh, '>', 'hook') +{ + print $fh "function good () print ('marker') return 0, nil end\n", + "function bad () print ('marker') return 1, 'disallowed' end\n"; + close $fh; + ok (-r 'hook', 'Created hook'); +} + +my $output = qx{../task rc:hook.rc version}; +if ($output =~ /PUC-Rio/) +{ + my $good = $ENV{'PWD'} . '/hook:good'; + my $bad = $ENV{'PWD'} . '/hook:bad'; + + qx{echo 'y'|../task rc:hook.rc config -- hook.pre-completed "$bad"}; + qx{../task rc:hook.rc add foo}; + $output = qx{../task rc:hook.rc done 1}; + like ($output, qr/disallowed/, 'pre-completed hook rejected completion'); + + qx{echo 'y'|../task rc:hook.rc config -- hook.pre-completed "$good"}; + $output = qx{../task rc:hook.rc done 1}; + like ($output, qr/Marked 1 task as done/, 'pre-completed hook allowed completion'); +} +else +{ + pass ('pre-complete hook rejected completion - skip: no Lua support'); + pass ('pre-complete hook allowed completion - skip: no Lua support'); +} + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'undo.data'; +ok (!-r 'undo.data', 'Removed undo.data'); + +unlink 'hook'; +ok (!-r 'hook', 'Removed hook'); + +unlink 'hook.rc'; +ok (!-r 'hook.rc', 'Removed hook.rc'); + +exit 0; + diff --git a/src/tests/hook.pre-exit.t b/src/tests/hook.pre-exit.t index 07d2fa17b..45f97ec90 100755 --- a/src/tests/hook.pre-exit.t +++ b/src/tests/hook.pre-exit.t @@ -34,6 +34,7 @@ use Test::More tests => 7; if (open my $fh, '>', 'hook.rc') { print $fh "data.location=.\n", + "hooks=on\n", "hook.pre-exit=" . $ENV{'PWD'} . "/hook:test\n"; close $fh; ok (-r 'hook.rc', 'Created hook.rc'); diff --git a/src/tests/rc.t b/src/tests/rc.t index 38faee75b..5470225c4 100755 --- a/src/tests/rc.t +++ b/src/tests/rc.t @@ -29,7 +29,7 @@ use strict; use warnings; use File::Path; -use Test::More tests => 12; +use Test::More tests => 13; # Create the rc file, using rc.name:value. unlink 'foo.rc'; @@ -68,6 +68,12 @@ qx{echo 'y'|../task rc:foo.rc config must_be_unique}; $output = qx{../task rc:foo.rc config}; unlike ($output, qr/^must_be_unique/ms, 'config removing a value'); +# 'report.:b' is designed to get past the config command checks for recognized +# names. +qx{echo 'y'|../task rc:foo.rc config -- report.:b +c}; +$output = qx{../task rc:foo.rc config}; +like ($output, qr/^report\.:b\s+\+c/ms, 'the -- operator is working'); + rmtree 'foo', 0, 0; ok (!-r 'foo', 'Removed foo');