cli_test.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 io
  18. import logging
  19. import pathlib
  20. import subprocess
  21. import unittest.mock
  22. import _pytest.logging
  23. import icalendar
  24. import ical2vdir
  25. # pylint: disable=protected-access
  26. def test_entrypoint_help() -> None:
  27. subprocess.run(["ical2vdir", "--help"], check=True, stdout=subprocess.PIPE)
  28. def test__main_create_all(
  29. caplog: _pytest.logging.LogCaptureFixture,
  30. tmp_path: pathlib.Path,
  31. google_calendar_file: io.BufferedReader,
  32. ) -> None:
  33. with unittest.mock.patch("sys.stdin", google_calendar_file):
  34. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  35. with caplog.at_level(logging.INFO):
  36. ical2vdir._main()
  37. created_item_paths = sorted(tmp_path.iterdir())
  38. assert [p.name for p in created_item_paths] == [
  39. "1234567890qwertyuiopasdfgh@google.com.ics",
  40. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics",
  41. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics",
  42. ]
  43. assert len(caplog.records) == len(created_item_paths)
  44. for item_path in created_item_paths:
  45. assert any(
  46. item_path.name in record.message and "creating" in record.message
  47. for record in caplog.records
  48. )
  49. event = icalendar.cal.Event.from_ical(created_item_paths[1].read_bytes())
  50. assert isinstance(event, icalendar.cal.Event)
  51. assert event["UID"] == "recurr1234567890qwertyuiop@google.com"
  52. assert event["SUMMARY"] == "recurring"
  53. def test__main_create_some(
  54. caplog: _pytest.logging.LogCaptureFixture,
  55. tmp_path: pathlib.Path,
  56. google_calendar_file: io.BufferedReader,
  57. ) -> None:
  58. with unittest.mock.patch("sys.stdin", google_calendar_file):
  59. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  60. ical2vdir._main()
  61. tmp_path.joinpath(
  62. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  63. ).unlink()
  64. google_calendar_file.seek(0)
  65. with caplog.at_level(logging.INFO):
  66. ical2vdir._main()
  67. assert len(caplog.records) == 1
  68. assert caplog.records[0].message.startswith("creating")
  69. assert caplog.records[0].message.endswith(
  70. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  71. )
  72. def test__main_update(
  73. caplog: _pytest.logging.LogCaptureFixture,
  74. tmp_path: pathlib.Path,
  75. google_calendar_file: io.BufferedReader,
  76. ) -> None:
  77. with unittest.mock.patch("sys.stdin", google_calendar_file):
  78. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
  79. ical2vdir._main()
  80. tmp_path.joinpath(
  81. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  82. ).unlink()
  83. updated_path = tmp_path.joinpath(
  84. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  85. )
  86. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  87. with updated_path.open("wb") as updated_file:
  88. updated_file.write(updated_ical)
  89. google_calendar_file.seek(0)
  90. with caplog.at_level(logging.INFO):
  91. ical2vdir._main()
  92. assert len(caplog.records) == 2
  93. log_records = sorted(caplog.records, key=lambda r: r.message)
  94. assert log_records[0].message.startswith("creating")
  95. assert log_records[0].message.endswith(
  96. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  97. )
  98. assert log_records[1].message.startswith("updating")
  99. assert log_records[1].message.endswith(
  100. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  101. )
  102. def test__main_update_silent(
  103. caplog: _pytest.logging.LogCaptureFixture,
  104. tmp_path: pathlib.Path,
  105. google_calendar_file: io.BufferedReader,
  106. ) -> None:
  107. with unittest.mock.patch("sys.stdin", google_calendar_file):
  108. with unittest.mock.patch(
  109. "sys.argv", ["", "--output-dir", str(tmp_path), "--silent"]
  110. ):
  111. ical2vdir._main()
  112. tmp_path.joinpath(
  113. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  114. ).unlink()
  115. updated_path = tmp_path.joinpath(
  116. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  117. )
  118. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  119. with updated_path.open("wb") as updated_file:
  120. updated_file.write(updated_ical)
  121. google_calendar_file.seek(0)
  122. with caplog.at_level(logging.INFO):
  123. ical2vdir._main()
  124. assert len(caplog.records) == 0
  125. def test__main_update_verbose(
  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), "--verbose"]
  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. ical2vdir._main()
  146. assert any(
  147. r.message.endswith("1234567890qwertyuiopasdfgh@google.com.ics is up to date")
  148. for r in caplog.records
  149. )
  150. assert any(
  151. r.message.startswith("creating")
  152. and r.message.endswith(".20150924T090000+0200.ics")
  153. for r in caplog.records
  154. )
  155. assert any(
  156. r.message.startswith("updating")
  157. and r.message.endswith(".20150908T090000+0200.ics")
  158. for r in caplog.records
  159. )
  160. def test__main_delete(
  161. caplog: _pytest.logging.LogCaptureFixture,
  162. tmp_path: pathlib.Path,
  163. google_calendar_file: io.BufferedReader,
  164. ) -> None:
  165. tmp_path.joinpath("will-be-deleted.ics").touch()
  166. with unittest.mock.patch("sys.stdin", google_calendar_file):
  167. with unittest.mock.patch(
  168. "sys.argv", ["", "--output-dir", str(tmp_path), "--delete"]
  169. ):
  170. with caplog.at_level(logging.INFO):
  171. ical2vdir._main()
  172. assert len(list(tmp_path.iterdir())) == 3
  173. assert not any(p.name == "will-be-deleted.ics" for p in tmp_path.iterdir())
  174. assert caplog.records[-1].message.startswith("removing")
  175. assert caplog.records[-1].message.endswith("will-be-deleted.ics")