File: test_plat_win.py

package info (click to toggle)
send2trash 1.6.0~b1%2Bgit20210122.2eb3242-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 184 kB
  • sloc: python: 737; makefile: 2
file content (155 lines) | stat: -rw-r--r-- 4,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# coding: utf-8
import os
import shutil
import sys
import unittest
from os import path as op
from tempfile import gettempdir

from send2trash import send2trash as s2t

# import the two versions as well as the "automatic" version
if sys.platform == "win32":
    from send2trash.plat_win_modern import send2trash as s2t_modern
    from send2trash.plat_win_legacy import send2trash as s2t_legacy


@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)


@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]

    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!")

    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 _trash_folder(self, fcn):
        fcn(self.dirname)
        self.assertFalse(op.exists(self.dirname))

    def test_trash_file(self):
        self._trash_file(s2t)

    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 test_trash_file_modern(self):
        self._trash_file(s2t_modern)

    def test_trash_multifile_modern(self):
        self._trash_multifile(s2t_modern)

    @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 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)