1
0
mirror of https://github.com/arsenetar/send2trash.git synced 2026-01-22 14:41:40 +00:00

Windows Performance Improvement & Multi-Item support (#43)

* Initial IFileOperation for Windows

- Try using IFileOperation instead of SHFileOperation
  - Use pywin32 to accomplish this
  - Implement fallback when pywin32 not available
- Handles paths like `C:\` just fine bu the `\\?\` paths in the test
  cause issue
- Add batching for IFileOperation version (performance)
- Minor formatting applied by editor

* Fix issue with paths starting with \\?\

- Strip these characters off if present just like old implementation

* Add windows version check, legacy list support

- Add check for windows version for IFileOperation
- Add list support to legacy version
- Remove some debugging code
- Fix bug in path converson

Not sure if there is a better way to layout this file

* Split plat_win into legacy and modern

* Update other platforms for list support

Formatter also ran on these so some other minor changes.

* Add unit tests for multi-file calls
This commit is contained in:
2020-06-03 11:49:41 -05:00
committed by GitHub
parent 9ede898c3e
commit d078554052
8 changed files with 424 additions and 198 deletions

View File

@@ -9,12 +9,16 @@ from tempfile import gettempdir
from send2trash import send2trash as s2t
@unittest.skipIf(sys.platform != 'win32', 'Windows only')
@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.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)
@@ -23,29 +27,38 @@ class TestNormal(unittest.TestCase):
dirname = op.dirname(path)
if not op.isdir(dirname):
os.makedirs(dirname)
with open(path, 'w') as writer:
writer.write('send2trash test')
with open(path, "w") as writer:
writer.write("send2trash test")
def test_trash_file(self):
s2t(self.file)
self.assertFalse(op.exists(self.file))
def test_trash_multifile(self):
s2t(self.files)
self.assertFalse(any([op.exists(file) for file in self.files]))
def test_file_not_found(self):
file = op.join(self.dirname, 'otherfile.txt')
file = op.join(self.dirname, "otherfile.txt")
self.assertRaises(WindowsError, s2t, file)
@unittest.skipIf(sys.platform != 'win32', 'Windows only')
@unittest.skipIf(sys.platform != "win32", "Windows only")
class TestLongPath(unittest.TestCase):
def setUp(self):
filename = 'A' * 100
self.dirname = '\\\\?\\' + op.join(gettempdir(), filename)
self.file = op.join(
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')
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]
def tearDown(self):
shutil.rmtree(self.dirname, ignore_errors=True)
@@ -54,16 +67,21 @@ class TestLongPath(unittest.TestCase):
dirname = op.dirname(path)
if not op.isdir(dirname):
os.makedirs(dirname)
with open(path, 'w') as writer:
writer.write('Looong filename!')
with open(path, "w") as writer:
writer.write("Looong filename!")
def test_trash_file(self):
s2t(self.file)
self.assertFalse(op.exists(self.file))
def test_trash_multifile(self):
s2t(self.files)
self.assertFalse(any([op.exists(file) for file in self.files]))
@unittest.skipIf(
op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
'Cannot trash long path from other drive')
"Cannot trash long path from other drive",
)
def test_trash_folder(self):
s2t(self.dirname)
self.assertFalse(op.exists(self.dirname))