diff --git a/whipper/command/cd.py b/whipper/command/cd.py index 488faef..7060f86 100644 --- a/whipper/command/cd.py +++ b/whipper/command/cd.py @@ -497,6 +497,10 @@ Log files will log the path to tracks relative to this directory. continue _ripIfNotRipped(i + 1) + # NOTE: Seems like some kind of with … or try: … finally: … clause + # would be more appropriate, since otherwise this would potentially + # leave stray files lying around in case of crashes etc. + # if (self.options.fetch_cover_art == "embed" and self.coverArtPath is not None): logger.debug('deleting cover art file at: %r', self.coverArtPath) diff --git a/whipper/test/76df3287-6cda-33eb-8e9a-044b5e15ffdd.jpg b/whipper/test/76df3287-6cda-33eb-8e9a-044b5e15ffdd.jpg new file mode 100644 index 0000000..07179a3 Binary files /dev/null and b/whipper/test/76df3287-6cda-33eb-8e9a-044b5e15ffdd.jpg differ diff --git a/whipper/test/test_common_program.py b/whipper/test/test_common_program.py index 36cc7a6..8871de9 100644 --- a/whipper/test/test_common_program.py +++ b/whipper/test/test_common_program.py @@ -2,8 +2,10 @@ # vi:si:et:sw=4:sts=4:ts=4 +import os import unittest +from tempfile import NamedTemporaryFile from whipper.common import program, mbngs, config from whipper.command.cd import DEFAULT_DISC_TEMPLATE @@ -38,3 +40,54 @@ class PathTestCase(unittest.TestCase): path = prog.getPath('/tmp', '%A/%d', 'mbdiscid', md, 0) self.assertEqual(path, '/tmp/Jeff Buckley/Grace') + + +# TODO: Test cover art embedding too. +class CoverArtTestCase(unittest.TestCase): + + @staticmethod + def _mock_get_front_image(release_id): + """ + Mock `musicbrainzngs.get_front_image` function. + + Reads a local cover art image and returns its binary data. + + :param release_id: a release id (self.program.metadata.mbid) + :type release_id: str + :returns: the binary content of the local cover art image + :rtype: bytes + """ + filename = '%s.jpg' % release_id + path = os.path.join(os.path.dirname(__file__), filename) + with open(path, 'rb') as f: + return f.read() + + def _mock_getCoverArt(self, path, release_id): + """ + Mock `common.program.getCoverArt` function. + + :param path: where to store the fetched image + :type path: str + :param release_id: a release id (self.program.metadata.mbid) + :type release_id: str + :returns: path to the downloaded cover art + :rtype: str + """ + cover_art_path = os.path.join(path, 'cover.jpg') + + data = self._mock_get_front_image(release_id) + + with NamedTemporaryFile(suffix='.cover.jpg', delete=False) as f: + f.write(data) + os.chmod(f.name, 0o644) + os.replace(f.name, cover_art_path) + return cover_art_path + + def testCoverArtPath(self): + """Test whether a fetched cover art is saved properly.""" + # Using: Dummy by Portishead + # https://musicbrainz.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd + path = os.path.dirname(__file__) + release_id = "76df3287-6cda-33eb-8e9a-044b5e15ffdd" + coverArtPath = self._mock_getCoverArt(path, release_id) + self.assertTrue(os.path.isfile(coverArtPath))