Example #1
0
 def run_mocked_command(self, modify_file_args={}, stdin=[], args=[]):
     """Run the edit command, with mocked stdin and yaml writing, and
     passing `args` to `run_command`."""
     m = ModifyFileMocker(**modify_file_args)
     with patch('beetsplug.edit.edit', side_effect=m.action):
         with control_stdin('\n'.join(stdin)):
             self.run_command('edit', *args)
Example #2
0
    def test_plugin_callback_return(self):
        """Test that plugin callbacks that return a value exit the loop."""
        class DummyPlugin(plugins.BeetsPlugin):
            def __init__(self):
                super(DummyPlugin, self).__init__()
                self.register_listener('before_choose_candidate',
                                       self.return_choices)

            def return_choices(self, session, task):
                return [ui.commands.PromptChoice('f', u'Foo', self.foo)]

            def foo(self, session, task):
                return action.SKIP

        self.register_plugin(DummyPlugin)
        # Default options + extra choices by the plugin ('Foo', 'Bar')
        opts = (u'Apply', u'More candidates', u'Skip', u'Use as-is',
                u'as Tracks', u'Group albums', u'Enter search',
                u'enter Id', u'aBort') + (u'Foo',)

        # DummyPlugin.foo() should be called once
        with helper.control_stdin('f\n'):
            self.importer.run()

        # input_options should be called once, as foo() returns SKIP
        self.mock_input_options.assert_called_once_with(opts, default='a',
                                                        require=ANY)
Example #3
0
    def test_plugin_callback(self):
        """Test that plugin callbacks are being called upon user choice."""
        class DummyPlugin(plugins.BeetsPlugin):
            def __init__(self):
                super(DummyPlugin, self).__init__()
                self.register_listener('before_choose_candidate',
                                       self.return_choices)

            def return_choices(self, session, task):
                return [ui.commands.PromptChoice('f', u'Foo', self.foo)]

            def foo(self, session, task):
                pass

        self.register_plugin(DummyPlugin)
        # Default options + extra choices by the plugin ('Foo', 'Bar')
        opts = (u'Apply', u'More candidates', u'Skip', u'Use as-is',
                u'as Tracks', u'Group albums', u'Enter search',
                u'enter Id', u'aBort') + (u'Foo',)

        # DummyPlugin.foo() should be called once
        with patch.object(DummyPlugin, 'foo', autospec=True) as mock_foo:
            with helper.control_stdin('\n'.join(['f', 's'])):
                self.importer.run()
            self.assertEqual(mock_foo.call_count, 1)

        # input_options should be called twice, as foo() returns None
        self.assertEqual(self.mock_input_options.call_count, 2)
        self.mock_input_options.assert_called_with(opts, default='a',
                                                   require=ANY)
Example #4
0
    def test_subcommand_update_database_false(self):
        item = self.add_item_fixture(
            year=2016,
            day=13,
            month=3,
            comments=u'test comment'
        )
        item.write()
        item_id = item.id

        self.config['zero']['fields'] = ['comments']
        self.config['zero']['update_database'] = False
        self.config['zero']['auto'] = False

        self.load_plugins('zero')
        with control_stdin('y'):
            self.run_command('zero')

        mf = MediaFile(syspath(item.path))
        item = self.lib.get_item(item_id)

        self.assertEqual(item['year'], 2016)
        self.assertEqual(mf.year, 2016)
        self.assertEqual(item['comments'], u'test comment')
        self.assertEqual(mf.comments, None)
Example #5
0
 def test_transcode_from_lossy(self):
     self.config['convert']['never_convert_lossy_files'] = False
     [item] = self.add_item_fixtures(ext='ogg')
     with control_stdin('y'):
         self.run_command('convert', item.path)
     converted = os.path.join(self.convert_dest, 'converted.mp3')
     self.assertFileTag(converted, 'mp3')
Example #6
0
 def test_transcode_from_lossy(self):
     self.config["convert"]["never_convert_lossy_files"] = False
     [item] = self.add_item_fixtures(ext="ogg")
     with control_stdin("y"):
         self.run_command("convert", item.path)
     converted = os.path.join(self.convert_dest, "converted.mp3")
     self.assertFileTag(converted, "mp3")
Example #7
0
 def run_mocked_interpreter(self, modify_file_args={}, stdin=[]):
     """Run the edit command during an import session, with mocked stdin and
     yaml writing.
     """
     m = ModifyFileMocker(**modify_file_args)
     with patch('beetsplug.edit.edit', side_effect=m.action):
         with control_stdin('\n'.join(stdin)):
             self.importer.run()
Example #8
0
    def test_warning_threshold_backwards_compat(self, open_mock):
        self.config["play"]["warning_treshold"] = 1
        self.add_item(title=u"another NiceTitle")

        with control_stdin("a"):
            self.run_command(u"play", u"nice")

        open_mock.assert_not_called()
Example #9
0
    def test_warning_threshold(self):
        self.config['play']['warning_threshold'] = 1
        self.add_item(title='another NiceTitle')

        with control_stdin("a"):
            self.run_command(u'play', u'nice')

        self.open_mock.assert_not_called()
Example #10
0
    def test_convert_keep_new(self):
        self.assertEqual(os.path.splitext(self.item.path)[1], '.ogg')

        with control_stdin('y'):
            self.run_command('convert', '--keep-new', self.item.path)

        self.item.load()
        self.assertEqual(os.path.splitext(self.item.path)[1], '.mp3')
Example #11
0
    def test_convert_keep_new(self):
        self.assertEqual(os.path.splitext(self.item.path)[1], ".ogg")

        with control_stdin("y"):
            self.run_command("convert", "--keep-new", self.item.path)

        self.item.load()
        self.assertEqual(os.path.splitext(self.item.path)[1], ".mp3")
Example #12
0
    def test_print_tracks_output_as_tracks(self):
        """Test the output of the "print tracks" choice, as singletons."""
        self.matcher.matching = AutotagStub.BAD

        with capture_stdout() as output:
            with control_stdin("\n".join(["t", "s", "p", "s"])):
                # as Tracks; Skip; Print tracks; Skip
                self.importer.run()

        # Manually build the string for comparing the output.
        tracklist = u"Print tracks? " u"02. Tag Title 2 - Tag Artist (0:01)"
        self.assertIn(tracklist, output.getvalue())
Example #13
0
    def test_skip_warning_threshold_bypass(self, open_mock):
        self.config['play']['warning_threshold'] = 1
        self.other_item = self.add_item(title='another NiceTitle')

        expected_playlist = u'{0}\n{1}'.format(
            self.item.path.decode('utf-8'),
            self.other_item.path.decode('utf-8'))

        with control_stdin("a"):
            self.run_and_assert(
                open_mock,
                [u'-y', u'NiceTitle'],
                expected_playlist=expected_playlist)
Example #14
0
    def test_embed_album_art(self):
        self.config["convert"]["embed"] = True
        image_path = os.path.join(_common.RSRC, "image-2x3.jpg")
        self.album.artpath = image_path
        self.album.store()
        with open(os.path.join(image_path)) as f:
            image_data = f.read()

        with control_stdin("y"):
            self.run_command("convert", self.item.path)
        converted = os.path.join(self.convert_dest, "converted.mp3")
        mediafile = MediaFile(converted)
        self.assertEqual(mediafile.images[0].data, image_data)
Example #15
0
    def test_embed_album_art(self):
        self.config['convert']['embed'] = True
        image_path = os.path.join(_common.RSRC, 'image-2x3.jpg')
        self.album.artpath = image_path
        self.album.store()
        with open(os.path.join(image_path)) as f:
            image_data = f.read()

        with control_stdin('y'):
            self.run_command('convert', self.item.path)
        converted = os.path.join(self.convert_dest, 'converted.mp3')
        mediafile = MediaFile(converted)
        self.assertEqual(mediafile.images[0].data, image_data)
Example #16
0
    def test_print_tracks_output(self):
        """Test the output of the "print tracks" choice."""
        self.matcher.matching = AutotagStub.BAD

        with capture_stdout() as output:
            with control_stdin('\n'.join(['p', 's'])):
                # Print tracks; Skip
                self.importer.run()

        # Manually build the string for comparing the output.
        tracklist = ('Print tracks? '
                     '01. Tag Title 1 - Tag Artist (0:01)\n'
                     '02. Tag Title 2 - Tag Artist (0:01)')
        self.assertIn(tracklist, output.getvalue())
Example #17
0
    def test_no_fields(self):
        item = self.add_item_fixture(year=2016)
        item.write()
        mediafile = MediaFile(syspath(item.path))
        self.assertEqual(mediafile.year, 2016)

        item_id = item.id

        self.load_plugins('zero')
        with control_stdin('y'):
            self.run_command('zero')

        item = self.lib.get_item(item_id)

        self.assertEqual(item['year'], 2016)
        self.assertEqual(mediafile.year, 2016)
Example #18
0
    def test_whitelist_and_blacklist(self):
        item = self.add_item_fixture(year=2016)
        item.write()
        mf = MediaFile(syspath(item.path))
        self.assertEqual(mf.year, 2016)

        item_id = item.id
        self.config['zero']['fields'] = [u'year']
        self.config['zero']['keep_fields'] = [u'comments']

        self.load_plugins('zero')
        with control_stdin('y'):
            self.run_command('zero')

        item = self.lib.get_item(item_id)

        self.assertEqual(item['year'], 2016)
        self.assertEqual(mf.year, 2016)
Example #19
0
 def test_rejecet_confirmation(self):
     with control_stdin('n'):
         self.run_command('convert', self.item.path)
     converted = os.path.join(self.convert_dest, 'converted.mp3')
     self.assertFalse(os.path.isfile(converted))
Example #20
0
 def test_convert(self):
     with control_stdin('y'):
         self.run_command('convert', self.item.path)
     converted = os.path.join(self.convert_dest, 'converted.mp3')
     self.assertFileTag(converted, 'mp3')
Example #21
0
 def modify_inp(self, inp, *args):
     with control_stdin(inp):
         self.run_command('modify', *args)
Example #22
0
 def test_format_option(self):
     with control_stdin("y"):
         self.run_command("convert", "--format", "opus", self.item.path)
         converted = os.path.join(self.convert_dest, "converted.ops")
     self.assertFileTag(converted, "opus")
Example #23
0
 def modify_inp(self, inp, *args):
     with control_stdin(inp):
         self.run_command('modify', *args)
Example #24
0
 def test_convert(self):
     with control_stdin('y'):
         self.run_convert()
     converted = os.path.join(self.convert_dest, b'converted.mp3')
     self.assertFileTag(converted, 'mp3')
Example #25
0
 def modify(self, *args):
     with control_stdin('y'):
         ui._raw_main(['modify'] + list(args), self.lib)
Example #26
0
 def test_transcode_from_lossles(self):
     [item] = self.add_item_fixtures(ext='flac')
     with control_stdin('y'):
         self.run_convert_path(item.path)
     converted = os.path.join(self.convert_dest, 'converted.mp3')
     self.assertFileTag(converted, 'mp3')
Example #27
0
 def test_convert(self):
     with control_stdin('y'):
         self.run_convert()
     converted = os.path.join(self.convert_dest, b'converted.mp3')
     self.assertFileTag(converted, 'mp3')
Example #28
0
 def test_transcode_from_lossy_prevented(self):
     [item] = self.add_item_fixtures(ext="ogg")
     with control_stdin("y"):
         self.run_command("convert", item.path)
     converted = os.path.join(self.convert_dest, "converted.ogg")
     self.assertNoFileTag(converted, "mp3")
Example #29
0
 def test_format_option(self):
     with control_stdin('y'):
         self.run_command('convert', '--format', 'opus', self.item.path)
         converted = os.path.join(self.convert_dest, 'converted.ops')
     self.assertFileTag(converted, 'opus')
Example #30
0
 def modify_inp(self, inp, *args):
     with control_stdin(inp):
         ui._raw_main(['modify'] + list(args), self.lib)
Example #31
0
 def modify_inp(self, inp, *args):
     with control_stdin(inp):
         ui._raw_main(['modify'] + list(args), self.lib)
Example #32
0
 def test_transcode_from_lossy_prevented(self):
     [item] = self.add_item_fixtures(ext='ogg')
     with control_stdin('y'):
         self.run_command('convert', item.path)
     converted = os.path.join(self.convert_dest, 'converted.ogg')
     self.assertNoFileTag(converted, 'mp3')
Example #33
0
 def test_convert(self):
     with control_stdin("y"):
         self.run_command("convert", self.item.path)
     converted = os.path.join(self.convert_dest, "converted.mp3")
     self.assertFileTag(converted, "mp3")