Skip to content

Commit

Permalink
Merge pull request #544 from slafs/slafs/test-against-older-djangos
Browse files Browse the repository at this point in the history
Add support for older Djangos
  • Loading branch information
davidt committed Feb 26, 2016
2 parents 8b30436 + 235a226 commit f2366b5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ python:
- 3.5
sudo: false
env:
- TOXENV=py27-django16
- TOXENV=py34-django16
- TOXENV=pypy-django16
- TOXENV=py27-django17
- TOXENV=py34-django17
- TOXENV=pypy-django17
- TOXENV=py27-django18
- TOXENV=pypy-django18
- TOXENV=py34-django18
Expand Down
8 changes: 7 additions & 1 deletion tests/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings, modify_settings
from django.test.utils import override_settings

try:
from django.test.utils import modify_settings
except ImportError:
# Django < 1.7
from tests.utils import modify_settings

from pipeline.collector import default_collector
from pipeline.storage import PipelineStorage
Expand Down
57 changes: 56 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import os

from django.test import override_settings
from django.conf import settings
from django.utils import six

try:
from django.test import override_settings
except ImportError:
# Django < 1.7
from django.test.utils import override_settings


def _(path):
Expand All @@ -11,3 +18,51 @@ def _(path):
class pipeline_settings(override_settings):
def __init__(self, **kwargs):
self.options = {'PIPELINE': kwargs}


# Django < 1.7 (copy-pasted from Django 1.7)
class modify_settings(override_settings):
"""
Like override_settings, but makes it possible to append, prepend or remove
items instead of redefining the entire list.
"""
def __init__(self, *args, **kwargs):
if args:
# Hack used when instantiating from SimpleTestCase._pre_setup.
assert not kwargs
self.operations = args[0]
else:
assert not args
self.operations = list(kwargs.items())

def save_options(self, test_func):
if test_func._modified_settings is None:
test_func._modified_settings = self.operations
else:
# Duplicate list to prevent subclasses from altering their parent.
test_func._modified_settings = list(
test_func._modified_settings) + self.operations

def enable(self):
self.options = {}
for name, operations in self.operations:
try:
# When called from SimpleTestCase._pre_setup, values may be
# overridden several times; cumulate changes.
value = self.options[name]
except KeyError:
value = list(getattr(settings, name, []))
for action, items in operations.items():
# items my be a single value or an iterable.
if isinstance(items, six.string_types):
items = [items]
if action == 'append':
value = value + [item for item in items if item not in value]
elif action == 'prepend':
value = [item for item in items if item not in value] + value
elif action == 'remove':
value = [item for item in value if item not in items]
else:
raise ValueError("Unsupported action: %s" % action)
self.options[name] = value
super(modify_settings, self).enable()
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
{py27,pypy,py34}-django{18,19},py35-django19,docs
{py27,pypy,py34}-django{16,17,18,19},py35-django19,docs

[testenv]
basepython =
Expand All @@ -11,6 +11,8 @@ basepython =
deps =
py{27,py}: mock
py{27,py}: futures
django16: Django>=1.6,<1.7
django17: Django>=1.7,<1.8
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
jinja2
Expand Down

0 comments on commit f2366b5

Please sign in to comment.