cli_test.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import io
  2. import logging
  3. import pathlib
  4. import subprocess
  5. import unittest.mock
  6. import icalendar
  7. import ics2vdir
  8. # pylint: disable=protected-access
  9. def test_entrypoint_help():
  10. subprocess.run(["ics2vdir", "--help"], check=True, stdout=subprocess.PIPE)
  11. def test__main_create_all(
  12. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  13. ):
  14. with unittest.mock.patch("sys.stdin", google_calendar_file):
  15. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
  16. with caplog.at_level(logging.INFO):
  17. ics2vdir._main()
  18. created_item_paths = sorted(temp_dir_path.iterdir())
  19. assert [p.name for p in created_item_paths] == [
  20. "1234567890qwertyuiopasdfgh@google.com.ics",
  21. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics",
  22. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics",
  23. ]
  24. assert len(caplog.records) == len(created_item_paths)
  25. for item_path in created_item_paths:
  26. assert any(
  27. item_path.name in record.message and "creating" in record.message
  28. for record in caplog.records
  29. )
  30. event = icalendar.cal.Event.from_ical(created_item_paths[1].read_bytes())
  31. assert isinstance(event, icalendar.cal.Event)
  32. assert event["UID"] == "recurr1234567890qwertyuiop@google.com"
  33. assert event["SUMMARY"] == "recurring"
  34. def test__main_create_some(
  35. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  36. ):
  37. with unittest.mock.patch("sys.stdin", google_calendar_file):
  38. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
  39. ics2vdir._main()
  40. temp_dir_path.joinpath(
  41. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  42. ).unlink()
  43. google_calendar_file.seek(0)
  44. with caplog.at_level(logging.INFO):
  45. ics2vdir._main()
  46. assert len(caplog.records) == 1
  47. assert caplog.records[0].message.startswith("creating")
  48. assert caplog.records[0].message.endswith(
  49. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  50. )
  51. def test__main_update(
  52. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  53. ):
  54. with unittest.mock.patch("sys.stdin", google_calendar_file):
  55. with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
  56. ics2vdir._main()
  57. temp_dir_path.joinpath(
  58. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  59. ).unlink()
  60. updated_path = temp_dir_path.joinpath(
  61. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  62. )
  63. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  64. with updated_path.open("wb") as updated_file:
  65. updated_file.write(updated_ical)
  66. google_calendar_file.seek(0)
  67. with caplog.at_level(logging.INFO):
  68. ics2vdir._main()
  69. assert len(caplog.records) == 2
  70. log_records = sorted(caplog.records, key=lambda r: r.message)
  71. assert log_records[0].message.startswith("creating")
  72. assert log_records[0].message.endswith(
  73. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  74. )
  75. assert log_records[1].message.startswith("updating")
  76. assert log_records[1].message.endswith(
  77. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  78. )
  79. def test__main_update_silent(
  80. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  81. ):
  82. with unittest.mock.patch("sys.stdin", google_calendar_file):
  83. with unittest.mock.patch(
  84. "sys.argv", ["", "--output-dir", str(temp_dir_path), "--silent"]
  85. ):
  86. ics2vdir._main()
  87. temp_dir_path.joinpath(
  88. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  89. ).unlink()
  90. updated_path = temp_dir_path.joinpath(
  91. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  92. )
  93. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  94. with updated_path.open("wb") as updated_file:
  95. updated_file.write(updated_ical)
  96. google_calendar_file.seek(0)
  97. with caplog.at_level(logging.INFO):
  98. ics2vdir._main()
  99. assert len(caplog.records) == 0
  100. def test__main_update_verbose(
  101. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  102. ):
  103. with unittest.mock.patch("sys.stdin", google_calendar_file):
  104. with unittest.mock.patch(
  105. "sys.argv", ["", "--output-dir", str(temp_dir_path), "--verbose"]
  106. ):
  107. ics2vdir._main()
  108. temp_dir_path.joinpath(
  109. "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
  110. ).unlink()
  111. updated_path = temp_dir_path.joinpath(
  112. "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
  113. )
  114. updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
  115. with updated_path.open("wb") as updated_file:
  116. updated_file.write(updated_ical)
  117. google_calendar_file.seek(0)
  118. ics2vdir._main()
  119. assert any(
  120. r.message.endswith("1234567890qwertyuiopasdfgh@google.com.ics is up to date")
  121. for r in caplog.records
  122. )
  123. assert any(
  124. r.message.startswith("creating")
  125. and r.message.endswith(".20150924T090000+0200.ics")
  126. for r in caplog.records
  127. )
  128. assert any(
  129. r.message.startswith("updating")
  130. and r.message.endswith(".20150908T090000+0200.ics")
  131. for r in caplog.records
  132. )
  133. def test__main_delete(
  134. caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
  135. ):
  136. temp_dir_path.joinpath("will-be-deleted.ics").touch()
  137. with unittest.mock.patch("sys.stdin", google_calendar_file):
  138. with unittest.mock.patch(
  139. "sys.argv", ["", "--output-dir", str(temp_dir_path), "--delete"]
  140. ):
  141. with caplog.at_level(logging.INFO):
  142. ics2vdir._main()
  143. assert len(list(temp_dir_path.iterdir())) == 3
  144. assert not any(p.name == "will-be-deleted.ics" for p in temp_dir_path.iterdir())
  145. assert caplog.records[-1].message.startswith("removing")
  146. assert caplog.records[-1].message.endswith("will-be-deleted.ics")