Avoid TestHelper dependency in MediaFile tests

Part of #1966. This also introduces a nicer, more lightweight way to create and
destroy temporary directories, decoupling that functionality from the
giant morass that is TestHelper.
This commit is contained in:
Adrian Sampson 2016-11-26 18:55:58 -05:00
parent a88682e7bb
commit d8c0a42e6c
3 changed files with 30 additions and 13 deletions

View file

@ -333,6 +333,28 @@ class Bag(object):
return self.fields.get(key)
# Convenience methods for setting up a temporary sandbox directory for tests
# that need to interact with the filesystem.
class TempDirMixin(object):
"""Text mixin for creating and deleting a temporary directory.
"""
def create_temp_dir(self):
"""Create a temporary directory and assign it into `self.temp_dir`.
Call `remove_temp_dir` later to delete it.
"""
path = tempfile.mkdtemp()
if not isinstance(path, bytes):
path = path.encode('utf8')
self.temp_dir = path
def remove_temp_dir(self):
"""Delete the temporary directory created by `create_temp_dir`.
"""
shutil.rmtree(self.temp_dir)
# Platform mocking.
@contextmanager

View file

@ -20,7 +20,6 @@ from __future__ import division, absolute_import, print_function
import os
import shutil
import tempfile
import datetime
import time
import unittest
@ -293,7 +292,8 @@ class GenreListTestMixin(object):
assertCountEqual(self, mediafile.genres, [u'the genre', u'another'])
class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin):
class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin,
_common.TempDirMixin):
"""Test writing and reading tags. Subclasses must set ``extension`` and
``audio_properties``.
"""
@ -378,14 +378,10 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin):
]
def setUp(self):
name = tempfile.mkdtemp()
if not isinstance(name, bytes):
name = name.encode('utf8')
self.temp_dir = name
self.create_temp_dir()
def tearDown(self):
if os.path.isdir(self.temp_dir):
shutil.rmtree(self.temp_dir)
self.remove_temp_dir()
def test_read_nonexisting(self):
mediafile = self._mediafile_fixture('full')

View file

@ -23,7 +23,6 @@ import unittest
import mutagen.id3
from test import _common
from test.helper import TestHelper
from beets import mediafile
import six
@ -145,7 +144,7 @@ class InvalidValueToleranceTest(unittest.TestCase):
self.assertEqual(v, 1.0)
class SafetyTest(unittest.TestCase, TestHelper):
class SafetyTest(unittest.TestCase, _common.TempDirMixin):
def setUp(self):
self.create_temp_dir()
@ -210,7 +209,7 @@ class SideEffectsTest(unittest.TestCase):
self.assertEqual(old_mtime, new_mtime)
class MP4EncodingTest(unittest.TestCase, TestHelper):
class MP4EncodingTest(unittest.TestCase, _common.TempDirMixin):
def setUp(self):
self.create_temp_dir()
src = os.path.join(_common.RSRC, b'full.m4a')
@ -229,7 +228,7 @@ class MP4EncodingTest(unittest.TestCase, TestHelper):
self.assertEqual(new_mf.label, u'foo\xe8bar')
class MP3EncodingTest(unittest.TestCase, TestHelper):
class MP3EncodingTest(unittest.TestCase, _common.TempDirMixin):
def setUp(self):
self.create_temp_dir()
src = os.path.join(_common.RSRC, b'full.mp3')
@ -332,7 +331,7 @@ class SoundCheckTest(unittest.TestCase):
self.assertEqual(peak, 0.0)
class ID3v23Test(unittest.TestCase, TestHelper):
class ID3v23Test(unittest.TestCase, _common.TempDirMixin):
def _make_test(self, ext=b'mp3', id3v23=False):
self.create_temp_dir()
src = os.path.join(_common.RSRC,