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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
# This file is part of beets.
# Copyright 2016, Thomas Scholtes.
#
# 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.
import os
import shutil
import tempfile
import unittest
from shlex import quote
from test import _common
from test import helper
import beets
class PlaylistTestHelper(helper.TestHelper):
def setUp(self):
self.setup_beets()
self.lib = beets.library.Library(':memory:')
self.music_dir = os.path.expanduser(os.path.join('~', 'Music'))
i1 = _common.item()
i1.path = beets.util.normpath(os.path.join(
self.music_dir,
'a', 'b', 'c.mp3',
))
i1.title = 'some item'
i1.album = 'some album'
self.lib.add(i1)
self.lib.add_album([i1])
i2 = _common.item()
i2.path = beets.util.normpath(os.path.join(
self.music_dir,
'd', 'e', 'f.mp3',
))
i2.title = 'another item'
i2.album = 'another album'
self.lib.add(i2)
self.lib.add_album([i2])
i3 = _common.item()
i3.path = beets.util.normpath(os.path.join(
self.music_dir,
'x', 'y', 'z.mp3',
))
i3.title = 'yet another item'
i3.album = 'yet another album'
self.lib.add(i3)
self.lib.add_album([i3])
self.playlist_dir = tempfile.mkdtemp()
self.config['directory'] = self.music_dir
self.config['playlist']['playlist_dir'] = self.playlist_dir
self.setup_test()
self.load_plugins('playlist')
def setup_test(self):
raise NotImplementedError
def tearDown(self):
self.unload_plugins()
shutil.rmtree(self.playlist_dir)
self.teardown_beets()
class PlaylistQueryTestHelper(PlaylistTestHelper):
def test_name_query_with_absolute_paths_in_playlist(self):
q = 'playlist:absolute'
results = self.lib.items(q)
self.assertEqual({i.title for i in results}, {
'some item',
'another item',
})
def test_path_query_with_absolute_paths_in_playlist(self):
q = 'playlist:{}'.format(quote(os.path.join(
self.playlist_dir,
'absolute.m3u',
)))
results = self.lib.items(q)
self.assertEqual({i.title for i in results}, {
'some item',
'another item',
})
def test_name_query_with_relative_paths_in_playlist(self):
q = 'playlist:relative'
results = self.lib.items(q)
self.assertEqual({i.title for i in results}, {
'some item',
'another item',
})
def test_path_query_with_relative_paths_in_playlist(self):
q = 'playlist:{}'.format(quote(os.path.join(
self.playlist_dir,
'relative.m3u',
)))
results = self.lib.items(q)
self.assertEqual({i.title for i in results}, {
'some item',
'another item',
})
def test_name_query_with_nonexisting_playlist(self):
q = 'playlist:nonexisting'
results = self.lib.items(q)
self.assertEqual(set(results), set())
def test_path_query_with_nonexisting_playlist(self):
q = 'playlist:{}'.format(quote(os.path.join(
self.playlist_dir,
self.playlist_dir,
'nonexisting.m3u',
)))
results = self.lib.items(q)
self.assertEqual(set(results), set())
class PlaylistTestRelativeToLib(PlaylistQueryTestHelper, unittest.TestCase):
def setup_test(self):
with open(os.path.join(self.playlist_dir, 'absolute.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join(
self.music_dir, 'a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'd', 'e', 'f.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'nonexisting.mp3')))
with open(os.path.join(self.playlist_dir, 'relative.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join('a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join('d', 'e', 'f.mp3')))
f.write('{}\n'.format('nonexisting.mp3'))
self.config['playlist']['relative_to'] = 'library'
class PlaylistTestRelativeToDir(PlaylistQueryTestHelper, unittest.TestCase):
def setup_test(self):
with open(os.path.join(self.playlist_dir, 'absolute.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join(
self.music_dir, 'a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'd', 'e', 'f.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'nonexisting.mp3')))
with open(os.path.join(self.playlist_dir, 'relative.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join('a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join('d', 'e', 'f.mp3')))
f.write('{}\n'.format('nonexisting.mp3'))
self.config['playlist']['relative_to'] = self.music_dir
class PlaylistTestRelativeToPls(PlaylistQueryTestHelper, unittest.TestCase):
def setup_test(self):
with open(os.path.join(self.playlist_dir, 'absolute.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join(
self.music_dir, 'a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'd', 'e', 'f.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'nonexisting.mp3')))
with open(os.path.join(self.playlist_dir, 'relative.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.relpath(
os.path.join(self.music_dir, 'a', 'b', 'c.mp3'),
start=self.playlist_dir,
)))
f.write('{}\n'.format(os.path.relpath(
os.path.join(self.music_dir, 'd', 'e', 'f.mp3'),
start=self.playlist_dir,
)))
f.write('{}\n'.format(os.path.relpath(
os.path.join(self.music_dir, 'nonexisting.mp3'),
start=self.playlist_dir,
)))
self.config['playlist']['relative_to'] = 'playlist'
self.config['playlist']['playlist_dir'] = self.playlist_dir
class PlaylistUpdateTestHelper(PlaylistTestHelper):
def setup_test(self):
with open(os.path.join(self.playlist_dir, 'absolute.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join(
self.music_dir, 'a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'd', 'e', 'f.mp3')))
f.write('{}\n'.format(os.path.join(
self.music_dir, 'nonexisting.mp3')))
with open(os.path.join(self.playlist_dir, 'relative.m3u'), 'w') as f:
f.write('{}\n'.format(os.path.join('a', 'b', 'c.mp3')))
f.write('{}\n'.format(os.path.join('d', 'e', 'f.mp3')))
f.write('{}\n'.format('nonexisting.mp3'))
self.config['playlist']['auto'] = True
self.config['playlist']['relative_to'] = 'library'
class PlaylistTestItemMoved(PlaylistUpdateTestHelper, unittest.TestCase):
def test_item_moved(self):
# Emit item_moved event for an item that is in a playlist
results = self.lib.items('path:{}'.format(quote(
os.path.join(self.music_dir, 'd', 'e', 'f.mp3'))))
item = results[0]
beets.plugins.send(
'item_moved', item=item, source=item.path,
destination=beets.util.bytestring_path(
os.path.join(self.music_dir, 'g', 'h', 'i.mp3')))
# Emit item_moved event for an item that is not in a playlist
results = self.lib.items('path:{}'.format(quote(
os.path.join(self.music_dir, 'x', 'y', 'z.mp3'))))
item = results[0]
beets.plugins.send(
'item_moved', item=item, source=item.path,
destination=beets.util.bytestring_path(
os.path.join(self.music_dir, 'u', 'v', 'w.mp3')))
# Emit cli_exit event
beets.plugins.send('cli_exit', lib=self.lib)
# Check playlist with absolute paths
playlist_path = os.path.join(self.playlist_dir, 'absolute.m3u')
with open(playlist_path) as f:
lines = [line.strip() for line in f.readlines()]
self.assertEqual(lines, [
os.path.join(self.music_dir, 'a', 'b', 'c.mp3'),
os.path.join(self.music_dir, 'g', 'h', 'i.mp3'),
os.path.join(self.music_dir, 'nonexisting.mp3'),
])
# Check playlist with relative paths
playlist_path = os.path.join(self.playlist_dir, 'relative.m3u')
with open(playlist_path) as f:
lines = [line.strip() for line in f.readlines()]
self.assertEqual(lines, [
os.path.join('a', 'b', 'c.mp3'),
os.path.join('g', 'h', 'i.mp3'),
'nonexisting.mp3',
])
class PlaylistTestItemRemoved(PlaylistUpdateTestHelper, unittest.TestCase):
def test_item_removed(self):
# Emit item_removed event for an item that is in a playlist
results = self.lib.items('path:{}'.format(quote(
os.path.join(self.music_dir, 'd', 'e', 'f.mp3'))))
item = results[0]
beets.plugins.send('item_removed', item=item)
# Emit item_removed event for an item that is not in a playlist
results = self.lib.items('path:{}'.format(quote(
os.path.join(self.music_dir, 'x', 'y', 'z.mp3'))))
item = results[0]
beets.plugins.send('item_removed', item=item)
# Emit cli_exit event
beets.plugins.send('cli_exit', lib=self.lib)
# Check playlist with absolute paths
playlist_path = os.path.join(self.playlist_dir, 'absolute.m3u')
with open(playlist_path) as f:
lines = [line.strip() for line in f.readlines()]
self.assertEqual(lines, [
os.path.join(self.music_dir, 'a', 'b', 'c.mp3'),
os.path.join(self.music_dir, 'nonexisting.mp3'),
])
# Check playlist with relative paths
playlist_path = os.path.join(self.playlist_dir, 'relative.m3u')
with open(playlist_path) as f:
lines = [line.strip() for line in f.readlines()]
self.assertEqual(lines, [
os.path.join('a', 'b', 'c.mp3'),
'nonexisting.mp3',
])
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|