From 644fd4ed5f95715d2207b146de9e0eafeee96f37 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 29 Aug 2012 12:17:59 -0700 Subject: [PATCH 1/2] Deep copy _DEFAULT_SETTINGS instead of linking. This caused the defaults to be overwritten and edge case bugs with tests. The test for empty setting needed to be updated to reflect that the method for setting up the local settings sets extra settings. --- pelican/__init__.py | 3 ++- pelican/contents.py | 3 ++- pelican/settings.py | 10 +++++----- tests/test_settings.py | 8 ++++++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pelican/__init__.py b/pelican/__init__.py index ba48c4c7..8c4930f9 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -1,3 +1,4 @@ +import copy import os import re import sys @@ -29,7 +30,7 @@ class Pelican(object): before doing anything else. """ if settings is None: - settings = _DEFAULT_CONFIG + settings = copy.deepcopy(_DEFAULT_CONFIG) self.path = path or settings['PATH'] if not self.path: diff --git a/pelican/contents.py b/pelican/contents.py index a5e3be8f..851607a5 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import copy import locale import logging import functools @@ -29,7 +30,7 @@ class Page(object): if not metadata: metadata = {} if not settings: - settings = _DEFAULT_CONFIG + settings = copy.deepcopy(_DEFAULT_CONFIG) self.settings = settings self._content = content diff --git a/pelican/settings.py b/pelican/settings.py index 645a9809..92c68ddc 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import copy import imp import inspect import os @@ -81,7 +82,7 @@ def read_settings(filename=None): if filename: local_settings = get_settings_from_file(filename) else: - local_settings = _DEFAULT_CONFIG + local_settings = copy.deepcopy(_DEFAULT_CONFIG) configured_settings = configure_settings(local_settings, None, filename) return configured_settings @@ -89,10 +90,9 @@ def read_settings(filename=None): def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG): """ Load settings from a module, returning a dict. - """ - context = default_settings.copy() + context = copy.deepcopy(default_settings) if module is not None: context.update( (k, v) for k, v in inspect.getmembers(module) if k.isupper() @@ -114,7 +114,7 @@ def get_settings_from_file(filename, default_settings=_DEFAULT_CONFIG): def configure_settings(settings, default_settings=None, filename=None): """Provide optimizations, error checking, and warnings for loaded settings""" if default_settings is None: - default_settings = _DEFAULT_CONFIG + default_settings = copy.deepcopy(_DEFAULT_CONFIG) # Make the paths relative to the settings file if filename: @@ -138,7 +138,7 @@ def configure_settings(settings, default_settings=None, filename=None): for locale_ in locales: try: locale.setlocale(locale.LC_ALL, locale_) - break # break if it is successfull + break # break if it is successful except locale.Error: pass else: diff --git a/tests/test_settings.py b/tests/test_settings.py index 25df74bd..7b534616 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,6 +1,7 @@ +import copy from os.path import dirname, abspath, join -from pelican.settings import read_settings, configure_settings, _DEFAULT_CONFIG +from pelican.settings import read_settings, configure_settings, _DEFAULT_CONFIG, DEFAULT_THEME from .support import unittest @@ -31,7 +32,10 @@ class TestSettingsConfiguration(unittest.TestCase): def test_read_empty_settings(self): """providing no file should return the default values.""" settings = read_settings(None) - self.assertDictEqual(settings, _DEFAULT_CONFIG) + expected = copy.deepcopy(_DEFAULT_CONFIG) + expected["FEED_DOMAIN"] = '' #This is added by configure settings + self.maxDiff = None + self.assertDictEqual(settings, expected) def test_configure_settings(self): """Manipulations to settings should be applied correctly.""" From 663d1e7347116b46746c728278747658a4c36ea1 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Thu, 30 Aug 2012 14:50:52 -0700 Subject: [PATCH 2/2] Added extra tests to help prevent regression. --- tests/test_settings.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_settings.py b/tests/test_settings.py index 7b534616..873df824 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -37,6 +37,22 @@ class TestSettingsConfiguration(unittest.TestCase): self.maxDiff = None self.assertDictEqual(settings, expected) + def test_settings_return_independent(self): + """Make sure that the results from one settings call doesn't + effect past or future instances.""" + self.PATH = abspath(dirname(__file__)) + default_conf = join(self.PATH, 'default_conf.py') + settings = read_settings(default_conf) + settings['SITEURL'] = 'new-value' + new_settings = read_settings(default_conf) + self.assertNotEqual(new_settings['SITEURL'], settings['SITEURL']) + + def test_defaults_not_overwritten(self): + """This assumes 'SITENAME': 'A Pelican Blog'""" + settings = read_settings(None) + settings['SITENAME'] = 'Not a Pelican Blog' + self.assertNotEqual(settings['SITENAME'], _DEFAULT_CONFIG['SITENAME']) + def test_configure_settings(self): """Manipulations to settings should be applied correctly."""