addid should ACK, and not crash or be silent on error conditions

This commit is contained in:
Stein Magnus Jodal 2010-06-06 22:04:50 +02:00
parent 6623007448
commit d135066b0d
2 changed files with 20 additions and 3 deletions

View File

@ -256,9 +256,12 @@ class MpdFrontend(object):
if songpos is not None:
songpos = int(songpos)
track = self.backend.library.lookup(uri)
if track is not None:
self.backend.current_playlist.add(track, at_position=songpos)
return ('Id', track.id)
if track is None:
raise MpdAckError(u'No such song')
if songpos and songpos > self.backend.current_playlist.playlist.length:
raise MpdAckError(u'Position out of bounds')
self.backend.current_playlist.add(track, at_position=songpos)
return ('Id', track.id)
@handle_pattern(r'^delete "(?P<start>\d+):(?P<end>\d+)*"$')
def _current_playlist_delete_range(self, start, end=None):

View File

@ -547,6 +547,20 @@ class CurrentPlaylistHandlerTest(unittest.TestCase):
self.assert_(u'Id: 137' in result)
self.assert_(u'OK' in result)
def test_addid_with_songpos_out_of_bounds_should_ack(self):
needle = Track(uri='dummy://foo', id=137)
self.b.library._library = [Track(), Track(), needle, Track()]
self.b.current_playlist.playlist = Playlist(
tracks=[Track(), Track(), Track(), Track(), Track()])
self.assertEqual(self.b.current_playlist.playlist.length, 5)
result = self.h.handle_request(u'addid "dummy://foo" "6"')
self.assert_(u'ACK Position out of bounds' in result)
def test_addid_with_uri_not_found_in_library_should_ack(self):
self.b.library._library = [Track(), Track(), Track()]
result = self.h.handle_request(u'addid "dummy://foo"')
self.assert_(u'ACK No such song' in result)
def test_clear(self):
self.b.current_playlist.playlist = Playlist(
tracks=[Track(), Track(), Track(), Track(), Track()])