diff options
| author | Michael Merickel <michael@merickel.org> | 2015-12-09 20:34:54 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2015-12-09 22:31:24 -0600 |
| commit | 5e3439059daa94543f9437a280fed8d804cc7596 (patch) | |
| tree | 8972067e13090ee250cda3fa352bca88e2a64780 | |
| parent | 6923cae7f493c39b17367a3935a26065d4795ea6 (diff) | |
| download | pyramid-5e3439059daa94543f9437a280fed8d804cc7596.tar.gz pyramid-5e3439059daa94543f9437a280fed8d804cc7596.tar.bz2 pyramid-5e3439059daa94543f9437a280fed8d804cc7596.zip | |
fix broken tests
| -rw-r--r-- | pyramid/config/views.py | 35 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_views.py | 173 | ||||
| -rw-r--r-- | pyramid/tests/test_static.py | 74 |
3 files changed, 178 insertions, 104 deletions
diff --git a/pyramid/config/views.py b/pyramid/config/views.py index ed7ae42ce..e5bf1203e 100644 --- a/pyramid/config/views.py +++ b/pyramid/config/views.py @@ -38,6 +38,7 @@ from pyramid.interfaces import ( from pyramid import renderers +from pyramid.asset import resolve_asset_spec from pyramid.compat import ( string_types, urlparse, @@ -1985,14 +1986,12 @@ class StaticURLInfo(object): self.cache_busters = [] def generate(self, path, request, **kw): - disable_cache_buster = ( - request.registry.settings['pyramid.prevent_cachebust']) for (url, spec, route_name) in self.registrations: if path.startswith(spec): subpath = path[len(spec):] if WIN: # pragma: no cover subpath = subpath.replace('\\', '/') # windows - if not disable_cache_buster: + if self.cache_busters: subpath, kw = self._bust_asset_path( request.registry, spec, subpath, kw) if url is None: @@ -2099,6 +2098,9 @@ class StaticURLInfo(object): config.action(None, callable=register, introspectables=(intr,)) def add_cache_buster(self, config, spec, cachebust): + if config.registry.settings.get('pyramid.prevent_cachebust'): + return + # ensure the spec always has a trailing slash as we only support # adding cache busters to folders, not files if os.path.isabs(spec): # FBO windows @@ -2130,20 +2132,25 @@ class StaticURLInfo(object): config.action(None, callable=register, introspectables=(intr,)) def _bust_asset_path(self, registry, spec, subpath, kw): - pkg_name, pkg_subpath = spec.split(':') + pkg_name, pkg_subpath = resolve_asset_spec(spec) rawspec = None - overrides = registry.queryUtility(IPackageOverrides, name=pkg_name) - if overrides is not None: - resource_name = posixpath.join(pkg_subpath, subpath) - sources = overrides.filtered_sources(resource_name) - for source, filtered_path in sources: - rawspec = source.get_path(filtered_path) - if hasattr(source, 'pkg_name'): - rawspec = '{0}:{1}'.format(source.pkg_name, rawspec) - break + + if pkg_name is not None: + overrides = registry.queryUtility(IPackageOverrides, name=pkg_name) + if overrides is not None: + resource_name = posixpath.join(pkg_subpath, subpath) + sources = overrides.filtered_sources(resource_name) + for source, filtered_path in sources: + rawspec = source.get_path(filtered_path) + if hasattr(source, 'pkg_name'): + rawspec = '{0}:{1}'.format(source.pkg_name, rawspec) + break + + if rawspec is None: + rawspec = '{0}:{1}{2}'.format(pkg_name, pkg_subpath, subpath) if rawspec is None: - rawspec = '{0}:{1}{2}'.format(pkg_name, pkg_subpath, subpath) + rawspec = pkg_subpath + subpath for base_spec, cachebust in reversed(self.cache_busters): if rawspec.startswith(base_spec): diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py index 020ed131d..eda8d8b05 100644 --- a/pyramid/tests/test_config/test_views.py +++ b/pyramid/tests/test_config/test_views.py @@ -1,3 +1,4 @@ +import os import unittest from pyramid import testing @@ -3887,49 +3888,35 @@ class TestStaticURLInfo(unittest.TestCase): def test_generate_registration_miss(self): inst = self._makeOne() - registrations = [ - (None, 'spec', 'route_name', None), - ('http://example.com/foo/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [ + (None, 'spec', 'route_name'), + ('http://example.com/foo/', 'package:path/', None)] request = self._makeRequest() result = inst.generate('package:path/abc', request) self.assertEqual(result, 'http://example.com/foo/abc') - def test_generate_registration_no_registry_on_request(self): - inst = self._makeOne() - registrations = [ - ('http://example.com/foo/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations - request = self._makeRequest() - del request.registry - result = inst.generate('package:path/abc', request) - self.assertEqual(result, 'http://example.com/foo/abc') - def test_generate_slash_in_name1(self): inst = self._makeOne() - registrations = [ - ('http://example.com/foo/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [('http://example.com/foo/', 'package:path/', None)] request = self._makeRequest() result = inst.generate('package:path/abc', request) self.assertEqual(result, 'http://example.com/foo/abc') def test_generate_slash_in_name2(self): inst = self._makeOne() - registrations = [ - ('http://example.com/foo/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [('http://example.com/foo/', 'package:path/', None)] request = self._makeRequest() result = inst.generate('package:path/', request) self.assertEqual(result, 'http://example.com/foo/') def test_generate_quoting(self): + from pyramid.interfaces import IStaticURLInfo config = testing.setUp() try: config.add_static_view('images', path='mypkg:templates') - inst = self._makeOne() request = testing.DummyRequest() request.registry = config.registry + inst = config.registry.getUtility(IStaticURLInfo) result = inst.generate('mypkg:templates/foo%2Fbar', request) self.assertEqual(result, 'http://example.com/images/foo%252Fbar') finally: @@ -3937,8 +3924,7 @@ class TestStaticURLInfo(unittest.TestCase): def test_generate_route_url(self): inst = self._makeOne() - registrations = [(None, 'package:path/', '__viewname/', None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [(None, 'package:path/', '__viewname/')] def route_url(n, **kw): self.assertEqual(n, '__viewname/') self.assertEqual(kw, {'subpath':'abc', 'a':1}) @@ -3950,8 +3936,7 @@ class TestStaticURLInfo(unittest.TestCase): def test_generate_url_unquoted_local(self): inst = self._makeOne() - registrations = [(None, 'package:path/', '__viewname/', None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [(None, 'package:path/', '__viewname/')] def route_url(n, **kw): self.assertEqual(n, '__viewname/') self.assertEqual(kw, {'subpath':'abc def', 'a':1}) @@ -3963,16 +3948,15 @@ class TestStaticURLInfo(unittest.TestCase): def test_generate_url_quoted_remote(self): inst = self._makeOne() - registrations = [('http://example.com/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [('http://example.com/', 'package:path/', None)] request = self._makeRequest() result = inst.generate('package:path/abc def', request, a=1) self.assertEqual(result, 'http://example.com/abc%20def') def test_generate_url_with_custom_query(self): inst = self._makeOne() - registrations = [('http://example.com/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + registrations = [('http://example.com/', 'package:path/', None)] + inst.registrations = registrations request = self._makeRequest() result = inst.generate('package:path/abc def', request, a=1, _query='(openlayers)') @@ -3981,12 +3965,10 @@ class TestStaticURLInfo(unittest.TestCase): def test_generate_url_with_custom_anchor(self): inst = self._makeOne() - registrations = [('http://example.com/', 'package:path/', None, None)] - inst._get_registrations = lambda *x: registrations + inst.registrations = [('http://example.com/', 'package:path/', None)] request = self._makeRequest() uc = text_(b'La Pe\xc3\xb1a', 'utf-8') - result = inst.generate('package:path/abc def', request, a=1, - _anchor=uc) + result = inst.generate('package:path/abc def', request, a=1, _anchor=uc) self.assertEqual(result, 'http://example.com/abc%20def#La%20Pe%C3%B1a') @@ -3995,52 +3977,102 @@ class TestStaticURLInfo(unittest.TestCase): kw['foo'] = 'bar' return 'foo' + '/' + subpath, kw inst = self._makeOne() - inst.registrations = [(None, 'package:path/', '__viewname', cachebust)] + inst.registrations = [(None, 'package:path/', '__viewname')] inst.cache_busters = [('package:path/', cachebust)] request = self._makeRequest() + called = [False] def route_url(n, **kw): + called[0] = True self.assertEqual(n, '__viewname') - self.assertEqual(kw, {'subpath':'foo/abc', 'foo':'bar'}) + self.assertEqual(kw, {'subpath': 'foo/abc', 'foo': 'bar'}) request.route_url = route_url inst.generate('package:path/abc', request) + self.assertTrue(called[0]) + + def test_generate_url_cachebust_abspath(self): + here = os.path.dirname(__file__) + os.sep + def cachebust(pathspec, subpath, kw): + kw['foo'] = 'bar' + return 'foo' + '/' + subpath, kw + inst = self._makeOne() + inst.registrations = [(None, here, '__viewname')] + inst.cache_busters = [(here, cachebust)] + request = self._makeRequest() + called = [False] + def route_url(n, **kw): + called[0] = True + self.assertEqual(n, '__viewname') + self.assertEqual(kw, {'subpath': 'foo/abc', 'foo': 'bar'}) + request.route_url = route_url + inst.generate(here + 'abc', request) + self.assertTrue(called[0]) + + def test_generate_url_cachebust_nomatch(self): + def fake_cb(*a, **kw): raise AssertionError + inst = self._makeOne() + inst.registrations = [(None, 'package:path/', '__viewname')] + inst.cache_busters = [('package:path2/', fake_cb)] + request = self._makeRequest() + called = [False] + def route_url(n, **kw): + called[0] = True + self.assertEqual(n, '__viewname') + self.assertEqual(kw, {'subpath': 'abc'}) + request.route_url = route_url + inst.generate('package:path/abc', request) + self.assertTrue(called[0]) + + def test_generate_url_cachebust_with_overrides(self): + config = testing.setUp() + try: + config.add_static_view('static', 'path') + config.override_asset( + 'pyramid.tests.test_config:path/', + 'pyramid.tests.test_config:other_path/') + def cb(pathspec, subpath, kw): + kw['_query'] = {'x': 'foo'} + return subpath, kw + config.add_cache_buster('other_path', cb) + request = testing.DummyRequest() + result = request.static_url('path/foo.png') + self.assertEqual(result, 'http://example.com/static/foo.png?x=foo') + finally: + testing.tearDown() def test_add_already_exists(self): config = DummyConfig() inst = self._makeOne() inst.registrations = [('http://example.com/', 'package:path/', None)] inst.add(config, 'http://example.com', 'anotherpackage:path') - expected = [ - ('http://example.com/', 'anotherpackage:path/', None, None)] + expected = [('http://example.com/', 'anotherpackage:path/', None)] self.assertEqual(inst.registrations, expected) def test_add_package_root(self): config = DummyConfig() inst = self._makeOne() inst.add(config, 'http://example.com', 'package:') - expected = [('http://example.com/', 'package:', None, None)] + expected = [('http://example.com/', 'package:', None)] self.assertEqual(inst.registrations, expected) def test_add_url_withendslash(self): config = DummyConfig() inst = self._makeOne() inst.add(config, 'http://example.com/', 'anotherpackage:path') - expected = [ - ('http://example.com/', 'anotherpackage:path/', None, None)] + expected = [('http://example.com/', 'anotherpackage:path/', None)] self.assertEqual(inst.registrations, expected) def test_add_url_noendslash(self): config = DummyConfig() inst = self._makeOne() inst.add(config, 'http://example.com', 'anotherpackage:path') - expected = [ - ('http://example.com/', 'anotherpackage:path/', None, None)] + expected = [('http://example.com/', 'anotherpackage:path/', None)] self.assertEqual(inst.registrations, expected) def test_add_url_noscheme(self): config = DummyConfig() inst = self._makeOne() inst.add(config, '//example.com', 'anotherpackage:path') - expected = [('//example.com/', 'anotherpackage:path/', None, None)] + expected = [('//example.com/', 'anotherpackage:path/', None)] self.assertEqual(inst.registrations, expected) def test_add_viewname(self): @@ -4049,7 +4081,7 @@ class TestStaticURLInfo(unittest.TestCase): config = DummyConfig() inst = self._makeOne() inst.add(config, 'view', 'anotherpackage:path', cache_max_age=1) - expected = [(None, 'anotherpackage:path/', '__view/', None)] + expected = [(None, 'anotherpackage:path/', '__view/')] self.assertEqual(inst.registrations, expected) self.assertEqual(config.route_args, ('__view/', 'view/*subpath')) self.assertEqual(config.view_kw['permission'], NO_PERMISSION_REQUIRED) @@ -4060,7 +4092,7 @@ class TestStaticURLInfo(unittest.TestCase): config.route_prefix = '/abc' inst = self._makeOne() inst.add(config, 'view', 'anotherpackage:path',) - expected = [(None, 'anotherpackage:path/', '__/abc/view/', None)] + expected = [(None, 'anotherpackage:path/', '__/abc/view/')] self.assertEqual(inst.registrations, expected) self.assertEqual(config.route_args, ('__/abc/view/', 'view/*subpath')) @@ -4097,20 +4129,47 @@ class TestStaticURLInfo(unittest.TestCase): config = DummyConfig() config.registry.settings['pyramid.prevent_cachebust'] = True inst = self._makeOne() - inst.add(config, 'view', 'mypackage:path', cachebust=True) - cachebust = config.registry._static_url_registrations[0][3] - self.assertEqual(cachebust, None) + cachebust = DummyCacheBuster('foo') + inst.add_cache_buster(config, 'mypackage:path', cachebust) + self.assertEqual(inst.cache_busters, []) - def test_add_cachebust_custom(self): + def test_add_cachebuster(self): config = DummyConfig() inst = self._makeOne() - inst.add(config, 'view', 'mypackage:path', - cachebust=DummyCacheBuster('foo')) - cachebust = config.registry._static_url_registrations[0][3] - subpath, kw = cachebust('some/path', {}) + inst.add_cache_buster(config, 'mypackage:path', DummyCacheBuster('foo')) + cachebust = inst.cache_busters[-1][1] + subpath, kw = cachebust('mypackage:some/path', 'some/path', {}) self.assertEqual(subpath, 'some/path') self.assertEqual(kw['x'], 'foo') + def test_add_cachebuster_abspath(self): + here = os.path.dirname(__file__) + config = DummyConfig() + inst = self._makeOne() + cb = DummyCacheBuster('foo') + inst.add_cache_buster(config, here, cb) + self.assertEqual(inst.cache_busters, [(here + '/', cb)]) + + def test_add_cachebuster_overwrite(self): + config = DummyConfig() + inst = self._makeOne() + cb1 = DummyCacheBuster('foo') + cb2 = DummyCacheBuster('bar') + inst.add_cache_buster(config, 'mypackage:path/', cb1) + inst.add_cache_buster(config, 'mypackage:path', cb2) + self.assertEqual(inst.cache_busters, + [('mypackage:path/', cb2)]) + + def test_add_cachebuster_for_more_specific_path(self): + config = DummyConfig() + inst = self._makeOne() + cb1 = DummyCacheBuster('foo') + cb2 = DummyCacheBuster('bar') + inst.add_cache_buster(config, 'mypackage:path', cb1) + inst.add_cache_buster(config, 'mypackage:path/sub', cb2) + self.assertEqual(inst.cache_busters, + [('mypackage:path/', cb1), ('mypackage:path/sub/', cb2)]) + class Test_view_description(unittest.TestCase): def _callFUT(self, view): from pyramid.config.views import view_description @@ -4130,9 +4189,14 @@ class Test_view_description(unittest.TestCase): class DummyRegistry: + utility = None + def __init__(self): self.settings = {} + def queryUtility(self, type_or_iface, name=None, default=None): + return self.utility or default + from zope.interface import implementer from pyramid.interfaces import ( IResponse, @@ -4193,6 +4257,9 @@ class DummySecurityPolicy: return self.permitted class DummyConfig: + def __init__(self): + self.registry = DummyRegistry() + route_prefix = '' def add_route(self, *args, **kw): self.route_args = args diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py index 4a07c2cb1..73f242add 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -385,29 +385,29 @@ class TestQueryStringConstantCacheBuster(unittest.TestCase): fut = self._makeOne().tokenize self.assertEqual(fut('whatever'), 'foo') - def test_pregenerate(self): - fut = self._makeOne().pregenerate + def test_it(self): + fut = self._makeOne() self.assertEqual( - fut('foo', ('bar',), {}), - (('bar',), {'_query': {'x': 'foo'}})) + fut('foo', 'bar', {}), + ('bar', {'_query': {'x': 'foo'}})) - def test_pregenerate_change_param(self): - fut = self._makeOne('y').pregenerate + def test_change_param(self): + fut = self._makeOne('y') self.assertEqual( - fut('foo', ('bar',), {}), - (('bar',), {'_query': {'y': 'foo'}})) + fut('foo', 'bar', {}), + ('bar', {'_query': {'y': 'foo'}})) - def test_pregenerate_query_is_already_tuples(self): - fut = self._makeOne().pregenerate + def test_query_is_already_tuples(self): + fut = self._makeOne() self.assertEqual( - fut('foo', ('bar',), {'_query': [('a', 'b')]}), - (('bar',), {'_query': (('a', 'b'), ('x', 'foo'))})) + fut('foo', 'bar', {'_query': [('a', 'b')]}), + ('bar', {'_query': (('a', 'b'), ('x', 'foo'))})) - def test_pregenerate_query_is_tuple_of_tuples(self): - fut = self._makeOne().pregenerate + def test_query_is_tuple_of_tuples(self): + fut = self._makeOne() self.assertEqual( - fut('foo', ('bar',), {'_query': (('a', 'b'),)}), - (('bar',), {'_query': (('a', 'b'), ('x', 'foo'))})) + fut('foo', 'bar', {'_query': (('a', 'b'),)}), + ('bar', {'_query': (('a', 'b'), ('x', 'foo'))})) class TestManifestCacheBuster(unittest.TestCase): @@ -417,55 +417,55 @@ class TestManifestCacheBuster(unittest.TestCase): def test_it(self): manifest_path = os.path.join(here, 'fixtures', 'manifest.json') - fut = self._makeOne(manifest_path).pregenerate - self.assertEqual(fut('foo', ('bar',), {}), (['bar'], {})) + fut = self._makeOne(manifest_path) + self.assertEqual(fut('foo', 'bar', {}), ('bar', {})) self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-test.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-test.css', {})) def test_it_with_relspec(self): - fut = self._makeOne('fixtures/manifest.json').pregenerate - self.assertEqual(fut('foo', ('bar',), {}), (['bar'], {})) + fut = self._makeOne('fixtures/manifest.json') + self.assertEqual(fut('foo', 'bar', {}), ('bar', {})) self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-test.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-test.css', {})) def test_it_with_absspec(self): - fut = self._makeOne('pyramid.tests:fixtures/manifest.json').pregenerate - self.assertEqual(fut('foo', ('bar',), {}), (['bar'], {})) + fut = self._makeOne('pyramid.tests:fixtures/manifest.json') + self.assertEqual(fut('foo', 'bar', {}), ('bar', {})) self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-test.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-test.css', {})) def test_reload(self): manifest_path = os.path.join(here, 'fixtures', 'manifest.json') new_manifest_path = os.path.join(here, 'fixtures', 'manifest2.json') inst = self._makeOne('foo', reload=True) inst.getmtime = lambda *args, **kwargs: 0 - fut = inst.pregenerate + fut = inst # test without a valid manifest self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main.css', {})) # swap to a real manifest, setting mtime to 0 inst.manifest_path = manifest_path self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-test.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-test.css', {})) # ensure switching the path doesn't change the result inst.manifest_path = new_manifest_path self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-test.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-test.css', {})) # update mtime, should cause a reload inst.getmtime = lambda *args, **kwargs: 1 self.assertEqual( - fut('foo', ('css', 'main.css'), {}), - (['css', 'main-678b7c80.css'], {})) + fut('foo', 'css/main.css', {}), + ('css/main-678b7c80.css', {})) def test_invalid_manifest(self): self.assertRaises(IOError, lambda: self._makeOne('foo')) |
