Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Fix tests for Python 3 and the newer fanstatic version (this package …
Browse files Browse the repository at this point in the history
…was never updated).
  • Loading branch information
janwijbrand committed Jan 16, 2018
1 parent 580f818 commit e2c2b98
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 53 deletions.
23 changes: 21 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,41 @@ def read(*rnames):


long_description = (
read('src/zope/fanstatic/README.txt')
+ '\n' +
read('CHANGES.txt')
+ '\n' +
'Download\n'
'********\n'
)


setup(
name='zope.fanstatic',
version='0.13dev',
description="Fanstatic integration for Zope.",
long_description=long_description,
classifiers=['Framework :: Zope3'],
keywords='',
author='Zope Foundation and Contributors',
author_email='zope-dev@zope.org',
url='http://pypi.python.org/pypi/zope.fanstatic',
license='ZPL 2.1',
classifiers=[
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: Zope Public License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Framework :: Zope3',
],
packages=find_packages('src'),
namespace_packages=['zope'],
package_dir={'': 'src'},
Expand All @@ -50,7 +68,8 @@ def read(*rnames):
'zope.principalregistry',
'zope.security',
'zope.securitypolicy',
'zope.site'
'zope.site',
'zope.testbrowser',
],
},
)
25 changes: 10 additions & 15 deletions src/zope/fanstatic/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ We need to be in a request to make this work, so let's up a request to
a page we have set up in ``ftesting.zcml`` that should cause the
inclusion of a single resource in its header::

>>> from zope.app.wsgi.testlayer import Browser
>>> from zope.testbrowser.wsgi import Browser
>>> browser = Browser()
>>> browser.open('http://localhost/zope.fanstatic.test_single')
>>> print browser.contents
>>> print(browser.contents)
<html>
<head>
<script type="text/javascript" src="http://localhost/fanstatic/foo/a.js"></script>
<BLANKLINE>
</head>
<script type="text/javascript" src="http://localhost/fanstatic/foo/a.js"></script></head>
<body>
<p>the widget HTML itself</p>
</body>
Expand All @@ -38,30 +36,27 @@ If a resource happens to need another resource, this resource is also
automatically included::

>>> browser.open('http://localhost/zope.fanstatic.test_multiple')
>>> print browser.contents
>>> print(browser.contents)
<html>
<head>
<script type="text/javascript" src="http://localhost/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="http://localhost/fanstatic/foo/b.js"></script>
<BLANKLINE>
</head>
<script type="text/javascript" src="http://localhost/fanstatic/foo/b.js"></script></head>
<body>
<p>the widget HTML itself</p>
</body>
</html>

Let's force all javascript resources to be forced to be included at
the bottom now, just before the ``</body>`` tag::
Bottom rendering of resources, just before the ``</body>`` tag::

>>> browser.open('http://localhost/zope.fanstatic.test_bottom')
>>> print browser.contents
>>> print(browser.contents)
<html>
<head>
</head>
<body>
<p>the widget HTML itself</p>
<script type="text/javascript" src="http://localhost/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="http://localhost/fanstatic/foo/b.js"></script></body>
<script type="text/javascript" src="http://localhost/fanstatic/foo/c.js"></script>
<script type="text/javascript" src="http://localhost/fanstatic/foo/d.js"></script></body>
</html>

In-template resources
Expand All @@ -71,7 +66,7 @@ zope.fanstatic provides support for rendering resource publisher
aware URLs to in-template resources::

>>> browser.open('http://localhost/zope.fanstatic.test_inline_resource')
>>> print browser.contents
>>> print(browser.contents)
<html>
<head>
</head>
Expand Down
14 changes: 9 additions & 5 deletions src/zope/fanstatic/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from zope.app.wsgi.testlayer import BrowserLayer
import fanstatic
import zope.testbrowser.wsgi
import zope.app.wsgi.testlayer

class ZopeFanstaticBrowserLayer(BrowserLayer):
""" A zope testlayer with fanstatic Injector. """

def setup_middleware(self, app):
return fanstatic.Injector(app)
class ZopeFanstaticBrowserLayer(
zope.testbrowser.wsgi.TestBrowserLayer,
zope.app.wsgi.testlayer.BrowserLayer):
"""A zope testlayer with fanstatic Injector.
"""

def setup_middleware(self, app):
return fanstatic.Injector(app, bottom=True)
1 change: 1 addition & 0 deletions src/zope/fanstatic/tests/foo_dir/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
1 change: 1 addition & 0 deletions src/zope/fanstatic/tests/foo_dir/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
25 changes: 18 additions & 7 deletions src/zope/fanstatic/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,49 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import unittest
import doctest
import fanstatic
import unittest
import zope.fanstatic.tests

from zope.interface import Interface
from zope.component import getGlobalSiteManager
from zope.fanstatic.testing import ZopeFanstaticBrowserLayer
from zope.fanstatic.tests.view import foo
from zope.fanstatic.zcml import create_factory
from zope.interface import Interface
from zope.publisher.interfaces.browser import IBrowserRequest

import fanstatic
from zope.fanstatic.zcml import create_factory
from zope.fanstatic.tests.view import foo
from zope.fanstatic.testing import ZopeFanstaticBrowserLayer
import zope.fanstatic.tests

class TestLayer(ZopeFanstaticBrowserLayer):

def testSetUp(self):
super(TestLayer, self).testSetUp()
# Because it is difficult to dynamically register a
# entry_point in tests, we do the setup by hand:
registry = fanstatic.get_library_registry()
registry.add(foo)
resource_factory = create_factory(foo)
getGlobalSiteManager().registerAdapter(
resource_factory, (IBrowserRequest,), Interface, foo.name)

def testTearDown(self):
super(TestLayer, self).testTearDown()
registry = fanstatic.get_library_registry()
registry.clear()


layer = TestLayer(zope.fanstatic.tests)


class NoInjectorTestLayer(TestLayer):

def setup_middleware(self, app):
return app


no_injector_layer = NoInjectorTestLayer(zope.fanstatic.tests)


def test_suite():
readme = doctest.DocFileSuite(
'../README.txt',
Expand Down
18 changes: 10 additions & 8 deletions src/zope/fanstatic/tests/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,41 @@
#
##############################################################################
from fanstatic import Library, Resource
from fanstatic import get_needed

foo = Library("foo", "foo_dir")

a = Resource(foo, "a.js")

b = Resource(foo, "b.js", depends=[a])

c = Resource(foo, "c.js", bottom=True)

d = Resource(foo, "d.js", bottom=True, depends=[c])


class TestSingle(object):
def widget(self):
a.need()
return "the widget HTML itself"


class TestMultiple(object):
def widget(self):
b.need()
return "the widget HTML itself"


class TestBottom(object):
def widget(self):
b.need()
# XXX this does not use any official API and needs to be
# reconsidered. Its done anyway now to make the tests pass,
# instead of just removing the corresponding test.
get_needed()._bottom = True
get_needed()._force_bottom = True
d.need()
return "the widget HTML itself"


class TestInlineResource(object):
pass


class TestError(object):
def widget(self):
b.need()
raise Exception('I am not a teapot')

16 changes: 8 additions & 8 deletions src/zope/fanstatic/zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from zope.interface import Interface
from zope import component
from zope.publisher.interfaces.browser import IBrowserRequest

import fanstatic

from zope import component
from zope.fanstatic.zopesupport import ZopeFanstaticResource
from zope.interface import Interface
from zope.publisher.interfaces.browser import IBrowserRequest


def create_factory(library):
def factory(request):
return ZopeFanstaticResource(request, library)
return factory


def action_setup(_context):
"""Publish all fanstatic library entry points as resources.
"""
Expand All @@ -32,7 +33,6 @@ def action_setup(_context):
adapts = (IBrowserRequest,)
provides = Interface
_context.action(
discriminator = ('adapter', adapts, provides, library.name),
callable = component.provideAdapter,
args = (factory, adapts, provides, library.name))

discriminator=('adapter', adapts, provides, library.name),
callable=component.provideAdapter,
args=(factory, adapts, provides, library.name))
17 changes: 9 additions & 8 deletions src/zope/fanstatic/zopesupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from zope.interface import implements
import fanstatic

from zope.component import adapter
from zope.errorview.interfaces import IHandleExceptionEvent
from zope.fanstatic.interfaces import IZopeFanstaticResource
from zope.interface import implementer
from zope.publisher.interfaces import IEndRequestEvent
from zope.traversing.browser.absoluteurl import absoluteURL
from zope.traversing.browser.interfaces import IAbsoluteURL
from zope.traversing.interfaces import ITraversable
from zope.errorview.interfaces import IHandleExceptionEvent

import fanstatic

from zope.fanstatic.interfaces import IZopeFanstaticResource

def ensure_base_url(needed, request):
if not isinstance(needed, fanstatic.NeededResources):
Expand Down Expand Up @@ -53,17 +53,20 @@ def set_base_url(event):
needed = fanstatic.get_needed()
ensure_base_url(needed, event.request)


@adapter(IHandleExceptionEvent)
def clear_needed_resources(event):
needed = fanstatic.get_needed()
if isinstance(needed, fanstatic.NeededResources):
# Only if there's a concrete NeededResources.
needed.clear()


_sentinel = object()

class ZopeFanstaticResource(object):

@implementer(IZopeFanstaticResource, ITraversable, IAbsoluteURL)
class ZopeFanstaticResource(object):
# Hack to get ++resource++foo/bar/baz.jpg *paths* working in Zope
# Pagetemplates. Note that ++resource+foo/bar/baz.jpg *URLs* will
# not work with this hack!
Expand All @@ -72,8 +75,6 @@ class ZopeFanstaticResource(object):
# / get() interface, to support rendering URLs to resources from
# code.

implements(IZopeFanstaticResource, ITraversable, IAbsoluteURL)

def __init__(self, request, library, name=''):
self.request = request
self.library = library
Expand Down

0 comments on commit e2c2b98

Please sign in to comment.