Debian Codegraph @

tests/test_plat_other.py

raw symbols SCIP
1 # encoding: utf-8
2 import pytest
3 import codecs
4 import os
5 import sys
6 from os import path as op
7 from send2trash.compat import PY3
8 from send2trash import TrashPermissionError
9
10 try:
11 from configparser import ConfigParser
12 except ImportError:
13 # py2
14 from ConfigParser import ConfigParser # noqa: F401
15
16 from tempfile import mkdtemp, NamedTemporaryFile
17 import shutil
18 import stat
19 import uuid
20
21 if sys.platform != "win32":
22 import send2trash.plat_other
23 from send2trash.plat_other import send2trash as s2t
24
25 INFO_SUFFIX = send2trash.plat_other.INFO_SUFFIX.decode()
26 HOMETRASH = send2trash.plat_other.HOMETRASH
27 else:
28 pytest.skip("Skipping non-windows tests", allow_module_level=True)
29
30
31 @pytest.fixture
32 def testfile():
33 file = NamedTemporaryFile(dir=op.expanduser("~"), prefix="send2trash_test", delete=False)
34 file.close()
35 assert op.exists(file.name) is True
36 yield file
37 # Cleanup trash files on supported platforms
38 if sys.platform != "win32":
39 name = op.basename(file.name)
40 # Remove trash files if they exist
41 if op.exists(op.join(HOMETRASH, "files", name)):
42 os.remove(op.join(HOMETRASH, "files", name))
43 os.remove(op.join(HOMETRASH, "info", name + INFO_SUFFIX))
44 if op.exists(file.name):
45 os.remove(file.name)
46
47
48 @pytest.fixture
49 def testfiles():
50 files = list(
51 map(
52 lambda index: NamedTemporaryFile(
53 dir=op.expanduser("~"),
54 prefix="send2trash_test{}".format(index),
55 delete=False,
56 ),
57 range(10),
58 )
59 )
60 [file.close() for file in files]
61 assert all([op.exists(file.name) for file in files]) is True
62 yield files
63 filenames = [op.basename(file.name) for file in files]
64 [os.remove(op.join(HOMETRASH, "files", filename)) for filename in filenames]
65 [os.remove(op.join(HOMETRASH, "info", filename + INFO_SUFFIX)) for filename in filenames]
66
67
68 def test_trash(testfile):
69 s2t(testfile.name)
70 assert op.exists(testfile.name) is False
71
72
73 def test_multitrash(testfiles):
74 filenames = [file.name for file in testfiles]
75 s2t(filenames)
76 assert any([op.exists(filename) for filename in filenames]) is False
77
78
79 def touch(path):
80 with open(path, "a"):
81 os.utime(path, None)
82
83
84 def _filesys_enc():
85 enc = sys.getfilesystemencoding()
86 # Get canonical name of codec
87 return codecs.lookup(enc).name
88
89
90 @pytest.fixture
91 def gen_unicode_file():
92 name = u"send2trash_tést1"
93 file = op.join(op.expanduser(b"~"), name.encode("utf-8"))
94 touch(file)
95 assert op.exists(file) is True
96 yield file
97 # Cleanup trash files on supported platforms
98 if sys.platform != "win32" and op.exists(op.join(HOMETRASH, "files", name)):
99 os.remove(op.join(HOMETRASH, "files", name))
100 os.remove(op.join(HOMETRASH, "info", name + INFO_SUFFIX))
101 if op.exists(file):
102 os.remove(file)
103
104
105 @pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
106 def test_trash_bytes(gen_unicode_file):
107 s2t(gen_unicode_file)
108 assert not op.exists(gen_unicode_file)
109
110
111 @pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
112 def test_trash_unicode(gen_unicode_file):
113 s2t(gen_unicode_file.decode(sys.getfilesystemencoding()))
114 assert not op.exists(gen_unicode_file)
115
116
117 class ExtVol:
118 def __init__(self, path):
119 self.trash_topdir = path
120 if PY3:
121 self.trash_topdir_b = os.fsencode(self.trash_topdir)
122 else:
123 self.trash_topdir_b = self.trash_topdir
124
125 def s_getdev(path):
126 from send2trash.plat_other import is_parent
127
128 st = os.lstat(path)
129 if is_parent(self.trash_topdir, path):
130 return "dev"
131 return st.st_dev
132
133 def s_ismount(path):
134 if op.realpath(path) in (
135 op.realpath(self.trash_topdir),
136 op.realpath(self.trash_topdir_b),
137 ):
138 return True
139 return old_ismount(path)
140
141 self.old_ismount = old_ismount = op.ismount
142 self.old_getdev = send2trash.plat_other.get_dev
143 send2trash.plat_other.os.path.ismount = s_ismount
144 send2trash.plat_other.get_dev = s_getdev
145
146 def cleanup(self):
147 send2trash.plat_other.get_dev = self.old_getdev
148 send2trash.plat_other.os.path.ismount = self.old_ismount
149 shutil.rmtree(self.trash_topdir)
150
151
152 @pytest.fixture
153 def gen_ext_vol():
154 trash_topdir = mkdtemp(prefix="s2t")
155 volume = ExtVol(trash_topdir)
156 file_name = "test.txt"
157 file_path = op.join(volume.trash_topdir, file_name)
158 touch(file_path)
159 assert op.exists(file_path) is True
160 yield volume, file_name, file_path
161 volume.cleanup()
162
163
164 def test_trash_topdir(gen_ext_vol):
165 trash_dir = op.join(gen_ext_vol[0].trash_topdir, ".Trash")
166 os.mkdir(trash_dir, 0o777 | stat.S_ISVTX)
167
168 s2t(gen_ext_vol[2])
169 assert op.exists(gen_ext_vol[2]) is False
170 assert op.exists(op.join(trash_dir, str(os.getuid()), "files", gen_ext_vol[1])) is True
171 assert (
172 op.exists(
173 op.join(
174 trash_dir,
175 str(os.getuid()),
176 "info",
177 gen_ext_vol[1] + INFO_SUFFIX,
178 )
179 )
180 is True
181 )
182 # info relative path (if another test is added, with the same fileName/Path,
183 # then it gets renamed etc.)
184 cfg = ConfigParser()
185 cfg.read(op.join(trash_dir, str(os.getuid()), "info", gen_ext_vol[1] + INFO_SUFFIX))
186 assert (gen_ext_vol[1] == cfg.get("Trash Info", "Path", raw=True)) is True
187
188
189 def test_trash_topdir_fallback(gen_ext_vol):
190 s2t(gen_ext_vol[2])
191 assert op.exists(gen_ext_vol[2]) is False
192 assert (
193 op.exists(
194 op.join(
195 gen_ext_vol[0].trash_topdir,
196 ".Trash-" + str(os.getuid()),
197 "files",
198 gen_ext_vol[1],
199 )
200 )
201 is True
202 )
203
204
205 def test_trash_topdir_failure(gen_ext_vol):
206 os.chmod(gen_ext_vol[0].trash_topdir, 0o500) # not writable to induce the exception
207 pytest.raises(TrashPermissionError, s2t, [gen_ext_vol[2]])
208 os.chmod(gen_ext_vol[0].trash_topdir, 0o700) # writable to allow deletion
209
210
211 def test_trash_symlink(gen_ext_vol):
212 # Generating a random uuid named path for symlink
213 sl_dir = op.join(op.expanduser("~"), "s2t_" + str(uuid.uuid4()))
214 os.mkdir(op.join(gen_ext_vol[0].trash_topdir, "subdir"), 0o700)
215 file_path = op.join(gen_ext_vol[0].trash_topdir, "subdir", gen_ext_vol[1])
216 touch(file_path)
217 os.symlink(op.join(gen_ext_vol[0].trash_topdir, "subdir"), sl_dir)
218 s2t(op.join(sl_dir, gen_ext_vol[1]))
219 assert op.exists(file_path) is False
220 assert (
221 op.exists(
222 op.join(
223 gen_ext_vol[0].trash_topdir,
224 ".Trash-" + str(os.getuid()),
225 "files",
226 gen_ext_vol[1],
227 )
228 )
229 is True
230 )
231 os.remove(sl_dir)