2017-08-01 10:28:40 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
import codecs
|
2011-03-10 09:55:46 +00:00
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
from os import path as op
|
2011-03-13 18:43:24 +00:00
|
|
|
import send2trash.plat_other
|
|
|
|
from send2trash.plat_other import send2trash as s2t
|
2017-08-04 00:47:58 +00:00
|
|
|
from send2trash.compat import PY3
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2018-07-26 12:30:39 +00:00
|
|
|
try:
|
|
|
|
from configparser import ConfigParser
|
|
|
|
except ImportError:
|
|
|
|
# py2
|
|
|
|
from ConfigParser import ConfigParser
|
2011-03-13 19:17:13 +00:00
|
|
|
from tempfile import mkdtemp, NamedTemporaryFile, mktemp
|
2011-03-13 18:43:24 +00:00
|
|
|
import shutil
|
|
|
|
import stat
|
2017-08-01 10:28:40 +00:00
|
|
|
import sys
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2011-03-13 18:43:24 +00:00
|
|
|
# Could still use cleaning up. But no longer relies on ramfs.
|
2011-03-12 10:48:19 +00:00
|
|
|
|
2017-08-01 10:28:40 +00:00
|
|
|
HOMETRASH = send2trash.plat_other.HOMETRASH
|
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2011-03-13 18:43:24 +00:00
|
|
|
def touch(path):
|
2020-06-03 16:49:41 +00:00
|
|
|
with open(path, "a"):
|
2017-08-04 01:03:34 +00:00
|
|
|
os.utime(path, None)
|
2011-03-12 10:48:19 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2011-03-10 09:55:46 +00:00
|
|
|
class TestHomeTrash(unittest.TestCase):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.file = NamedTemporaryFile(
|
2020-06-03 16:49:41 +00:00
|
|
|
dir=op.expanduser("~"), prefix="send2trash_test", delete=False
|
|
|
|
)
|
2011-03-10 09:55:46 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def test_trash(self):
|
|
|
|
s2t(self.file.name)
|
|
|
|
self.assertFalse(op.exists(self.file.name))
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
name = op.basename(self.file.name)
|
2020-06-03 16:49:41 +00:00
|
|
|
os.remove(op.join(HOMETRASH, "files", name))
|
|
|
|
os.remove(op.join(HOMETRASH, "info", name + ".trashinfo"))
|
|
|
|
|
|
|
|
|
|
|
|
class TestHomeMultiTrash(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.files = list(
|
|
|
|
map(
|
|
|
|
lambda index: NamedTemporaryFile(
|
|
|
|
dir=op.expanduser("~"),
|
|
|
|
prefix="send2trash_test{}".format(index),
|
|
|
|
delete=False,
|
|
|
|
),
|
|
|
|
range(10),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_multitrash(self):
|
|
|
|
filenames = [file.name for file in self.files]
|
|
|
|
s2t(filenames)
|
|
|
|
self.assertFalse(any([op.exists(filename) for filename in filenames]))
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
filenames = [op.basename(file.name) for file in self.files]
|
|
|
|
[os.remove(op.join(HOMETRASH, "files", filename)) for filename in filenames]
|
|
|
|
[
|
|
|
|
os.remove(op.join(HOMETRASH, "info", filename + ".trashinfo"))
|
|
|
|
for filename in filenames
|
|
|
|
]
|
2011-03-13 18:43:24 +00:00
|
|
|
|
2017-08-01 10:28:40 +00:00
|
|
|
|
|
|
|
def _filesys_enc():
|
2017-08-04 01:03:34 +00:00
|
|
|
enc = sys.getfilesystemencoding()
|
|
|
|
# Get canonical name of codec
|
|
|
|
return codecs.lookup(enc).name
|
2017-08-01 10:28:40 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
|
|
|
@unittest.skipIf(_filesys_enc() == "ascii", "ASCII filesystem")
|
2017-08-01 10:28:40 +00:00
|
|
|
class TestUnicodeTrash(unittest.TestCase):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
self.name = u"send2trash_tést1"
|
|
|
|
self.file = op.join(op.expanduser(b"~"), self.name.encode("utf-8"))
|
2017-08-04 01:03:34 +00:00
|
|
|
touch(self.file)
|
2017-08-01 10:28:40 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def test_trash_bytes(self):
|
|
|
|
s2t(self.file)
|
|
|
|
assert not op.exists(self.file)
|
2017-08-01 10:28:40 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def test_trash_unicode(self):
|
|
|
|
s2t(self.file.decode(sys.getfilesystemencoding()))
|
|
|
|
assert not op.exists(self.file)
|
2017-08-01 10:28:40 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def tearDown(self):
|
|
|
|
if op.exists(self.file):
|
|
|
|
os.remove(self.file)
|
2017-08-01 10:28:40 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
trash_file = op.join(HOMETRASH, "files", self.name)
|
2017-08-04 01:03:34 +00:00
|
|
|
if op.exists(trash_file):
|
|
|
|
os.remove(trash_file)
|
2020-06-03 16:49:41 +00:00
|
|
|
os.remove(op.join(HOMETRASH, "info", self.name + ".trashinfo"))
|
|
|
|
|
2011-03-10 09:55:46 +00:00
|
|
|
|
|
|
|
#
|
2011-03-13 18:43:24 +00:00
|
|
|
# Tests for files on some other volume than the user's home directory.
|
|
|
|
#
|
|
|
|
# What we need to stub:
|
|
|
|
# * plat_other.get_dev (to make sure the file will not be on the home dir dev)
|
|
|
|
# * os.path.ismount (to make our topdir look like a top dir)
|
2011-03-10 09:55:46 +00:00
|
|
|
#
|
2011-03-13 18:43:24 +00:00
|
|
|
class TestExtVol(unittest.TestCase):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
self.trashTopdir = mkdtemp(prefix="s2t")
|
2017-08-04 01:03:34 +00:00
|
|
|
if PY3:
|
|
|
|
trashTopdir_b = os.fsencode(self.trashTopdir)
|
|
|
|
else:
|
|
|
|
trashTopdir_b = self.trashTopdir
|
2020-06-03 16:49:41 +00:00
|
|
|
self.fileName = "test.txt"
|
2017-08-04 01:03:34 +00:00
|
|
|
self.filePath = op.join(self.trashTopdir, self.fileName)
|
|
|
|
touch(self.filePath)
|
|
|
|
|
|
|
|
self.old_ismount = old_ismount = op.ismount
|
|
|
|
self.old_getdev = send2trash.plat_other.get_dev
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def s_getdev(path):
|
|
|
|
from send2trash.plat_other import is_parent
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
st = os.lstat(path)
|
|
|
|
if is_parent(self.trashTopdir, path):
|
2020-06-03 16:49:41 +00:00
|
|
|
return "dev"
|
2017-08-04 01:03:34 +00:00
|
|
|
return st.st_dev
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def s_ismount(path):
|
2020-06-03 16:49:41 +00:00
|
|
|
if op.realpath(path) in (
|
|
|
|
op.realpath(self.trashTopdir),
|
|
|
|
op.realpath(trashTopdir_b),
|
|
|
|
):
|
2017-08-04 01:03:34 +00:00
|
|
|
return True
|
|
|
|
return old_ismount(path)
|
|
|
|
|
|
|
|
send2trash.plat_other.os.path.ismount = s_ismount
|
|
|
|
send2trash.plat_other.get_dev = s_getdev
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
send2trash.plat_other.get_dev = self.old_getdev
|
|
|
|
send2trash.plat_other.os.path.ismount = self.old_ismount
|
|
|
|
shutil.rmtree(self.trashTopdir)
|
2011-03-10 09:55:46 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2011-03-13 18:43:24 +00:00
|
|
|
class TestTopdirTrash(TestExtVol):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
TestExtVol.setUp(self)
|
|
|
|
# Create a .Trash dir w/ a sticky bit
|
2020-06-03 16:49:41 +00:00
|
|
|
self.trashDir = op.join(self.trashTopdir, ".Trash")
|
|
|
|
os.mkdir(self.trashDir, 0o777 | stat.S_ISVTX)
|
2017-08-04 01:03:34 +00:00
|
|
|
|
|
|
|
def test_trash(self):
|
|
|
|
s2t(self.filePath)
|
|
|
|
self.assertFalse(op.exists(self.filePath))
|
2020-06-03 16:49:41 +00:00
|
|
|
self.assertTrue(
|
|
|
|
op.exists(op.join(self.trashDir, str(os.getuid()), "files", self.fileName))
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
op.exists(
|
|
|
|
op.join(
|
|
|
|
self.trashDir,
|
|
|
|
str(os.getuid()),
|
|
|
|
"info",
|
|
|
|
self.fileName + ".trashinfo",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-08-04 01:03:34 +00:00
|
|
|
# info relative path (if another test is added, with the same fileName/Path,
|
|
|
|
# then it gets renamed etc.)
|
|
|
|
cfg = ConfigParser()
|
2020-06-03 16:49:41 +00:00
|
|
|
cfg.read(
|
|
|
|
op.join(
|
|
|
|
self.trashDir, str(os.getuid()), "info", self.fileName + ".trashinfo"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.assertEqual(self.fileName, cfg.get("Trash Info", "Path", raw=True))
|
|
|
|
|
2011-03-10 09:55:46 +00:00
|
|
|
|
|
|
|
# Test .Trash-UID
|
2011-03-13 18:43:24 +00:00
|
|
|
class TestTopdirTrashFallback(TestExtVol):
|
2017-08-04 01:03:34 +00:00
|
|
|
def test_trash(self):
|
|
|
|
touch(self.filePath)
|
|
|
|
s2t(self.filePath)
|
|
|
|
self.assertFalse(op.exists(self.filePath))
|
2020-06-03 16:49:41 +00:00
|
|
|
self.assertTrue(
|
|
|
|
op.exists(
|
|
|
|
op.join(
|
|
|
|
self.trashTopdir,
|
|
|
|
".Trash-" + str(os.getuid()),
|
|
|
|
"files",
|
|
|
|
self.fileName,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2011-03-10 09:55:46 +00:00
|
|
|
|
|
|
|
# Test failure
|
2011-03-13 18:43:24 +00:00
|
|
|
class TestTopdirFailure(TestExtVol):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
TestExtVol.setUp(self)
|
2020-06-03 16:49:41 +00:00
|
|
|
os.chmod(self.trashTopdir, 0o500) # not writable to induce the exception
|
2011-03-10 09:55:46 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def test_trash(self):
|
|
|
|
with self.assertRaises(OSError):
|
|
|
|
s2t(self.filePath)
|
|
|
|
self.assertTrue(op.exists(self.filePath))
|
2011-03-10 09:55:46 +00:00
|
|
|
|
2017-08-04 01:03:34 +00:00
|
|
|
def tearDown(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
os.chmod(self.trashTopdir, 0o700) # writable to allow deletion
|
2017-08-04 01:03:34 +00:00
|
|
|
TestExtVol.tearDown(self)
|
2011-03-13 18:43:24 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
2011-03-13 19:17:13 +00:00
|
|
|
# Make sure it will find the mount point properly for a file in a symlinked path
|
|
|
|
class TestSymlink(TestExtVol):
|
2017-08-04 01:03:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
TestExtVol.setUp(self)
|
|
|
|
# Use mktemp (race conditioney but no symlink equivalent)
|
|
|
|
# Since is_parent uses realpath(), and our getdev uses is_parent,
|
|
|
|
# this should work
|
2020-06-03 16:49:41 +00:00
|
|
|
self.slDir = mktemp(prefix="s2t", dir=op.expanduser("~"))
|
2017-08-04 01:03:34 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
os.mkdir(op.join(self.trashTopdir, "subdir"), 0o700)
|
|
|
|
self.filePath = op.join(self.trashTopdir, "subdir", self.fileName)
|
2017-08-04 01:03:34 +00:00
|
|
|
touch(self.filePath)
|
2020-06-03 16:49:41 +00:00
|
|
|
os.symlink(op.join(self.trashTopdir, "subdir"), self.slDir)
|
2017-08-04 01:03:34 +00:00
|
|
|
|
|
|
|
def test_trash(self):
|
|
|
|
s2t(op.join(self.slDir, self.fileName))
|
|
|
|
self.assertFalse(op.exists(self.filePath))
|
2020-06-03 16:49:41 +00:00
|
|
|
self.assertTrue(
|
|
|
|
op.exists(
|
|
|
|
op.join(
|
|
|
|
self.trashTopdir,
|
|
|
|
".Trash-" + str(os.getuid()),
|
|
|
|
"files",
|
|
|
|
self.fileName,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-08-04 01:03:34 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.remove(self.slDir)
|
|
|
|
TestExtVol.tearDown(self)
|
2011-03-13 19:17:13 +00:00
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-08-04 01:03:34 +00:00
|
|
|
unittest.main()
|