test_cleanup.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import collections
  2. import logging
  3. import os
  4. import pathlib
  5. import unittest.mock
  6. import _pytest.logging
  7. import free_disk
  8. # pylint: disable=protected-access
  9. _DiskUsage = collections.namedtuple("_DiskUsage", ("free",))
  10. def _folder_content_size_bytes(path: pathlib.Path) -> int:
  11. return sum(p.stat().st_size for p in path.rglob("*") if p.is_file())
  12. def test__main_remove_some(
  13. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  14. ) -> None:
  15. tmp_path.joinpath("a").write_bytes(b"a" * 4)
  16. tmp_path.joinpath("b").write_bytes(b"b" * 3)
  17. tmp_path.joinpath("c").write_bytes(b"c" * 5)
  18. tmp_path.joinpath("d").write_bytes(b"d" * 2)
  19. tmp_path.joinpath("e").write_bytes(b"d" * 7)
  20. with unittest.mock.patch(
  21. "shutil.disk_usage",
  22. lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
  23. ), unittest.mock.patch(
  24. "sys.argv", ["", "--free-bytes", "30B", str(tmp_path)]
  25. ), caplog.at_level(
  26. logging.DEBUG
  27. ):
  28. free_disk._main()
  29. assert {p.name for p in tmp_path.rglob("*")} == {"e", "d"}
  30. assert caplog.record_tuples[:-1] == [
  31. ("root", logging.DEBUG, m)
  32. for m in [
  33. "Required free bytes: 30",
  34. "_DiskUsage(free=21)",
  35. f"Removed file {tmp_path.joinpath('a')}",
  36. f"Removed file {tmp_path.joinpath('b')}",
  37. f"Removed file {tmp_path.joinpath('c')}",
  38. ]
  39. ]
  40. assert caplog.records[-1].levelno == logging.INFO
  41. assert caplog.records[-1].message.startswith(
  42. "Removed 3 file(s) with modification date <= 20"
  43. )
  44. def test__main_remove_from_subfolder(
  45. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  46. ) -> None:
  47. tmp_path.joinpath("a").mkdir()
  48. tmp_path.joinpath("a", "aa").write_bytes(b"a" * 4)
  49. tmp_path.joinpath("b").write_bytes(b"b" * 3)
  50. tmp_path.joinpath("c").write_bytes(b"c" * 5)
  51. with unittest.mock.patch(
  52. "shutil.disk_usage",
  53. lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
  54. ), unittest.mock.patch(
  55. "sys.argv", ["", "--free-bytes", "35B", str(tmp_path)]
  56. ), caplog.at_level(
  57. logging.DEBUG
  58. ):
  59. free_disk._main()
  60. assert {p.name for p in tmp_path.rglob("*")} == {"a", "c"}
  61. assert caplog.record_tuples[:-1] == [
  62. ("root", logging.DEBUG, m)
  63. for m in [
  64. "Required free bytes: 35",
  65. "_DiskUsage(free=30)",
  66. f"Removed file {tmp_path.joinpath('a', 'aa')}",
  67. f"Removed file {tmp_path.joinpath('b')}",
  68. ]
  69. ]
  70. assert caplog.records[-1].levelno == logging.INFO
  71. assert caplog.records[-1].message.startswith(
  72. "Removed 2 file(s) with modification date <= 20"
  73. )
  74. def test__main_sufficient_space(
  75. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  76. ) -> None:
  77. tmp_path.joinpath("a").write_bytes(b"a" * 4)
  78. tmp_path.joinpath("b").write_bytes(b"b" * 3)
  79. tmp_path.joinpath("c").write_bytes(b"c" * 5)
  80. with unittest.mock.patch(
  81. "shutil.disk_usage",
  82. lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
  83. ), unittest.mock.patch(
  84. "sys.argv", ["", "--free-bytes", "30B", str(tmp_path)]
  85. ), caplog.at_level(
  86. logging.DEBUG
  87. ):
  88. free_disk._main()
  89. assert {p.name for p in tmp_path.rglob("*")} == {"a", "b", "c"}
  90. assert caplog.record_tuples == [
  91. ("root", logging.DEBUG, "Required free bytes: 30"),
  92. ("root", logging.DEBUG, "_DiskUsage(free=30)"),
  93. ("root", logging.DEBUG, "Requirement already fulfilled"),
  94. ]
  95. def test__main_no_files(
  96. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  97. ) -> None:
  98. with unittest.mock.patch(
  99. "shutil.disk_usage", return_value=_DiskUsage(free=21)
  100. ), unittest.mock.patch(
  101. "sys.argv", ["", "--free-bytes", "30B", str(tmp_path)]
  102. ), caplog.at_level(
  103. logging.DEBUG
  104. ):
  105. free_disk._main()
  106. assert caplog.record_tuples == [
  107. ("root", logging.DEBUG, "Required free bytes: 30"),
  108. ("root", logging.DEBUG, "_DiskUsage(free=21)"),
  109. ("root", logging.WARNING, "No files to remove"),
  110. ]
  111. def test__main_path_regex_absolute(
  112. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  113. ) -> None:
  114. tmp_path.joinpath("a").mkdir()
  115. tmp_path.joinpath("a", "aa").write_bytes(b"a" * 4)
  116. tmp_path.joinpath("b").write_bytes(b"b" * 3)
  117. tmp_path.joinpath("c").write_bytes(b"c" * 5)
  118. with unittest.mock.patch(
  119. "shutil.disk_usage",
  120. lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
  121. ), unittest.mock.patch(
  122. "sys.argv",
  123. ["", "--free-bytes", "35B", str(tmp_path), "--delete-re", r".*a/a|/b|b|.*c$"],
  124. ), caplog.at_level(
  125. logging.INFO
  126. ):
  127. free_disk._main()
  128. assert {p.name for p in tmp_path.rglob("*")} == {"a", "b"}
  129. assert caplog.records[-1].message.startswith(
  130. "Removed 2 file(s) with modification date <= 20"
  131. )
  132. def test__main_path_regex_relative(
  133. caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
  134. ) -> None:
  135. tmp_path.joinpath("a").mkdir()
  136. tmp_path.joinpath("a", "aa").write_bytes(b"a" * 4)
  137. tmp_path.joinpath("b").write_bytes(b"b" * 3)
  138. tmp_path.joinpath("c").write_bytes(b"c" * 5)
  139. with unittest.mock.patch(
  140. "shutil.disk_usage",
  141. lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
  142. ), unittest.mock.patch(
  143. "sys.argv",
  144. ["", "--free-bytes", "35B", ".", "--delete-re", r"\./a/a|b|\./c$"],
  145. ), caplog.at_level(
  146. logging.INFO
  147. ):
  148. old_working_dir = os.getcwd()
  149. try:
  150. os.chdir(tmp_path)
  151. free_disk._main()
  152. finally:
  153. os.chdir(old_working_dir)
  154. assert {p.name for p in tmp_path.rglob("*")} == {"a", "b"}
  155. assert caplog.records[-1].message.startswith(
  156. "Removed 2 file(s) with modification date <= 20"
  157. )