You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.1 KiB

  1. # encoding: utf-8
  2. import os
  3. import shutil
  4. import sys
  5. import pytest
  6. from os import path as op
  7. from send2trash import send2trash as s2t
  8. # import the two versions as well as the "automatic" version
  9. if sys.platform == "win32":
  10. from send2trash.plat_win_modern import send2trash as s2t_modern
  11. from send2trash.plat_win_legacy import send2trash as s2t_legacy
  12. else:
  13. pytest.skip("Skipping windows-only tests", allow_module_level=True)
  14. def _create_tree(path):
  15. dirname = op.dirname(path)
  16. if not op.isdir(dirname):
  17. os.makedirs(dirname)
  18. with open(path, "w") as writer:
  19. writer.write("send2trash test")
  20. @pytest.fixture
  21. def testdir(tmp_path):
  22. dirname = "\\\\?\\" + str(tmp_path)
  23. assert op.exists(dirname) is True
  24. yield dirname
  25. shutil.rmtree(dirname, ignore_errors=True)
  26. @pytest.fixture
  27. def testfile(testdir):
  28. file = op.join(testdir, "testfile.txt")
  29. _create_tree(file)
  30. assert op.exists(file) is True
  31. yield file
  32. # Note dir will cleanup the file
  33. @pytest.fixture
  34. def testfiles(testdir):
  35. files = [op.join(testdir, "testfile{}.txt".format(index)) for index in range(10)]
  36. [_create_tree(file) for file in files]
  37. assert all([op.exists(file) for file in files]) is True
  38. yield files
  39. # Note dir will cleanup the files
  40. def _trash_folder(dir, fcn):
  41. fcn(dir)
  42. assert op.exists(dir) is False
  43. def _trash_file(file, fcn):
  44. fcn(file)
  45. assert op.exists(file) is False
  46. def _trash_multifile(files, fcn):
  47. fcn(files)
  48. assert any([op.exists(file) for file in files]) is False
  49. def _file_not_found(dir, fcn):
  50. file = op.join(dir, "otherfile.txt")
  51. pytest.raises(OSError, fcn, file)
  52. def _multi_byte_unicode(dir, fcn):
  53. single_file = op.join(dir, "😇.txt")
  54. _create_tree(single_file)
  55. assert op.exists(single_file) is True
  56. fcn(single_file)
  57. assert op.exists(single_file) is False
  58. files = [op.join(dir, "😇{}.txt".format(index)) for index in range(10)]
  59. [_create_tree(file) for file in files]
  60. assert all([op.exists(file) for file in files]) is True
  61. fcn(files)
  62. assert any([op.exists(file) for file in files]) is False
  63. def test_trash_folder(testdir):
  64. _trash_folder(testdir, s2t)
  65. def test_trash_file(testfile):
  66. _trash_file(testfile, s2t)
  67. def test_trash_multifile(testfiles):
  68. _trash_multifile(testfiles, s2t)
  69. def test_file_not_found(testdir):
  70. _file_not_found(testdir, s2t)
  71. def test_trash_folder_modern(testdir):
  72. _trash_folder(testdir, s2t_modern)
  73. def test_trash_file_modern(testfile):
  74. _trash_file(testfile, s2t_modern)
  75. def test_trash_multifile_modern(testfiles):
  76. _trash_multifile(testfiles, s2t_modern)
  77. def test_file_not_found_modern(testdir):
  78. _file_not_found(testdir, s2t_modern)
  79. def test_multi_byte_unicode_modern(testdir):
  80. _multi_byte_unicode(testdir, s2t_modern)
  81. def test_trash_folder_legacy(testdir):
  82. _trash_folder(testdir, s2t_legacy)
  83. def test_trash_file_legacy(testfile):
  84. _trash_file(testfile, s2t_legacy)
  85. def test_trash_multifile_legacy(testfiles):
  86. _trash_multifile(testfiles, s2t_legacy)
  87. def test_file_not_found_legacy(testdir):
  88. _file_not_found(testdir, s2t_legacy)
  89. def test_multi_byte_unicode_legacy(testdir):
  90. _multi_byte_unicode(testdir, s2t_legacy)
  91. # Long path tests
  92. @pytest.fixture
  93. def longdir(tmp_path):
  94. dirname = "\\\\?\\" + str(tmp_path)
  95. name = "A" * 100
  96. yield op.join(dirname, name, name, name)
  97. shutil.rmtree(dirname, ignore_errors=True)
  98. @pytest.fixture
  99. def longfile(longdir):
  100. name = "A" * 100
  101. path = op.join(longdir, name + "{}.txt")
  102. file = path.format("")
  103. _create_tree(file)
  104. assert op.exists(file) is True
  105. yield file
  106. @pytest.fixture
  107. def longfiles(longdir):
  108. name = "A" * 100
  109. path = op.join(longdir, name + "{}.txt")
  110. files = [path.format(index) for index in range(10)]
  111. [_create_tree(file) for file in files]
  112. assert all([op.exists(file) for file in files]) is True
  113. yield files
  114. # NOTE: both legacy and modern test "pass" on windows, however sometimes with the same path
  115. # they do not actually recycle files but delete them. Noticed this when testing with the
  116. # recycle bin open, noticed later tests actually worked, modern version can actually detect
  117. # when this happens but not stop it at this moment, and we need a way to verify it when testing.
  118. def test_trash_long_file_modern(longfile):
  119. _trash_file(longfile, s2t_modern)
  120. def test_trash_long_multifile_modern(longfiles):
  121. _trash_multifile(longfiles, s2t_modern)
  122. # @pytest.skipif(
  123. # op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
  124. # "Cannot trash long path from other drive",
  125. # )
  126. # def test_trash_long_folder_modern(self):
  127. # self._trash_folder(s2t_modern)
  128. def test_trash_long_file_legacy(longfile):
  129. _trash_file(longfile, s2t_legacy)
  130. def test_trash_long_multifile_legacy(longfiles):
  131. _trash_multifile(longfiles, s2t_legacy)
  132. # @pytest.skipif(
  133. # op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
  134. # "Cannot trash long path from other drive",
  135. # )
  136. # def test_trash_long_folder_legacy(self):
  137. # self._trash_folder(s2t_legacy)