From 25495d675c44a75d7dedfe123f30a858f9cd60be Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 21:39:37 -0800 Subject: [PATCH 01/15] Add minimal (no asserts) test for play plugin --- test/test_play.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/test_play.py diff --git a/test/test_play.py b/test/test_play.py new file mode 100644 index 000000000..8a37509cd --- /dev/null +++ b/test/test_play.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +"""Tests for the play plugin""" + +from __future__ import (division, absolute_import, print_function, + unicode_literals) + +from mock import patch, Mock + +from test._common import unittest +from test.helper import TestHelper + +from beetsplug.play import PlayPlugin + + +class PlayPluginTest(unittest.TestCase, TestHelper): + def setUp(self): + self.setup_beets() + self.load_plugins('play') + self.add_item(title='aNiceTitle') + + def tearDown(self): + self.teardown_beets() + self.unload_plugins() + + @patch('beetsplug.play.util.interactive_open', Mock()) + def test_basic(self): + self.run_command('play', 'title:aNiceTitle') + +def suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == b'__main__': + unittest.main(defaultTest='suite') From 3807d4fc57be97bb72b24178d508f3f0f172c21b Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 21:50:13 -0800 Subject: [PATCH 02/15] fix flake8 warnings --- test/test_play.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 8a37509cd..68d8c2ea8 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -10,8 +10,6 @@ from mock import patch, Mock from test._common import unittest from test.helper import TestHelper -from beetsplug.play import PlayPlugin - class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): @@ -27,6 +25,7 @@ class PlayPluginTest(unittest.TestCase, TestHelper): def test_basic(self): self.run_command('play', 'title:aNiceTitle') + def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From d15b996dc4a741390507a96d6facf113f8da0869 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 22:33:36 -0800 Subject: [PATCH 03/15] Verify that the generated playlist contains the path to the item --- test/test_play.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 68d8c2ea8..f1f1ba3ab 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -5,7 +5,7 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) -from mock import patch, Mock +from mock import patch, ANY from test._common import unittest from test.helper import TestHelper @@ -15,16 +15,21 @@ class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): self.setup_beets() self.load_plugins('play') - self.add_item(title='aNiceTitle') + self.item = self.add_item(title='aNiceTitle') def tearDown(self): self.teardown_beets() self.unload_plugins() - @patch('beetsplug.play.util.interactive_open', Mock()) - def test_basic(self): + @patch('beetsplug.play.util.interactive_open') + def test_basic(self, open_mock): self.run_command('play', 'title:aNiceTitle') + open_mock.assert_called_once_with(ANY, None) + playlist = open(open_mock.call_args[0][0][0], 'r') + self.assertEqual(self.item.path.decode('utf-8') + '\n', + playlist.read().decode('utf-8')) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From 362d625f6975451b140d124ef419a1f99b6b4a0c Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 23:33:36 -0800 Subject: [PATCH 04/15] add test for --args option --- test/test_play.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_play.py b/test/test_play.py index f1f1ba3ab..4a57decfb 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -30,6 +30,16 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.assertEqual(self.item.path.decode('utf-8') + '\n', playlist.read().decode('utf-8')) + @patch('beetsplug.play.util.interactive_open') + def test_args_option(self, open_mock): + self.config['play']['command'] = 'true' + self.run_command('play', '-A', 'foo', 'title:aNiceTitle') + + open_mock.assert_called_once_with(ANY, 'true foo') + playlist = open(open_mock.call_args[0][0][0], 'r') + self.assertEqual(self.item.path.decode('utf-8') + '\n', + playlist.read().decode('utf-8')) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From 3ad02e1a748531329484ec066c6caea008b1f7f6 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 23:35:38 -0800 Subject: [PATCH 05/15] Remove duplicate code --- test/test_play.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 4a57decfb..53f108343 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -26,9 +26,7 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.run_command('play', 'title:aNiceTitle') open_mock.assert_called_once_with(ANY, None) - playlist = open(open_mock.call_args[0][0][0], 'r') - self.assertEqual(self.item.path.decode('utf-8') + '\n', - playlist.read().decode('utf-8')) + self.assertPlaylistCorrect(open_mock) @patch('beetsplug.play.util.interactive_open') def test_args_option(self, open_mock): @@ -36,9 +34,13 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.run_command('play', '-A', 'foo', 'title:aNiceTitle') open_mock.assert_called_once_with(ANY, 'true foo') + self.assertPlaylistCorrect(open_mock) + + def assertPlaylistCorrect(self, open_mock): playlist = open(open_mock.call_args[0][0][0], 'r') self.assertEqual(self.item.path.decode('utf-8') + '\n', playlist.read().decode('utf-8')) + def suite(): From 6b49b0ff238b2f52f6f061e60d385f4bae11c4a9 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 23:40:52 -0800 Subject: [PATCH 06/15] add test for $args --- test/test_play.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test_play.py b/test/test_play.py index 53f108343..99382a00a 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -36,11 +36,18 @@ class PlayPluginTest(unittest.TestCase, TestHelper): open_mock.assert_called_once_with(ANY, 'true foo') self.assertPlaylistCorrect(open_mock) + @patch('beetsplug.play.util.interactive_open') + def test_args_option_in_middle(self, open_mock): + self.config['play']['command'] = 'true $args other' + self.run_command('play', '-A', 'foo', 'title:aNiceTitle') + + open_mock.assert_called_once_with(ANY, 'true foo other') + self.assertPlaylistCorrect(open_mock) + def assertPlaylistCorrect(self, open_mock): playlist = open(open_mock.call_args[0][0][0], 'r') self.assertEqual(self.item.path.decode('utf-8') + '\n', playlist.read().decode('utf-8')) - def suite(): From a47de98653d2ff62ee6780983c48078a10d83d5c Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sat, 2 Jan 2016 23:54:29 -0800 Subject: [PATCH 07/15] add relative_to test --- test/test_play.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 99382a00a..07bf64c0d 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -30,23 +30,32 @@ class PlayPluginTest(unittest.TestCase, TestHelper): @patch('beetsplug.play.util.interactive_open') def test_args_option(self, open_mock): - self.config['play']['command'] = 'true' + self.config['play']['command'] = 'echo' self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - open_mock.assert_called_once_with(ANY, 'true foo') + open_mock.assert_called_once_with(ANY, 'echo foo') self.assertPlaylistCorrect(open_mock) @patch('beetsplug.play.util.interactive_open') def test_args_option_in_middle(self, open_mock): - self.config['play']['command'] = 'true $args other' + self.config['play']['command'] = 'echo $args other' self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - open_mock.assert_called_once_with(ANY, 'true foo other') + open_mock.assert_called_once_with(ANY, 'echo foo other') self.assertPlaylistCorrect(open_mock) - def assertPlaylistCorrect(self, open_mock): + @patch('beetsplug.play.util.interactive_open') + def test_relative_to(self, open_mock): + self.config['play']['command'] = 'echo' + self.config['play']['relative_to'] = '/something' + self.run_command('play', 'title:aNiceTitle') + + open_mock.assert_called_once_with(ANY, 'echo') + self.assertPlaylistCorrect(open_mock, '..{}\n') + + def assertPlaylistCorrect(self, open_mock, expected='{}\n'): playlist = open(open_mock.call_args[0][0][0], 'r') - self.assertEqual(self.item.path.decode('utf-8') + '\n', + self.assertEqual(expected.format(self.item.path.decode('utf-8')), playlist.read().decode('utf-8')) From 6ad0c8a490f87cb46899f0a1c11ca6e4000c2afb Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 00:05:49 -0800 Subject: [PATCH 08/15] Add album option test --- test/test_play.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 07bf64c0d..d007978f4 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -11,24 +11,30 @@ from test._common import unittest from test.helper import TestHelper +@patch('beetsplug.play.util.interactive_open') class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): self.setup_beets() self.load_plugins('play') - self.item = self.add_item(title='aNiceTitle') + self.item = self.add_item(album='a nice älbum', title='aNiceTitle') + self.lib.add_album([self.item]) def tearDown(self): self.teardown_beets() self.unload_plugins() - @patch('beetsplug.play.util.interactive_open') def test_basic(self, open_mock): self.run_command('play', 'title:aNiceTitle') open_mock.assert_called_once_with(ANY, None) self.assertPlaylistCorrect(open_mock) - @patch('beetsplug.play.util.interactive_open') + def test_album_option(self, open_mock): + self.run_command('play', '-a', 'nice') + + open_mock.assert_called_once_with(ANY, None) + self.assertPlaylistCorrect(open_mock) + def test_args_option(self, open_mock): self.config['play']['command'] = 'echo' self.run_command('play', '-A', 'foo', 'title:aNiceTitle') @@ -36,7 +42,6 @@ class PlayPluginTest(unittest.TestCase, TestHelper): open_mock.assert_called_once_with(ANY, 'echo foo') self.assertPlaylistCorrect(open_mock) - @patch('beetsplug.play.util.interactive_open') def test_args_option_in_middle(self, open_mock): self.config['play']['command'] = 'echo $args other' self.run_command('play', '-A', 'foo', 'title:aNiceTitle') @@ -44,7 +49,6 @@ class PlayPluginTest(unittest.TestCase, TestHelper): open_mock.assert_called_once_with(ANY, 'echo foo other') self.assertPlaylistCorrect(open_mock) - @patch('beetsplug.play.util.interactive_open') def test_relative_to(self, open_mock): self.config['play']['command'] = 'echo' self.config['play']['relative_to'] = '/something' From 4a1a70e23d2db0f5a51adbba092fb28ebd24c13f Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 00:18:02 -0800 Subject: [PATCH 09/15] add 3 more tests --- test/test_play.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_play.py b/test/test_play.py index d007978f4..4166c895f 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -5,6 +5,8 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) +import os + from mock import patch, ANY from test._common import unittest @@ -35,6 +37,16 @@ class PlayPluginTest(unittest.TestCase, TestHelper): open_mock.assert_called_once_with(ANY, None) self.assertPlaylistCorrect(open_mock) + def test_use_folders(self, open_mock): + self.config['play']['use_folders'] = True + self.run_command('play', '-a', 'nice') + + open_mock.assert_called_once_with(ANY, None) + playlist = open(open_mock.call_args[0][0][0], 'r') + self.assertEqual('{}\n'.format( + os.path.dirname(self.item.path.decode('utf-8'))), + playlist.read().decode('utf-8')) + def test_args_option(self, open_mock): self.config['play']['command'] = 'echo' self.run_command('play', '-A', 'foo', 'title:aNiceTitle') @@ -57,6 +69,16 @@ class PlayPluginTest(unittest.TestCase, TestHelper): open_mock.assert_called_once_with(ANY, 'echo') self.assertPlaylistCorrect(open_mock, '..{}\n') + def test_raw(self, open_mock): + self.config['play']['raw'] = True + self.run_command('play', 'nice') + + open_mock.assert_called_once_with([self.item.path], None) + + def test_not_found(self, open_mock): + self.run_command('play', 'not found') + open_mock.assert_not_called() + def assertPlaylistCorrect(self, open_mock, expected='{}\n'): playlist = open(open_mock.call_args[0][0][0], 'r') self.assertEqual(expected.format(self.item.path.decode('utf-8')), From 7f372a6a59c748992b58c4d419c1b0ddb8ec018e Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 09:48:22 -0800 Subject: [PATCH 10/15] Add last 2 tests -- 100% line coverage --- test/test_play.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/test_play.py b/test/test_play.py index 4166c895f..6d0986102 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -10,8 +10,9 @@ import os from mock import patch, ANY from test._common import unittest -from test.helper import TestHelper +from test.helper import TestHelper, control_stdin +from beets.ui import UserError @patch('beetsplug.play.util.interactive_open') class PlayPluginTest(unittest.TestCase, TestHelper): @@ -79,6 +80,19 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.run_command('play', 'not found') open_mock.assert_not_called() + def test_warning_threshold(self, open_mock): + self.config['play']['warning_treshold'] = 1 + item2 = self.add_item(title='another NiceTitle') + with control_stdin("a"): + self.run_command('play', 'nice') + + open_mock.assert_not_called() + + def test_command_failed(self, open_mock): + open_mock.side_effect = OSError("some reason") + with self.assertRaises(UserError): + self.run_command('play', 'title:aNiceTitle') + def assertPlaylistCorrect(self, open_mock, expected='{}\n'): playlist = open(open_mock.call_args[0][0][0], 'r') self.assertEqual(expected.format(self.item.path.decode('utf-8')), From 989b4719a6108c0d9fea6c7fafde198a2b2a34e3 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 11:07:02 -0800 Subject: [PATCH 11/15] Rearrange test_play.py to minimize duplication --- test/test_play.py | 64 +++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 6d0986102..cccf1fbc1 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -26,17 +26,35 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.teardown_beets() self.unload_plugins() - def test_basic(self, open_mock): - self.run_command('play', 'title:aNiceTitle') + def do_test(self, open_mock, args=('title:aNiceTitle',), expected_cmd=None, expected_playlist='{}\n'): + self.run_command('play', *args) - open_mock.assert_called_once_with(ANY, None) - self.assertPlaylistCorrect(open_mock) + open_mock.assert_called_once_with(ANY, expected_cmd) + expected_playlist_content = expected_playlist.format(self.item.path.decode('utf-8')) + with open(open_mock.call_args[0][0][0], 'r') as playlist: + self.assertEqual(expected_playlist_content, playlist.read().decode('utf-8')) + + def test_basic(self, open_mock): + self.do_test(open_mock) def test_album_option(self, open_mock): - self.run_command('play', '-a', 'nice') + self.do_test(open_mock, ['-a', 'nice']) - open_mock.assert_called_once_with(ANY, None) - self.assertPlaylistCorrect(open_mock) + def test_args_option(self, open_mock): + self.config['play']['command'] = 'echo' + + self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo') + + def test_args_option_in_middle(self, open_mock): + self.config['play']['command'] = 'echo $args other' + + self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo other') + + def test_relative_to(self, open_mock): + self.config['play']['command'] = 'echo' + self.config['play']['relative_to'] = '/something' + + self.do_test(open_mock, expected_cmd='echo', expected_playlist='..{}\n') def test_use_folders(self, open_mock): self.config['play']['use_folders'] = True @@ -48,41 +66,22 @@ class PlayPluginTest(unittest.TestCase, TestHelper): os.path.dirname(self.item.path.decode('utf-8'))), playlist.read().decode('utf-8')) - def test_args_option(self, open_mock): - self.config['play']['command'] = 'echo' - self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo foo') - self.assertPlaylistCorrect(open_mock) - - def test_args_option_in_middle(self, open_mock): - self.config['play']['command'] = 'echo $args other' - self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo foo other') - self.assertPlaylistCorrect(open_mock) - - def test_relative_to(self, open_mock): - self.config['play']['command'] = 'echo' - self.config['play']['relative_to'] = '/something' - self.run_command('play', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo') - self.assertPlaylistCorrect(open_mock, '..{}\n') - def test_raw(self, open_mock): self.config['play']['raw'] = True + self.run_command('play', 'nice') open_mock.assert_called_once_with([self.item.path], None) def test_not_found(self, open_mock): self.run_command('play', 'not found') + open_mock.assert_not_called() def test_warning_threshold(self, open_mock): self.config['play']['warning_treshold'] = 1 item2 = self.add_item(title='another NiceTitle') + with control_stdin("a"): self.run_command('play', 'nice') @@ -90,15 +89,10 @@ class PlayPluginTest(unittest.TestCase, TestHelper): def test_command_failed(self, open_mock): open_mock.side_effect = OSError("some reason") + with self.assertRaises(UserError): self.run_command('play', 'title:aNiceTitle') - def assertPlaylistCorrect(self, open_mock, expected='{}\n'): - playlist = open(open_mock.call_args[0][0][0], 'r') - self.assertEqual(expected.format(self.item.path.decode('utf-8')), - playlist.read().decode('utf-8')) - - def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From 4d55c5bf7e234e15a118b9376af3cad269fc9813 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 11:14:24 -0800 Subject: [PATCH 12/15] Do patching in setUp --- test/test_play.py | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index cccf1fbc1..3c04ab269 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -14,81 +14,83 @@ from test.helper import TestHelper, control_stdin from beets.ui import UserError -@patch('beetsplug.play.util.interactive_open') class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): self.setup_beets() self.load_plugins('play') self.item = self.add_item(album='a nice älbum', title='aNiceTitle') self.lib.add_album([self.item]) + self.open_patcher = patch('beetsplug.play.util.interactive_open') + self.open_mock = self.open_patcher.start() def tearDown(self): + self.open_patcher.stop() self.teardown_beets() self.unload_plugins() - def do_test(self, open_mock, args=('title:aNiceTitle',), expected_cmd=None, expected_playlist='{}\n'): + def do_test(self, args=('title:aNiceTitle',), expected_cmd=None, expected_playlist='{}\n'): self.run_command('play', *args) - open_mock.assert_called_once_with(ANY, expected_cmd) + self.open_mock.assert_called_once_with(ANY, expected_cmd) expected_playlist_content = expected_playlist.format(self.item.path.decode('utf-8')) - with open(open_mock.call_args[0][0][0], 'r') as playlist: + with open(self.open_mock.call_args[0][0][0], 'r') as playlist: self.assertEqual(expected_playlist_content, playlist.read().decode('utf-8')) - def test_basic(self, open_mock): - self.do_test(open_mock) + def test_basic(self): + self.do_test() - def test_album_option(self, open_mock): - self.do_test(open_mock, ['-a', 'nice']) + def test_album_option(self): + self.do_test(['-a', 'nice']) - def test_args_option(self, open_mock): + def test_args_option(self): self.config['play']['command'] = 'echo' - self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo') + self.do_test(['-A', 'foo', 'title:aNiceTitle'], 'echo foo') - def test_args_option_in_middle(self, open_mock): + def test_args_option_in_middle(self): self.config['play']['command'] = 'echo $args other' - self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo other') + self.do_test(['-A', 'foo', 'title:aNiceTitle'], 'echo foo other') - def test_relative_to(self, open_mock): + def test_relative_to(self): self.config['play']['command'] = 'echo' self.config['play']['relative_to'] = '/something' - self.do_test(open_mock, expected_cmd='echo', expected_playlist='..{}\n') + self.do_test(expected_cmd='echo', expected_playlist='..{}\n') - def test_use_folders(self, open_mock): + def test_use_folders(self): self.config['play']['use_folders'] = True self.run_command('play', '-a', 'nice') - open_mock.assert_called_once_with(ANY, None) - playlist = open(open_mock.call_args[0][0][0], 'r') + self.open_mock.assert_called_once_with(ANY, None) + playlist = open(self.open_mock.call_args[0][0][0], 'r') self.assertEqual('{}\n'.format( os.path.dirname(self.item.path.decode('utf-8'))), playlist.read().decode('utf-8')) - def test_raw(self, open_mock): + def test_raw(self): self.config['play']['raw'] = True self.run_command('play', 'nice') - open_mock.assert_called_once_with([self.item.path], None) + self.open_mock.assert_called_once_with([self.item.path], None) - def test_not_found(self, open_mock): + def test_not_found(self): self.run_command('play', 'not found') - open_mock.assert_not_called() + self.open_mock.assert_not_called() - def test_warning_threshold(self, open_mock): + def test_warning_threshold(self): self.config['play']['warning_treshold'] = 1 item2 = self.add_item(title='another NiceTitle') with control_stdin("a"): self.run_command('play', 'nice') - open_mock.assert_not_called() + self.open_mock.assert_not_called() - def test_command_failed(self, open_mock): - open_mock.side_effect = OSError("some reason") + def test_command_failed(self): + self.open_mock.side_effect = OSError("some reason") with self.assertRaises(UserError): self.run_command('play', 'title:aNiceTitle') From dfce9e19aaf9af7088b344b518392fec0e50650e Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 13:11:30 -0800 Subject: [PATCH 13/15] Fix tests due to change in default command behavior --- test/test_play.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 3c04ab269..6d1f977f3 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -13,6 +13,7 @@ from test._common import unittest from test.helper import TestHelper, control_stdin from beets.ui import UserError +from beets.util import open_anything class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): @@ -22,13 +23,14 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.lib.add_album([self.item]) self.open_patcher = patch('beetsplug.play.util.interactive_open') self.open_mock = self.open_patcher.start() + self.config['play']['command'] = 'echo' def tearDown(self): self.open_patcher.stop() self.teardown_beets() self.unload_plugins() - def do_test(self, args=('title:aNiceTitle',), expected_cmd=None, expected_playlist='{}\n'): + def do_test(self, args=('title:aNiceTitle',), expected_cmd='echo', expected_playlist='{}\n'): self.run_command('play', *args) self.open_mock.assert_called_once_with(ANY, expected_cmd) @@ -43,8 +45,6 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.do_test(['-a', 'nice']) def test_args_option(self): - self.config['play']['command'] = 'echo' - self.do_test(['-A', 'foo', 'title:aNiceTitle'], 'echo foo') def test_args_option_in_middle(self): @@ -59,10 +59,11 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.do_test(expected_cmd='echo', expected_playlist='..{}\n') def test_use_folders(self): + self.config['play']['command'] = None self.config['play']['use_folders'] = True self.run_command('play', '-a', 'nice') - self.open_mock.assert_called_once_with(ANY, None) + self.open_mock.assert_called_once_with(ANY, open_anything()) playlist = open(self.open_mock.call_args[0][0][0], 'r') self.assertEqual('{}\n'.format( os.path.dirname(self.item.path.decode('utf-8'))), @@ -73,7 +74,7 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.run_command('play', 'nice') - self.open_mock.assert_called_once_with([self.item.path], None) + self.open_mock.assert_called_once_with([self.item.path], 'echo') def test_not_found(self): self.run_command('play', 'not found') From 875876fd1d8738f4c901ba63f849f5c3b71c0b02 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 16:49:51 -0800 Subject: [PATCH 14/15] flake8 fixes --- test/test_play.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 6d1f977f3..f17d72840 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -15,6 +15,7 @@ from test.helper import TestHelper, control_stdin from beets.ui import UserError from beets.util import open_anything + class PlayPluginTest(unittest.TestCase, TestHelper): def setUp(self): self.setup_beets() @@ -30,13 +31,14 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.teardown_beets() self.unload_plugins() - def do_test(self, args=('title:aNiceTitle',), expected_cmd='echo', expected_playlist='{}\n'): + def do_test(self, args=('title:aNiceTitle',), expected_cmd='echo', + expected_playlist='{}\n'): self.run_command('play', *args) self.open_mock.assert_called_once_with(ANY, expected_cmd) - expected_playlist_content = expected_playlist.format(self.item.path.decode('utf-8')) + exp_playlist = expected_playlist.format(self.item.path.decode('utf-8')) with open(self.open_mock.call_args[0][0][0], 'r') as playlist: - self.assertEqual(expected_playlist_content, playlist.read().decode('utf-8')) + self.assertEqual(exp_playlist, playlist.read().decode('utf-8')) def test_basic(self): self.do_test() @@ -83,7 +85,7 @@ class PlayPluginTest(unittest.TestCase, TestHelper): def test_warning_threshold(self): self.config['play']['warning_treshold'] = 1 - item2 = self.add_item(title='another NiceTitle') + self.add_item(title='another NiceTitle') with control_stdin("a"): self.run_command('play', 'nice') @@ -96,6 +98,7 @@ class PlayPluginTest(unittest.TestCase, TestHelper): with self.assertRaises(UserError): self.run_command('play', 'title:aNiceTitle') + def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From 6ba92be18db9a195c61a0e6b4633fc6a1ae2ec8a Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 23:34:16 -0800 Subject: [PATCH 15/15] Add std license header --- test/test_play.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_play.py b/test/test_play.py index f17d72840..d25d7b657 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -1,4 +1,17 @@ # -*- coding: utf-8 -*- +# This file is part of beets. +# Copyright 2016, Jesse Weinstein +# +# 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. """Tests for the play plugin"""