mirror of
https://github.com/arsenetar/send2trash.git
synced 2026-01-22 14:41:40 +00:00
Updates for tests on windows
- Other platform tests WIP - Windows long directory tests WIP
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
# coding: utf-8
|
||||
# encoding: utf-8
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import unittest
|
||||
import pytest
|
||||
from os import path as op
|
||||
from tempfile import gettempdir
|
||||
|
||||
from send2trash import send2trash as s2t
|
||||
|
||||
@@ -12,144 +11,165 @@ from send2trash import send2trash as s2t
|
||||
if sys.platform == "win32":
|
||||
from send2trash.plat_win_modern import send2trash as s2t_modern
|
||||
from send2trash.plat_win_legacy import send2trash as s2t_legacy
|
||||
else:
|
||||
pytest.skip("Skipping windows-only tests", allow_module_level=True)
|
||||
|
||||
|
||||
@unittest.skipIf(sys.platform != "win32", "Windows only")
|
||||
class TestNormal(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dirname = "\\\\?\\" + op.join(gettempdir(), "python.send2trash")
|
||||
self.file = op.join(self.dirname, "testfile.txt")
|
||||
self._create_tree(self.file)
|
||||
self.files = [
|
||||
op.join(self.dirname, "testfile{}.txt".format(index)) for index in range(10)
|
||||
]
|
||||
[self._create_tree(file) for file in self.files]
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.dirname, ignore_errors=True)
|
||||
|
||||
def _create_tree(self, path):
|
||||
dirname = op.dirname(path)
|
||||
if not op.isdir(dirname):
|
||||
os.makedirs(dirname)
|
||||
with open(path, "w") as writer:
|
||||
writer.write("send2trash test")
|
||||
|
||||
def _trash_file(self, fcn):
|
||||
fcn(self.file)
|
||||
self.assertFalse(op.exists(self.file))
|
||||
|
||||
def _trash_multifile(self, fcn):
|
||||
fcn(self.files)
|
||||
self.assertFalse(any([op.exists(file) for file in self.files]))
|
||||
|
||||
def _file_not_found(self, fcn):
|
||||
file = op.join(self.dirname, "otherfile.txt")
|
||||
self.assertRaises(WindowsError, fcn, file)
|
||||
|
||||
def test_trash_file(self):
|
||||
self._trash_file(s2t)
|
||||
|
||||
def test_trash_multifile(self):
|
||||
self._trash_multifile(s2t)
|
||||
|
||||
def test_file_not_found(self):
|
||||
self._file_not_found(s2t)
|
||||
|
||||
def test_trash_file_modern(self):
|
||||
self._trash_file(s2t_modern)
|
||||
|
||||
def test_trash_multifile_modern(self):
|
||||
self._trash_multifile(s2t_modern)
|
||||
|
||||
def test_file_not_found_modern(self):
|
||||
self._file_not_found(s2t_modern)
|
||||
|
||||
def test_trash_file_legacy(self):
|
||||
self._trash_file(s2t_legacy)
|
||||
|
||||
def test_trash_multifile_legacy(self):
|
||||
self._trash_multifile(s2t_legacy)
|
||||
|
||||
def test_file_not_found_legacy(self):
|
||||
self._file_not_found(s2t_legacy)
|
||||
def _create_tree(path):
|
||||
dirname = op.dirname(path)
|
||||
if not op.isdir(dirname):
|
||||
os.makedirs(dirname)
|
||||
with open(path, "w") as writer:
|
||||
writer.write("send2trash test")
|
||||
|
||||
|
||||
@unittest.skipIf(sys.platform != "win32", "Windows only")
|
||||
class TestLongPath(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.functions = {s2t: "auto", s2t_legacy: "legacy", s2t_modern: "modern"}
|
||||
filename = "A" * 100
|
||||
self.dirname = "\\\\?\\" + op.join(gettempdir(), filename)
|
||||
path = op.join(
|
||||
self.dirname,
|
||||
filename,
|
||||
filename, # From there, the path is not trashable from Explorer
|
||||
filename,
|
||||
filename + "{}.txt",
|
||||
)
|
||||
self.file = path.format("")
|
||||
self._create_tree(self.file)
|
||||
self.files = [path.format(index) for index in range(10)]
|
||||
[self._create_tree(file) for file in self.files]
|
||||
@pytest.fixture
|
||||
def testdir(tmp_path):
|
||||
dirname = "\\\\?\\" + str(tmp_path)
|
||||
yield dirname
|
||||
shutil.rmtree(dirname, ignore_errors=True)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.dirname, ignore_errors=True)
|
||||
|
||||
def _create_tree(self, path):
|
||||
dirname = op.dirname(path)
|
||||
if not op.isdir(dirname):
|
||||
os.makedirs(dirname)
|
||||
with open(path, "w") as writer:
|
||||
writer.write("Looong filename!")
|
||||
@pytest.fixture
|
||||
def testfile(testdir):
|
||||
file = op.join(testdir, "testfile.txt")
|
||||
_create_tree(file)
|
||||
yield file
|
||||
# Note dir will cleanup the file
|
||||
|
||||
def _trash_file(self, fcn):
|
||||
fcn(self.file)
|
||||
self.assertFalse(op.exists(self.file))
|
||||
|
||||
def _trash_multifile(self, fcn):
|
||||
fcn(self.files)
|
||||
self.assertFalse(any([op.exists(file) for file in self.files]))
|
||||
@pytest.fixture
|
||||
def testfiles(testdir):
|
||||
files = [op.join(testdir, "testfile{}.txt".format(index)) for index in range(10)]
|
||||
[_create_tree(file) for file in files]
|
||||
yield files
|
||||
# Note dir will cleanup the files
|
||||
|
||||
def _trash_folder(self, fcn):
|
||||
fcn(self.dirname)
|
||||
self.assertFalse(op.exists(self.dirname))
|
||||
|
||||
def test_trash_file(self):
|
||||
self._trash_file(s2t)
|
||||
def _trash_folder(dir, fcn):
|
||||
fcn(dir)
|
||||
assert op.exists(dir) is False
|
||||
|
||||
def test_trash_multifile(self):
|
||||
self._trash_multifile(s2t)
|
||||
|
||||
@unittest.skipIf(
|
||||
op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
||||
"Cannot trash long path from other drive",
|
||||
)
|
||||
def test_trash_folder(self):
|
||||
self._trash_folder(s2t)
|
||||
def _trash_file(file, fcn):
|
||||
fcn(file)
|
||||
assert op.exists(file) is False
|
||||
|
||||
def test_trash_file_modern(self):
|
||||
self._trash_file(s2t_modern)
|
||||
|
||||
def test_trash_multifile_modern(self):
|
||||
self._trash_multifile(s2t_modern)
|
||||
def _trash_multifile(files, fcn):
|
||||
fcn(files)
|
||||
assert any([op.exists(file) for file in files]) is False
|
||||
|
||||
@unittest.skipIf(
|
||||
op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
||||
"Cannot trash long path from other drive",
|
||||
)
|
||||
def test_trash_folder_modern(self):
|
||||
self._trash_folder(s2t_modern)
|
||||
|
||||
def test_trash_file_legacy(self):
|
||||
self._trash_file(s2t_legacy)
|
||||
def _file_not_found(dir, fcn):
|
||||
file = op.join(dir, "otherfile.txt")
|
||||
pytest.raises(WindowsError, fcn, file)
|
||||
|
||||
def test_trash_multifile_legacy(self):
|
||||
self._trash_multifile(s2t_legacy)
|
||||
|
||||
@unittest.skipIf(
|
||||
op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
||||
"Cannot trash long path from other drive",
|
||||
)
|
||||
def test_trash_folder_legacy(self):
|
||||
self._trash_folder(s2t_legacy)
|
||||
def test_trash_folder(testdir):
|
||||
_trash_folder(testdir, s2t)
|
||||
|
||||
|
||||
def test_trash_file(testfile):
|
||||
_trash_file(testfile, s2t)
|
||||
|
||||
|
||||
def test_trash_multifile(testfiles):
|
||||
_trash_multifile(testfiles, s2t)
|
||||
|
||||
|
||||
def test_file_not_found(testdir):
|
||||
_file_not_found(testdir, s2t)
|
||||
|
||||
|
||||
def test_trash_folder_modern(testdir):
|
||||
_trash_folder(testdir, s2t_modern)
|
||||
|
||||
|
||||
def test_trash_file_modern(testfile):
|
||||
_trash_file(testfile, s2t_modern)
|
||||
|
||||
|
||||
def test_trash_multifile_modern(testfiles):
|
||||
_trash_multifile(testfiles, s2t_modern)
|
||||
|
||||
|
||||
def test_file_not_found_modern(testdir):
|
||||
_file_not_found(testdir, s2t_modern)
|
||||
|
||||
|
||||
def test_trash_folder_legacy(testdir):
|
||||
_trash_folder(testdir, s2t_legacy)
|
||||
|
||||
|
||||
def test_trash_file_legacy(testfile):
|
||||
_trash_file(testfile, s2t_legacy)
|
||||
|
||||
|
||||
def test_trash_multifile_legacy(testfiles):
|
||||
_trash_multifile(testfiles, s2t_legacy)
|
||||
|
||||
|
||||
def test_file_not_found_legacy(testdir):
|
||||
_file_not_found(testdir, s2t_legacy)
|
||||
|
||||
|
||||
# Long path tests
|
||||
@pytest.fixture
|
||||
def longdir(tmp_path):
|
||||
dirname = "\\\\?\\" + str(tmp_path)
|
||||
name = "A" * 100
|
||||
yield op.join(dirname, name, name, name)
|
||||
shutil.rmtree(dirname, ignore_errors=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def longfile(longdir):
|
||||
name = "A" * 100
|
||||
path = op.join(longdir, name + "{}.txt")
|
||||
file = path.format("")
|
||||
_create_tree(file)
|
||||
yield file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def longfiles(longdir):
|
||||
name = "A" * 100
|
||||
path = op.join(longdir, name + "{}.txt")
|
||||
files = [path.format(index) for index in range(10)]
|
||||
[_create_tree(file) for file in files]
|
||||
yield files
|
||||
|
||||
|
||||
# NOTE: both legacy and modern test "pass" on windows, but actually are not moving files to the
|
||||
# recycle bin, this was tested on latest windows 10, thought to have worked previously
|
||||
def test_trash_long_file_modern(longfile):
|
||||
_trash_file(longfile, s2t_modern)
|
||||
|
||||
|
||||
def test_trash_long_multifile_modern(longfiles):
|
||||
_trash_multifile(longfiles, s2t_modern)
|
||||
|
||||
|
||||
# @pytest.skipif(
|
||||
# op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
||||
# "Cannot trash long path from other drive",
|
||||
# )
|
||||
# def test_trash_long_folder_modern(self):
|
||||
# self._trash_folder(s2t_modern)
|
||||
|
||||
|
||||
def test_trash_long_file_legacy(longfile):
|
||||
_trash_file(longfile, s2t_legacy)
|
||||
|
||||
|
||||
def test_trash_long_multifile_legacy(longfiles):
|
||||
_trash_multifile(longfiles, s2t_legacy)
|
||||
|
||||
|
||||
# @pytest.skipif(
|
||||
# op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
||||
# "Cannot trash long path from other drive",
|
||||
# )
|
||||
# def test_trash_long_folder_legacy(self):
|
||||
# self._trash_folder(s2t_legacy)
|
||||
|
||||
Reference in New Issue
Block a user