Add m3u tests
This commit is contained in:
parent
72d4ee274d
commit
ce3d23f8cc
@ -2,6 +2,7 @@ import logging
|
||||
from multiprocessing.reduction import reduce_connection
|
||||
import os
|
||||
import pickle
|
||||
import urllib
|
||||
|
||||
logger = logging.getLogger('mopidy.utils')
|
||||
|
||||
@ -109,7 +110,25 @@ def m3u_to_uris(file_path):
|
||||
- Relative paths of songs should be with respect to location of M3U.
|
||||
- Paths are normaly platform specific.
|
||||
- Lines starting with # should be ignored.
|
||||
- m3u files are latin-1, m3u8 files UTF-8
|
||||
- m3u files are latin-1.
|
||||
- This function does not bother with Extended M3U directives.
|
||||
"""
|
||||
pass
|
||||
|
||||
uris = []
|
||||
folder = os.path.dirname(file_path)
|
||||
|
||||
with open(file_path) as m3u:
|
||||
for line in m3u.readlines():
|
||||
line = line.strip().decode('latin1')
|
||||
|
||||
if line.startswith('#'):
|
||||
continue
|
||||
|
||||
if line.startswith('file:'):
|
||||
uris.append(line)
|
||||
else:
|
||||
file = os.path.join(folder, line)
|
||||
path = urllib.pathname2url(file.encode('utf-8'))
|
||||
uris.append('file:' + path)
|
||||
|
||||
return uris
|
||||
|
||||
2
tests/data/comment.m3u
Normal file
2
tests/data/comment.m3u
Normal file
@ -0,0 +1,2 @@
|
||||
# test
|
||||
song1.mp3
|
||||
0
tests/data/empty.m3u
Normal file
0
tests/data/empty.m3u
Normal file
1
tests/data/encoding.m3u
Normal file
1
tests/data/encoding.m3u
Normal file
@ -0,0 +1 @@
|
||||
<EFBFBD><EFBFBD><EFBFBD>.mp3
|
||||
1
tests/data/one.m3u
Normal file
1
tests/data/one.m3u
Normal file
@ -0,0 +1 @@
|
||||
song1.mp3
|
||||
66
tests/utils_test.py
Normal file
66
tests/utils_test.py
Normal file
@ -0,0 +1,66 @@
|
||||
#encoding: utf-8
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
import urllib
|
||||
|
||||
from mopidy.utils import m3u_to_uris
|
||||
|
||||
def data(name):
|
||||
folder = os.path.dirname(__file__)
|
||||
folder = os.path.join(folder, 'data')
|
||||
folder = os.path.abspath(folder)
|
||||
return os.path.join(folder, name)
|
||||
|
||||
|
||||
song1_path = data('song1.mp3')
|
||||
song2_path = data('song2.mp3')
|
||||
encoded_path = data(u'æøå.mp3')
|
||||
song1_uri = 'file:' + urllib.pathname2url(song1_path)
|
||||
song2_uri = 'file:' + urllib.pathname2url(song2_path)
|
||||
encoded_uri = 'file:' + urllib.pathname2url(encoded_path.encode('utf-8'))
|
||||
|
||||
|
||||
class M3UToUriTest(unittest.TestCase):
|
||||
def test_empty_file(self):
|
||||
uris = m3u_to_uris(data('empty.m3u'))
|
||||
self.assertEqual([], uris)
|
||||
|
||||
def test_basic_file(self):
|
||||
uris = m3u_to_uris(data('one.m3u'))
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_file_with_comment(self):
|
||||
uris = m3u_to_uris(data('comment.m3u'))
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_file_with_absolute_files(self):
|
||||
with tempfile.NamedTemporaryFile() as file:
|
||||
file.write(song1_path)
|
||||
file.flush()
|
||||
|
||||
uris = m3u_to_uris(file.name)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_file_with_multiple_absolute_files(self):
|
||||
with tempfile.NamedTemporaryFile() as file:
|
||||
file.write(song1_path+'\n')
|
||||
file.write('# comment \n')
|
||||
file.write(song2_path)
|
||||
file.flush()
|
||||
|
||||
uris = m3u_to_uris(file.name)
|
||||
self.assertEqual([song1_uri, song2_uri], uris)
|
||||
|
||||
def test_file_with_uri(self):
|
||||
with tempfile.NamedTemporaryFile() as file:
|
||||
file.write(song1_uri)
|
||||
file.flush()
|
||||
|
||||
uris = m3u_to_uris(file.name)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_encoding_is_latin1(self):
|
||||
uris = m3u_to_uris(data('encoding.m3u'))
|
||||
self.assertEqual([encoded_uri], uris)
|
||||
Loading…
Reference in New Issue
Block a user