2021-03-03 01:33:44 +00:00
|
|
|
# encoding: utf-8
|
2018-02-16 14:07:05 +00:00
|
|
|
import os
|
2019-04-30 16:28:09 +00:00
|
|
|
import shutil
|
2018-02-16 14:07:05 +00:00
|
|
|
import sys
|
2021-03-03 01:33:44 +00:00
|
|
|
import pytest
|
2018-02-16 14:07:05 +00:00
|
|
|
from os import path as op
|
|
|
|
|
|
|
|
from send2trash import send2trash as s2t
|
|
|
|
|
2020-06-12 03:42:00 +00:00
|
|
|
# import the two versions as well as the "automatic" version
|
2021-01-29 05:42:11 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
from send2trash.plat_win_modern import send2trash as s2t_modern
|
|
|
|
from send2trash.plat_win_legacy import send2trash as s2t_legacy
|
2021-03-03 01:33:44 +00:00
|
|
|
else:
|
|
|
|
pytest.skip("Skipping windows-only tests", allow_module_level=True)
|
2020-06-12 03:42:00 +00:00
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
|
2021-03-03 01:33:44 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def testdir(tmp_path):
|
|
|
|
dirname = "\\\\?\\" + str(tmp_path)
|
2021-03-18 01:52:16 +00:00
|
|
|
assert op.exists(dirname) is True
|
2021-03-03 01:33:44 +00:00
|
|
|
yield dirname
|
|
|
|
shutil.rmtree(dirname, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def testfile(testdir):
|
|
|
|
file = op.join(testdir, "testfile.txt")
|
|
|
|
_create_tree(file)
|
2021-03-18 01:52:16 +00:00
|
|
|
assert op.exists(file) is True
|
2021-03-03 01:33:44 +00:00
|
|
|
yield file
|
|
|
|
# Note dir will cleanup the file
|
|
|
|
|
|
|
|
|
|
|
|
@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]
|
2021-03-18 01:52:16 +00:00
|
|
|
assert all([op.exists(file) for file in files]) is True
|
2021-03-03 01:33:44 +00:00
|
|
|
yield files
|
|
|
|
# Note dir will cleanup the files
|
|
|
|
|
|
|
|
|
|
|
|
def _trash_folder(dir, fcn):
|
|
|
|
fcn(dir)
|
|
|
|
assert op.exists(dir) is False
|
|
|
|
|
|
|
|
|
|
|
|
def _trash_file(file, fcn):
|
|
|
|
fcn(file)
|
|
|
|
assert op.exists(file) is False
|
|
|
|
|
|
|
|
|
|
|
|
def _trash_multifile(files, fcn):
|
|
|
|
fcn(files)
|
|
|
|
assert any([op.exists(file) for file in files]) is False
|
|
|
|
|
|
|
|
|
|
|
|
def _file_not_found(dir, fcn):
|
|
|
|
file = op.join(dir, "otherfile.txt")
|
2021-03-11 00:57:35 +00:00
|
|
|
pytest.raises(OSError, fcn, file)
|
2021-03-03 01:33:44 +00:00
|
|
|
|
|
|
|
|
2021-03-11 03:41:30 +00:00
|
|
|
def _multi_byte_unicode(dir, fcn):
|
2021-03-18 02:51:51 +00:00
|
|
|
single_file = op.join(dir, "😇.txt")
|
|
|
|
_create_tree(single_file)
|
|
|
|
assert op.exists(single_file) is True
|
|
|
|
fcn(single_file)
|
|
|
|
assert op.exists(single_file) is False
|
2021-03-11 03:41:30 +00:00
|
|
|
files = [op.join(dir, "😇{}.txt".format(index)) for index in range(10)]
|
|
|
|
[_create_tree(file) for file in files]
|
2021-03-18 02:51:51 +00:00
|
|
|
assert all([op.exists(file) for file in files]) is True
|
2021-03-11 03:41:30 +00:00
|
|
|
fcn(files)
|
|
|
|
assert any([op.exists(file) for file in files]) is False
|
|
|
|
|
|
|
|
|
2021-03-03 01:33:44 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-03-11 03:41:30 +00:00
|
|
|
def test_multi_byte_unicode_modern(testdir):
|
|
|
|
_multi_byte_unicode(testdir, s2t_modern)
|
|
|
|
|
|
|
|
|
2021-03-03 01:33:44 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-03-11 03:41:30 +00:00
|
|
|
def test_multi_byte_unicode_legacy(testdir):
|
|
|
|
_multi_byte_unicode(testdir, s2t_legacy)
|
|
|
|
|
|
|
|
|
2021-03-03 01:33:44 +00:00
|
|
|
# Long path tests
|
|
|
|
@pytest.fixture
|
|
|
|
def longdir(tmp_path):
|
|
|
|
dirname = "\\\\?\\" + str(tmp_path)
|
|
|
|
name = "A" * 100
|
|
|
|
yield op.join(dirname, name, name, name)
|
2021-08-20 01:45:35 +00:00
|
|
|
try:
|
|
|
|
shutil.rmtree(dirname, ignore_errors=True)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2021-03-03 01:33:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def longfile(longdir):
|
|
|
|
name = "A" * 100
|
|
|
|
path = op.join(longdir, name + "{}.txt")
|
|
|
|
file = path.format("")
|
|
|
|
_create_tree(file)
|
2021-03-18 01:52:16 +00:00
|
|
|
assert op.exists(file) is True
|
2021-03-03 01:33:44 +00:00
|
|
|
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]
|
2021-03-18 01:52:16 +00:00
|
|
|
assert all([op.exists(file) for file in files]) is True
|
2021-03-03 01:33:44 +00:00
|
|
|
yield files
|
|
|
|
|
|
|
|
|
2021-03-18 01:52:16 +00:00
|
|
|
# NOTE: both legacy and modern test "pass" on windows, however sometimes with the same path
|
|
|
|
# they do not actually recycle files but delete them. Noticed this when testing with the
|
|
|
|
# recycle bin open, noticed later tests actually worked, modern version can actually detect
|
|
|
|
# when this happens but not stop it at this moment, and we need a way to verify it when testing.
|
2021-03-03 01:33:44 +00:00
|
|
|
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)
|