Debian Codegraph @

tests/test_plat_win.py

raw symbols SCIP
1 # encoding: utf-8
2 import os
3 import shutil
4 import sys
5 import pytest
6 from os import path as op
7
8 from send2trash import send2trash as s2t
9
10 # import the two versions as well as the "automatic" version
11 if sys.platform == "win32":
12 from send2trash.win.modern import send2trash as s2t_modern
13 from send2trash.win.legacy import send2trash as s2t_legacy
14 else:
15 pytest.skip("Skipping windows-only tests", allow_module_level=True)
16
17
18 def _create_tree(path):
19 dirname = op.dirname(path)
20 if not op.isdir(dirname):
21 os.makedirs(dirname)
22 with open(path, "w") as writer:
23 writer.write("send2trash test")
24
25
26 @pytest.fixture
27 def testdir(tmp_path):
28 dirname = "\\\\?\\" + str(tmp_path)
29 assert op.exists(dirname) is True
30 yield dirname
31 shutil.rmtree(dirname, ignore_errors=True)
32
33
34 @pytest.fixture
35 def testfile(testdir):
36 file = op.join(testdir, "testfile.txt")
37 _create_tree(file)
38 assert op.exists(file) is True
39 yield file
40 # Note dir will cleanup the file
41
42
43 @pytest.fixture
44 def testfiles(testdir):
45 files = [op.join(testdir, "testfile{}.txt".format(index)) for index in range(10)]
46 [_create_tree(file) for file in files]
47 assert all([op.exists(file) for file in files]) is True
48 yield files
49 # Note dir will cleanup the files
50
51
52 def _trash_folder(dir, fcn):
53 fcn(dir)
54 assert op.exists(dir) is False
55
56
57 def _trash_file(file, fcn):
58 fcn(file)
59 assert op.exists(file) is False
60
61
62 def _trash_multifile(files, fcn):
63 fcn(files)
64 assert any([op.exists(file) for file in files]) is False
65
66
67 def _file_not_found(dir, fcn):
68 file = op.join(dir, "otherfile.txt")
69 pytest.raises(OSError, fcn, file)
70
71
72 def _multi_byte_unicode(dir, fcn):
73 single_file = op.join(dir, "😇.txt")
74 _create_tree(single_file)
75 assert op.exists(single_file) is True
76 fcn(single_file)
77 assert op.exists(single_file) is False
78 files = [op.join(dir, "😇{}.txt".format(index)) for index in range(10)]
79 [_create_tree(file) for file in files]
80 assert all([op.exists(file) for file in files]) is True
81 fcn(files)
82 assert any([op.exists(file) for file in files]) is False
83
84
85 def test_trash_folder(testdir):
86 _trash_folder(testdir, s2t)
87
88
89 def test_trash_file(testfile):
90 _trash_file(testfile, s2t)
91
92
93 def test_trash_multifile(testfiles):
94 _trash_multifile(testfiles, s2t)
95
96
97 def test_file_not_found(testdir):
98 _file_not_found(testdir, s2t)
99
100
101 def test_trash_folder_modern(testdir):
102 _trash_folder(testdir, s2t_modern)
103
104
105 def test_trash_file_modern(testfile):
106 _trash_file(testfile, s2t_modern)
107
108
109 def test_trash_multifile_modern(testfiles):
110 _trash_multifile(testfiles, s2t_modern)
111
112
113 def test_file_not_found_modern(testdir):
114 _file_not_found(testdir, s2t_modern)
115
116
117 def test_multi_byte_unicode_modern(testdir):
118 _multi_byte_unicode(testdir, s2t_modern)
119
120
121 def test_trash_folder_legacy(testdir):
122 _trash_folder(testdir, s2t_legacy)
123
124
125 def test_trash_file_legacy(testfile):
126 _trash_file(testfile, s2t_legacy)
127
128
129 def test_trash_multifile_legacy(testfiles):
130 _trash_multifile(testfiles, s2t_legacy)
131
132
133 def test_file_not_found_legacy(testdir):
134 _file_not_found(testdir, s2t_legacy)
135
136
137 def test_multi_byte_unicode_legacy(testdir):
138 _multi_byte_unicode(testdir, s2t_legacy)
139
140
141 # Long path tests
142 @pytest.fixture
143 def longdir(tmp_path):
144 dirname = "\\\\?\\" + str(tmp_path)
145 name = "A" * 100
146 yield op.join(dirname, name, name, name)
147 try:
148 shutil.rmtree(dirname, ignore_errors=True)
149 except TypeError:
150 pass
151
152
153 @pytest.fixture
154 def longfile(longdir):
155 name = "A" * 100
156 path = op.join(longdir, name + "{}.txt")
157 file = path.format("")
158 _create_tree(file)
159 assert op.exists(file) is True
160 yield file
161
162
163 @pytest.fixture
164 def longfiles(longdir):
165 name = "A" * 100
166 path = op.join(longdir, name + "{}.txt")
167 files = [path.format(index) for index in range(10)]
168 [_create_tree(file) for file in files]
169 assert all([op.exists(file) for file in files]) is True
170 yield files
171
172
173 # NOTE: both legacy and modern test "pass" on windows, however sometimes with the same path
174 # they do not actually recycle files but delete them. Noticed this when testing with the
175 # recycle bin open, noticed later tests actually worked, modern version can actually detect
176 # when this happens but not stop it at this moment, and we need a way to verify it when testing.
177 def test_trash_long_file_modern(longfile):
178 _trash_file(longfile, s2t_modern)
179
180
181 def test_trash_long_multifile_modern(longfiles):
182 _trash_multifile(longfiles, s2t_modern)
183
184
185 # @pytest.skipif(
186 # op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
187 # "Cannot trash long path from other drive",
188 # )
189 # def test_trash_long_folder_modern(self):
190 # self._trash_folder(s2t_modern)
191
192
193 def test_trash_long_file_legacy(longfile):
194 _trash_file(longfile, s2t_legacy)
195
196
197 def test_trash_long_multifile_legacy(longfiles):
198 _trash_multifile(longfiles, s2t_legacy)
199
200
201 # @pytest.skipif(
202 # op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
203 # "Cannot trash long path from other drive",
204 # )
205 # def test_trash_long_folder_legacy(self):
206 # self._trash_folder(s2t_legacy)
207
208
209 def test_trash_nothing_legacy():
210 try:
211 s2t_legacy([])
212 except Exception as ex:
213 assert False, "Exception thrown when trashing nothing: {}".format(ex)
214
215
216 def test_trash_nothing_modern():
217 try:
218 s2t_modern([])
219 except Exception as ex:
220 assert False, "Exception thrown when trashing nothing: {}".format(ex)