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_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_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_advices(self): stub_base_which(self) stub_mod_call(self, cli) build_dir = mkdtemp(self) advices = [] driver = cli.KarmaDriver.create() spec = Spec(build_dir=build_dir) spec.advise(AFTER_TEST, advices.append, AFTER_TEST) spec.advise(BEFORE_TEST, advices.append, BEFORE_TEST) driver.test_spec(spec) # XXX should AFTER_TEST also run if test failed? # XXX what other advices should apply, i.e. failure/error/success self.assertEqual(advices, [BEFORE_TEST, AFTER_TEST])
def test_complete_webpack(package_names, export_target): """ Accompanied testing entry point for the complete_webpack artifact. """ # importing in here as calmjs.dev is an optional dependency. from calmjs.dev.toolchain import KarmaToolchain spec = Spec( export_target=export_target, test_package_names=package_names, ) spec.advise(SETUP, webpack_advice, spec) return KarmaToolchain(), spec
def test_complete_rjs(package_names, export_target): """ Accompanied testing entry point for the complete_rjs artifact. """ # importing in here as calmjs.dev is an optional dependency. from calmjs.dev.toolchain import KarmaToolchain spec = Spec( export_target=export_target, test_package_names=package_names, ) spec.advise(SETUP, rjs_advice, spec) return KarmaToolchain(), spec
def test_null_toolchain_transpile_sources(self): source_dir = mkdtemp(self) build_dir = mkdtemp(self) source_file = join(source_dir, 'source.js') with open(source_file, 'w') as fd: fd.write('var dummy = function () {};\n') spec = Spec( build_dir=build_dir, transpile_source_map={ 'namespace.dummy.source': source_file, }, ) self.toolchain(spec) # name, and relative filename to the build_path self.assertEqual( spec, { 'build_dir': build_dir, 'transpile_source_map': { 'namespace.dummy.source': source_file, }, 'bundled_paths': {}, 'compiled_paths': { 'namespace.dummy.source': 'namespace.dummy.source', }, 'module_names': ['namespace.dummy.source'], 'prepare': 'prepared', 'assemble': 'assembled', 'link': 'linked', }) self.assertTrue(exists(join(build_dir, 'namespace.dummy.source.js')))
def test_prepare_failure_export_target(self): tmpdir = utils.mkdtemp(self) rjs = toolchain.RJSToolchain() with open(join(tmpdir, 'r.js'), 'w'): # mock a r.js file. pass spec = Spec(build_dir=tmpdir) spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js') with self.assertRaises(RuntimeError) as e: rjs.prepare(spec) self.assertEqual(str(e.exception), "'export_target' not found in spec") # what can possibly go wrong? spec['export_target'] = join(spec[rjs.rjs_bin_key], 'build.js') with self.assertRaises(RuntimeError) as e: rjs.prepare(spec) self.assertEqual(str(e.exception), "'export_target' will not be writable") spec['export_target'] = join(tmpdir, 'build.js') with self.assertRaises(RuntimeError) as e: rjs.prepare(spec) self.assertEqual( str(e.exception), "'export_target' must not be same as " "'build_manifest_path'")
def test_transpile_generic_to_umd_node_amd_compat_rjs_basic_padded(self): """ Similar as above, but insufficient lines will just result in not much happening... """ source = StringIO('\n' 'var dummy = function () {};\n' '\n' 'exports.dummy = dummy;\n') target = SourceWriter(StringIO()) spec = Spec() toolchain._transpile_generic_to_umd_node_amd_compat_rjs( spec, source, target) self.assertEqual( target.getvalue().splitlines()[2:8], [ ' var exports = {};', # no break after this. ' var dummy = function () {};', '', ' exports.dummy = dummy;', '', ' return exports;', ]) self.assertEqual( target.mappings[:7], [[], [], [], [(8, 0, 1, 0)], [(0, 0, 1, 0)], [(8, 0, 1, 0)], []]) target_main = SourceWriter(StringIO()) toolchain._rjs_transpiler(spec, source, target_main) self.assertEqual(target.getvalue(), target_main.getvalue())
def test_find_node_module_pkg_name(self): remember_cwd(self) text_handler = loaderplugin.WebpackLoaderHandler(None, 'text') working_dir = mkdtemp(self) os.chdir(working_dir) toolchain = Toolchain() spec = Spec() # base test without any Node.js packages available self.assertEqual( 'text-loader', text_handler.find_node_module_pkg_name(toolchain, spec)) # now provide a package named simply 'text' create_mock_npm_package(working_dir, 'text', 'index.js') # which, being available, will resolve directly to 'text' due to # ambiguity. self.assertEqual( 'text', text_handler.find_node_module_pkg_name(toolchain, spec)) # however, if a -loader suffixed package (i.e. 'text-loader') is # available, the -loader version will be returned instead. create_mock_npm_package(working_dir, 'text-loader', 'index.js') self.assertEqual( 'text-loader', text_handler.find_node_module_pkg_name(toolchain, spec))
def test_modname_loader_map(self): srcfile = join(mkdtemp(self), 'some.css') spec = Spec( build_dir=mkdtemp(self), calmjs_webpack_modname_loader_map={'some.css': ['style', 'css']}, ) toolchain = Toolchain() with open(srcfile, 'w') as fd: fd.write('.body {}') reg = LoaderPluginRegistry('calmjs.webpack.loaders') reg.records['style'] = text = loaderplugin.WebpackLoaderHandler( reg, 'style') reg.records['css'] = loaderplugin.WebpackLoaderHandler(reg, 'css') modpaths, targets, export_module_names = text(toolchain, spec, 'style!css!some.css', srcfile, 'some.css', 'style!css!some.css') self.assertEqual({'style!css!some.css': 'style!css!some.css'}, modpaths) self.assertEqual({ 'some.css': 'some.css', './some.css': 'some.css', }, targets) self.assertEqual([], export_module_names)
def test_call_loader_chaining(self): srcfile = join(mkdtemp(self), 'some.css') spec = Spec(build_dir=mkdtemp(self)) toolchain = Toolchain() with open(srcfile, 'w') as fd: fd.write('body { color: #000; }') reg = LoaderPluginRegistry('calmjs.webpack.loaders') reg.records['text'] = text = loaderplugin.WebpackLoaderHandler( reg, 'text') reg.records['css'] = loaderplugin.WebpackLoaderHandler(reg, 'css') modpaths, targets, export_module_names = text(toolchain, spec, 'text!css!some.css', srcfile, 'some.css', 'text!css!some.css') self.assertEqual({'text!css!some.css': 'text!css!some.css'}, modpaths) self.assertEqual({ 'some.css': 'some.css', './some.css': 'some.css', }, targets) self.assertEqual(['text!css!some.css'], export_module_names) self.assertTrue(exists(join(spec['build_dir'], 'some.css')))
def test_process_test_files_assorted(self): from calmjs.webpack.cli import default_toolchain as toolchain # the bare minimum spec spec = Spec( build_dir=mkdtemp(self), test_module_paths_map={ # the values are normally absolute paths. 'example/tests/test_main': 'test_main.js', 'example/tests/data.json': 'data.json', 'text!example/tests/data.txt': 'data.txt', }, karma_config={ 'webpack': { 'resolve': { 'alias': {}, }, }, 'preprocessors': {}, }) with pretty_logging(stream=StringIO()) as s: test_files, loaderspath = dev._process_test_files(toolchain, spec) self.assertEqual({'test_main.js'}, test_files) self.assertEqual({'text!example/tests/data.txt': 'data.txt'}, loaderspath) self.assertIn( "only aliasing modpath 'example/tests/data.json' to target " "'data.json'", s.getvalue())
def test_prepare_failure_export_target(self): tmpdir = utils.mkdtemp(self) webpack = toolchain.WebpackToolchain() with open(join(tmpdir, 'webpack'), 'w'): # mock a webpack executable. pass spec = Spec(build_dir=tmpdir) spec[webpack.webpack_bin_key] = join(tmpdir, 'webpack') with self.assertRaises(RuntimeError) as e: webpack.prepare(spec) self.assertEqual(str(e.exception), "'export_target' not found in spec") # what can possibly go wrong? spec['export_target'] = join(spec[webpack.webpack_bin_key], 'tail') with self.assertRaises(RuntimeError) as e: webpack.prepare(spec) self.assertEqual(str(e.exception), "'export_target' will not be writable") spec['export_target'] = join(tmpdir, 'config.js') with self.assertRaises(RuntimeError) as e: webpack.prepare(spec) self.assertEqual( str(e.exception), "'export_target' must not be same as " "'webpack_config_js'")
def test_build_bundle_no_indent(self): bundle_dir = utils.mkdtemp(self) build_dir = utils.mkdtemp(self) transpile_source_map = {} transpile_source_map.update(self._example_package_map) bundle_source_map = {} export_target = join(bundle_dir, 'example.package.js') rjs = toolchain.RJSToolchain() spec = Spec( transpile_source_map=transpile_source_map, bundle_source_map=bundle_source_map, export_target=export_target, build_dir=build_dir, transpile_no_indent=True, ) rjs(spec) self.assertTrue(exists(export_target)) stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' '%s\n' 'var main = requirejs("example/package/main");\n' 'main.main(true);\n', spec['node_config_js'], ) self.assertIn( join('example', 'package', 'bad.js') + ':%d:%d' % ( self._bad_notdefinedsymbol ), stderr ) self.assertEqual(stdout, '2\n4\n')
def test_build_bundle_standard(self): bundle_dir = utils.mkdtemp(self) build_dir = utils.mkdtemp(self) transpile_source_map = {} transpile_source_map.update(self._example_package_map) bundle_source_map = {} export_target = join(bundle_dir, 'example.package.js') rjs = toolchain.RJSToolchain() spec = Spec( transpile_source_map=transpile_source_map, bundle_source_map=bundle_source_map, export_target=export_target, build_dir=build_dir, ) rjs(spec) self.assertTrue(exists(export_target)) # verify that the bundle works with node stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var main = requirejs("example/package/main");\n' 'main.main();\n', export_target, ) self.assertEqual(stderr, '') self.assertEqual(stdout, '2\n4\n')
def test_karma_setup_not_webpack_artifact(self): karma_config = karma.build_base_config() src_dir = mkdtemp(self) fake_artifact = join(src_dir, 'fake_artifact.js') with open(fake_artifact, 'w') as fd: fd.write('(function(root, factory) { factory() })') fd.write('(this, function() {});') build_dir = mkdtemp(self) spec = Spec( karma_config=karma_config, build_dir=build_dir, test_module_paths_map={ 'some/package/tests/test_module': '/src/some/package/tests/test_module.js' }, artifact_paths=[fake_artifact], toolchain_bin_path=self.setup_fake_webpack(), ) with pretty_logging(stream=StringIO()) as s: karma_webpack(spec) log = s.getvalue() self.assertIn("unable to extract calmjs related exports from", log) self.assertIn(fake_artifact, log)
def test_karma_setup_basic_test_single(self): karma_config = karma.build_base_config() build_dir = mkdtemp(self) spec = Spec( karma_config=karma_config, build_dir=build_dir, test_module_paths_map={ 'some/package/tests/test_module': '/src/some/package/tests/test_module.js' }, webpack_single_test_bundle=True, toolchain_bin_path=self.setup_fake_webpack(), ) karma_webpack(spec) unified_module = join(build_dir, '__calmjs_tests__.js') self.assertEqual(spec['karma_config']['files'], [unified_module]) self.assertEqual( spec['karma_config']['webpack']['resolve']['alias'] ['some/package/tests/test_module'], '/src/some/package/tests/test_module.js') with open(unified_module) as fd: self.assertIn('some/package/tests/test_module', fd.read())
def test_prepare_compile_assemble_verify_loaders_not_found(self): working_dir = utils.mkdtemp(self) mock_text_loader(working_dir) src_dir = utils.mkdtemp(self) index_file = join(src_dir, 'index.js') with open(index_file, 'w') as fd: fd.write('var hello = require("text!hello/world.txt");\n') webpack = toolchain.WebpackToolchain() spec = Spec( **{ 'build_dir': self.build_dir, 'export_target': join(working_dir, 'export.js'), webpack.webpack_bin_key: join(self.build_dir, 'webpack'), LOADERPLUGIN_SOURCEPATH_MAPS: {}, 'transpile_sourcepath': { 'index': index_file, }, 'working_dir': working_dir, 'verify_imports': True, }) with pretty_logging(stream=mocks.StringIO()) as s: webpack.prepare(spec) webpack.compile(spec) webpack.assemble(spec) self.assertIn("not in modules: %s" % (['text!hello/world.txt'], ), s.getvalue())
def test_apply_coverage_join(self): spec = Spec( karma_config={ 'webpack': { 'module': { 'rules': [{ 'loader': 'demo-loader' }], }, }, }, build_dir=mkdtemp(self), test_covered_build_dir_paths=['afile.js'], test_covered_test_paths=['some/test/file'], ) dev._apply_coverage(None, spec) self.assertEqual( { "module": { "rules": [{ 'loader': 'demo-loader' }, { "loader": "sourcemap-istanbul-instrumenter-loader", "include": [ 'some/test/file', join(spec['build_dir'], 'afile.js') ], }] }, }, spec['karma_config']['webpack'])
def test_call_dir_nesting(self): srcfile = join(mkdtemp(self), 'some.file.txt') tgtfile = join('dir', 'some.file.txt') spec = Spec(build_dir=mkdtemp(self)) toolchain = Toolchain() with open(srcfile, 'w') as fd: fd.write('hello world') reg = LoaderPluginRegistry('calmjs.webpack.loaders') text = loaderplugin.WebpackLoaderHandler(reg, 'text') modpaths, targets, export_module_names = text(toolchain, spec, 'text!some.file.txt', srcfile, tgtfile, 'text!some.file.txt') self.assertTrue(exists(join(spec['build_dir'], 'dir', 'some.file.txt'))) self.assertEqual({'text!some.file.txt': 'text!some.file.txt'}, modpaths) self.assertEqual( { 'some.file.txt': tgtfile, './some.file.txt': tgtfile, }, targets) self.assertEqual(['text!some.file.txt'], export_module_names)
def test_normalize_and_register_webpackloaders_empty(self): sourcepath_map = {} spec = Spec() self.assertEqual({}, normalize_and_register_webpackloaders( spec, sourcepath_map)) self.assertEqual({}, spec['calmjs_webpack_modname_loader_map'])
def create_spec(self, **kwargs): """ Subclasses should override this if they take actual parameters. It must produce a ``Spec`` from the given keyword arguments. """ return Spec(**kwargs)
def test_plugin_package_chained_loaders_initial_simple(self): working_dir = mkdtemp(self) reg, base, extra, base_dir, extra_dir = self.create_base_extra_plugins( working_dir) simple = reg.records['simple'] = LoaderPluginHandler(reg, 'simple') toolchain = NullToolchain() spec = Spec(working_dir=working_dir) with pretty_logging(stream=StringIO()) as stream: self.assertEqual( {}, simple.generate_handler_sourcepath(toolchain, spec, { 'simple!fun.file': 'fun.file', }), ) with pretty_logging(stream=StringIO()) as stream: self.assertEqual({ 'extra': join(extra_dir, 'extra.js'), }, simple.generate_handler_sourcepath(toolchain, spec, { 'simple!extra!fun.file': 'fun.file', }), ) self.assertIn("for loader plugin 'extra'", stream.getvalue())
def test_karma_test_files_located(self): karma_config = karma.build_base_config() karma_config['files'] = ['example/package/lib.js'] spec = Spec( karma_config=karma_config, build_dir=mkdtemp(self), rjs_loader_plugin_registry=get(RJS_LOADER_PLUGIN_REGISTRY_NAME), export_module_names=['preexported'], test_module_paths_map={ 'example/package/tests/test_some_module': '/src/example/package/tests/test_some_module.js', 'example/package/tests/some_test_data': '/src/example/package/tests/some_test_data.js', }, ) with pretty_logging(stream=StringIO()): karma_requirejs(spec) with open(spec['karma_requirejs_test_script']) as fd: script = parse(fd.read()) # this is the node for the json in the build file deps = json.loads( script.children()[0].children()[0].initializer.to_ecma()) tests = json.loads( script.children()[1].children()[0].initializer.to_ecma()) self.assertEqual(['example/package/tests/test_some_module'], tests) self.assertEqual( ['preexported', 'example/package/tests/some_test_data'], deps)
def test_plugin_loaders_modname_source_to_target(self): class InterceptHandler(LoaderPluginHandler): def modname_source_to_target(self, *a, **kw): # failed to inspect and call parent return 'intercepted' reg = LoaderPluginRegistry('simloaders', _working_set=WorkingSet({})) base = reg.records['base'] = LoaderPluginHandler(reg, 'base') extra = reg.records['extra'] = LoaderPluginHandler(reg, 'extra') reg.records['intercept'] = InterceptHandler(reg, 'intercept') toolchain = NullToolchain() spec = Spec() self.assertEqual('fun.file', base.modname_source_to_target( toolchain, spec, 'base!fun.file', '/some/path/fun.file')) self.assertEqual('fun.file', extra.modname_source_to_target( toolchain, spec, 'extra!fun.file', '/some/path/fun.file')) self.assertEqual('fun.file', base.modname_source_to_target( toolchain, spec, 'extra!base!fun.file', '/some/path/fun.file')) # no plugin was found, so no modification self.assertEqual('noplugin!fun.file', base.modname_source_to_target( toolchain, spec, 'extra!noplugin!fun.file', '/some/path/fun.file')) # chained of the same type self.assertEqual('fun.file', base.modname_source_to_target( toolchain, spec, 'base!base!base!fun.file', '/some/path/fun.file')) # chained but overloaded self.assertEqual('intercepted', base.modname_source_to_target( toolchain, spec, 'base!intercept!base!fun.file', '/some/path/fun.file'))
def test_base_plugin_generate_handler_sourcepath(self): base = BaseLoaderPluginHandler(None, 'base') toolchain = NullToolchain() spec = Spec(working_dir=mkdtemp(self)) self.assertEqual( base.generate_handler_sourcepath(toolchain, spec, { 'base!bad': 'base!bad', }), {})
def test_extract_builder_result(self): self.assertEqual( 2, len(extract_builder_result(( NullToolchain(), Spec(), )))) self.assertEqual((None, None), extract_builder_result(( Spec(), NullToolchain(), ))) self.assertEqual((None, None), extract_builder_result(( NullToolchain(), None, ))) self.assertEqual((None, None), extract_builder_result(None))
def test_coverage_generation_build_dir(self): spec = Spec( build_dir=mkdtemp(self), test_covered_build_dir_paths=['afile.js'], ) loader = dev._generate_coverage_loader(None, spec) self.assertTrue(loader['include'][0].startswith(spec['build_dir'])) self.assertTrue(loader['include'][0].endswith('afile.js'))
def test_spec_usage(self): spec = Spec(a=1, b=2, c=3) self.assertEqual(spec['a'], 1) self.assertEqual(spec['b'], 2) self.assertEqual(spec['c'], 3) spec['d'] = 4 self.assertEqual(spec['d'], 4)