Skip to content

Commit

Permalink
Use repr instead of str in debugging messages, to avoid premature imp…
Browse files Browse the repository at this point in the history
…orts of the urlconf. Fixes #10.
  • Loading branch information
mikebryant committed Oct 8, 2014
1 parent c3d4c4f commit ee85fef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 6 additions & 6 deletions django_autoconfig/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def apply_changes(self, settings):
for item in [self.setting_value] + self.before + self.after:
if item not in settings[self.setting_name]:
settings[self.setting_name] = list(settings[self.setting_name]) + [item]
LOGGER.debug("Added %s to %s.", item, self.setting_name)
LOGGER.debug("Added %r to %r.", item, self.setting_name)
changes += 1
elif self.setting_value not in settings[self.setting_name]:
return changes
Expand All @@ -86,7 +86,7 @@ def apply_changes(self, settings):
current_value.remove(self.setting_value)
current_value.insert(location, self.setting_value)
settings[self.setting_name] = current_value
LOGGER.debug("Moved %s %s %s.", self.setting_value, list_name, item)
LOGGER.debug("Moved %r %r %r.", self.setting_value, list_name, item)
changes += 1

return changes
Expand All @@ -101,10 +101,10 @@ def merge_dictionaries(current, new, only_defaults=False):
if key not in current:
if hasattr(global_settings, key):
current[key] = getattr(global_settings, key)
LOGGER.debug("Set %s to global default %s.", key, current[key])
LOGGER.debug("Set %r to global default %r.", key, current[key])
else:
current[key] = copy.copy(value)
LOGGER.debug("Set %s to %s.", key, current[key])
LOGGER.debug("Set %r to %r.", key, current[key])
changes += 1
continue
elif only_defaults:
Expand All @@ -116,13 +116,13 @@ def merge_dictionaries(current, new, only_defaults=False):
for element in value:
if element not in current_value:
current[key] = list(current_value) + [element]
LOGGER.debug("Added %s to %s.", element, key)
LOGGER.debug("Added %r to %r.", element, key)
changes += 1
else:
# If we don't know what to do with it, replace it.
if current_value != value:
current[key] = value
LOGGER.debug("Set %s to %s.", key, current[key])
LOGGER.debug("Set %r to %r.", key, current[key])
changes += 1
return changes

Expand Down
29 changes: 27 additions & 2 deletions django_autoconfig/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import copy
from django.core.exceptions import ImproperlyConfigured
import django.core.urlresolvers
from django.core.urlresolvers import resolve, RegexURLResolver
from django.core.urlresolvers import resolve
from django import test
from django.test.utils import override_settings

class ConfigureSettingsTestCase(test.TestCase):
'''Test the configure_settings method.'''
Expand Down Expand Up @@ -158,6 +157,32 @@ def test_contrib_autoconfig(self):
autoconfig.configure_settings(self.settings_dict)
self.assertIn('django.contrib.sessions', self.settings_dict['INSTALLED_APPS'])

def test_logging_premature_imports(self):
'''
Test that logging doesn't cause premature imports.
'''

import logging, StringIO
output = StringIO.StringIO()
stream_handler = logging.StreamHandler(output)
logger = logging.getLogger('django_autoconfig.autoconfig')
logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG)

self.triggered = False
class Trap(object):
def __init__(self, sentinel):
self.sentinel = sentinel
def __str__(self):
self.sentinel.triggered = True
return 'triggered!'

autoconfig.merge_dictionaries({}, {'LOGIN_URL': Trap(self)})
self.assertFalse(self.triggered)

logger.removeHandler(stream_handler)


class ConfigureUrlsTestCase(test.TestCase):
'''Test the autoconfiguration of the urlconf.'''
urls = 'django_autoconfig.autourlconf'
Expand Down

0 comments on commit ee85fef

Please sign in to comment.