def test_rendering_base_url(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources(resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <script type="text/javascript" src="/fanstatic/foo/a.js"></script> <script type="text/javascript" src="/fanstatic/foo/c.js"></script>''' needed = NeededResources( base_url='http://localhost/static', resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\ <link rel="stylesheet" type="text/css" href="http://localhost/static/fanstatic/foo/b.css" /> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/a.js"></script> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/c.js"></script>''' # The base_url has been set. assert needed.has_base_url() needed.set_base_url('foo') # The base_url can only be set once. assert needed._base_url == 'http://localhost/static'
def test_rendering_base_url(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed(resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <script type="text/javascript" src="/fanstatic/foo/a.js"></script> <script type="text/javascript" src="/fanstatic/foo/c.js"></script>''' needed = init_needed(base_url='http://localhost/static', resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\ <link rel="stylesheet" type="text/css" href="http://localhost/static/fanstatic/foo/b.css" /> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/a.js"></script> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/c.js"></script>''' # The base_url has been set. assert needed.has_base_url() needed.set_base_url('foo') # The base_url can only be set once. assert needed._base_url == 'http://localhost/static'
def test_setting_compile_False_should_not_call_compiler_and_minifier( compilers): compilers.add_compiler(MockCompiler()) compilers.add_minifier(MockMinifier()) lib = Library('lib', '') a = Resource(lib, 'a.js', compiler='mock', minifier='mock') needed = init_needed(resources=[a]) incl = Inclusion(needed) incl.render() assert not compilers.compiler('mock').calls assert not compilers.minifier('mock').calls
def test_setting_compile_False_should_not_call_compiler_and_minifier( compilers): compilers.add_compiler(MockCompiler()) compilers.add_minifier(MockMinifier()) lib = Library('lib', '') a = Resource(lib, 'a.js', compiler='mock', minifier='mock') needed = NeededResources(resources=[a]) incl = Inclusion(needed) incl.render() assert not compilers.compiler('mock').calls assert not compilers.minifier('mock').calls
def test_setting_compile_True_should_call_compiler_and_minifier(compilers): compilers.add_compiler(MockCompiler()) compilers.add_minifier(MockMinifier()) lib = Library('lib', '') a = Resource(lib, 'a.js', compiler='mock', minifier='mock') needed = init_needed(resources=[a]) incl = Inclusion(needed, compile=True) incl.render() mock_compiler = compilers.compiler('mock') mock_minifier = compilers.minifier('mock') assert len(mock_compiler.calls) == 1 assert mock_compiler.calls[0] == a assert len(mock_minifier.calls) == 1 assert mock_minifier.calls[0] == a
def test_empty_base_url_and_publisher_signature(): ''' When the base_url is not set and the publisher_signature is an empty string, render a URL without them. ''' foo = Library('foo', '') x1 = Resource(foo, 'a.js') needed = init_needed(publisher_signature='', resources=[x1]) incl = Inclusion(needed) assert incl.render() == '''\
def test_setting_compile_True_should_call_compiler_and_minifier( compilers): compilers.add_compiler(MockCompiler()) compilers.add_minifier(MockMinifier()) lib = Library('lib', '') a = Resource(lib, 'a.js', compiler='mock', minifier='mock') needed = NeededResources(resources=[a]) incl = Inclusion(needed, compile=True) incl.render() mock_compiler = compilers.compiler('mock') mock_minifier = compilers.minifier('mock') assert len(mock_compiler.calls) == 1 assert mock_compiler.calls[0] == a assert len(mock_minifier.calls) == 1 assert mock_minifier.calls[0] == a
def test_empty_base_url_and_publisher_signature(): ''' When the base_url is not set and the publisher_signature is an empty string, render a URL without them. ''' foo = Library('foo', '') x1 = Resource(foo, 'a.js') needed = NeededResources(publisher_signature='', resources=[x1]) incl = Inclusion(needed) assert incl.render() == '''\
def test_custom_renderer_for_resource(): foo = Library('foo', '') from fanstatic.core import render_print_css def render_unknown(url): return '<unknown href="%s"/>' % url a = Resource(foo, 'printstylesheet.css', renderer=render_print_css) b = Resource(foo, 'nothing.unknown', renderer=render_unknown) needed = init_needed() needed.need(a) incl = Inclusion(needed) assert incl.render() == """\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/printstylesheet.css" media="print" />""" needed.need(b) incl = Inclusion(needed) assert incl.render() == """\
def test_custom_renderer_for_resource(): foo = Library('foo', '') from fanstatic.core import render_print_css a = Resource(foo, 'printstylesheet.css', renderer=render_print_css) needed = NeededResources() needed.need(a) incl = Inclusion(needed) assert incl.render() == """\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/printstylesheet.css" media="print" />""" def render_unknown(url): return '<unknown href="%s"/>' % url b = Resource(foo, 'nothing.unknown', renderer=render_unknown) needed.need(b) incl = Inclusion(needed) assert incl.render() == """\
def test_rendering_base_url_assign(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources(resources=[y1]) needed.set_base_url('http://localhost/static') incl = Inclusion(needed) assert incl.render() == '''\
def test_rendering(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed(resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\
def test_rendering(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources(resources=[y1]) incl = Inclusion(needed) assert incl.render() == '''\
def test_rendering_base_url_assign(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed(resources=[y1]) needed.set_base_url('http://localhost/static') incl = Inclusion(needed) assert incl.render() == '''\
def test_sass_resource(tmpdir): compiler = fanstatic.CompilerRegistry.instance()['sass'] if not compiler.available: pytest.skip('`%s` not found on PATH' % compiler.command) lib = Library('lib', str(tmpdir), compilers={'.css': 'sass'}) a = Resource(lib, 'a.css') tmpdir.join('a.scss').write('''\ #navbar { li { a { font-weight: bold; } } }''') # Before compilation, the resource is not present. assert not tmpdir.join('a.css').check() needed = NeededResources(resources=[a]) incl = Inclusion(needed, compile=True) incl.render() # After compilation, the resource is present, and compiled using the sass # compiler. assert '#navbar li a' in tmpdir.join('a.css').read()
def test_resource_subclass_render(): foo = Library('foo', '') class MyResource(Resource): def render(self, library_url): return '<myresource reference="%s/%s"/>' % (library_url, self.relpath) a = MyResource(foo, 'printstylesheet.css') needed = NeededResources(resources=[a]) incl = Inclusion(needed) assert incl.render() == """\
def test_sass_resource(tmpdir): compiler = fanstatic.CompilerRegistry.instance()['sass'] if not compiler.available: pytest.skip('`%s` not found on PATH' % compiler.command) lib = Library('lib', str(tmpdir), compilers={'.css': 'sass'}) a = Resource(lib, 'a.css') tmpdir.join('a.scss').write('''\ #navbar { li { a { font-weight: bold; } } }''') # Before compilation, the resource is not present. assert not tmpdir.join('a.css').check() needed = init_needed(resources=[a]) incl = Inclusion(needed, compile=True) incl.render() # After compilation, the resource is present, and compiled using the sass # compiler. assert '#navbar li a' in tmpdir.join('a.css').read()
def test_render_bundle(): foo = Library('foo', '') x1 = Resource(foo, 'a.css') x2 = Resource(foo, 'b.css') x3 = Resource(foo, 'c.css', dont_bundle=True) needed = NeededResources(resources=[x1, x2, x3]) incl = Inclusion(needed) assert incl.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' needed = NeededResources(resources=[x1, x2, x3]) incl = Inclusion(needed, bundle=True) assert incl.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' x4 = Resource(foo, 'subdir/subdir/x4.css') x5 = Resource(foo, 'subdir/subdir/x5.css') needed = NeededResources(resources=[x1, x2, x4, x5]) incl = Inclusion(needed, bundle=True) assert incl.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
def test_resource_subclass_render(): foo = Library('foo', '') class MyResource(Resource): def render(self, library_url): return '<myresource reference="%s/%s"/>' % (library_url, self.relpath) a = MyResource(foo, 'printstylesheet.css') needed = init_needed(resources=[a]) incl = Inclusion(needed) assert incl.render() == """\
def test_render_bundle(): foo = Library('foo', '') x1 = Resource(foo, 'a.css') x2 = Resource(foo, 'b.css') x3 = Resource(foo, 'c.css', dont_bundle=True) x4 = Resource(foo, 'subdir/subdir/x4.css') x5 = Resource(foo, 'subdir/subdir/x5.css') needed = init_needed(resources=[x1, x2, x3]) incl = Inclusion(needed) assert incl.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' needed = init_needed(resources=[x1, x2, x3]) incl = Inclusion(needed, bundle=True) assert incl.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' needed = init_needed(resources=[x1, x2, x4, x5]) incl = Inclusion(needed, bundle=True) assert incl.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
def test_register_inclusion_renderer(): foo = Library('foo', '') with pytest.raises(UnknownResourceExtensionError): # The renderer for '.unknown' is not yet defined. Resource(foo, 'nothing.unknown') def render_unknown(url): return '<link rel="unknown" href="%s" />' % url register_inclusion_renderer('.unknown', render_unknown) a = Resource(foo, 'nothing.unknown') needed = NeededResources(resources=[a]) incl = Inclusion(needed) assert incl.render() == \ '<link rel="unknown" href="/fanstatic/foo/nothing.unknown" />'
def test_register_inclusion_renderer(): foo = Library('foo', '') with pytest.raises(UnknownResourceExtensionError): # The renderer for '.unknown' is not yet defined. Resource(foo, 'nothing.unknown') def render_unknown(url): return '<link rel="unknown" href="%s" />' % url register_inclusion_renderer('.unknown', render_unknown) a = Resource(foo, 'stuff.unknown') needed = init_needed(resources=[a]) incl = Inclusion(needed) assert incl.render() == \ '<link rel="unknown" href="/fanstatic/foo/stuff.unknown" />'