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 setUp(self): self.fs = fake_fs.FakeFS() self.fs.AddFile('/x/tvcm/__init__.js', """ 'use strict'; /* ohai */ """) self.fs.AddFile( '/x/foo/my_module.js', """ 'use strict'; tvcm.require('foo.other_module'); tvcm.exportTo('foo', function() { }); """) self.fs.AddFile( '/x/foo/other_module.js', """ 'use strict'; tvcm.requireRawScript('foo/raw/raw_script.js'); tvcm.exportTo('foo', function() { HelloWorld(); }); """) self.fs.AddFile('/x/foo/raw/raw_script.js', """ /* raw script */ """) self.project = project_module.Project(['/x'], include_tvcm_paths=False)
def setUp(self): self.fs = fake_fs.FakeFS() self.fs.AddFile( '/x/foo/my_module.html', """ <!DOCTYPE html> <link rel="import" href="/foo/other_module.html"> """) self.fs.AddFile( '/x/foo/other_module.html', """ <!DOCTYPE html> <script src="/foo/raw/raw_script.js"></script> <script> 'use strict'; HelloWorld(); </script> """) self.fs.AddFile('/x/foo/raw/raw_script.js', """ /* raw script */ """) self.fs.AddFile('/x/platform.min.js', """ """) self.fs.AddFile('/x/polymer.min.js', """ """) self.project = project_module.Project(['/x'], include_tvcm_paths=False)
def testDepsExceptionContext(self): fs = fake_fs.FakeFS() fs.AddFile( '/x/src/my_module.js', """ 'use strict'; tvcm.require('tvcm.foo'); tvcm.exportTo('foo', function() { }); """) fs.AddFile( '/x/tvcm/foo.js', """ 'use strict'; tvcm.require('missing'); tvcm.exportTo('foo', function() { }); """) fs.AddFile('/x/tvcm/__init__.js', '/* nothing */') project = project_module.Project(['/x'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) with fs: exc = None try: my_module = loader.LoadModule(module_name='src.my_module') assertFalse('Expected an exception') except module.DepsException, e: exc = e self.assertEquals(['src.my_module', 'tvcm.foo'], exc.context)
def testPolymerConversion2(self): file_contents = {} file_contents['/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(['/tvcm/', '/tmp/'], include_tvcm_paths=False) 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['/tmp/a/b/my_component.html'] = """ <!DOCTYPE html> <style> .some-rule { background-image: url('../something.jpg'); } </style> """ file_contents['/tmp/a/something.jpg'] = 'jpgdata' with fake_fs.FakeFS(file_contents): project = project_module.Project(['/tvcm/', '/tmp/'], include_tvcm_paths=False) 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(['/tmp/a/b/my_component.html', '/tmp/a/something.jpg'])) f = StringIO.StringIO() ctl = html_generation_controller.HTMLGenerationController() my_component.AppendHTMLContentsToFile(f, ctl) html = f.getvalue().rstrip() expected_html = """ .some-rule { background-image: url(data:image/jpg;base64,anBnZGF0YQ==); } """.rstrip() print html
def testBasic(self): fs = fake_fs.FakeFS() fs.AddFile('/blah/x', 'foobar') with fs: assert os.path.exists(os.path.normpath('/blah/x')) self.assertEquals('foobar', open(os.path.normpath('/blah/x'), 'r').read())
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(['/x/y', '/x/raw/'], include_tvcm_paths=False) 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([ '/x/y/z/foo.html', '/x/raw/bar.js', '/x/y/z/foo.css', '/x/y/z/foo.jpeg', '/x/y/z/foo2.html' ], dependent_filenames)
def testWalk(self): fs = fake_fs.FakeFS() fs.AddFile('/x/w2/w3/z3.txt', '') fs.AddFile('/x/w/z.txt', '') fs.AddFile('/x/y.txt', '') fs.AddFile('/a.txt', 'foobar') with fs: gen = os.walk(os.path.normpath('/')) r = gen.next() self.assertEquals((os.path.normpath('/'), ['x'], ['a.txt']), r) r = gen.next() self.assertEquals((os.path.normpath('/x'), ['w', 'w2'], ['y.txt']), r) r = gen.next() self.assertEquals((os.path.normpath('/x/w'), [], ['z.txt']), r) r = gen.next() self.assertEquals((os.path.normpath('/x/w2'), ['w3'], []), r) r = gen.next() self.assertEquals((os.path.normpath('/x/w2/w3'), [], ['z3.txt']), r) self.assertRaises(StopIteration, gen.next)
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 testModulesThatAreDirectores(self): fs = fake_fs.FakeFS() fs.AddFile('/x/foo/__init__.js', """'use strict'; tvcm.exportTo('foo', function(){});""") fs.AddFile('/x/tvcm/__init__.js', '/* nothing */') project = project_module.Project(['/x'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) with fs: foo_module = loader.LoadModule(module_name='foo') self.assertEquals('foo', foo_module.name) self.assertEquals('/x/foo/__init__.js', foo_module.filename)
def testExceptionRaisedWhenOldStyleModuleRootExists(self): fs = fake_fs.FakeFS() fs.AddFile('/x/foo/__init__.js', """'use strict';""") fs.AddFile('/x/foo.js', """'use strict';""") project = project_module.Project(['/x'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) with fs: self.assertRaises(module.DepsException, lambda: loader.LoadModule(module_name='foo')) self.assertRaises( module.DepsException, lambda: loader.LoadModule( module_filename='/x/foo/__init__.js'))
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="/tvcm/foo.html"> }); """) fs.AddFile('/x/tvcm/foo.html', """ <!DOCTYPE html> }); """) project = project_module.Project(['/x'], include_tvcm_paths=False) 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(['tvcm.foo'], dep_names)
def testRawScript(self): fs = fake_fs.FakeFS() fs.AddFile('/x/y/z/foo.js', """ 'use strict'; tvcm.requireRawScript('bar.js'); """) fs.AddFile('/x/raw/bar.js', 'hello') fs.AddFile('/x/y/tvcm/__init__.js', '/* nothing */') project = project_module.Project(['/x/y', '/x/raw/'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) with fs: my_module = loader.LoadModule(module_name='z.foo') self.assertEquals(1, len(my_module.dependent_raw_scripts)) rs = my_module.dependent_raw_scripts[0] self.assertEquals('hello', rs.contents) self.assertEquals('/x/raw/bar.js', rs.filename)
def test_module(self): fs = fake_fs.FakeFS() fs.AddFile( '/src/x.js', """ 'use strict'; tvcm.require('y'); tvcm.require('z'); tvcm.exportTo('xyz', function() { }); """) fs.AddFile( '/src/y.js', """ 'use strict'; tvcm.require('z'); tvcm.exportTo('xyz', function() { }); """) fs.AddFile( '/src/z.js', """ 'use strict'; tvcm.exportTo('xyz', function() { }); """) fs.AddFile('/src/tvcm/__init__.js', '/* nothing */') with fs: project = project_module.Project(['/src/'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) x_module = loader.LoadModule('x') self.assertEquals([ loader.loaded_modules['tvcm'], 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['tvcm'], loader.loaded_modules['z'], loader.loaded_modules['y'], x_module ], load_sequence)
def testSmoke(self): input_api = FakeInputAPI() output_api = FakeOutputAPI() fs = fake_fs.FakeFS() fs.AddFile('/x/y.js', """ 'use strict'; function test() { return 3; } """) fs.AddFile('/x/y.css', """ .foo { a: b; b: c; } """) input_api.affected_files += ['/x/y.js', '/x/y.css'] with fs: checker = presubmit_checker.PresubmitChecker(input_api, output_api) checker.RunChecks()
def testDepsExceptionContext(self): fs = fake_fs.FakeFS() fs.AddFile( '/x/src/my_module.html', """ <!DOCTYPE html> <link rel="import" href="/tvcm/foo.html"> """) fs.AddFile( '/x/tvcm/foo.html', """ <!DOCTYPE html> <link rel="import" href="missing.html"> """) project = project_module.Project(['/x'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) with fs: exc = None try: my_module = loader.LoadModule(module_name='src.my_module') assertFalse('Expected an exception') except module.DepsException, e: exc = e self.assertEquals(['src.my_module', 'tvcm.foo'], exc.context)
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/tvcm.html', '<!DOCTYPE html>') with fs: project = project_module.Project(['/src/'], include_tvcm_paths=False) 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 testBasic(self): fs = fake_fs.FakeFS() fs.AddFile( '/x/src/my_module.js', """ 'use strict'; tvcm.require('tvcm.foo'); tvcm.exportTo('foo', function() { }); """) fs.AddFile( '/x/tvcm/foo.js', """ 'use strict'; tvcm.require('tvcm.foo'); tvcm.exportTo('foo', function() { }); """) fs.AddFile('/x/tvcm/__init__.js', '/* nothing */') project = project_module.Project(['/x'], include_tvcm_paths=False) 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(['tvcm', 'tvcm.foo'], dep_names)
def setUp(self): self.fs = fake_fs.FakeFS() self.fs.AddFile( '/x/foo/my_module.html', """ <!DOCTYPE html> <link rel="import" href="/foo/other_module.html"> """) self.fs.AddFile( '/x/foo/other_module.html', """ <!DOCTYPE html> <script src="/foo/raw/raw_script.js"></script> <script> 'use strict'; HelloWorld(); </script> """) self.fs.AddFile('/x/foo/raw/raw_script.js', """ /* raw script */ """) self.fs.AddFile('/x/components/polymer/polymer.js', """ """) self.project = project_module.Project(['/x'])
def testWithableOpen(self): fs = fake_fs.FakeFS() fs.AddFile('/blah/x', 'foobar') with fs: with open(os.path.normpath('/blah/x'), 'r') as f: self.assertEquals('foobar', f.read())
def testBasic(self): file_contents = {} file_contents['/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> <polymer-element name="start"> <template> </template> <script> 'use strict'; console.log('inline script for start.html got written'); </script> </polymer-element> """ file_contents['/tvcm/tvcm.html'] = """<!DOCTYPE html> """ file_contents['/components/widget.html'] = """ <!DOCTYPE html> <link rel="import" href="/tvcm.html"> <widget name="widget.html"></widget> <script> 'use strict'; console.log('inline script for widget.html'); </script> """ file_contents['/tmp/a/common.css'] = """ /* /tmp/a/common.css was written */ """ file_contents['/raw/raw_script.js'] = """ console.log('/raw/raw_script.js was written'); """ file_contents['/raw/platform.min.js'] = """ """ file_contents['/raw/polymer.min.js'] = """ """ with fake_fs.FakeFS(file_contents): project = project_module.Project( ['/tvcm/', '/tmp/', '/components/', '/raw/'], include_tvcm_paths=False) loader = resource_loader.ResourceLoader(project) a_b_start_module = loader.LoadModule(module_name='a.b.start') load_sequence = project.CalcLoadSequenceForModules( [a_b_start_module]) # Check load sequence names. load_sequence_names = [x.name for x in load_sequence] self.assertEquals(['tvcm', '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 # Check HTML generation. html = generate.GenerateStandaloneHTMLAsString( load_sequence, title='', flattened_js_url="/blah.js") assert '<polymer-element name="start">' in html assert 'inline script for widget.html' not in html assert 'common.css' in html