| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | # ical2vdir - convert .ics file to vdir directory## Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>## 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 3 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, see <https://www.gnu.org/licenses/>.import copyimport osimport pathlibimport tempfileimport unittest.mockimport icalendar.calimport pytestimport ical2vdirdef _normalize_ical(ical: bytes) -> bytes:    return ical.replace(b"\n", b"\r\n")_SINGLE_EVENT_ICAL = _normalize_ical(    b"""BEGIN:VEVENTSUMMARY:partyDTSTART:20201024T100000ZDTEND:20201026T120000ZDTSTAMP:20200205T160640ZUID:1qa2ws3ed4rf5tg@google.comSEQUENCE:0CREATED:20191231T103841ZDESCRIPTION:LAST-MODIFIED:20191231T103841ZLOCATION:STATUS:CONFIRMEDTRANSP:OPAQUEEND:VEVENT""")# pylint: disable=protected-access# tmp_path fixture: https://github.com/pytest-dev/pytest/blob/5.4.3/src/_pytest/tmpdir.py#L191def test__write_event_cleanup(tmp_path: pathlib.Path) -> None:    event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)    with pytest.raises(IsADirectoryError):        ical2vdir._write_event(event, tmp_path)    assert tmp_path.is_dir()  # did not overwritedef test__write_event_move_failed(tmp_path: pathlib.Path) -> None:    event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)    output_path = tmp_path.joinpath("test.ics")    with unittest.mock.patch("os.unlink") as unlink_mock, unittest.mock.patch(        "shutil.move", side_effect=Exception("test")    ), pytest.raises(Exception, match=r"^test$"):        ical2vdir._write_event(event, output_path)    assert not output_path.exists()    unlink_mock.assert_called_once()  # cleanup temporary file    unlink_args, _ = unlink_mock.call_args    (temp_path,) = unlink_args    assert os.path.dirname(temp_path) == os.path.dirname(tempfile.mkdtemp())@pytest.mark.parametrize(    ("event_ical", "expected_filename"),    [        (            _SINGLE_EVENT_ICAL,            "1qa2ws3ed4rf5tg@google.com.ics",        ),        (            b"""BEGIN:VEVENTSUMMARY:workDTSTART;TZID=Europe/Vienna:20150924T090000DTEND;TZID=Europe/Vienna:20150924T123000DTSTAMP:20200205T160640ZUID:1qa2ws3ed4rf5tg@google.comRECURRENCE-ID;TZID=Europe/Vienna:20150924T090000SEQUENCE:5CREATED:20140228T212925ZDESCRIPTION:LAST-MODIFIED:20150908T181423ZLOCATION:STATUS:CONFIRMEDTRANSP:TRANSPARENTEND:VEVENT""",            "1qa2ws3ed4rf5tg@google.com.20150924T090000+0200.ics",        ),    ],)def test__event_vdir_filename(event_ical: bytes, expected_filename: str) -> None:    event = icalendar.cal.Event.from_ical(event_ical)    assert ical2vdir._event_vdir_filename(event) == expected_filename@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])def test__sync_event_create(tmp_path: pathlib.Path, event_ical: bytes) -> None:    event = icalendar.cal.Event.from_ical(event_ical)    ical2vdir._sync_event(event, tmp_path)    (ics_path,) = tmp_path.iterdir()    assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])def test__sync_event_update(tmp_path: pathlib.Path, event_ical: bytes) -> None:    event = icalendar.cal.Event.from_ical(event_ical)    ical2vdir._sync_event(event, tmp_path)    event["SUMMARY"] += " suffix"    ical2vdir._sync_event(event, tmp_path)    (ics_path,) = tmp_path.iterdir()    assert ics_path.name == event["UID"] + ".ics"    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(        b"party", b"party suffix"    )@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])def test__sync_event_unchanged(tmp_path: pathlib.Path, event_ical: bytes) -> None:    event = icalendar.cal.Event.from_ical(event_ical)    ical2vdir._sync_event(event, tmp_path)    (ics_path,) = tmp_path.iterdir()    old_stat = copy.deepcopy(ics_path.stat())    ical2vdir._sync_event(event, tmp_path)    assert ics_path.stat() == old_stat    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
 |