From fa891d7d669de6301019a9dc91ce6676be6b45cb Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Mon, 4 Oct 2021 13:20:53 +0200 Subject: [PATCH 1/3] should be fixed when organizer has no mail --- lib/src/exceptions/icalendar_exception.dart | 2 +- lib/src/model/icalendar.dart | 9 ++++++--- test/issue_33_test.dart | 19 +++++++++++++++++++ test/parsing_test.dart | 2 +- test/test_resources/organizer_no_email.ics | 16 ++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 test/issue_33_test.dart create mode 100644 test/test_resources/organizer_no_email.ics diff --git a/lib/src/exceptions/icalendar_exception.dart b/lib/src/exceptions/icalendar_exception.dart index 61cca7b..9df4d77 100644 --- a/lib/src/exceptions/icalendar_exception.dart +++ b/lib/src/exceptions/icalendar_exception.dart @@ -21,7 +21,7 @@ class ICalendarEndException extends ICalendarFormatException { /// Exception thrown when there is an empty line. class EmptyLineException extends ICalendarFormatException { - const EmptyLineException(String msg) : super(msg); + const EmptyLineException() : super('Empty line are not allowed'); } /// Exception thrown when `VERSION` is missing. diff --git a/lib/src/model/icalendar.dart b/lib/src/model/icalendar.dart index 0f87fed..fe6afa8 100644 --- a/lib/src/model/icalendar.dart +++ b/lib/src/model/icalendar.dart @@ -223,19 +223,23 @@ class ICalendar { Map? lastEvent = {}; String? currentName; + // Ensure first line is BEGIN:VCALENDAR if (lines.first.trim() != 'BEGIN:VCALENDAR') { throw ICalendarBeginException( 'The first line must be BEGIN:VCALENDAR but was ${lines.first}.'); } + + // Ensure last line is END:VCALENDAR if (lines.last.trim() != 'END:VCALENDAR') { throw ICalendarEndException( 'The last line must be END:VCALENDAR but was ${lines.last}.'); } + for (var i = 0; i < lines.length; i++) { final line = StringBuffer(lines[i].trim()); if (line.isEmpty && !allowEmptyLine) { - throw const EmptyLineException('Empty line are not allowed'); + throw const EmptyLineException(); } final exp = RegExp(r'^ '); @@ -245,7 +249,7 @@ class ICalendar { } final dataLine = line.toString().split(':'); - if (dataLine.length < 2 || + if ((dataLine.length < 2 && !dataLine[0].contains(';')) || (dataLine.isNotEmpty && dataLine[0].toUpperCase() != dataLine[0] && !dataLine[0].contains(';'))) { @@ -256,7 +260,6 @@ class ICalendar { } final dataName = dataLine[0].split(';'); - final name = dataName[0]; dataName.removeRange(0, 1); diff --git a/test/issue_33_test.dart b/test/issue_33_test.dart new file mode 100644 index 0000000..6251a01 --- /dev/null +++ b/test/issue_33_test.dart @@ -0,0 +1,19 @@ +import 'package:icalendar_parser/icalendar_parser.dart'; +import 'package:test/test.dart'; + +import 'test_utils.dart'; + +void main() { + group('Issue #33', () { + test('No email in ORGANIZER', () { + final eventText = readFileLines('organizer_no_email.ics'); + final iCal = ICalendar.fromLines(eventText); + expect(iCal.data.length, 1); + + final event = iCal.data[0]; + final organizer = event['organizer'] as Map; + expect(organizer['name'], 'Joe Jackson'); + expect(organizer['mail'], ''); + }); + }); +} diff --git a/test/parsing_test.dart b/test/parsing_test.dart index 5f265b3..bbf9711 100644 --- a/test/parsing_test.dart +++ b/test/parsing_test.dart @@ -93,7 +93,7 @@ void main() { ); }); - test('American History', () async { + test('American History', () { final eventText = readFileLines('american_history.ics'); final iCalParsed = ICalendar.fromLines(eventText); expect( diff --git a/test/test_resources/organizer_no_email.ics b/test/test_resources/organizer_no_email.ics new file mode 100644 index 0000000..ce2d002 --- /dev/null +++ b/test/test_resources/organizer_no_email.ics @@ -0,0 +1,16 @@ +BEGIN:VCALENDAR +VERSION:2.0 +CALSCALE:GREGORIAN +PRODID:adamgibbons/ics +METHOD:PUBLISH +BEGIN:VEVENT +UID:b11ae923-9898-44ca-8f92-ed2ebe36863f +SUMMARY:Cool event +DTSTAMP:20211004T101600Z +DTSTART:20211006T063000Z +DTEND:20211006T073000Z +DESCRIPTION:Some description +STATUS:CANCELLED +ORGANIZER;CN=Joe Jackson +END:VEVENT +END:VCALENDAR \ No newline at end of file From 9fc7cbf13a57feb7269d56c3a6a497bf7c05dc1b Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Mon, 4 Oct 2021 13:22:38 +0200 Subject: [PATCH 2/3] added 2nd test --- test/issue_33_test.dart | 13 ++++++++++++- test/test_resources/organizer_no_email_2.ics | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/test_resources/organizer_no_email_2.ics diff --git a/test/issue_33_test.dart b/test/issue_33_test.dart index 6251a01..7e4b9b8 100644 --- a/test/issue_33_test.dart +++ b/test/issue_33_test.dart @@ -5,7 +5,7 @@ import 'test_utils.dart'; void main() { group('Issue #33', () { - test('No email in ORGANIZER', () { + test('No email in ORGANIZER - 1', () { final eventText = readFileLines('organizer_no_email.ics'); final iCal = ICalendar.fromLines(eventText); expect(iCal.data.length, 1); @@ -15,5 +15,16 @@ void main() { expect(organizer['name'], 'Joe Jackson'); expect(organizer['mail'], ''); }); + + test('No email in ORGANIZER - 2', () { + final eventText = readFileLines('organizer_no_email_2.ics'); + final iCal = ICalendar.fromLines(eventText); + expect(iCal.data.length, 1); + + final event = iCal.data[0]; + final organizer = event['organizer'] as Map; + expect(organizer['name'], 'Joe Jackson'); + expect(organizer['mail'], ''); + }); }); } diff --git a/test/test_resources/organizer_no_email_2.ics b/test/test_resources/organizer_no_email_2.ics new file mode 100644 index 0000000..2e64d76 --- /dev/null +++ b/test/test_resources/organizer_no_email_2.ics @@ -0,0 +1,16 @@ +BEGIN:VCALENDAR +VERSION:2.0 +CALSCALE:GREGORIAN +PRODID:adamgibbons/ics +METHOD:PUBLISH +BEGIN:VEVENT +UID:b11ae923-9898-44ca-8f92-ed2ebe36863f +SUMMARY:Cool event +DTSTAMP:20211004T101600Z +DTSTART:20211006T063000Z +DTEND:20211006T073000Z +DESCRIPTION:Some description +CATEGORIES:APPOINTMENT +ORGANIZER;CN=Joe Jackson +END:VEVENT +END:VCALENDAR \ No newline at end of file From d17463018ea7bf19ee366ab9582f444cc9b52ad9 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Mon, 4 Oct 2021 13:26:13 +0200 Subject: [PATCH 3/3] update version to publish fix --- CHANGELOG.md | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f18caa0..a5dfca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.0.1] - 04/10/2021 + +* Fixed [issue #33](https://github.com/TesteurManiak/icalendar_parser/issues/33): Exception on ORGANIZER field parsing + ## [1.0.0] - 22/09/2021 * Fixed `fromString` constructor diff --git a/pubspec.yaml b/pubspec.yaml index af8e1ba..3febc8a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: icalendar_parser description: Package to parse iCalendar (.ics) files written in pure Dart. -version: 1.0.0 +version: 1.0.1 homepage: https://github.com/TesteurManiak repository: https://github.com/TesteurManiak/icalendar_parser