def test_cannot_add_dependency_loop(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') b1 = Resource(foo, 'b1.js', depends=[a1]) with pytest.raises(ValueError): a1.add_dependency(b1)
def test_resource_name_conflict_raises_error(compilers): compilers.add_minifier(MockMinifier()) lib = Library('lib', '', minifiers={'.js': 'mock'}) a = Resource(lib, 'a.js') with pytest.raises(fanstatic.ConfigurationError) as exc: Resource(lib, 'a.min.js') assert str(exc.value) == 'Resource path a.min.js is already defined.'
def test_add_dependency_resource_to_group(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') b1 = Group([a1]) c1 = Resource(foo, 'c1.js', depends=[b1]) assert a1.depends == set([]) assert a1.resources == set([a1]) assert b1.depends == set([a1]) assert b1.resources == set([a1]) assert c1.depends == set([b1]) assert c1.resources == set([a1, c1]) a2 = Resource(foo, 'a2.js') b1.add_dependency(a2) assert b1.depends == set([a1, a2]) assert b1.resources == set([a1, a2]) assert c1.depends == set([b1]) assert c1.resources == set([a1, a2, c1]) # Adding it twice does not change anything. b1.add_dependency(a2) assert b1.depends == set([a1, a2]) assert b1.resources == set([a1, a2]) assert c1.depends == set([b1]) assert c1.resources == set([a1, a2, c1])
def test_slot_with_default_uses_default_if_nothing_given_in_need(): lib = Library('lib', '') default_resource_for_slot = Resource(lib, 'b.js') slot = Slot(lib, '.js', default=default_resource_for_slot) a = Resource(lib, 'a.js', depends=[slot]) needed = init_needed(resources=[a]) relpaths = [r.relpath for r in sort_resources(needed.resources())] assert relpaths == ['b.js', 'a.js']
def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) assert rollup_resources([b1]) == set([b1])
def test_generate_source(): foo = Library('foo', '') i1 = Resource(foo, 'i1.js') i2 = Resource(foo, 'i2.js', depends=[i1]) i3 = Resource(foo, 'i3.js', depends=[i2]) i5 = Resource(foo, 'i5.js', depends=[i3]) assert generate_code(i1=i1, i2=i2, i3=i3, i5=i5) == '''\
def test_rollup_without_mode(): foo = Library('foo', '') h1 = Resource(foo, 'h1.js', debug='h1-debug.js') h2 = Resource(foo, 'h2.js', debug='h2-debug.js') gianth = Resource(foo, 'gianth.js', supersedes=[h1, h2]) needed = NeededResources(resources=[h1, h2], rollup=True, debug=True) # no mode available for rollup, use the rollup. assert needed.resources() == [gianth]
def test_rollup_size_competing(): foo = Library('foo', '') d1 = Resource(foo, 'd1.js') d2 = Resource(foo, 'd2.js') d3 = Resource(foo, 'd3.js') giant = Resource(foo, 'giant.js', supersedes=[d1, d2]) giant_bigger = Resource(foo, 'giant-bigger.js', supersedes=[d1, d2, d3]) assert rollup_resources([d1, d2, d3]) == set([giant_bigger])
def test_sort_resources_topological(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a5 = Resource(foo, 'a5.js', depends=[a3]) assert sort_resources_topological([a5, a3, a1, a2]) == [a1, a2, a3, a5]
def test_bundle(): foo = Library('foo', '') a = Resource(foo, 'a.css') b = Resource(foo, 'b.css') resources = bundle_resources([a, b]) assert len(resources) == 1 bundle = resources[0] assert bundle.resources() == [a, b]
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_inclusion_rollup_and_debug_missing(): foo = Library('foo', '') h1 = Resource(foo, 'h1.js', debug='h1-debug.js') h2 = Resource(foo, 'h2.js', debug='h2-debug.js') gianth = Resource(foo, 'gianth.js', supersedes=[h1, h2]) needed = init_needed(resources=[h1, h2]) incl = Inclusion(needed, rollup=True, mode='debug') # No mode debug version of the rollup use it instead assert incl.resources == [gianth]
def test_rollup_larger(): foo = Library('foo', '') c1 = Resource(foo, 'c1.css') c2 = Resource(foo, 'c2.css') c3 = Resource(foo, 'c3.css') giant = Resource(foo, 'giant.css', supersedes=[c1, c2, c3]) assert rollup_resources([c1]) == set([c1]) assert rollup_resources([c1, c2]) == set([c1, c2]) assert rollup_resources([c1, c2, c3]) == set([giant])
def test_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() needed.need(y1) assert needed.resources() == set([x2, x1, y1])
def test_bundle_different_directory(): # resources with different directories aren't bundled foo = Library('foo', '') a = Resource(foo, 'first/a.css') b = Resource(foo, 'second/b.css') resources = bundle_resources([a, b]) assert len(resources) == 2 assert resources[0] is a assert resources[1] is b
def test_bundle_different_renderer(): # resources with different renderers aren't bundled foo = Library('foo', '') a = Resource(foo, 'a.css') b = Resource(foo, 'b.js') resources = bundle_resources([a, b]) assert len(resources) == 2 assert resources[0] is a assert resources[1] is b
def test_bundle_dont_bundle_at_the_start(): foo = Library('foo', '') a = Resource(foo, 'a.css', dont_bundle=True) b = Resource(foo, 'b.css') c = Resource(foo, 'c.css') resources = bundle_resources([a, b, c]) assert len(resources) == 2 assert resources[0] is a assert resources[1].resources() == [b, c]
def test_bundle_dont_bundle_at_the_end(): foo = Library('foo', '') a = Resource(foo, 'a.css') b = Resource(foo, 'b.css') c = Resource(foo, 'c.css', dont_bundle=True) resources = bundle_resources([a, b, c]) assert len(resources) == 2 assert resources[0].resources() == [a, b] assert resources[-1] is c
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_resource_register_with_library(): foo = Library('foo', '') x1 = Resource(foo, 'a.js', minified='a.min.js') assert len(foo.known_resources) == 2 assert x1 in foo.known_resources.values() # Can not use the same relpath for two Resource declarations. with pytest.raises(ConfigurationError): x2 = Resource(foo, 'a.js')
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() needed.need(y1) assert needed.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() needed.need(y1) needed.set_base_url('http://localhost/static') assert needed.render() == '''\
def test_library_dependency_cycles(): A = Library('A', '') B = Library('B', '') a1 = Resource(A, 'a1.js') b1 = Resource(B, 'b1.js') a2 = Resource(A, 'a2.js', depends=[b1]) # This definition would create a library dependency cycle if permitted. with pytest.raises(LibraryDependencyCycleError): b2 = Resource(B, 'b2.js', depends=[a1]) # This is an example of an indirect library dependency cycle. C = Library('C', '') D = Library('D', '') E = Library('E', '') c1 = Resource(C, 'c1.js') d1 = Resource(D, 'd1.js', depends=[c1]) d2 = Resource(D, 'd2.js') e1 = Resource(E, 'e1.js', depends=[d2]) # ASCII ART # # C E D # # c1 <--------- d1 # # c2 --> e1 --> d2 # with pytest.raises(LibraryDependencyCycleError): c2 = Resource(C, 'c2.js', depends=[e1])
def test_library_ordering_bug(): jquery_lib = Library('jquery', '') jqueryui_lib = Library('jqueryui', '') obviel_lib = Library('obviel', '') bread_lib = Library('bread', '') app_lib = Library('app', '') jquery = Resource(jquery_lib, 'jquery.js') jqueryui = Resource(jqueryui_lib, 'jqueryui.js', depends=[jquery]) obviel = Resource(obviel_lib, 'obviel.js', depends=[jquery]) obviel_forms = Resource(obviel_lib, 'obviel_forms.js', depends=[obviel]) obviel_datepicker = Resource(obviel_lib, 'obviel_datepicker.js', depends=[obviel_forms, jqueryui]) vtab = Resource(bread_lib, 'vtab.js', depends=[jqueryui]) tabview = Resource(bread_lib, 'tabview.js', depends=[obviel, vtab]) bread = Resource(bread_lib, 'bread.js', depends=[tabview, obviel_forms]) app = Resource(app_lib, 'app.js', depends=[bread, obviel_datepicker]) needed = NeededResources() needed.need(app) resources = needed.resources() for resource in resources: print resource, resource.library.library_nr assert resources == [ jquery, jqueryui, obviel, obviel_forms, obviel_datepicker, vtab, tabview, bread, app ]
def test_html_insert(): 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]) injector = TopBottomInjector({}) html = b"<html><head>something more</head></html>" assert injector(html, needed) == b'''\
def test_bundle_different_library(): # resources with different libraries aren't bundled l1 = Library('l1', '') l2 = Library('l2', '') a = Resource(l1, 'a.js') b = Resource(l2, 'b.js') resources = bundle_resources([a, b]) assert len(resources) == 2 assert resources[0] is a assert resources[1] is b
def test_top_bottom_insert(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) html = "<html><head>rest of head</head><body>rest of body</body></html>" needed = NeededResources(bottom=True, force_bottom=True) needed.need(y1) assert needed.render_topbottom_into_html(html) == '''\
def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources()
def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5]
def test_redundant_resource_reorder(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1]