File: test_playlist.py

package info (click to toggle)
quodlibet 4.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,016 kB
  • sloc: python: 85,817; sh: 385; xml: 110; makefile: 91
file content (181 lines) | stat: -rw-r--r-- 5,343 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
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
# Copyright 2022 Felix Krull
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import os
from textwrap import dedent

from quodlibet.formats import AudioFile

from tests.plugin import PluginTestCase
from tests.helper import temp_filename


class TPlaylistExport(PluginTestCase):
    def setUp(self):
        self.mod = self.modules["Playlist Export"]

    def test_m3u_playlist(self):
        plugin = self.mod.PlaylistExport()
        song1 = AudioFile(
            {
                "~filename": "/a/b/c.mp3",
                "~#length": 123,
                "artist": "a",
                "title": "c",
            }
        )
        song2 = AudioFile(
            {
                "~filename": "/a/b/d.mp3",
                "~#length": 400,
                "artist": "b",
                "title": "d",
            }
        )

        with temp_filename() as playlist_file_path:
            plugin.save_playlist(
                [song1, song2], playlist_file_path, self.mod.FORMAT_M3U, relative=False
            )
            with open(playlist_file_path, "r") as f:
                result = f.read()

        assert result == dedent(
            """\
            #EXTM3U
            #EXTINF:123,a - c
            /a/b/c.mp3
            #EXTINF:400,b - d
            /a/b/d.mp3
            """
        )

    def test_m3u_playlist_relative(self):
        plugin = self.mod.PlaylistExport()
        with temp_filename(as_path=True) as playlist_file_path:
            song1 = audio_file(
                filename=playlist_file_path.parent / "a" / "b.mp3",
                length=23,
                artist="a",
                title="b",
            )
            song2 = audio_file(
                filename=playlist_file_path.parent.parent / "c.mp3",
                length=1,
                artist="a",
                title="c",
            )

            plugin.save_playlist(
                [song1, song2], playlist_file_path, self.mod.FORMAT_M3U, relative=True
            )
            result = playlist_file_path.read_text()

        assert result == dedent(
            f"""\
            #EXTM3U
            #EXTINF:23,a - b
            {os.path.join("a", "b.mp3")}
            #EXTINF:1,a - c
            {os.path.join(os.pardir, "c.mp3")}
            """
        )

    def test_pls_playlist(self):
        plugin = self.mod.PlaylistExport()
        song1 = audio_file(filename="/a/b/c.mp3", length=123, artist="a", title="c")
        song2 = audio_file(filename="/a/b/d.mp3", length=400, artist="b", title="d")

        with temp_filename() as playlist_file_path:
            plugin.save_playlist(
                [song1, song2], playlist_file_path, self.mod.FORMAT_PLS, relative=False
            )
            with open(playlist_file_path, "r") as f:
                result = f.read()

        assert result == dedent(
            """\
            [playlist]
            File1=/a/b/c.mp3
            Title1=a - c
            Length1=123
            File2=/a/b/d.mp3
            Title2=b - d
            Length2=400
            NumberOfEntries=2
            Version=2
            """
        )

    def test_pls_playlist_relative(self):
        plugin = self.mod.PlaylistExport()
        with temp_filename(as_path=True) as playlist_file_path:
            song1 = audio_file(
                filename=playlist_file_path.parent / "a" / "b.mp3",
                length=23,
                artist="a",
                title="b",
            )
            song2 = audio_file(
                filename=playlist_file_path.parent.parent / "c.mp3",
                length=1,
                artist="a",
                title="c",
            )

            plugin.save_playlist(
                [song1, song2], playlist_file_path, self.mod.FORMAT_PLS, relative=True
            )
            result = playlist_file_path.read_text()

        assert result == dedent(
            f"""\
            [playlist]
            File1={os.path.join("a", "b.mp3")}
            Title1=a - b
            Length1=23
            File2={os.path.join(os.pardir, "c.mp3")}
            Title2=a - c
            Length2=1
            NumberOfEntries=2
            Version=2
            """
        )

    def test_m3u_relative_path_starting_with_octothorpe(self):
        plugin = self.mod.PlaylistExport()
        with temp_filename(as_path=True) as playlist_file_path:
            song = audio_file(
                filename=playlist_file_path.parent / "#file.mp3",
                length=1,
                artist="a",
                title="b",
            )

            plugin.save_playlist(
                [song], playlist_file_path, self.mod.FORMAT_M3U, relative=True
            )
            result = playlist_file_path.read_text()

        assert result == dedent(
            f"""\
            #EXTM3U
            #EXTINF:1,a - b
            {os.path.join(os.curdir, "#file.mp3")}
            """
        )


def audio_file(filename, length, artist, title):
    return AudioFile(
        {
            "~filename": str(filename),
            "~#length": length,
            "artist": artist,
            "title": title,
        }
    )