Bug - import.yaml

- Fixed bug in import for YAML and TODO that failed to recognize that
  a task was completed or deleted, and consequently set a Task::pending
  status.
- Enhanced unit tests to verify this.
This commit is contained in:
Paul Beckingham 2010-08-08 11:06:58 -04:00
parent 41f2520094
commit 5b8dbd8ff1
3 changed files with 66 additions and 10 deletions

View file

@ -457,9 +457,18 @@ const std::vector <Task>& TDB::getAllModified ()
// Note: mLocations[0] is where all tasks are written. // Note: mLocations[0] is where all tasks are written.
void TDB::add (const Task& task) void TDB::add (const Task& task)
{ {
mNew.push_back (task); Task t (task);
mI2U[task.id] = task.get ("uuid"); if (task.get ("uuid") == "")
mU2I[task.get ("uuid")] = task.id; {
std::string unique = ::uuid ();
t.set ("uuid", unique);
}
else
t.set ("uuid", task.get ("uuid"));
mNew.push_back (t);
mI2U[task.id] = t.get ("uuid");
mU2I[task.get ("uuid")] = t.id;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -171,9 +171,12 @@ static fileType determineFileType (const std::vector <std::string>& lines)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void decorateTask (Task& task) static void decorateTask (Task& task)
{ {
if (!task.has ("entry"))
{
char entryTime[16]; char entryTime[16];
sprintf (entryTime, "%u", (unsigned int) time (NULL)); sprintf (entryTime, "%u", (unsigned int) time (NULL));
task.set ("entry", entryTime); task.set ("entry", entryTime);
}
task.setStatus (Task::pending); task.setStatus (Task::pending);
@ -798,6 +801,10 @@ static std::string importTodoSh_2_0 (const std::vector <std::string>& lines)
context.parse (); context.parse ();
decorateTask (context.task); decorateTask (context.task);
// Override the Task::pending that decorateTask applies.
if (!isPending)
context.task.setStatus (Task::completed);
context.task.set ("uuid", uuid ()); context.task.set ("uuid", uuid ());
if (isPending) if (isPending)
@ -1169,6 +1176,7 @@ static std::string importYAML (const std::vector <std::string>& lines)
bool inAnno = false; bool inAnno = false;
std::string annoEntry; std::string annoEntry;
Task::status status = Task::pending;
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
@ -1193,12 +1201,31 @@ static std::string importYAML (const std::vector <std::string>& lines)
{ {
if (t.size ()) if (t.size ())
{ {
// Generate a UUID if not present.
if (t.get ("uuid") == "")
t.set ("uuid", uuid ());
// Add defaults.
decorateTask (t);
t.setStatus (status);
// TODO Fail on UUID collision.
context.tdb.add (t); context.tdb.add (t);
t.clear (); t.clear ();
++count; ++count;
} }
} }
else if (name == "status")
{
if (value == "waiting") status = Task::waiting;
else if (value == "completed") status = Task::completed;
else if (value == "deleted") status = Task::deleted;
else if (value == "recurring") status = Task::recurring;
else status = Task::pending;
}
else if (name == "annotation") else if (name == "annotation")
inAnno = true; inAnno = true;

View file

@ -28,7 +28,7 @@
use strict; use strict;
use warnings; use warnings;
use Test::More tests => 11; use Test::More tests => 14;
# Create the rc file. # Create the rc file.
if (open my $fh, '>', 'import.rc') if (open my $fh, '>', 'import.rc')
@ -49,15 +49,18 @@ if (open my $fh, '>', 'import.txt')
description: zero description: zero
project: A project: A
status: pending status: pending
entry: 1234567889
task: task:
uuid: 11111111-1111-1111-1111-111111111111 uuid: 11111111-1111-1111-1111-111111111111
description: one description: one
project: B project: B
status: pending status: pending
entry: 1234567889
task: task:
uuid: 22222222-2222-2222-2222-222222222222 uuid: 22222222-2222-2222-2222-222222222222
description: two description: two
status: completed status: completed
entry: 1234567889
end: 1234567890 end: 1234567890
... ...
EOF EOF
@ -68,13 +71,30 @@ EOF
my $output = qx{../task rc:import.rc import import.txt}; my $output = qx{../task rc:import.rc import import.txt};
like ($output, qr/Imported 3 tasks successfully./, 'no errors'); like ($output, qr/Imported 3 tasks successfully./, 'no errors');
# Imported 3 tasks successfully.
$output = qx{../task rc:import.rc list}; $output = qx{../task rc:import.rc list};
like ($output, qr/1.+A.+zero/, 't1'); # ID Project Pri Due Active Age Description
like ($output, qr/2.+B.+one/, 't2'); # -- ------- --- --- ------ ------- -----------
# 1 A 1.5 yrs zero
# 2 B 1.5 yrs one
#
# 2 tasks
like ($output, qr/1.+A.+zero/, 't1 present');
like ($output, qr/2.+B.+one/, 't2 present');
unlike ($output, qr/3.+two/, 't3 missing');
$output = qx{../task rc:import.rc completed}; $output = qx{../task rc:import.rc completed};
like ($output, qr/2\/13\/2009.+two/, 't3'); # Complete Project Pri Age Description
# --------- ------- --- ------- -----------
# 2/13/2009 1.5 yrs two
#
# 1 task
unlike ($output, qr/1.+A.+zero/, 't1 missing');
unlike ($output, qr/2.+B.+one/, 't2 missing');
like ($output, qr/2\/13\/2009.+two/, 't3 present');
# Cleanup. # Cleanup.
unlink 'import.txt'; unlink 'import.txt';