diff --git a/CHANGELOG.md b/CHANGELOG.md index 86bb594..75691ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and I do try to adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.h ### Fixed +* `plann select ... edit --set-dtend=...` on an event crashed with `TypeError: CalendarObjectResource.set_end() got an unexpected keyword argument 'check_dependent'`, so an event could not be given a new end time. It now moves the event, keeping its duration. `--set-dtend` on a task, `--set-due` on an event and either on a journal crashed too; they now give an error saying which one the item has. * The `plann agenda --help` text now lists two runnable `plann select` commands giving the same events and tasks as `agenda`. In the task one, the `--sort-key '{PRIORITY:?0}'` template had no effect and `list --bottom-up` stood where `select --skip-parents` belongs, and the event one lacked the sort keys. It also pointed to a nonexistent USAGE.md instead of USER_GUIDE.md. * `plann add -l LINE event` dropped the extra iCalendar lines; they are now added to the event, as they were to tasks and journals. * A task with DTSTART as a date and DUE as a timestamp (or the other way around) made `interactive manage-tasks` and other selections crash with `KeyError: 'DTEND'`. The date is now made a timestamp at midnight, in the timezone of the other one if it has a named one, else in the local timezone; the time of day on the other one is kept (it used to be dropped), and the result is valid iCalendar. diff --git a/plann/lib.py b/plann/lib.py index 8586bb6..acb5bb5 100644 --- a/plann/lib.py +++ b/plann/lib.py @@ -666,8 +666,16 @@ def _set_something(obj, arg, value): obj.set_relation(reltype=arg, other=val) elif arg == 'duration': obj.set_duration(value) - elif arg in ('due', 'dtend'): ## moves DTSTART too - _edit sets DTSTART after this - getattr(obj, f"set_{arg}")(value, move_dtstart=True, check_dependent=True) + elif arg in ('due', 'dtend'): + ## moves DTSTART too - _edit sets DTSTART after this + right = {'VTODO': 'due', 'VEVENT': 'dtend'}.get(comp.name) + if arg != right: + hint = f", it ends with {right.upper()}" if right else "" + raise click.UsageError(f"a {comp.name} has no {arg.upper()}{hint}") + if arg == 'due': + obj.set_due(value, move_dtstart=True, check_dependent=True) + else: ## parent/child due checks are VTODO-only + obj.set_dtend(value, move_dtstart=True) elif _is_comma_list_attr(arg): ## a list (already processed by _process_set_arg) or a raw comma string tokens = _comma_list_tokens(arg, value) diff --git a/tests/test_lib.py b/tests/test_lib.py index 36907f6..c646036 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -1,8 +1,9 @@ from datetime import datetime, timedelta, timezone from unittest.mock import patch +import click import pytest -from caldav import Calendar, Todo +from caldav import Calendar, Event, Journal, Todo from plann.lib import ( _add_category, @@ -240,6 +241,34 @@ def test_add_set_category(): assert _comma_list_set(t, 'CATEGORIES') == {'zoo', 'bar'} +def test_set_dtend_on_event(): + """edit --set-dtend on an event moves it, keeping its duration. + Event.set_dtend takes no check_dependent argument (that is a + VTODO-only concept), so passing one crashed with a TypeError.""" + e = Event() + e.data = todo.replace('VTODO', 'VEVENT').replace('DUE:', 'DTEND:') + _set_something(e, 'dtend', datetime(1997, 4, 20, 4, 59, 59, tzinfo=utc)) + comp = e.icalendar_component + assert comp['DTEND'].dt == datetime(1997, 4, 20, 4, 59, 59, tzinfo=utc) + assert comp['DTSTART'].dt == datetime(1997, 4, 19, 13, 30, tzinfo=utc) + + +@pytest.mark.parametrize("objclass, comp_name, arg", [ + (Todo, 'VTODO', 'dtend'), + (Event, 'VEVENT', 'due'), + (Journal, 'VJOURNAL', 'due'), + (Journal, 'VJOURNAL', 'dtend'), +]) +def test_set_end_on_wrong_component(objclass, comp_name, arg): + """A task ends with DUE, an event with DTEND, and a journal has + neither: the wrong one is a usage error naming the right one, + not an AttributeError.""" + obj = objclass() + obj.data = todo.replace('VTODO', comp_name).replace('DUE:19970416T045959Z\n', '') + with pytest.raises(click.UsageError, match=comp_name): + _set_something(obj, arg, datetime(1997, 4, 20, tzinfo=utc)) + + def test_add_set_resources(): """RESOURCES is the other RFC 5545 comma-list property and should behave like CATEGORIES: append via _add_comma_list, replace via plural set."""