cli_test.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_some(
  77. caplog: _pytest.logging.LogCaptureFixture,
  78. tmp_path: pathlib.Path,
  79. google_calendar_file: io.BufferedReader,
  80. ) -> None:
  81. with unittest.mock.patch("sys.stdin", google_calendar_file):
  82. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  83. ical2vdir._main()
  84. tmp_path.joinpath(
  85. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  86. ).unlink()
  87. google_calendar_file.seek(0)
  88. with caplog.at_level(logging.INFO):
  89. ical2vdir._main()
  90. assert len(caplog.records) == 1
  91. assert caplog.records[0].message.startswith("creating")
  92. assert caplog.records[0].message.endswith(
  93. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  94. )
  95. def test__main_update(
  96. caplog: _pytest.logging.LogCaptureFixture,
  97. tmp_path: pathlib.Path,
  98. google_calendar_file: io.BufferedReader,
  99. ) -> None:
  100. with unittest.mock.patch("sys.stdin", google_calendar_file):
  101. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  102. ical2vdir._main()
  103. tmp_path.joinpath(
  104. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  105. ).unlink()
  106. updated_path = tmp_path.joinpath(
  107. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  108. )
  109. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  110. with updated_path.open("wb") as updated_file:
  111. updated_file.write(updated_ical)
  112. google_calendar_file.seek(0)
  113. with caplog.at_level(logging.INFO):
  114. ical2vdir._main()
  115. assert len(caplog.records) == 2
  116. log_records = sorted(caplog.records, key=lambda r: r.message)
  117. assert log_records[0].message.startswith("creating")
  118. assert log_records[0].message.endswith(
  119. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  120. )
  121. assert log_records[1].message.startswith("updating")
  122. assert log_records[1].message.endswith(
  123. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  124. )
  125. def test__main_update_silent(
  126. caplog: _pytest.logging.LogCaptureFixture,
  127. tmp_path: pathlib.Path,
  128. google_calendar_file: io.BufferedReader,
  129. ) -> None:
  130. with unittest.mock.patch("sys.stdin", google_calendar_file):
  131. with unittest.mock.patch(
  132. "sys.argv", ["", "--output-dir", str(tmp_path), "--silent"]
  133. ):
  134. ical2vdir._main()
  135. tmp_path.joinpath(
  136. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  137. ).unlink()
  138. updated_path = tmp_path.joinpath(
  139. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  140. )
  141. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  142. with updated_path.open("wb") as updated_file:
  143. updated_file.write(updated_ical)
  144. google_calendar_file.seek(0)
  145. with caplog.at_level(logging.INFO):
  146. ical2vdir._main()
  147. assert len(caplog.records) == 0
  148. def test__main_update_verbose(
  149. caplog: _pytest.logging.LogCaptureFixture,
  150. tmp_path: pathlib.Path,
  151. google_calendar_file: io.BufferedReader,
  152. ) -> None:
  153. with unittest.mock.patch("sys.stdin", google_calendar_file):
  154. with unittest.mock.patch(
  155. "sys.argv", ["", "--output-dir", str(tmp_path), "--verbose"]
  156. ):
  157. ical2vdir._main()
  158. tmp_path.joinpath(
  159. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  160. ).unlink()
  161. updated_path = tmp_path.joinpath(
  162. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  163. )
  164. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  165. with updated_path.open("wb") as updated_file:
  166. updated_file.write(updated_ical)
  167. google_calendar_file.seek(0)
  168. ical2vdir._main()
  169. assert any(
  170. r.message.endswith("1234567890qwertyuiopasdfgh@google.com.ics is up to date")
  171. for r in caplog.records
  172. )
  173. assert any(
  174. r.message.startswith("creating")
  175. and r.message.endswith(".20150924T090000+0200.ics")
  176. for r in caplog.records
  177. )
  178. assert any(
  179. r.message.startswith("updating")
  180. and r.message.endswith(".20150908T090000+0200.ics")
  181. for r in caplog.records
  182. )
  183. def test__main_delete(
  184. caplog: _pytest.logging.LogCaptureFixture,
  185. tmp_path: pathlib.Path,
  186. google_calendar_file: io.BufferedReader,
  187. ) -> None:
  188. tmp_path.joinpath("will-be-deleted.ics").touch()
  189. with unittest.mock.patch("sys.stdin", google_calendar_file):
  190. with unittest.mock.patch(
  191. "sys.argv", ["", "--output-dir", str(tmp_path), "--delete"]
  192. ):
  193. with caplog.at_level(logging.INFO):
  194. ical2vdir._main()
  195. assert len(list(tmp_path.iterdir())) == 3
  196. assert not any(p.name == "will-be-deleted.ics" for p in tmp_path.iterdir())
  197. assert caplog.records[-1].message.startswith("removing")
  198. assert caplog.records[-1].message.endswith("will-be-deleted.ics")