diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-12-17 16:00:02 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-12-17 16:00:02 +0000 |
| commit | bc857e7e6e71a4001f03c608a18bac7dab36ccff (patch) | |
| tree | a34b0b761a92791bb60b2fbfafabe79e4a675682 /repoze/bfg/testing.py | |
| parent | 9d73300fcef0c0cd4af9c439a900d15fa4651914 (diff) | |
| download | pyramid-bc857e7e6e71a4001f03c608a18bac7dab36ccff.tar.gz pyramid-bc857e7e6e71a4001f03c608a18bac7dab36ccff.tar.bz2 pyramid-bc857e7e6e71a4001f03c608a18bac7dab36ccff.zip | |
Features
--------
- The ``Configurator`` object now has two new methods: ``begin`` and
``end``. The ``begin`` method is meant to be called before any
"configuration" begins (e.g. before ``add_view``, et. al are
called). The ``end`` method is meant to be called after all
"configuration" is complete.
Previously, before there was imperative configuration at all (1.1
and prior), configuration begin and end was invariably implied by
the process of loading a ZCML file. When a ZCML load happened, the
threadlocal data structure containing the request and registry was
modified before the load, and torn down after the load, making sure
that all framework code that needed ``get_current_registry`` for the
duration of the ZCML load was satisfied.
Some API methods called during imperative configuration, (such as
``Configurator.add_view`` when a renderer is involved) end up for
historical reasons calling ``get_current_registry``. However, in
1.2a5 and below, the Configurator supplied no functionality that
allowed people to make sure that ``get_current_registry`` returned
the registry implied by the configurator being used. ``begin`` now
serves this purpose. Inversely, ``end`` pops the thread local
stack, undoing the actions of ``begin``.
We make this boundary explicit to reduce the potential for confusion
when the configurator is used in different circumstances (e.g. in
unit tests and app code vs. just in initial app setup).
Existing code written for 1.2a1-1.2a5 which does not call ``begin``
or ``end`` continues to work in the same manner it did before. It
is however suggested that this code be changed to call ``begin`` and
``end`` to reduce the potential for confusion in the future.
- All ``paster`` templates which generate an application skeleton now
make use of the new ``begin`` and ``end`` methods of the
Configurator they use in their respective copies of ``run.py`` and
``tests.py``.
Documentation
-------------
- All documentation that makes use of a ``Configurator`` object to do
application setup and test setup now makes use of the new ``begin``
and ``end`` methods of the configurator.
Bug Fixes
---------
- When a ``repoze.bfg.exceptions.NotFound`` or
``repoze.bfg.exceptions.Forbidden`` *class* (as opposed to instance)
was raised as an exception within a root factory (or route root
factory), the exception would not be caught properly by the
``repoze.bfg.`` Router and it would propagate to up the call stack,
as opposed to rendering the not found view or the forbidden view as
would have been expected.
Diffstat (limited to 'repoze/bfg/testing.py')
| -rw-r--r-- | repoze/bfg/testing.py | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 428f1c11f..434131321 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -21,6 +21,7 @@ from repoze.bfg.interfaces import ITraverser from repoze.bfg.interfaces import IView from repoze.bfg.interfaces import IViewPermission +from repoze.bfg.configuration import Configurator from repoze.bfg.exceptions import Forbidden from repoze.bfg.registry import Registry from repoze.bfg.security import Allowed @@ -592,17 +593,22 @@ def setUp(registry=None, request=None, hook_zca=True): .. note:: The ``hook_zca`` argument is new as of :mod:`repoze.bfg` 1.2. + + .. warning:: Although this method of tearing a test setup down + will never disappear, after :mod:`repoze.bfg` 1.2a6, + using the ``begin`` and ``end`` methods of a + ``Configurator`` are prefered to using + ``repoze.bfg.testing.setUp`` and + ``repoze.bfg.testing.tearDown``. See + :ref:`unittesting_chapter` for more information. """ manager.clear() if registry is None: registry = Registry('testing') - manager.push({'registry':registry, 'request':request}) + config = Configurator(registry=registry) + config.begin(request=request) if hook_zca: - try: - from zope.component import getSiteManager - getSiteManager.sethook(get_current_registry) - except ImportError: # pragma: no cover - pass + hook_zca_api() def tearDown(unhook_zca=True): """Undo the effects ``repoze.bfg.testing.setUp``. Use this @@ -619,20 +625,24 @@ def tearDown(unhook_zca=True): .. note:: The ``unhook_zca`` argument is new as of :mod:`repoze.bfg` 1.2. + .. warning:: Although this method of tearing a test setup down + will never disappear, after :mod:`repoze.bfg` 1.2a6, + using the ``begin`` and ``end`` methods of a + ``Configurator`` are prefered to using + ``repoze.bfg.testing.setUp`` and + ``repoze.bfg.testing.tearDown``. See + :ref:`unittesting_chapter` for more information. + """ if unhook_zca: - try: - from zope.component import getSiteManager - getSiteManager.reset() - except ImportError: # pragma: no cover - pass + unhook_zca_api() info = manager.pop() manager.clear() if info is not None: - reg = info['registry'] - if hasattr(reg, '__init__') and hasattr(reg, '__name__'): + registry = info['registry'] + if hasattr(registry, '__init__') and hasattr(registry, '__name__'): try: - reg.__init__(reg.__name__) + registry.__init__(registry.__name__) except TypeError: # calling __init__ is largely for the benefit of # people who want to use the global ZCA registry; @@ -648,3 +658,17 @@ def cleanUp(*arg, **kw): extensive production usage, it will never be removed.""" setUp(*arg, **kw) +def hook_zca_api(): + try: + from zope.component import getSiteManager + getSiteManager.sethook(get_current_registry) + except ImportError: # pragma: no cover + pass + +def unhook_zca_api(): + try: + from zope.component import getSiteManager + getSiteManager.reset() + except ImportError: # pragma: no cover + pass + |
