From d3ff28b2c2affd0e96f626cc80db3fa3e28ac308 Mon Sep 17 00:00:00 2001 From: jaca Date: Tue, 25 Oct 2022 19:53:20 +0200 Subject: [PATCH 1/3] add tests for error trimming --- .../tests/calendars/big_bad_calendar.ics | 41 +++++++++++++++++++ .../tests/calendars/small_bad_calendar.ics | 3 ++ .../tests/test_components_break_on_bad_ics.py | 11 +++++ 3 files changed, 55 insertions(+) create mode 100644 src/icalendar/tests/calendars/big_bad_calendar.ics create mode 100644 src/icalendar/tests/calendars/small_bad_calendar.ics diff --git a/src/icalendar/tests/calendars/big_bad_calendar.ics b/src/icalendar/tests/calendars/big_bad_calendar.ics new file mode 100644 index 00000000..88fbc6f8 --- /dev/null +++ b/src/icalendar/tests/calendars/big_bad_calendar.ics @@ -0,0 +1,41 @@ +BEGIN:VCALENDAR +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT +BEGIN:VEVENT +END:VEVENT diff --git a/src/icalendar/tests/calendars/small_bad_calendar.ics b/src/icalendar/tests/calendars/small_bad_calendar.ics new file mode 100644 index 00000000..94228f1b --- /dev/null +++ b/src/icalendar/tests/calendars/small_bad_calendar.ics @@ -0,0 +1,3 @@ +BEGIN:VCALENDAR +BEGIN:VEVENT +END:VEVENT diff --git a/src/icalendar/tests/test_components_break_on_bad_ics.py b/src/icalendar/tests/test_components_break_on_bad_ics.py index 27ddc95e..3bf7d1d5 100644 --- a/src/icalendar/tests/test_components_break_on_bad_ics.py +++ b/src/icalendar/tests/test_components_break_on_bad_ics.py @@ -26,3 +26,14 @@ def test_rdate_dosent_become_none_on_invalid_input_issue_464(events): assert events.issue_464_invalid_rdate.is_broken assert ('RDATE', 'Expected period format, got: 199709T180000Z/PT5H30M') in events.issue_464_invalid_rdate.errors assert not b'RDATE:None' in events.issue_464_invalid_rdate.to_ical() + +@pytest.mark.parametrize('calendar_name', [ + 'big_bad_calendar', + 'small_bad_calendar', + 'multiple_calendar_components', +]) +def test_error_message_doesnt_get_too_big(calendars, calendar_name): + with pytest.raises(ValueError) as exception: + calendars[calendar_name] + assert len(str(exception).split(': ')[1]) <= 100 + From 4c5f47c8f5f6f74591f981373bfb502457ac0290 Mon Sep 17 00:00:00 2001 From: jaca Date: Sat, 29 Oct 2022 13:37:28 +0200 Subject: [PATCH 2/3] add error trimming for Calendar.from_ical as per #472 --- src/icalendar/cal.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index e2e364ca..cf7dc9cd 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -390,14 +390,22 @@ def from_ical(cls, st, multiple=False): if multiple: return comps if len(comps) > 1: - raise ValueError(f'Found multiple components where ' - f'only one is allowed: {st!r}') + raise ValueError(cls._format_error( + 'Found multiple components where only one is allowed', st)) if len(comps) < 1: - raise ValueError(f'Found no components where ' - f'exactly one is required: ' - f'{st!r}') + raise ValueError(cls._format_error( + 'Found no components where exactly one is required', st)) return comps[0] + def _format_error(error_description, bad_input, elipsis='[...]'): + # there's three character more in the error, ie. ' ' x2 and a ':' + max_error_length = 100 - 3 + if len(error_description) + len(bad_input) + len(elipsis) > max_error_length: + truncate_to = max_error_length - len(error_description) - len(elipsis) + return f'{error_description}: {bad_input[:truncate_to]} {elipsis}' + else: + return f'{error_description}: {bad_input}' + def content_line(self, name, value, sorted=True): """Returns property as content line. """ From 9b4ba63b7e74dfcff83fc98743c83a3d763b4d68 Mon Sep 17 00:00:00 2001 From: jaca Date: Sat, 29 Oct 2022 13:43:10 +0200 Subject: [PATCH 3/3] add changelog entry --- CHANGES.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 65a7f666..a8d91e68 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,7 +6,10 @@ Changelog Minor changes: -- ... +- Calendar.from_ical no longer throws long errors + Ref: #473 + Fixes: #472 + [jacadzaca] Breaking changes: