beets/test/test_convert.py
Adrian Sampson 77af4ce040 more test isolation (#670)
Doing test-specific cleanup in tearDown before general sandbox deletion helps
avoiding contamination of global state between tests when cleanup fails.

Current Windows status:

    Ran 1106 tests in 72.373s
    FAILED (SKIP=10, errors=13, failures=15)

Closer!
2014-04-13 13:57:22 -07:00

98 lines
3.3 KiB
Python

# This file is part of beets.
# Copyright 2014, 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.path
from _common import unittest
from helper import TestHelper, control_stdin
class ImportConvertTest(unittest.TestCase, TestHelper):
def setUp(self):
self.setup_beets(disk=True) # Converter is threaded
self.importer = self.create_importer()
self.load_plugins('convert')
self.config['convert'] = {
'dest': os.path.join(self.temp_dir, 'convert'),
# Append string so we can determine if the file was
# converted
'command': u'cp $source $dest; printf convert >> $dest',
# Enforce running convert
'max_bitrate': 1,
'auto': True,
'quiet': False,
}
def tearDown(self):
self.unload_plugins()
self.teardown_beets()
def test_import_converted(self):
self.importer.run()
item = self.lib.items().get()
self.assertConverted(item.path)
def test_import_original_on_convert_error(self):
# `false` exits with non-zero code
self.config['convert']['command'] = u'false'
self.importer.run()
item = self.lib.items().get()
self.assertIsNotNone(item)
self.assertTrue(os.path.isfile(item.path))
def assertConverted(self, path):
with open(path) as f:
f.seek(-7, os.SEEK_END)
self.assertEqual(f.read(), 'convert',
'{0} was not converted'.format(path))
class ConvertCliTest(unittest.TestCase, TestHelper):
def setUp(self):
self.setup_beets(disk=True) # Converter is threaded
self.item, = self.add_item_fixtures(ext='ogg')
self.load_plugins('convert')
self.convert_dest = os.path.join(self.temp_dir, 'convert_dest')
self.config['convert']['dest'] = str(self.convert_dest)
self.config['convert']['command'] = u'cp $source $dest'
self.config['convert']['paths']['default'] = u'converted'
def tearDown(self):
self.unload_plugins()
self.teardown_beets()
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.assertTrue(os.path.isfile(converted))
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')
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == '__main__':
unittest.main(defaultTest='suite')