Debian Codegraph @

test/util/test_m3ufile.py

raw
1 """Testsuite for the M3UFile class."""
2
3 import sys
4 from pathlib import Path
5
6 import pytest
7
8 from beets.test._common import RSRC
9 from beets.test.helper import PathsMixin
10 from beets.util import bytestring_path
11 from beets.util.m3u import EmptyPlaylistError, M3UFile
12
13
14 class TestM3UFile(PathsMixin):
15 """Tests the M3UFile class."""
16
17 def test_playlist_write_empty(self):
18 """Test whether saving an empty playlist file raises an error."""
19 the_playlist_file = self.temp_path / "playlist.m3u8"
20 m3ufile = M3UFile(the_playlist_file)
21 with pytest.raises(EmptyPlaylistError):
22 m3ufile.write()
23
24 def test_playlist_write(self):
25 """Test saving ascii paths to a playlist file."""
26 the_playlist_file = self.temp_path / "playlist.m3u8"
27 m3ufile = M3UFile(the_playlist_file)
28 m3ufile.set_contents(
29 [
30 bytestring_path("/This/is/a/path/to_a_file.mp3"),
31 bytestring_path("/This/is/another/path/to_a_file.mp3"),
32 ]
33 )
34 m3ufile.write()
35 assert the_playlist_file.exists()
36
37 def test_playlist_write_unicode(self):
38 """Test saving unicode paths to a playlist file."""
39 the_playlist_file = self.temp_path / "playlist.m3u8"
40 m3ufile = M3UFile(the_playlist_file)
41 m3ufile.set_contents(
42 [
43 bytestring_path("/This/is/å/path/to_a_file.mp3"),
44 bytestring_path("/This/is/another/path/tö_a_file.mp3"),
45 ]
46 )
47 m3ufile.write()
48 assert the_playlist_file.exists()
49
50 @pytest.mark.skipif(sys.platform != "win32", reason="win32")
51 def test_playlist_write_and_read_unicode_windows(self):
52 """Test saving unicode paths to a playlist file on Windows."""
53 the_playlist_file = (
54 self.temp_path / "playlist_write_and_read_windows.m3u8"
55 )
56 m3ufile = M3UFile(the_playlist_file)
57 m3ufile.set_contents(
58 [
59 bytestring_path(r"x:\This\is\å\path\to_a_file.mp3"),
60 bytestring_path(r"x:\This\is\another\path\tö_a_file.mp3"),
61 ]
62 )
63 m3ufile.write()
64 assert the_playlist_file.exists()
65 m3ufile_read = M3UFile(the_playlist_file)
66 m3ufile_read.load()
67 assert m3ufile.media_list[0] == bytestring_path(
68 Path("x:\\") / "This" / "is" / "å" / "path" / "to_a_file.mp3"
69 )
70 assert m3ufile.media_list[1] == bytestring_path(
71 r"x:\This\is\another\path\tö_a_file.mp3"
72 ), bytestring_path(
73 Path("x:\\") / "This" / "is" / "another" / "path" / "tö_a_file.mp3"
74 )
75
76 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
77 def test_playlist_load_ascii(self):
78 """Test loading ascii paths from a playlist file."""
79 the_playlist_file = RSRC / "playlist.m3u"
80 m3ufile = M3UFile(the_playlist_file)
81 m3ufile.load()
82 assert m3ufile.media_list[0] == bytestring_path(
83 "/This/is/a/path/to_a_file.mp3"
84 )
85
86 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
87 def test_playlist_load_unicode(self):
88 """Test loading unicode paths from a playlist file."""
89 the_playlist_file = RSRC / "playlist.m3u8"
90 m3ufile = M3UFile(the_playlist_file)
91 m3ufile.load()
92 assert m3ufile.media_list[0] == bytestring_path(
93 "/This/is/å/path/to_a_file.mp3"
94 )
95
96 @pytest.mark.skipif(sys.platform != "win32", reason="win32")
97 def test_playlist_load_unicode_windows(self):
98 """Test loading unicode paths from a playlist file."""
99 the_playlist_file = RSRC / "playlist_windows.m3u8"
100 winpath = bytestring_path(
101 Path("x:\\") / "This" / "is" / "å" / "path" / "to_a_file.mp3"
102 )
103 m3ufile = M3UFile(the_playlist_file)
104 m3ufile.load()
105 assert m3ufile.media_list[0] == winpath
106
107 def test_playlist_load_extm3u(self):
108 """Test loading a playlist with an #EXTM3U header."""
109 the_playlist_file = RSRC / "playlist.m3u"
110 m3ufile = M3UFile(the_playlist_file)
111 m3ufile.load()
112 assert m3ufile.extm3u
113
114 def test_playlist_load_non_extm3u(self):
115 """Test loading a playlist without an #EXTM3U header."""
116 the_playlist_file = RSRC / "playlist_non_ext.m3u"
117 m3ufile = M3UFile(the_playlist_file)
118 m3ufile.load()
119 assert not m3ufile.extm3u