def testGetAllDependentFilenamesRecursive(self): fs = fake_fs.FakeFS() fs.AddFile('/x/y/z/foo.html', """ <!DOCTYPE html> <link rel="import" href="/z/foo2.html"> <link rel="stylesheet" href="/z/foo.css"> <script src="/bar.js"></script> """) fs.AddFile('/x/y/z/foo.css', """ .x .y { background-image: url(foo.jpeg); } """) fs.AddFile('/x/y/z/foo.jpeg', '') fs.AddFile('/x/y/z/foo2.html', """ <!DOCTYPE html> """) fs.AddFile('/x/raw/bar.js', 'hello') project = project_module.Project([ os.path.normpath('/x/y'), os.path.normpath('/x/raw/')]) loader = resource_loader.ResourceLoader(project) with fs: my_module = loader.LoadModule(module_name='z.foo') self.assertEquals(1, len(my_module.dependent_raw_scripts)) dependent_filenames = my_module.GetAllDependentFilenamesRecursive() self.assertEquals( [ os.path.normpath('/x/y/z/foo.html'), os.path.normpath('/x/raw/bar.js'), os.path.normpath('/x/y/z/foo.css'), os.path.normpath('/x/y/z/foo.jpeg'), os.path.normpath('/x/y/z/foo2.html'), ], dependent_filenames)
def testImages(self): fs = fake_fs.FakeFS() fs.AddFile( '/src/foo/x.css', """ .x .y { background-image: url(../images/bar.jpeg); } """) fs.AddFile('/src/images/bar.jpeg', 'hello world') with fs: project = project_module.Project([os.path.normpath('/src/')]) loader = resource_loader.ResourceLoader(project) foo_x = loader.LoadStyleSheet('foo.x') self.assertEquals(1, len(foo_x.images)) r0 = foo_x.images[0] self.assertEquals(os.path.normpath('/src/images/bar.jpeg'), r0.absolute_path) inlined = foo_x.contents_with_inlined_images self.assertEquals( """ .x .y { background-image: url(data:image/jpeg;base64,%s); } """ % base64.standard_b64encode('hello world'), inlined)
def test_module(self): fs = fake_fs.FakeFS() fs.AddFile('/src/x.html', """ <!DOCTYPE html> <link rel="import" href="/y.html"> <link rel="import" href="/z.html"> <script> 'use strict'; </script> """) fs.AddFile('/src/y.html', """ <!DOCTYPE html> <link rel="import" href="/z.html"> """) fs.AddFile('/src/z.html', """ <!DOCTYPE html> """) fs.AddFile('/src/py_vulcanize.html', '<!DOCTYPE html>') with fs: project = project_module.Project([os.path.normpath('/src/')]) loader = resource_loader.ResourceLoader(project) x_module = loader.LoadModule('x') self.assertEquals([loader.loaded_modules['y'], loader.loaded_modules['z']], x_module.dependent_modules) already_loaded_set = set() load_sequence = [] x_module.ComputeLoadSequenceRecursive(load_sequence, already_loaded_set) self.assertEquals([loader.loaded_modules['z'], loader.loaded_modules['y'], x_module], load_sequence)
def testPolymerConversion2(self): file_contents = {} file_contents[os.path.normpath('/tmp/a/b/my_component.html')] = """ <!DOCTYPE html> <polymer-element name="my-component"> <template> </template> <script> 'use strict'; Polymer ( ); </script> </polymer-element> """ with fake_fs.FakeFS(file_contents): project = project_module.Project([ os.path.normpath('/py_vulcanize/'), os.path.normpath('/tmp/') ]) loader = resource_loader.ResourceLoader(project) my_component = loader.LoadModule(module_name='a.b.my_component') f = StringIO.StringIO() my_component.AppendJSContentsToFile( f, use_include_tags_for_scripts=False, dir_for_include_tag_root=None) js = f.getvalue().rstrip() expected_js = """ 'use strict'; Polymer ( 'my-component'); """.rstrip() self.assertEquals(expected_js, js)
def testInlineStylesheetURLs(self): file_contents = {} file_contents[os.path.normpath('/tmp/a/b/my_component.html')] = """ <!DOCTYPE html> <style> .some-rule { background-image: url('../something.jpg'); } </style> """ file_contents[os.path.normpath('/tmp/a/something.jpg')] = 'jpgdata' with fake_fs.FakeFS(file_contents): project = project_module.Project([ os.path.normpath('/py_vulcanize/'), os.path.normpath('/tmp/')]) loader = resource_loader.ResourceLoader(project) my_component = loader.LoadModule(module_name='a.b.my_component') computed_deps = [] my_component.AppendDirectlyDependentFilenamesTo(computed_deps) self.assertEquals(set(computed_deps), set([os.path.normpath('/tmp/a/b/my_component.html'), os.path.normpath('/tmp/a/something.jpg')])) f = six.StringIO() ctl = html_generation_controller.HTMLGenerationController() my_component.AppendHTMLContentsToFile(f, ctl) html = f.getvalue().rstrip() # FIXME: This is apparently not used. expected_html = """ .some-rule { background-image: url(data:image/jpg;base64,anBnZGF0YQ==); } """.rstrip()
def testImportsCauseFailure(self): fs = fake_fs.FakeFS() fs.AddFile('/src/foo/x.css', """ @import url(awesome.css); """) with fs: project = project_module.Project([os.path.normpath('/src')]) loader = resource_loader.ResourceLoader(project) self.assertRaises(Exception, lambda: loader.LoadStyleSheet('foo.x'))
def testURLResolveFails(self): fs = fake_fs.FakeFS() fs.AddFile( '/src/foo/x.css', """ .x .y { background-image: url(../images/missing.jpeg); } """) with fs: project = project_module.Project([os.path.normpath('/src')]) loader = resource_loader.ResourceLoader(project) self.assertRaises(module.DepsException, lambda: loader.LoadStyleSheet('foo.x'))
def testBasic(self): fs = fake_fs.FakeFS() fs.AddFile('/x/src/my_module.html', """ <!DOCTYPE html> <link rel="import" href="/py_vulcanize/foo.html"> }); """) fs.AddFile('/x/py_vulcanize/foo.html', """ <!DOCTYPE html> }); """) project = project_module.Project([os.path.normpath('/x')]) loader = resource_loader.ResourceLoader(project) with fs: my_module = loader.LoadModule(module_name='src.my_module') dep_names = [x.name for x in my_module.dependent_modules] self.assertEquals(['py_vulcanize.foo'], dep_names)
def setUp(self): self.fs = fake_fs.FakeFS() self.fs.AddFile( '/x/foo/my_module.html', ('<!DOCTYPE html>\n' '<link rel="import" href="/foo/other_module.html">\n')) self.fs.AddFile('/x/foo/other_module.html', ('<!DOCTYPE html>\n' '<script src="/foo/raw/raw_script.js"></script>\n' '<script>\n' ' \'use strict\';\n' ' HelloWorld();\n' '</script>\n')) self.fs.AddFile('/x/foo/raw/raw_script.js', '\n/* raw script */\n') self.fs.AddFile('/x/components/polymer/polymer.min.js', '\n') self.project = project_module.Project([os.path.normpath('/x')])
def testDepsExceptionContext(self): fs = fake_fs.FakeFS() fs.AddFile('/x/src/my_module.html', """ <!DOCTYPE html> <link rel="import" href="/py_vulcanize/foo.html"> """) fs.AddFile('/x/py_vulcanize/foo.html', """ <!DOCTYPE html> <link rel="import" href="missing.html"> """) project = project_module.Project([os.path.normpath('/x')]) loader = resource_loader.ResourceLoader(project) with fs: exc = None try: loader.LoadModule(module_name='src.my_module') assert False, 'Expected an exception' except module.DepsException as e: exc = e self.assertEquals( ['src.my_module', 'py_vulcanize.foo'], exc.context)
def testBasicModuleGeneration(self): file_contents = {} file_contents[os.path.normpath('/tmp/a/b/start.html')] = """ <!DOCTYPE html> <link rel="import" href="/widget.html"> <link rel="stylesheet" href="../common.css"> <script src="/raw_script.js"></script> <script src="/excluded_script.js"></script> <dom-module id="start"> <template> </template> <script> 'use strict'; console.log('inline script for start.html got written'); </script> </dom-module> """ file_contents[os.path.normpath( '/py_vulcanize/py_vulcanize.html')] = """<!DOCTYPE html> """ file_contents[os.path.normpath('/components/widget.html')] = """ <!DOCTYPE html> <link rel="import" href="/py_vulcanize.html"> <widget name="widget.html"></widget> <script> 'use strict'; console.log('inline script for widget.html'); </script> """ file_contents[os.path.normpath('/tmp/a/common.css')] = """ /* /tmp/a/common.css was written */ """ file_contents[os.path.normpath('/raw/raw_script.js')] = """ console.log('/raw/raw_script.js was written'); """ file_contents[os.path.normpath( '/raw/components/polymer/polymer.min.js')] = """ """ with fake_fs.FakeFS(file_contents): project = project_module.Project([ os.path.normpath('/py_vulcanize/'), os.path.normpath('/tmp/'), os.path.normpath('/components/'), os.path.normpath('/raw/') ]) loader = resource_loader.ResourceLoader(project) a_b_start_module = loader.LoadModule( module_name='a.b.start', excluded_scripts=['\/excluded_script.js']) load_sequence = project.CalcLoadSequenceForModules( [a_b_start_module]) # Check load sequence names. load_sequence_names = [x.name for x in load_sequence] self.assertEquals(['py_vulcanize', 'widget', 'a.b.start'], load_sequence_names) # Check module_deps on a_b_start_module def HasDependentModule(module, name): return [x for x in module.dependent_modules if x.name == name] assert HasDependentModule(a_b_start_module, 'widget') # Check JS generation. js = generate.GenerateJS(load_sequence) assert 'inline script for start.html' in js assert 'inline script for widget.html' in js assert '/raw/raw_script.js' in js assert 'excluded_script.js' not in js # Check HTML generation. html = generate.GenerateStandaloneHTMLAsString( load_sequence, title='', flattened_js_url='/blah.js') assert '<dom-module id="start">' in html assert 'inline script for widget.html' not in html assert 'common.css' in html
def CreateVulcanizer(self): from py_vulcanize import project as project_module return project_module.Project(self.source_paths)