2018-02-16 14:07:05 +00:00
|
|
|
# coding: utf-8
|
|
|
|
import os
|
2019-04-30 16:28:09 +00:00
|
|
|
import shutil
|
2018-02-16 14:07:05 +00:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from os import path as op
|
|
|
|
from tempfile import gettempdir
|
|
|
|
|
|
|
|
from send2trash import send2trash as s2t
|
|
|
|
|
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
@unittest.skipIf(sys.platform != "win32", "Windows only")
|
2019-04-30 16:28:09 +00:00
|
|
|
class TestNormal(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
self.dirname = "\\\\?\\" + op.join(gettempdir(), "python.send2trash")
|
|
|
|
self.file = op.join(self.dirname, "testfile.txt")
|
2019-04-30 16:28:09 +00:00
|
|
|
self._create_tree(self.file)
|
2020-06-03 16:49:41 +00:00
|
|
|
self.files = [
|
|
|
|
op.join(self.dirname, "testfile{}.txt".format(index)) for index in range(10)
|
|
|
|
]
|
|
|
|
[self._create_tree(file) for file in self.files]
|
2019-04-30 16:28:09 +00:00
|
|
|
|
|
|
|
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)
|
2020-06-03 16:49:41 +00:00
|
|
|
with open(path, "w") as writer:
|
|
|
|
writer.write("send2trash test")
|
2019-04-30 16:28:09 +00:00
|
|
|
|
|
|
|
def test_trash_file(self):
|
|
|
|
s2t(self.file)
|
|
|
|
self.assertFalse(op.exists(self.file))
|
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
def test_trash_multifile(self):
|
|
|
|
s2t(self.files)
|
|
|
|
self.assertFalse(any([op.exists(file) for file in self.files]))
|
|
|
|
|
2019-04-30 16:28:09 +00:00
|
|
|
def test_file_not_found(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
file = op.join(self.dirname, "otherfile.txt")
|
2019-04-30 16:28:09 +00:00
|
|
|
self.assertRaises(WindowsError, s2t, file)
|
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
|
|
|
|
@unittest.skipIf(sys.platform != "win32", "Windows only")
|
2018-02-16 14:07:05 +00:00
|
|
|
class TestLongPath(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-06-03 16:49:41 +00:00
|
|
|
filename = "A" * 100
|
|
|
|
self.dirname = "\\\\?\\" + op.join(gettempdir(), filename)
|
|
|
|
path = op.join(
|
2018-02-16 14:07:05 +00:00
|
|
|
self.dirname,
|
|
|
|
filename,
|
|
|
|
filename, # From there, the path is not trashable from Explorer
|
|
|
|
filename,
|
2020-06-03 16:49:41 +00:00
|
|
|
filename + "{}.txt",
|
|
|
|
)
|
|
|
|
self.file = path.format("")
|
2018-02-16 14:07:05 +00:00
|
|
|
self._create_tree(self.file)
|
2020-06-03 16:49:41 +00:00
|
|
|
self.files = [path.format(index) for index in range(10)]
|
|
|
|
[self._create_tree(file) for file in self.files]
|
2018-02-16 14:07:05 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2019-04-30 16:28:09 +00:00
|
|
|
shutil.rmtree(self.dirname, ignore_errors=True)
|
2018-02-16 14:07:05 +00:00
|
|
|
|
|
|
|
def _create_tree(self, path):
|
2019-04-30 16:28:09 +00:00
|
|
|
dirname = op.dirname(path)
|
|
|
|
if not op.isdir(dirname):
|
2018-02-16 14:07:05 +00:00
|
|
|
os.makedirs(dirname)
|
2020-06-03 16:49:41 +00:00
|
|
|
with open(path, "w") as writer:
|
|
|
|
writer.write("Looong filename!")
|
2018-02-16 14:07:05 +00:00
|
|
|
|
|
|
|
def test_trash_file(self):
|
|
|
|
s2t(self.file)
|
|
|
|
self.assertFalse(op.exists(self.file))
|
|
|
|
|
2020-06-03 16:49:41 +00:00
|
|
|
def test_trash_multifile(self):
|
|
|
|
s2t(self.files)
|
|
|
|
self.assertFalse(any([op.exists(file) for file in self.files]))
|
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
@unittest.skipIf(
|
|
|
|
op.splitdrive(os.getcwd())[0] != op.splitdrive(gettempdir())[0],
|
2020-06-03 16:49:41 +00:00
|
|
|
"Cannot trash long path from other drive",
|
|
|
|
)
|
2018-02-16 14:07:05 +00:00
|
|
|
def test_trash_folder(self):
|
|
|
|
s2t(self.dirname)
|
|
|
|
self.assertFalse(op.exists(self.dirname))
|