forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
240 lines (197 loc) · 8.23 KB
/
test_util.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for base utils from the beets.util package.
"""
import os
import platform
import re
import subprocess
import sys
import unittest
from unittest.mock import Mock, patch
from beets import util
from beets.test import _common
class UtilTest(unittest.TestCase):
def test_open_anything(self):
with _common.system_mock("Windows"):
self.assertEqual(util.open_anything(), "start")
with _common.system_mock("Darwin"):
self.assertEqual(util.open_anything(), "open")
with _common.system_mock("Tagada"):
self.assertEqual(util.open_anything(), "xdg-open")
@patch("os.execlp")
@patch("beets.util.open_anything")
def test_interactive_open(self, mock_open, mock_execlp):
mock_open.return_value = "tagada"
util.interactive_open(["foo"], util.open_anything())
mock_execlp.assert_called_once_with("tagada", "tagada", "foo")
mock_execlp.reset_mock()
util.interactive_open(["foo"], "bar")
mock_execlp.assert_called_once_with("bar", "bar", "foo")
def test_sanitize_unix_replaces_leading_dot(self):
with _common.platform_posix():
p = util.sanitize_path("one/.two/three")
self.assertNotIn(".", p)
def test_sanitize_windows_replaces_trailing_dot(self):
with _common.platform_windows():
p = util.sanitize_path("one/two./three")
self.assertNotIn(".", p)
def test_sanitize_windows_replaces_illegal_chars(self):
with _common.platform_windows():
p = util.sanitize_path(':*?"<>|')
self.assertNotIn(":", p)
self.assertNotIn("*", p)
self.assertNotIn("?", p)
self.assertNotIn('"', p)
self.assertNotIn("<", p)
self.assertNotIn(">", p)
self.assertNotIn("|", p)
def test_sanitize_windows_replaces_trailing_space(self):
with _common.platform_windows():
p = util.sanitize_path("one/two /three")
self.assertNotIn(" ", p)
def test_sanitize_path_works_on_empty_string(self):
with _common.platform_posix():
p = util.sanitize_path("")
self.assertEqual(p, "")
def test_sanitize_with_custom_replace_overrides_built_in_sub(self):
with _common.platform_posix():
p = util.sanitize_path(
"a/.?/b",
[
(re.compile(r"foo"), "bar"),
],
)
self.assertEqual(p, "a/.?/b")
def test_sanitize_with_custom_replace_adds_replacements(self):
with _common.platform_posix():
p = util.sanitize_path(
"foo/bar",
[
(re.compile(r"foo"), "bar"),
],
)
self.assertEqual(p, "bar/bar")
@unittest.skip("unimplemented: #359")
def test_sanitize_empty_component(self):
with _common.platform_posix():
p = util.sanitize_path(
"foo//bar",
[
(re.compile(r"^$"), "_"),
],
)
self.assertEqual(p, "foo/_/bar")
@unittest.skipIf(sys.platform == "win32", "win32")
def test_convert_command_args_keeps_undecodeable_bytes(self):
arg = b"\x82" # non-ascii bytes
cmd_args = util.convert_command_args([arg])
self.assertEqual(
cmd_args[0], arg.decode(util.arg_encoding(), "surrogateescape")
)
@patch("beets.util.subprocess.Popen")
def test_command_output(self, mock_popen):
def popen_fail(*args, **kwargs):
m = Mock(returncode=1)
m.communicate.return_value = "foo", "bar"
return m
mock_popen.side_effect = popen_fail
with self.assertRaises(subprocess.CalledProcessError) as exc_context:
util.command_output(["taga", "\xc3\xa9"])
self.assertEqual(exc_context.exception.returncode, 1)
self.assertEqual(exc_context.exception.cmd, "taga \xc3\xa9")
def test_case_sensitive_default(self):
path = util.bytestring_path(
util.normpath(
"/this/path/does/not/exist",
)
)
self.assertEqual(
util.case_sensitive(path),
platform.system() != "Windows",
)
@unittest.skipIf(sys.platform == "win32", "fs is not case sensitive")
def test_case_sensitive_detects_sensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is not case sensitive
pass
@unittest.skipIf(sys.platform != "win32", "fs is case sensitive")
def test_case_sensitive_detects_insensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is case sensitive
pass
class PathConversionTest(_common.TestCase):
def test_syspath_windows_format(self):
with _common.platform_windows():
path = os.path.join("a", "b", "c")
outpath = util.syspath(path)
self.assertTrue(isinstance(outpath, str))
self.assertTrue(outpath.startswith("\\\\?\\"))
def test_syspath_windows_format_unc_path(self):
# The \\?\ prefix on Windows behaves differently with UNC
# (network share) paths.
path = "\\\\server\\share\\file.mp3"
with _common.platform_windows():
outpath = util.syspath(path)
self.assertTrue(isinstance(outpath, str))
self.assertEqual(outpath, "\\\\?\\UNC\\server\\share\\file.mp3")
def test_syspath_posix_unchanged(self):
with _common.platform_posix():
path = os.path.join("a", "b", "c")
outpath = util.syspath(path)
self.assertEqual(path, outpath)
def _windows_bytestring_path(self, path):
old_gfse = sys.getfilesystemencoding
sys.getfilesystemencoding = lambda: "mbcs"
try:
with _common.platform_windows():
return util.bytestring_path(path)
finally:
sys.getfilesystemencoding = old_gfse
def test_bytestring_path_windows_encodes_utf8(self):
path = "caf\xe9"
outpath = self._windows_bytestring_path(path)
self.assertEqual(path, outpath.decode("utf-8"))
def test_bytesting_path_windows_removes_magic_prefix(self):
path = "\\\\?\\C:\\caf\xe9"
outpath = self._windows_bytestring_path(path)
self.assertEqual(outpath, "C:\\caf\xe9".encode())
class PathTruncationTest(_common.TestCase):
def test_truncate_bytestring(self):
with _common.platform_posix():
p = util.truncate_path(b"abcde/fgh", 4)
self.assertEqual(p, b"abcd/fgh")
def test_truncate_unicode(self):
with _common.platform_posix():
p = util.truncate_path("abcde/fgh", 4)
self.assertEqual(p, "abcd/fgh")
def test_truncate_preserves_extension(self):
with _common.platform_posix():
p = util.truncate_path("abcde/fgh.ext", 5)
self.assertEqual(p, "abcde/f.ext")
class ConfitDeprecationTest(_common.TestCase):
def test_confit_deprecattion_warning_origin(self):
"""Test that importing `confit` raises a warning.
In addition, ensure that the warning originates from the actual
import statement, not the `confit` module.
"""
# See https://github.com/beetbox/beets/discussions/4024
with self.assertWarns(UserWarning) as w:
import beets.util.confit # noqa: F401
self.assertIn(__file__, w.filename)
self.assertNotIn("confit.py", w.filename)
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__":
unittest.main(defaultTest="suite")