1
0

cli_test.py 7.0 KB

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