More test fixes

- Fix travis windows env
- Fix exception type in test_plat_win
- Finish converting tests in test_plat_other, fix exception type
This commit is contained in:
Andrew Senetar 2021-03-10 18:57:35 -06:00
parent 37be84d46e
commit af0c1ba704
Signed by: arsenetar
GPG Key ID: C63300DCE48AB2F1
3 changed files with 187 additions and 207 deletions

View File

@ -4,7 +4,7 @@ matrix:
- os: windows - os: windows
language: sh language: sh
python: "3.8" python: "3.8"
env: "PATH=/c/Python:/c/Python/Scripts:$PATH" env: "PATH=/c/Python38:/c/Python38/Scripts:$PATH"
# Perform the manual steps on windows to install python3 # Perform the manual steps on windows to install python3
before_install: before_install:
- choco install python --version=3.8.6 - choco install python --version=3.8.6

View File

@ -5,12 +5,13 @@ import os
import sys import sys
from os import path as op from os import path as op
from send2trash.compat import PY3 from send2trash.compat import PY3
from send2trash import TrashPermissionError
try: try:
from configparser import ConfigParser from configparser import ConfigParser
except ImportError: except ImportError:
# py2 # py2
from ConfigParser import ConfigParser from ConfigParser import ConfigParser # noqa: F401
from tempfile import mkdtemp, NamedTemporaryFile, mktemp from tempfile import mkdtemp, NamedTemporaryFile, mktemp
import shutil import shutil
@ -24,228 +25,207 @@ if sys.platform != "win32":
else: else:
pytest.skip("Skipping non-windows tests", allow_module_level=True) pytest.skip("Skipping non-windows tests", allow_module_level=True)
# Could still use cleaning up. But no longer relies on ramfs.
# def touch(path): @pytest.fixture
# with open(path, "a"): def testfile():
# os.utime(path, None) file = NamedTemporaryFile(
dir=op.expanduser("~"), prefix="send2trash_test", delete=False
)
file.close()
yield file
# Cleanup trash files on supported platforms
if sys.platform != "win32":
name = op.basename(file.name)
# 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.name):
os.remove(file.name)
# class TestHomeTrash(unittest.TestCase): @pytest.fixture
# def setUp(self): def testfiles():
# self.file = NamedTemporaryFile( files = list(
# dir=op.expanduser("~"), prefix="send2trash_test", delete=False map(
# ) lambda index: NamedTemporaryFile(
dir=op.expanduser("~"),
# def test_trash(self): prefix="send2trash_test{}".format(index),
# s2t(self.file.name) delete=False,
# self.assertFalse(op.exists(self.file.name)) ),
range(10),
# def tearDown(self): )
# name = op.basename(self.file.name) )
# os.remove(op.join(HOMETRASH, "files", name)) [file.close() for file in files]
# os.remove(op.join(HOMETRASH, "info", name + ".trashinfo")) yield files
filenames = [op.basename(file.name) 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
]
# class TestHomeMultiTrash(unittest.TestCase): def test_trash(testfile):
# def setUp(self): s2t(testfile.name)
# self.files = list( assert op.exists(testfile.name) is False
# 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
# ]
# def _filesys_enc(): def test_multitrash(testfiles):
# enc = sys.getfilesystemencoding() filenames = [file.name for file in testfiles]
# # Get canonical name of codec s2t(filenames)
# return codecs.lookup(enc).name assert any([op.exists(filename) for filename in filenames]) is False
# @unittest.skipIf(_filesys_enc() == "ascii", "ASCII filesystem") def touch(path):
# class TestUnicodeTrash(unittest.TestCase): with open(path, "a"):
# def setUp(self): os.utime(path, None)
# self.name = u"send2trash_tést1"
# self.file = op.join(op.expanduser(b"~"), self.name.encode("utf-8"))
# touch(self.file)
# def test_trash_bytes(self):
# s2t(self.file)
# assert not op.exists(self.file)
# def test_trash_unicode(self):
# s2t(self.file.decode(sys.getfilesystemencoding()))
# assert not op.exists(self.file)
# def tearDown(self):
# if op.exists(self.file):
# os.remove(self.file)
# trash_file = op.join(HOMETRASH, "files", self.name)
# if op.exists(trash_file):
# os.remove(trash_file)
# os.remove(op.join(HOMETRASH, "info", self.name + ".trashinfo"))
# # def _filesys_enc():
# # Tests for files on some other volume than the user's home directory. enc = sys.getfilesystemencoding()
# # # Get canonical name of codec
# # What we need to stub: return codecs.lookup(enc).name
# # * 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)
# #
# class TestExtVol(unittest.TestCase):
# def setUp(self):
# self.trashTopdir = mkdtemp(prefix="s2t")
# if PY3:
# trashTopdir_b = os.fsencode(self.trashTopdir)
# else:
# trashTopdir_b = self.trashTopdir
# self.fileName = "test.txt"
# 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
# def s_getdev(path):
# from send2trash.plat_other import is_parent
# st = os.lstat(path)
# if is_parent(self.trashTopdir, path):
# return "dev"
# return st.st_dev
# def s_ismount(path):
# if op.realpath(path) in (
# op.realpath(self.trashTopdir),
# op.realpath(trashTopdir_b),
# ):
# 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)
# class TestTopdirTrash(TestExtVol): @pytest.fixture
# def setUp(self): def testUnicodefile():
# TestExtVol.setUp(self) name = u"send2trash_tést1"
# # Create a .Trash dir w/ a sticky bit file = op.join(op.expanduser(b"~"), name.encode("utf-8"))
# self.trashDir = op.join(self.trashTopdir, ".Trash") touch(file)
# os.mkdir(self.trashDir, 0o777 | stat.S_ISVTX) yield file
# Cleanup trash files on supported platforms
# def test_trash(self): if sys.platform != "win32":
# s2t(self.filePath) # Remove trash files if they exist
# self.assertFalse(op.exists(self.filePath)) if op.exists(op.join(HOMETRASH, "files", name)):
# self.assertTrue( os.remove(op.join(HOMETRASH, "files", name))
# op.exists(op.join(self.trashDir, str(os.getuid()), "files", self.fileName)) os.remove(op.join(HOMETRASH, "info", name + ".trashinfo"))
# ) if op.exists(file):
# self.assertTrue( os.remove(file)
# op.exists(
# op.join(
# self.trashDir,
# str(os.getuid()),
# "info",
# self.fileName + ".trashinfo",
# )
# )
# )
# # info relative path (if another test is added, with the same fileName/Path,
# # then it gets renamed etc.)
# cfg = ConfigParser()
# cfg.read(
# op.join(
# self.trashDir, str(os.getuid()), "info", self.fileName + ".trashinfo"
# )
# )
# self.assertEqual(self.fileName, cfg.get("Trash Info", "Path", raw=True))
# # Test .Trash-UID @pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
# class TestTopdirTrashFallback(TestExtVol): def test_trash_bytes(testUnicodefile):
# def test_trash(self): s2t(testUnicodefile)
# touch(self.filePath) assert not op.exists(testUnicodefile)
# s2t(self.filePath)
# self.assertFalse(op.exists(self.filePath))
# self.assertTrue(
# op.exists(
# op.join(
# self.trashTopdir,
# ".Trash-" + str(os.getuid()),
# "files",
# self.fileName,
# )
# )
# )
# # Test failure @pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
# class TestTopdirFailure(TestExtVol): def test_trash_unicode(testUnicodefile):
# def setUp(self): s2t(testUnicodefile.decode(sys.getfilesystemencoding()))
# TestExtVol.setUp(self) assert not op.exists(testUnicodefile)
# os.chmod(self.trashTopdir, 0o500) # not writable to induce the exception
# def test_trash(self):
# with self.assertRaises(OSError):
# s2t(self.filePath)
# self.assertTrue(op.exists(self.filePath))
# def tearDown(self):
# os.chmod(self.trashTopdir, 0o700) # writable to allow deletion
# TestExtVol.tearDown(self)
# # Make sure it will find the mount point properly for a file in a symlinked path class ExtVol:
# class TestSymlink(TestExtVol): def __init__(self, path):
# def setUp(self): self.trashTopdir = path
# TestExtVol.setUp(self) if PY3:
# # Use mktemp (race conditioney but no symlink equivalent) self.trashTopdir_b = os.fsencode(self.trashTopdir)
# # Since is_parent uses realpath(), and our getdev uses is_parent, else:
# # this should work self.trashTopdir_b = self.trashTopdir
# self.slDir = mktemp(prefix="s2t", dir=op.expanduser("~"))
# os.mkdir(op.join(self.trashTopdir, "subdir"), 0o700) def s_getdev(path):
# self.filePath = op.join(self.trashTopdir, "subdir", self.fileName) from send2trash.plat_other import is_parent
# touch(self.filePath)
# os.symlink(op.join(self.trashTopdir, "subdir"), self.slDir)
# def test_trash(self): st = os.lstat(path)
# s2t(op.join(self.slDir, self.fileName)) if is_parent(self.trashTopdir, path):
# self.assertFalse(op.exists(self.filePath)) return "dev"
# self.assertTrue( return st.st_dev
# op.exists(
# op.join(
# self.trashTopdir,
# ".Trash-" + str(os.getuid()),
# "files",
# self.fileName,
# )
# )
# )
# def tearDown(self): def s_ismount(path):
# os.remove(self.slDir) if op.realpath(path) in (
# TestExtVol.tearDown(self) op.realpath(self.trashTopdir),
op.realpath(self.trashTopdir_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
shutil.rmtree(self.trashTopdir)
@pytest.fixture
def testExtVol():
trashTopdir = mkdtemp(prefix="s2t")
volume = ExtVol(trashTopdir)
fileName = "test.txt"
filePath = op.join(volume.trashTopdir, fileName)
touch(filePath)
yield volume, fileName, filePath
volume.cleanup()
def test_trash_topdir(testExtVol):
trashDir = op.join(testExtVol[0].trashTopdir, ".Trash")
os.mkdir(trashDir, 0o777 | stat.S_ISVTX)
s2t(testExtVol[2])
assert op.exists(testExtVol[2]) is False
assert (
op.exists(op.join(trashDir, str(os.getuid()), "files", testExtVol[1])) is True
)
assert (
op.exists(
op.join(trashDir, str(os.getuid()), "info", testExtVol[1] + ".trashinfo",)
)
is True
)
# info relative path (if another test is added, with the same fileName/Path,
# then it gets renamed etc.)
cfg = ConfigParser()
cfg.read(op.join(trashDir, str(os.getuid()), "info", testExtVol[1] + ".trashinfo"))
assert (testExtVol[1] == cfg.get("Trash Info", "Path", raw=True)) is True
def test_trash_topdir_fallback(testExtVol):
s2t(testExtVol[2])
assert op.exists(testExtVol[2]) is False
assert (
op.exists(
op.join(
testExtVol[0].trashTopdir,
".Trash-" + str(os.getuid()),
"files",
testExtVol[1],
)
)
is True
)
def test_trash_topdir_failure(testExtVol):
os.chmod(testExtVol[0].trashTopdir, 0o500) # not writable to induce the exception
pytest.raises(TrashPermissionError, s2t, [testExtVol[2]])
os.chmod(testExtVol[0].trashTopdir, 0o700) # writable to allow deletion
def test_trash_symlink(testExtVol):
# Use mktemp (race conditioney but no symlink equivalent)
# Since is_parent uses realpath(), and our getdev uses is_parent,
# this should work
slDir = mktemp(prefix="s2t", dir=op.expanduser("~"))
os.mkdir(op.join(testExtVol[0].trashTopdir, "subdir"), 0o700)
filePath = op.join(testExtVol[0].trashTopdir, "subdir", testExtVol[1])
touch(filePath)
os.symlink(op.join(testExtVol[0].trashTopdir, "subdir"), slDir)
s2t(op.join(slDir, testExtVol[1]))
assert op.exists(filePath) is False
assert (
op.exists(
op.join(
testExtVol[0].trashTopdir,
".Trash-" + str(os.getuid()),
"files",
testExtVol[1],
)
)
is True
)
os.remove(slDir)

View File

@ -63,7 +63,7 @@ def _trash_multifile(files, fcn):
def _file_not_found(dir, fcn): def _file_not_found(dir, fcn):
file = op.join(dir, "otherfile.txt") file = op.join(dir, "otherfile.txt")
pytest.raises(WindowsError, fcn, file) pytest.raises(OSError, fcn, file)
def test_trash_folder(testdir): def test_trash_folder(testdir):