forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_m3ufile.py
159 lines (141 loc) · 5.73 KB
/
test_m3ufile.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
# This file is part of beets.
# Copyright 2022, J0J0 Todos.
#
# 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.
"""Testsuite for the M3UFile class."""
import sys
import unittest
from os import path
from shutil import rmtree
from tempfile import mkdtemp
from beets.test._common import RSRC
from beets.util import bytestring_path
from beets.util.m3u import EmptyPlaylistError, M3UFile
class M3UFileTest(unittest.TestCase):
"""Tests the M3UFile class."""
def test_playlist_write_empty(self):
"""Test whether saving an empty playlist file raises an error."""
tempdir = bytestring_path(mkdtemp())
the_playlist_file = path.join(tempdir, b"playlist.m3u8")
m3ufile = M3UFile(the_playlist_file)
with self.assertRaises(EmptyPlaylistError):
m3ufile.write()
rmtree(tempdir)
def test_playlist_write(self):
"""Test saving ascii paths to a playlist file."""
tempdir = bytestring_path(mkdtemp())
the_playlist_file = path.join(tempdir, b"playlist.m3u")
m3ufile = M3UFile(the_playlist_file)
m3ufile.set_contents(
[
bytestring_path("/This/is/a/path/to_a_file.mp3"),
bytestring_path("/This/is/another/path/to_a_file.mp3"),
]
)
m3ufile.write()
self.assertTrue(path.exists(the_playlist_file))
rmtree(tempdir)
def test_playlist_write_unicode(self):
"""Test saving unicode paths to a playlist file."""
tempdir = bytestring_path(mkdtemp())
the_playlist_file = path.join(tempdir, b"playlist.m3u8")
m3ufile = M3UFile(the_playlist_file)
m3ufile.set_contents(
[
bytestring_path("/This/is/å/path/to_a_file.mp3"),
bytestring_path("/This/is/another/path/tö_a_file.mp3"),
]
)
m3ufile.write()
self.assertTrue(path.exists(the_playlist_file))
rmtree(tempdir)
@unittest.skipUnless(sys.platform == "win32", "win32")
def test_playlist_write_and_read_unicode_windows(self):
"""Test saving unicode paths to a playlist file on Windows."""
tempdir = bytestring_path(mkdtemp())
the_playlist_file = path.join(
tempdir, b"playlist_write_and_read_windows.m3u8"
)
m3ufile = M3UFile(the_playlist_file)
m3ufile.set_contents(
[
bytestring_path(r"x:\This\is\å\path\to_a_file.mp3"),
bytestring_path(r"x:\This\is\another\path\tö_a_file.mp3"),
]
)
m3ufile.write()
self.assertTrue(path.exists(the_playlist_file))
m3ufile_read = M3UFile(the_playlist_file)
m3ufile_read.load()
self.assertEqual(
m3ufile.media_list[0],
bytestring_path(
path.join("x:\\", "This", "is", "å", "path", "to_a_file.mp3")
),
)
self.assertEqual(
m3ufile.media_list[1],
bytestring_path(r"x:\This\is\another\path\tö_a_file.mp3"),
bytestring_path(
path.join(
"x:\\", "This", "is", "another", "path", "tö_a_file.mp3"
)
),
)
rmtree(tempdir)
@unittest.skipIf(sys.platform == "win32", "win32")
def test_playlist_load_ascii(self):
"""Test loading ascii paths from a playlist file."""
the_playlist_file = path.join(RSRC, b"playlist.m3u")
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(
m3ufile.media_list[0],
bytestring_path("/This/is/a/path/to_a_file.mp3"),
)
@unittest.skipIf(sys.platform == "win32", "win32")
def test_playlist_load_unicode(self):
"""Test loading unicode paths from a playlist file."""
the_playlist_file = path.join(RSRC, b"playlist.m3u8")
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(
m3ufile.media_list[0],
bytestring_path("/This/is/å/path/to_a_file.mp3"),
)
@unittest.skipUnless(sys.platform == "win32", "win32")
def test_playlist_load_unicode_windows(self):
"""Test loading unicode paths from a playlist file."""
the_playlist_file = path.join(RSRC, b"playlist_windows.m3u8")
winpath = bytestring_path(
path.join("x:\\", "This", "is", "å", "path", "to_a_file.mp3")
)
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(m3ufile.media_list[0], winpath)
def test_playlist_load_extm3u(self):
"""Test loading a playlist with an #EXTM3U header."""
the_playlist_file = path.join(RSRC, b"playlist.m3u")
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertTrue(m3ufile.extm3u)
def test_playlist_load_non_extm3u(self):
"""Test loading a playlist without an #EXTM3U header."""
the_playlist_file = path.join(RSRC, b"playlist_non_ext.m3u")
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertFalse(m3ufile.extm3u)
def suite():
"""This testsuite's main function."""
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__":
unittest.main(defaultTest="suite")