203 lines
6.5 KiB

# encoding: utf-8
import pytest
import codecs
import os
import sys
from os import path as op
from send2trash.compat import PY3
from send2trash import TrashPermissionError
from configparser import ConfigParser
except ImportError:
# py2
from ConfigParser import ConfigParser # noqa: F401
from tempfile import mkdtemp, NamedTemporaryFile, mktemp
import shutil
import stat
if sys.platform != "win32":
import send2trash.plat_other
from send2trash.plat_other import send2trash as s2t
HOMETRASH = send2trash.plat_other.HOMETRASH
pytest.skip("Skipping non-windows tests", allow_module_level=True)
def testfile():
file = NamedTemporaryFile(dir=op.expanduser("~"), prefix="send2trash_test", delete=False)
assert op.exists( is True
yield file
# Cleanup trash files on supported platforms
if sys.platform != "win32":
name = op.basename(
# Remove trash files if they exist
if op.exists(op.join(HOMETRASH, "files", name)):
os.remove(op.join(HOMETRASH, "files", name))
os.remove(op.join(HOMETRASH, "info", name + ".trashinfo"))
if op.exists(
def testfiles():
files = list(
lambda index: NamedTemporaryFile(
dir=op.expanduser("~"), prefix="send2trash_test{}".format(index), delete=False,
[file.close() for file in files]
assert all([op.exists( for file in files]) is True
yield files
filenames = [op.basename( for file in files]
[os.remove(op.join(HOMETRASH, "files", filename)) for filename in filenames]
[os.remove(op.join(HOMETRASH, "info", filename + ".trashinfo")) for filename in filenames]
def test_trash(testfile):
assert op.exists( is False
def test_multitrash(testfiles):
filenames = [ for file in testfiles]
assert any([op.exists(filename) for filename in filenames]) is False
def touch(path):
with open(path, "a"):
os.utime(path, None)
def _filesys_enc():
enc = sys.getfilesystemencoding()
# Get canonical name of codec
return codecs.lookup(enc).name
def gen_unicode_file():
name = u"send2trash_tést1"
file = op.join(op.expanduser(b"~"), name.encode("utf-8"))
assert op.exists(file) is True
yield file
# Cleanup trash files on supported platforms
if sys.platform != "win32":
# Remove trash files if they exist
if op.exists(op.join(HOMETRASH, "files", name)):
os.remove(op.join(HOMETRASH, "files", name))
os.remove(op.join(HOMETRASH, "info", name + ".trashinfo"))
if op.exists(file):
@pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
def test_trash_bytes(gen_unicode_file):
assert not op.exists(gen_unicode_file)
@pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
def test_trash_unicode(gen_unicode_file):
assert not op.exists(gen_unicode_file)
class ExtVol:
def __init__(self, path):
self.trash_topdir = path
if PY3:
self.trash_topdir_b = os.fsencode(self.trash_topdir)
self.trash_topdir_b = self.trash_topdir
def s_getdev(path):
from send2trash.plat_other import is_parent
st = os.lstat(path)
if is_parent(self.trash_topdir, path):
return "dev"
return st.st_dev
def s_ismount(path):
if op.realpath(path) in (op.realpath(self.trash_topdir), op.realpath(self.trash_topdir_b),):
return True
return old_ismount(path)
self.old_ismount = old_ismount = op.ismount
self.old_getdev = send2trash.plat_other.get_dev
send2trash.plat_other.os.path.ismount = s_ismount
send2trash.plat_other.get_dev = s_getdev
def cleanup(self):
send2trash.plat_other.get_dev = self.old_getdev
send2trash.plat_other.os.path.ismount = self.old_ismount
def gen_ext_vol():
trash_topdir = mkdtemp(prefix="s2t")
volume = ExtVol(trash_topdir)
file_name = "test.txt"
file_path = op.join(volume.trash_topdir, file_name)
assert op.exists(file_path) is True
yield volume, file_name, file_path
def test_trash_topdir(gen_ext_vol):
trash_dir = op.join(gen_ext_vol[0].trash_topdir, ".Trash")
os.mkdir(trash_dir, 0o777 | stat.S_ISVTX)
assert op.exists(gen_ext_vol[2]) is False
assert op.exists(op.join(trash_dir, str(os.getuid()), "files", gen_ext_vol[1])) is True
assert op.exists(op.join(trash_dir, str(os.getuid()), "info", gen_ext_vol[1] + ".trashinfo",)) is True
# info relative path (if another test is added, with the same fileName/Path,
# then it gets renamed etc.)
cfg = ConfigParser(), str(os.getuid()), "info", gen_ext_vol[1] + ".trashinfo"))
assert (gen_ext_vol[1] == cfg.get("Trash Info", "Path", raw=True)) is True
def test_trash_topdir_fallback(gen_ext_vol):
assert op.exists(gen_ext_vol[2]) is False
assert (
op.exists(op.join(gen_ext_vol[0].trash_topdir, ".Trash-" + str(os.getuid()), "files", gen_ext_vol[1],)) is True
def test_trash_topdir_failure(gen_ext_vol):
os.chmod(gen_ext_vol[0].trash_topdir, 0o500) # not writable to induce the exception
pytest.raises(TrashPermissionError, s2t, [gen_ext_vol[2]])
os.chmod(gen_ext_vol[0].trash_topdir, 0o700) # writable to allow deletion
def test_trash_symlink(gen_ext_vol):
# Use mktemp (race conditioney but no symlink equivalent)
# Since is_parent uses realpath(), and our getdev uses is_parent,
# this should work
sl_dir = mktemp(prefix="s2t", dir=op.expanduser("~"))
os.mkdir(op.join(gen_ext_vol[0].trashTopdir, "subdir"), 0o700)
file_path = op.join(gen_ext_vol[0].trashTopdir, "subdir", gen_ext_vol[1])
os.symlink(op.join(gen_ext_vol[0].trashTopdir, "subdir"), sl_dir)
s2t(op.join(sl_dir, gen_ext_vol[1]))
assert op.exists(file_path) is False
assert (
op.exists(op.join(gen_ext_vol[0].trashTopdir, ".Trash-" + str(os.getuid()), "files", gen_ext_vol[1],)) is True