From 85ddfa43815439d0ebe1550c8a3f7fe2fee2e497 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 12 Apr 2011 14:26:48 -0700 Subject: [PATCH] relocatable test rsrc directory --- test/_common.py | 3 +++ test/test_autotag.py | 5 ++--- test/test_db.py | 7 ++++--- test/test_files.py | 14 +++++++------- test/test_importer.py | 12 ++++++------ test/test_mediafile.py | 18 +++++++++--------- test/test_mediafile_basic.py | 8 ++++---- test/test_query.py | 8 ++++++-- test/test_ui.py | 4 ++-- 9 files changed, 43 insertions(+), 36 deletions(-) diff --git a/test/_common.py b/test/_common.py index 9189964d8..b124a1a79 100644 --- a/test/_common.py +++ b/test/_common.py @@ -22,6 +22,9 @@ sys.path.insert(0, '..') import beets.library from beets import importer +# Test resources/sandbox path. +RSRC = os.path.join(os.path.dirname(__file__), 'rsrc') + # Dummy item creation. def item(): return beets.library.Item({ 'title': u'the title', diff --git a/test/test_autotag.py b/test/test_autotag.py index 1e97ae044..5072337a0 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -16,7 +16,6 @@ """ import unittest -import sys import os import shutil import re @@ -151,11 +150,11 @@ class AlbumDistanceTest(unittest.TestCase): self.assertNotEqual(autotag.distance(items, info), 0) def _mkmp3(path): - shutil.copyfile(os.path.join('rsrc', 'min.mp3'), path) + shutil.copyfile(os.path.join(_common.RSRC, 'min.mp3'), path) class AlbumsInDirTest(unittest.TestCase): def setUp(self): # create a directory structure for testing - self.base = os.path.abspath(os.path.join('rsrc', 'temp_albumsindir')) + self.base = os.path.abspath(os.path.join(_common.RSRC, 'tempdir')) os.mkdir(self.base) os.mkdir(os.path.join(self.base, 'album1')) diff --git a/test/test_db.py b/test/test_db.py index 9d7e25da1..8f516f42f 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -26,7 +26,8 @@ from _common import item import beets.library from beets import util -def lib(): return beets.library.Library('rsrc' + os.sep + 'test.blb') +def lib(): + return beets.library.Library(os.path.join(_common.RSRC, 'test.blb')) def boracay(l): return beets.library.Item(l.conn.execute('select * from items ' 'where id=3').fetchone()) np = util.normpath @@ -90,7 +91,7 @@ class AddTest(unittest.TestCase): self.assertEqual(new_grouping, self.i.grouping) def test_library_add_path_inserts_row(self): - i = beets.library.Item.from_path(os.path.join('rsrc', 'full.mp3')) + i = beets.library.Item.from_path(os.path.join(_common.RSRC, 'full.mp3')) self.lib.add(i) new_grouping = self.lib.conn.execute('select grouping from items ' 'where composer="the composer"').fetchone()['grouping'] @@ -369,7 +370,7 @@ class MigrationTest(unittest.TestCase): self.newer_fields = self.new_fields + [('field_four', 'int')] # Set up a library with old_fields. - self.libfile = os.path.join('rsrc', 'templib.blb') + self.libfile = os.path.join(_common.RSRC, 'templib.blb') old_lib = beets.library.Library(self.libfile, item_fields=self.old_fields) # Add an item to the old library. diff --git a/test/test_files.py b/test/test_files.py index 0f58af053..b52d56ff1 100644 --- a/test/test_files.py +++ b/test/test_files.py @@ -32,8 +32,8 @@ def touch(path): class MoveTest(unittest.TestCase): def setUp(self): # make a temporary file - self.path = join('rsrc', 'temp.mp3') - shutil.copy(join('rsrc', 'full.mp3'), self.path) + self.path = join(_common.RSRC, 'temp.mp3') + shutil.copy(join(_common.RSRC, 'full.mp3'), self.path) # add it to a temporary library self.lib = beets.library.Library(':memory:') @@ -41,7 +41,7 @@ class MoveTest(unittest.TestCase): self.lib.add(self.i) # set up the destination - self.libdir = join('rsrc', 'testlibdir') + self.libdir = join(_common.RSRC, 'testlibdir') self.lib.directory = self.libdir self.lib.path_formats = {'default': join('$artist', '$album', '$title')} self.i.artist = 'one' @@ -132,7 +132,7 @@ class AlbumFileTest(unittest.TestCase): self.lib = beets.library.Library(':memory:') self.lib.path_formats = \ {'default': join('$albumartist', '$album', '$title')} - self.libdir = os.path.join('rsrc', 'testlibdir') + self.libdir = os.path.join(_common.RSRC, 'testlibdir') self.lib.directory = self.libdir self.i = item() # Make a file for the item. @@ -174,7 +174,7 @@ class ArtFileTest(unittest.TestCase): def setUp(self): # Make library and item. self.lib = beets.library.Library(':memory:') - self.libdir = os.path.abspath(os.path.join('rsrc', 'testlibdir')) + self.libdir = os.path.abspath(os.path.join(_common.RSRC, 'testlibdir')) self.lib.directory = self.libdir self.i = item() self.i.path = self.lib.destination(self.i) @@ -247,7 +247,7 @@ class RemoveTest(unittest.TestCase): def setUp(self): # Make library and item. self.lib = beets.library.Library(':memory:') - self.libdir = os.path.abspath(os.path.join('rsrc', 'testlibdir')) + self.libdir = os.path.abspath(os.path.join(_common.RSRC, 'testlibdir')) self.lib.directory = self.libdir self.i = item() self.i.path = self.lib.destination(self.i) @@ -282,7 +282,7 @@ class RemoveTest(unittest.TestCase): self.assertTrue(os.path.exists(self.libdir)) def test_removing_item_outside_of_library_deletes_nothing(self): - self.lib.directory = os.path.abspath(os.path.join('rsrc', 'xxx')) + self.lib.directory = os.path.abspath(os.path.join(_common.RSRC, 'xxx')) parent = os.path.dirname(self.i.path) self.lib.remove(self.i, True) self.assertTrue(os.path.exists(parent)) diff --git a/test/test_importer.py b/test/test_importer.py index c8853922c..178032d62 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -34,15 +34,15 @@ class NonAutotaggedImportTest(unittest.TestCase): log = logging.getLogger('beets') log.setLevel(logging.CRITICAL) - self.libdb = os.path.join('rsrc', 'testlib.blb') + self.libdb = os.path.join(_common.RSRC, 'testlib.blb') self.lib = library.Library(self.libdb) - self.libdir = os.path.join('rsrc', 'testlibdir') + self.libdir = os.path.join(_common.RSRC, 'testlibdir') self.lib.directory = self.libdir self.lib.path_formats = { 'default': os.path.join('$artist', '$album', '$title') } - self.srcdir = os.path.join('rsrc', 'testsrcdir') + self.srcdir = os.path.join(_common.RSRC, 'testsrcdir') def tearDown(self): self.io.restore() @@ -62,7 +62,7 @@ class NonAutotaggedImportTest(unittest.TestCase): realpath = os.path.join(self.srcdir, *filepath) if not os.path.exists(os.path.dirname(realpath)): os.makedirs(os.path.dirname(realpath)) - shutil.copy(os.path.join('rsrc', 'full.mp3'), realpath) + shutil.copy(os.path.join(_common.RSRC, 'full.mp3'), realpath) f = mediafile.MediaFile(realpath) for attr in metadata: @@ -137,7 +137,7 @@ class NonAutotaggedImportTest(unittest.TestCase): class ImportApplyTest(unittest.TestCase, _common.ExtraAsserts): def setUp(self): - self.libdir = os.path.join('rsrc', 'testlibdir') + self.libdir = os.path.join(_common.RSRC, 'testlibdir') os.mkdir(self.libdir) self.lib = library.Library(':memory:', self.libdir) self.lib.path_formats = { @@ -147,7 +147,7 @@ class ImportApplyTest(unittest.TestCase, _common.ExtraAsserts): } self.srcpath = os.path.join(self.libdir, 'srcfile.mp3') - shutil.copy(os.path.join('rsrc', 'full.mp3'), self.srcpath) + shutil.copy(os.path.join(_common.RSRC, 'full.mp3'), self.srcpath) self.i = library.Item.from_path(self.srcpath) self.i.comp = False diff --git a/test/test_mediafile.py b/test/test_mediafile.py index b1163f51f..97cf8bc20 100644 --- a/test/test_mediafile.py +++ b/test/test_mediafile.py @@ -27,7 +27,7 @@ class EdgeTest(unittest.TestCase): # This is very hard to produce, so this is just the first 8192 # bytes of a file found "in the wild". emptylist = beets.mediafile.MediaFile( - os.path.join('rsrc', 'emptylist.mp3')) + os.path.join(_common.RSRC, 'emptylist.mp3')) genre = emptylist.genre self.assertEqual(genre, '') @@ -35,7 +35,7 @@ class EdgeTest(unittest.TestCase): # Ensures that release times delimited by spaces are ignored. # Amie Street produces such files. space_time = beets.mediafile.MediaFile( - os.path.join('rsrc', 'space_time.mp3')) + os.path.join(_common.RSRC, 'space_time.mp3')) self.assertEqual(space_time.year, 2009) self.assertEqual(space_time.month, 9) self.assertEqual(space_time.day, 4) @@ -44,7 +44,7 @@ class EdgeTest(unittest.TestCase): # Ensures that release times delimited by Ts are ignored. # The iTunes Store produces such files. t_time = beets.mediafile.MediaFile( - os.path.join('rsrc', 't_time.m4a')) + os.path.join(_common.RSRC, 't_time.m4a')) self.assertEqual(t_time.year, 1987) self.assertEqual(t_time.month, 3) self.assertEqual(t_time.day, 31) @@ -52,19 +52,19 @@ class EdgeTest(unittest.TestCase): def test_tempo_with_bpm(self): # Some files have a string like "128 BPM" in the tempo field # rather than just a number. - f = beets.mediafile.MediaFile(os.path.join('rsrc', 'bpm.mp3')) + f = beets.mediafile.MediaFile(os.path.join(_common.RSRC, 'bpm.mp3')) self.assertEqual(f.bpm, 128) def test_discc_alternate_field(self): # Different taggers use different vorbis comments to reflect # the disc and disc count fields: ensure that the alternative # style works. - f = beets.mediafile.MediaFile(os.path.join('rsrc', 'discc.ogg')) + f = beets.mediafile.MediaFile(os.path.join(_common.RSRC, 'discc.ogg')) self.assertEqual(f.disc, 4) self.assertEqual(f.disctotal, 5) def test_old_ape_version_bitrate(self): - f = beets.mediafile.MediaFile(os.path.join('rsrc', 'oldape.ape')) + f = beets.mediafile.MediaFile(os.path.join(_common.RSRC, 'oldape.ape')) self.assertEqual(f.bitrate, 0) _sc = beets.mediafile._safe_cast @@ -95,7 +95,7 @@ class InvalidValueToleranceTest(unittest.TestCase): class SafetyTest(unittest.TestCase): def _exccheck(self, fn, exc, data=''): - fn = os.path.join('rsrc', fn) + fn = os.path.join(_common.RSRC, fn) with open(fn, 'w') as f: f.write(data) try: @@ -131,7 +131,7 @@ class SafetyTest(unittest.TestCase): "ftyp") def test_broken_symlink(self): - fn = os.path.join('rsrc', 'brokenlink') + fn = os.path.join(_common.RSRC, 'brokenlink') os.symlink('does_not_exist', fn) try: self.assertRaises(beets.mediafile.UnreadableFileError, @@ -141,7 +141,7 @@ class SafetyTest(unittest.TestCase): class SideEffectsTest(unittest.TestCase): def setUp(self): - self.empty = os.path.join('rsrc', 'empty.mp3') + self.empty = os.path.join(_common.RSRC, 'empty.mp3') def test_opening_tagless_file_leaves_untouched(self): old_mtime = os.stat(self.empty).st_mtime diff --git a/test/test_mediafile_basic.py b/test/test_mediafile_basic.py index a217f13d1..9f103b80a 100644 --- a/test/test_mediafile_basic.py +++ b/test/test_mediafile_basic.py @@ -277,22 +277,22 @@ def suite(): # General tests. for kind, tagsets in test_files.items(): for tagset in tagsets: - path = os.path.join('rsrc', tagset + '.' + kind) + path = os.path.join(_common.RSRC, tagset + '.' + kind) correct_dict = correct_dicts[tagset] s.addTest(suite_for_file(path, correct_dict)) # Special test for missing ID3 tag. - s.addTest(suite_for_file(os.path.join('rsrc', 'empty.mp3'), + s.addTest(suite_for_file(os.path.join(_common.RSRC, 'empty.mp3'), correct_dicts['empty'], writing=False)) # Special test for advanced release date. - s.addTest(suite_for_file(os.path.join('rsrc', 'date.mp3'), + s.addTest(suite_for_file(os.path.join(_common.RSRC, 'date.mp3'), correct_dicts['date'])) # Read-only attribute tests. for fname, correct_dict in read_only_correct_dicts.iteritems(): - path = os.path.join('rsrc', fname) + path = os.path.join(_common.RSRC, fname) for field, value in correct_dict.iteritems(): s.addTest(MakeReadOnlyTest(path, field, value)()) diff --git a/test/test_query.py b/test/test_query.py index 7d26b178b..0200bd571 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -94,7 +94,9 @@ class AssertsMixin(object): class GetTest(unittest.TestCase, AssertsMixin): def setUp(self): - self.lib = beets.library.Library('rsrc' + os.sep + 'test.blb') + self.lib = beets.library.Library( + os.path.join(_common.RSRC, 'test.blb') + ) def test_get_empty(self): q = '' @@ -206,7 +208,9 @@ class MemoryGetTest(unittest.TestCase, AssertsMixin): class BrowseTest(unittest.TestCase, AssertsMixin): def setUp(self): - self.lib = beets.library.Library('rsrc' + os.sep + 'test.blb') + self.lib = beets.library.Library( + os.path.join(_common.RSRC, 'test.blb') + ) def test_artist_list(self): artists = list(self.lib.artists()) diff --git a/test/test_ui.py b/test/test_ui.py index 0bfbc7913..7dc51bcf5 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -72,12 +72,12 @@ class RemoveTest(unittest.TestCase): self.io = _common.DummyIO() self.io.install() - self.libdir = os.path.join('rsrc', 'testlibdir') + self.libdir = os.path.join(_common.RSRC, 'testlibdir') os.mkdir(self.libdir) # Copy a file into the library. self.lib = library.Library(':memory:', self.libdir) - self.i = library.Item.from_path(os.path.join('rsrc', 'full.mp3')) + self.i = library.Item.from_path(os.path.join(_common.RSRC, 'full.mp3')) self.lib.add(self.i, True) def tearDown(self):