cli_test.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # ical2vdir - convert .ics file to vdir directory
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import datetime
  18. import io
  19. import logging
  20. import pathlib
  21. import subprocess
  22. import unittest.mock
  23. import _pytest.logging # pylint: disable=import-private-name; tests
  24. import icalendar
  25. import ical2vdir
  26. # pylint: disable=protected-access
  27. def test_entrypoint_help() -> None:
  28. subprocess.run(["ical2vdir", "--help"], check=True, stdout=subprocess.PIPE)
  29. def test__main_create_all(
  30. caplog: _pytest.logging.LogCaptureFixture,
  31. tmp_path: pathlib.Path,
  32. google_calendar_file: io.BufferedReader,
  33. ) -> None:
  34. with unittest.mock.patch("sys.stdin", google_calendar_file):
  35. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  36. with caplog.at_level(logging.INFO):
  37. ical2vdir._main()
  38. created_item_paths = sorted(tmp_path.iterdir())
  39. assert [p.name for p in created_item_paths] == [
  40. "1234567890qwertyuiopasdfgh@google.com.ics",
  41. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics",
  42. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics",
  43. ]
  44. assert len(caplog.records) == len(created_item_paths)
  45. for item_path in created_item_paths:
  46. assert any(
  47. item_path.name in record.message and "creating" in record.message
  48. for record in caplog.records
  49. )
  50. event = icalendar.cal.Event.from_ical(created_item_paths[1].read_bytes())
  51. assert isinstance(event, icalendar.cal.Event)
  52. assert event["UID"] == "recurr1234567890qwertyuiop@google.com"
  53. assert event["SUMMARY"] == "recurring"
  54. def test__main_create_all_recurrence_id_date(
  55. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  56. ) -> None:
  57. with pathlib.Path(__file__).parent.joinpath(
  58. "resources", "nextcloud-recurring.ics"
  59. ).open("rb") as calendar_file:
  60. with unittest.mock.patch("sys.stdin", calendar_file), unittest.mock.patch(
  61. "sys.argv", ["", "--output-dir", str(tmp_path)]
  62. ), caplog.at_level(logging.WARNING):
  63. ical2vdir._main()
  64. created_item_paths = sorted(tmp_path.iterdir())
  65. assert [p.name for p in created_item_paths] == [
  66. "b0fea373-389b-48d5-b739-9de3e298f555.20260101.ics",
  67. "b0fea373-389b-48d5-b739-9de3e298f555.20260201.ics",
  68. "b0fea373-389b-48d5-b739-9de3e298f555.20260301.ics",
  69. ]
  70. assert not caplog.records
  71. event = icalendar.cal.Event.from_ical(created_item_paths[1].read_bytes())
  72. assert isinstance(event, icalendar.cal.Event)
  73. assert event["UID"] == "b0fea373-389b-48d5-b739-9de3e298f555"
  74. assert event["SUMMARY"] == "second"
  75. assert event["RECURRENCE-ID"].dt == datetime.date(2026, 2, 1)
  76. def test__main_create_all_tasks(
  77. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  78. ) -> None:
  79. with pathlib.Path(__file__).parent.joinpath(
  80. "resources", "nextcloud-tasks.ics"
  81. ).open("rb") as calendar_file:
  82. with unittest.mock.patch("sys.stdin", calendar_file), unittest.mock.patch(
  83. "sys.argv", ["", "--output-dir", str(tmp_path)]
  84. ), caplog.at_level(logging.WARNING):
  85. ical2vdir._main()
  86. created_item_paths = sorted(tmp_path.iterdir())
  87. assert [p.name for p in created_item_paths] == [
  88. "1e6554b1-7ec6-4b58-9688-1dd141ea22cd.ics",
  89. "d6c1f8fa-0dfa-4d31-a988-6e7876fe3222.ics",
  90. ]
  91. assert not caplog.records
  92. task = icalendar.cal.Event.from_ical(created_item_paths[0].read_bytes())
  93. assert isinstance(task, icalendar.cal.Todo)
  94. assert task["UID"] == "1e6554b1-7ec6-4b58-9688-1dd141ea22cd"
  95. assert task["SUMMARY"] == "test 2"
  96. assert task["DUE"].dt == datetime.date(2026, 2, 9)
  97. def test__main_create_some(
  98. caplog: _pytest.logging.LogCaptureFixture,
  99. tmp_path: pathlib.Path,
  100. google_calendar_file: io.BufferedReader,
  101. ) -> None:
  102. with unittest.mock.patch("sys.stdin", google_calendar_file):
  103. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  104. ical2vdir._main()
  105. tmp_path.joinpath(
  106. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  107. ).unlink()
  108. google_calendar_file.seek(0)
  109. with caplog.at_level(logging.INFO):
  110. ical2vdir._main()
  111. assert len(caplog.records) == 1
  112. assert caplog.records[0].message.startswith("creating")
  113. assert caplog.records[0].message.endswith(
  114. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  115. )
  116. def test__main_update(
  117. caplog: _pytest.logging.LogCaptureFixture,
  118. tmp_path: pathlib.Path,
  119. google_calendar_file: io.BufferedReader,
  120. ) -> None:
  121. with unittest.mock.patch("sys.stdin", google_calendar_file):
  122. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  123. ical2vdir._main()
  124. tmp_path.joinpath(
  125. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  126. ).unlink()
  127. updated_path = tmp_path.joinpath(
  128. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  129. )
  130. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  131. with updated_path.open("wb") as updated_file:
  132. updated_file.write(updated_ical)
  133. google_calendar_file.seek(0)
  134. with caplog.at_level(logging.INFO):
  135. ical2vdir._main()
  136. assert len(caplog.records) == 2
  137. log_records = sorted(caplog.records, key=lambda r: r.message)
  138. assert log_records[0].message.startswith("creating")
  139. assert log_records[0].message.endswith(
  140. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  141. )
  142. assert log_records[1].message.startswith("updating")
  143. assert log_records[1].message.endswith(
  144. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  145. )
  146. def test__main_update_silent(
  147. caplog: _pytest.logging.LogCaptureFixture,
  148. tmp_path: pathlib.Path,
  149. google_calendar_file: io.BufferedReader,
  150. ) -> None:
  151. with unittest.mock.patch("sys.stdin", google_calendar_file):
  152. with unittest.mock.patch(
  153. "sys.argv", ["", "--output-dir", str(tmp_path), "--silent"]
  154. ):
  155. ical2vdir._main()
  156. tmp_path.joinpath(
  157. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  158. ).unlink()
  159. updated_path = tmp_path.joinpath(
  160. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  161. )
  162. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  163. with updated_path.open("wb") as updated_file:
  164. updated_file.write(updated_ical)
  165. google_calendar_file.seek(0)
  166. with caplog.at_level(logging.INFO):
  167. ical2vdir._main()
  168. assert len(caplog.records) == 0
  169. def test__main_update_verbose(
  170. caplog: _pytest.logging.LogCaptureFixture,
  171. tmp_path: pathlib.Path,
  172. google_calendar_file: io.BufferedReader,
  173. ) -> None:
  174. with unittest.mock.patch("sys.stdin", google_calendar_file):
  175. with unittest.mock.patch(
  176. "sys.argv", ["", "--output-dir", str(tmp_path), "--verbose"]
  177. ):
  178. ical2vdir._main()
  179. tmp_path.joinpath(
  180. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  181. ).unlink()
  182. updated_path = tmp_path.joinpath(
  183. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  184. )
  185. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  186. with updated_path.open("wb") as updated_file:
  187. updated_file.write(updated_ical)
  188. google_calendar_file.seek(0)
  189. ical2vdir._main()
  190. assert any(
  191. r.message.endswith("1234567890qwertyuiopasdfgh@google.com.ics is up to date")
  192. for r in caplog.records
  193. )
  194. assert any(
  195. r.message.startswith("creating")
  196. and r.message.endswith(".20150924T090000+0200.ics")
  197. for r in caplog.records
  198. )
  199. assert any(
  200. r.message.startswith("updating")
  201. and r.message.endswith(".20150908T090000+0200.ics")
  202. for r in caplog.records
  203. )
  204. def test__main_delete(
  205. caplog: _pytest.logging.LogCaptureFixture,
  206. tmp_path: pathlib.Path,
  207. google_calendar_file: io.BufferedReader,
  208. ) -> None:
  209. tmp_path.joinpath("will-be-deleted.ics").touch()
  210. with unittest.mock.patch("sys.stdin", google_calendar_file):
  211. with unittest.mock.patch(
  212. "sys.argv", ["", "--output-dir", str(tmp_path), "--delete"]
  213. ):
  214. with caplog.at_level(logging.INFO):
  215. ical2vdir._main()
  216. assert len(list(tmp_path.iterdir())) == 3
  217. assert not any(p.name == "will-be-deleted.ics" for p in tmp_path.iterdir())
  218. assert caplog.records[-1].message.startswith("removing")
  219. assert caplog.records[-1].message.endswith("will-be-deleted.ics")