diff --git a/imagekit/cachefiles/backends.py b/imagekit/cachefiles/backends.py index e615f2e..bb0b710 100644 --- a/imagekit/cachefiles/backends.py +++ b/imagekit/cachefiles/backends.py @@ -52,8 +52,7 @@ class CachedFileBackend(object): @property def cache(self): if not getattr(self, '_cache', None): - from django.conf import settings - self._cache = get_cache(settings.IMAGEKIT_CACHE_BACKEND) + self._cache = get_cache() return self._cache def get_key(self, file): diff --git a/imagekit/utils.py b/imagekit/utils.py index fcbb4d4..eb01c1c 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -148,12 +148,12 @@ def call_strategy_method(file, method_name): fn(file) -def get_cache(backend, **kwargs): +def get_cache(backend=settings.IMAGEKIT_CACHE_BACKEND): try: from django.core.cache import caches except ImportError: from django.core.cache import get_cache - return get_cache(backend, **kwargs) + return get_cache(backend) return caches[backend] diff --git a/testrunner.py b/testrunner.py index e4d27c7..6b41b0f 100644 --- a/testrunner.py +++ b/testrunner.py @@ -16,4 +16,7 @@ def run_tests(): cls = get_runner(settings) runner = cls() failures = runner.run_tests(['tests']) + # Clean autogenerated junk before exit + from tests.utils import clear_imagekit_test_files + clear_imagekit_test_files() sys.exit(failures) diff --git a/tests/test_generateimage_tag.py b/tests/test_generateimage_tag.py index c39b794..433c86f 100644 --- a/tests/test_generateimage_tag.py +++ b/tests/test_generateimage_tag.py @@ -1,11 +1,12 @@ from django.template import TemplateSyntaxError from nose.tools import eq_, assert_false, raises, assert_not_equal from . import imagegenerators # noqa -from .utils import render_tag, get_html_attrs +from .utils import render_tag, get_html_attrs, clear_imagekit_cache def test_img_tag(): ttag = r"""{% generateimage 'testspec' source=img %}""" + clear_imagekit_cache() attrs = get_html_attrs(ttag) expected_attrs = set(['src', 'width', 'height']) eq_(set(attrs.keys()), expected_attrs) @@ -15,6 +16,7 @@ def test_img_tag(): def test_img_tag_attrs(): ttag = r"""{% generateimage 'testspec' source=img -- alt="Hello" %}""" + clear_imagekit_cache() attrs = get_html_attrs(ttag) eq_(attrs.get('alt'), 'Hello') @@ -42,11 +44,13 @@ def test_single_dimension_attr(): """ ttag = r"""{% generateimage 'testspec' source=img -- width="50" %}""" + clear_imagekit_cache() attrs = get_html_attrs(ttag) assert_false('height' in attrs) def test_assignment_tag(): ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}""" + clear_imagekit_cache() html = render_tag(ttag) assert_not_equal(html.strip(), '') diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 9b68af9..b995658 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -6,10 +6,11 @@ deserialized. This is important when using IK with Celery. from imagekit.cachefiles import ImageCacheFile from .imagegenerators import TestSpec -from .utils import create_photo, pickleback, get_unique_image_file +from .utils import create_photo, pickleback, get_unique_image_file, clear_imagekit_cache def test_imagespecfield(): + clear_imagekit_cache() instance = create_photo('pickletest2.jpg') thumbnail = pickleback(instance.thumbnail) thumbnail.generate() @@ -22,12 +23,14 @@ def test_circular_ref(): This corresponds to #234 """ + clear_imagekit_cache() instance = create_photo('pickletest3.jpg') instance.thumbnail # Cause thumbnail to be added to instance's __dict__ pickleback(instance) - + def test_cachefiles(): + clear_imagekit_cache() spec = TestSpec(source=get_unique_image_file()) file = ImageCacheFile(spec) file.url diff --git a/tests/test_thumbnail_tag.py b/tests/test_thumbnail_tag.py index e31304a..c938224 100644 --- a/tests/test_thumbnail_tag.py +++ b/tests/test_thumbnail_tag.py @@ -1,11 +1,12 @@ from django.template import TemplateSyntaxError from nose.tools import eq_, raises, assert_not_equal from . import imagegenerators # noqa -from .utils import render_tag, get_html_attrs +from .utils import render_tag, get_html_attrs, clear_imagekit_cache def test_img_tag(): ttag = r"""{% thumbnail '100x100' img %}""" + clear_imagekit_cache() attrs = get_html_attrs(ttag) expected_attrs = set(['src', 'width', 'height']) eq_(set(attrs.keys()), expected_attrs) @@ -15,6 +16,7 @@ def test_img_tag(): def test_img_tag_attrs(): ttag = r"""{% thumbnail '100x100' img -- alt="Hello" %}""" + clear_imagekit_cache() attrs = get_html_attrs(ttag) eq_(attrs.get('alt'), 'Hello') @@ -50,17 +52,20 @@ def test_html_attrs_assignment(): def test_assignment_tag(): ttag = r"""{% thumbnail '100x100' img as th %}{{ th.url }}""" + clear_imagekit_cache() html = render_tag(ttag) assert_not_equal(html, '') def test_single_dimension(): ttag = r"""{% thumbnail '100x' img as th %}{{ th.width }}""" + clear_imagekit_cache() html = render_tag(ttag) eq_(html, '100') def test_alternate_generator(): ttag = r"""{% thumbnail '1pxsq' '100x' img as th %}{{ th.width }}""" + clear_imagekit_cache() html = render_tag(ttag) eq_(html, '1') diff --git a/tests/utils.py b/tests/utils.py index bcf0dbd..8b16a09 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,10 +1,12 @@ from bs4 import BeautifulSoup import os -from django.conf import settings +import shutil from django.core.files import File from django.template import Context, Template from imagekit.cachefiles.backends import Simple, CacheFileState +from imagekit.conf import settings from imagekit.lib import Image, StringIO +from imagekit.utils import get_cache from nose.tools import assert_true, assert_false import pickle from tempfile import NamedTemporaryFile @@ -82,3 +84,23 @@ class DummyAsyncCacheFileBackend(Simple): def generate(self, file, force=False): pass + + +def clear_imagekit_cache(): + cache = get_cache() + cache.clear() + # Clear IMAGEKIT_CACHEFILE_DIR + cache_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGEKIT_CACHEFILE_DIR) + if os.path.exists(cache_dir): + shutil.rmtree(cache_dir) + + +def clear_imagekit_test_files(): + clear_imagekit_cache() + for fname in os.listdir(settings.MEDIA_ROOT): + if fname != 'reference.png': + path = os.path.join(settings.MEDIA_ROOT, fname) + if os.path.isdir(path): + shutil.rmtree(path) + else: + os.remove(path)