def test_webpack_core_compiled_raw(self): remember_cwd(self) chdir(self._env_root) build_dir = mkdtemp(self) src_dir = mkdtemp(self) src_template = join(src_dir, 'template.nja') with open(src_template, 'w') as fd: fd.write('<p>Hello, {name}</p>') spec = Spec( build_dir=build_dir, loaderplugin_sourcepath={ 'text!some/mold/template.nja': src_template, }, bundle_sourcepath={ 'nunjucks': join('node_modules', 'nunjucks', 'nunjucks.js'), }, ) with pretty_logging('nunja', stream=StringIO()) as stream: webpack(spec, ('raw')) # now trigger the advice spec.handle(BEFORE_COMPILE) # template remains in plugins self.assertEqual(spec['loaderplugin_sourcepath'], { 'text!some/mold/template.nja': src_template, }) # will not be applied in raw. self.assertIn('__nunja__/some/mold', spec['bundle_sourcepath']) self.assertIn( 'nunja cannot skip precompilation for webpack toolchain', stream.getvalue(), )
def test_webpack_core_compiled(self): remember_cwd(self) chdir(self._env_root) build_dir = mkdtemp(self) src_template = resource_filename( Requirement.parse('nunja'), join('nunja', '_core_', '_default_wrapper_', 'template.nja')) spec = Spec( build_dir=build_dir, loaderplugin_sourcepath={ 'fake!bad': '/some/broken/path', 'text!_core_/_default_wrapper_/template.nja': src_template, 'text!some/template.nja': src_template, 'text!some/other/data.json': src_template, }, bundle_sourcepath={ 'nunjucks': join('node_modules', 'nunjucks', 'nunjucks.js'), }, ) webpack(spec, ()) hex_name = to_hex('_core_/_default_wrapper_') precompiled_path = join(build_dir, hex_name + '.js') self.assertFalse(exists(precompiled_path)) self.assertNotIn('slim', spec['bundle_sourcepath']['nunjucks']) # now trigger the advice spec.handle(BEFORE_COMPILE) self.assertTrue(exists(precompiled_path)) with open(precompiled_path) as fd: precompiled = fd.read() core_path = resource_filename(Requirement.parse('nunja'), join('nunja', '__core__.js')) with open(core_path) as fd: core_compiled = fd.read() # the compiled template should be identical with the one that # is stored in this source tree for the core self.assertEqual(precompiled, core_compiled) self.assertEqual( spec['bundle_sourcepath']['__nunja__/_core_/_default_wrapper_'], precompiled_path, ) # this one untouched. self.assertEqual( spec['loaderplugin_sourcepath'], { 'fake!bad': '/some/broken/path', 'text!_core_/_default_wrapper_/template.nja': src_template, # all other ones that did not pass the test will be filtered # out for other processing 'text!some/template.nja': src_template, 'text!some/other/data.json': src_template, })
def test_rjs_core_compiled_failure_bad_template(self): remember_cwd(self) chdir(self._env_root) build_dir = mkdtemp(self) src_dir = mkdtemp(self) src_template = join(src_dir, 'template.nja') with open(src_template, 'w') as fd: fd.write('<p>Hello {%World%}</p>') spec = Spec( build_dir=build_dir, plugin_sourcepath={ 'text!mold/dummy/template.nja': src_template, }, bundle_sourcepath={}, ) build_dir = mkdtemp(self) rjs(spec, ()) with pretty_logging('nunja', stream=StringIO()) as stream: spec.handle(BEFORE_COMPILE) err = stream.getvalue() self.assertIn('ERROR', err) self.assertIn('failed to precompile', err) self.assertIn('Template render error: (mold/dummy/template.nja)', err)
def test_rjs_core_compiled_slim(self): remember_cwd(self) chdir(self._env_root) src_template = resource_filename( Requirement.parse('nunja'), join('nunja', '_core_', '_default_wrapper_', 'template.nja')) build_dir = mkdtemp(self) spec = Spec( build_dir=build_dir, plugin_sourcepath={ 'text!_core_/_default_wrapper_/template.nja': src_template, }, bundle_sourcepath={ 'nunjucks': join('node_modules', 'nunjucks', 'nunjucks.js'), }, ) rjs(spec, ('slim', )) hex_name = to_hex('_core_/_default_wrapper_') precompiled_path = join(build_dir, hex_name + '.js') spec.handle(BEFORE_COMPILE) self.assertIn('slim', spec['bundle_sourcepath']['nunjucks']) self.assertTrue(exists(precompiled_path)) self.assertEqual( { '__nunja__/_core_/_default_wrapper_': { 'exports': 'nunjucksPrecompiled' }, }, spec['shim'])
def test_rjs_core_compiled_slim_empty_case(self): remember_cwd(self) chdir(self._env_root) build_dir = mkdtemp(self) spec = Spec( build_dir=build_dir, plugin_sourcepath={}, bundle_sourcepath={}, ) rjs(spec, ('slim', )) spec.handle(BEFORE_COMPILE) self.assertNotIn('nunjucks', spec['bundle_sourcepath'])
def test_rjs_core_compiled_failure_missing_template(self): remember_cwd(self) chdir(self._env_root) build_dir = mkdtemp(self) src_template = join(build_dir, 'no_such_template.nja') spec = Spec( build_dir=build_dir, plugin_sourcepath={ 'text!nunja/dummy/no_such_template.nja': src_template, }, bundle_sourcepath={}, ) build_dir = mkdtemp(self) rjs(spec, ('slim', )) with pretty_logging('nunja', stream=StringIO()) as stream: spec.handle(BEFORE_COMPILE) err = stream.getvalue() self.assertIn('ERROR', err) self.assertIn('failed to precompile', err)