def test_npm_verbose_quiet(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() stub_stdouts(self) rt(['-v', 'foo', '--init', 'example.package1']) self.assertIn("generating a flattened", sys.stderr.getvalue()) self.assertNotIn("found 'package.json'", sys.stderr.getvalue()) # extra verbosity shouldn't blow up stub_stdouts(self) rt(['-vvvv', 'foo', '--init', 'example.package1']) self.assertIn("generating a flattened", sys.stderr.getvalue()) self.assertIn("found 'package.json'", sys.stderr.getvalue()) # q and v negates each other stub_stdouts(self) rt(['-v', '-q', 'foo', '--init', 'example.package2']) self.assertNotIn("generating a flattened", sys.stderr.getvalue()) self.assertNotIn("found 'package.json'", sys.stderr.getvalue()) self.assertIn("WARNING", sys.stderr.getvalue()) # extra quietness shouldn't blow up stub_stdouts(self) rt(['-qqqqq', 'foo', '--install', 'example.package2']) self.assertNotIn("WARNING", sys.stderr.getvalue())
def test_prepare_spec_artifacts(self): stub_stdouts(self) remember_cwd(self) tmpdir = mkdtemp(self) fake = join(tmpdir, 'fake.js') real = join(tmpdir, 'real.js') os.chdir(tmpdir) with open(real, 'w') as fd: fd.write('') with pretty_logging( logger='calmjs.dev', stream=mocks.StringIO()) as log: # note the relative paths spec = Spec(artifact_paths=['real.js', 'fake.js']) prepare_spec_artifacts(spec) # note that the full path is now specified. self.assertEqual(spec['artifact_paths'], [real]) self.assertIn('does not exists', log.getvalue()) self.assertIn(fake, log.getvalue()) # should still work with full paths. spec = Spec(artifact_paths=[real, fake]) prepare_spec_artifacts(spec) self.assertEqual(spec['artifact_paths'], [real])
def test_rjs_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, plugin_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: rjs(spec, ('raw')) # now trigger the advice spec.handle(BEFORE_COMPILE) # template remains in plugins self.assertEqual(spec['plugin_sourcepath'], { 'text!some/mold/template.nja': src_template, }) # will not be applied in raw. self.assertNotIn('__nunja__/some/mold', spec['bundle_sourcepath']) self.assertIn( 'nunja will be skipping precompilation for rjs toolchain', stream.getvalue(), )
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_cli_compile_all_service(self): # create a new working directory to install our current site utils.remember_cwd(self) working_dir = utils.mkdtemp(self) os.chdir(working_dir) # Trigger the compile using the module level compile function, # but without bundling spec = cli.compile_all( ['service'], source_registries=(self.registry_name,), bundlepath_method='none', ) self.assertEqual( spec['export_target'], join(working_dir, 'service.js')) # verify that the bundle works with node. First change back to # directory with requirejs library installed. os.chdir(self._env_root) # The execution should then work as expected on the bundle we # have. stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var rpclib = requirejs("service/rpc/lib");\n' 'console.log(rpclib.Library);\n', spec['export_target'], ) self.assertEqual(stderr, '') self.assertEqual(stdout, 'service.rpc.lib.Library\n')
def test_npm_binary_not_found_debugger(self): from calmjs import utils def fake_post_mortem(*a, **kw): sys.stdout.write('(Pdb) ') remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() # stub_stdin(self, u'quit\n') stub_stdouts(self) # ensure the binary is not found. stub_mod_call(self, cli, fake_error(IOError)) stub_item_attr_value(self, utils, 'post_mortem', fake_post_mortem) rt(['-dd', 'foo', '--install', 'example.package2']) self.assertIn("ERROR", sys.stderr.getvalue()) self.assertIn("invocation of the 'npm' binary failed;", sys.stderr.getvalue()) self.assertIn("terminating due to exception", sys.stderr.getvalue()) self.assertIn("Traceback ", sys.stderr.getvalue()) self.assertIn("(Pdb)", sys.stdout.getvalue()) stub_stdouts(self) self.assertNotIn("(Pdb)", sys.stdout.getvalue()) rt(['foo', '--install', 'example.package2', '--debugger']) self.assertIn("(Pdb)", sys.stdout.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_bower_all_the_actions(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) stub_stdouts(self) stub_mod_call(self, cli) stub_base_which(self, which_bower) rt = self.setup_runtime() rt([ 'bower', '--install', '--view', '--init', 'example.package1', 'example.package2' ]) # inside stdout result = json.loads(sys.stdout.getvalue()) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3') with open(join(tmpdir, 'bower.json')) as fd: result = json.load(fd) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3') args, kwargs = self.call_args self.assertEqual(args, (['bower', 'install'], )) env = kwargs.pop('env', {}) self.assertEqual(kwargs, {}) # have to do both, due to that this is an actual integration # test and values will differ between environments self.assertEqual(finalize_env(env), finalize_env(env))
def test_runtime_cli_method_none_with_empty_various(self): # this time, use the empty option utils.remember_cwd(self) utils.stub_stdouts(self) current_dir = utils.mkdtemp(self) export_target = join(current_dir, 'export_target.js') os.chdir(current_dir) with self.assertRaises(SystemExit) as e: # this should fail runtime.main([ 'rjs', 'service', 'site', '--bundlepath-method=none', '--export-target=' + export_target, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 1) os.chdir(current_dir) with self.assertRaises(SystemExit) as e: # this time, apply empty, which should automatically patch # the configuration runtime.main([ 'rjs', 'service', 'site', '--empty', '--bundlepath-method=none', '--export-target=' + export_target, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0)
def test_npm_all_the_actions(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) stub_stdouts(self) stub_mod_call(self, cli) stub_base_which(self, which_npm) rt = self.setup_runtime() rt([ 'foo', '--install', '--view', '--init', 'example.package1', 'example.package2' ]) # inside stdout result = json.loads(sys.stdout.getvalue()) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3') with open(join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3') # not foo install, but npm install since entry point specified # the actual runtime instance. self.assertEqual(self.call_args, (([which_npm, 'install'], ), {}))
def test_runtime_cli_method_none_with_empty_various(self): # this time, use the empty option utils.remember_cwd(self) utils.stub_stdouts(self) current_dir = utils.mkdtemp(self) export_target = join(current_dir, 'export_target.js') os.chdir(current_dir) with self.assertRaises(SystemExit) as e: # this should fail runtime.main([ 'rjs', 'service', 'site', '--bundle-map-method=none', '--export-target=' + export_target, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 1) os.chdir(current_dir) with self.assertRaises(SystemExit) as e: # this time, apply empty, which should automatically patch # the configuration runtime.main([ 'rjs', 'service', 'site', '--empty', '--bundle-map-method=none', '--export-target=' + export_target, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0)
def setUp(self): # save working directory remember_cwd(self) # All the pre-made setup. stub_mod_call(self, cli) app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~1.11.0'}, })), ), 'foo', '1.9.0') underscore = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'underscore': '~1.8.0'}, })), ), 'underscore', '1.8.0') named = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~3.0.0'}, 'name': 'named-js', })), ), 'named', '2.0.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) working_set.add(underscore, self._calmjs_testing_tmpdir) working_set.add(named, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) stub_mod_check_interactive(self, [cli], True) # also save this self.inst_interactive = npm.npm.cli_driver.interactive
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(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_cli_compile_all_service(self): # create a new working directory to install our current site utils.remember_cwd(self) working_dir = utils.mkdtemp(self) os.chdir(working_dir) # Trigger the compile using the module level compile function, # but without bundling spec = cli.compile_all( ['service'], source_registries=(self.registry_name,), bundle_map_method='none', ) self.assertEqual( spec['export_target'], join(working_dir, 'service.js')) # verify that the bundle works with node. First change back to # directory with requirejs library installed. os.chdir(self._env_root) # The execution should then work as expected on the bundle we # have. stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var rpclib = requirejs("service/rpc/lib");\n' 'console.log(rpclib.Library);\n', spec['export_target'], ) self.assertEqual(stderr, '') self.assertEqual(stdout, 'service.rpc.lib.Library\n')
def setUp(self): # save working directory remember_cwd(self) # All the pre-made setup. stub_mod_call(self, cli) app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~1.11.0'}, })), ), 'foo', '1.9.0') underscore = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'underscore': '~1.8.0'}, })), ), 'underscore', '1.8.0') named = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~3.0.0'}, 'name': 'named-js', })), ), 'named', '2.0.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) working_set.add(underscore, self._calmjs_testing_tmpdir) working_set.add(named, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) stub_check_interactive(self, True)
def test_remember_cwd_mkdtemp(self): cwd = os.getcwd() # must be done in this order as the cleanups are done FILO. utils.remember_cwd(self) tmpdir = mkdtemp(self) # this will mean that cwd would normally be in tmpdir before # the cwd cleanup gets called. os.chdir(tmpdir) self.assertNotEqual(cwd, os.getcwd()) self.doCleanups() self.assertEqual(cwd, os.getcwd())
def test_remember_cwd_mkdtemp_chdir_deep(self): cwd = os.getcwd() utils.remember_cwd(self) tmpdir = mkdtemp(self) newdir = join(tmpdir, 'some', 'nested', 'dir') os.makedirs(newdir) os.chdir(newdir) self.assertNotEqual(cwd, os.getcwd()) self.doCleanups() self.assertEqual(cwd, os.getcwd())
def test_found_posix_relpath(self): remember_cwd(self) sys.platform = 'posix' os.chdir(mkdtemp(self)) os.mkdir('bin') bin_dir = os.environ['PATH'] = join(os.path.curdir, 'bin') f = join(bin_dir, 'binary') with open(f, 'w'): pass os.chmod(f, 0o777) self.assertEqual(which(f), f)
def test_npm_init_integration(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() rt(['foo', '--init', 'example.package1']) with open(join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result['dependencies']['jquery'], '~3.1.0')
def test_npm_init_existing_malform(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() # create an existing malformed file with open(join(tmpdir, 'package.json'), 'w') as fd: fd.write('not a json') stub_stdouts(self) rt(['foo', '--init', 'example.package2']) self.assertIn("ignoring existing malformed", sys.stderr.getvalue())
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_critical_log_exception(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() stub_stdouts(self) # ensure the binary is not found. stub_mod_call(self, cli, fake_error(RuntimeError('fake error'))) rt(['foo', '--install', 'example.package2']) self.assertIn("CRITICAL calmjs.runtime RuntimeError: fake error", sys.stderr.getvalue())
def test_npm_interrupted(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() stub_stdouts(self) # ensure the binary is not found. stub_mod_call(self, cli, fake_error(KeyboardInterrupt)) rt(['foo', '--install', 'example.package2']) self.assertIn("CRITICAL", sys.stderr.getvalue()) self.assertIn("termination requested; aborted.", sys.stderr.getvalue())
def setup_runtime_main_env(self): # create a new working directory to install our current site utils.remember_cwd(self) current_dir = utils.mkdtemp(self) target_file = join(current_dir, 'bundle.js') # invoke installation of "fake_modules" copytree( join(self.dist_dir, 'fake_modules'), join(current_dir, 'fake_modules'), ) return current_dir, target_file
def test_npm_binary_not_found(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) rt = self.setup_runtime() stub_stdouts(self) # ensure the binary is not found. stub_mod_call(self, cli, fake_error(IOError)) rt(['foo', '--install', 'example.package2']) self.assertIn("ERROR", sys.stderr.getvalue()) self.assertIn("invocation of the 'npm' binary failed;", sys.stderr.getvalue())
def test_bower_view(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) stub_stdouts(self) rt = self.setup_runtime() rt(['bower', '--view', 'example.package1', 'example.package2']) result = json.loads(sys.stdout.getvalue()) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3') stub_stdouts(self) rt(['bower', 'example.package1', 'example.package2']) result = json.loads(sys.stdout.getvalue()) self.assertEqual(result['dependencies']['jquery'], '~3.1.0') self.assertEqual(result['dependencies']['underscore'], '~1.8.3')
def test_calmjs_main_console_entry_point_install(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) stub_stdouts(self) stub_mod_call(self, cli, fake_error(IOError)) with self.assertRaises(SystemExit) as e: runtime.main(['npm', '--init', 'calmjs']) # this should be fine, exit code 0 self.assertEqual(e.exception.args[0], 0) with self.assertRaises(SystemExit) as e: runtime.main(['npm', '--install', 'calmjs']) self.assertIn("invocation of the 'npm' binary failed;", sys.stderr.getvalue()) self.assertEqual(e.exception.args[0], 1)
def test_prepare_failure_which_fail(self): utils.stub_os_environ(self) utils.remember_cwd(self) # must go to a directory where r.js is guaranteed to not be # available through node_modules or the environmental PATH os.environ['NODE_PATH'] = '' os.environ['PATH'] = '' tmpdir = utils.mkdtemp(self) os.chdir(tmpdir) rjs = toolchain.RJSToolchain() spec = Spec() with self.assertRaises(RuntimeError) as e: rjs.prepare(spec) self.assertEqual(str(e.exception), "unable to locate '%s'" % (rjs.rjs_bin))
def test_prepare_failure_which_fail(self): utils.stub_os_environ(self) utils.remember_cwd(self) # must go to a directory where r.js is guaranteed to not be # available through node_modules or the environmental PATH os.environ['NODE_PATH'] = '' os.environ['PATH'] = '' tmpdir = utils.mkdtemp(self) os.chdir(tmpdir) rjs = toolchain.RJSToolchain() spec = Spec() with self.assertRaises(RuntimeError) as e: rjs.prepare(spec) self.assertEqual(str(e.exception), "unable to locate '%s'" % ( rjs.rjs_bin ))
def test_prepare_failure_which_fail(self): utils.stub_os_environ(self) utils.remember_cwd(self) # must go to a directory where webpack is guaranteed to not be # available through node_modules or the environmental PATH os.environ['NODE_PATH'] = '' os.environ['PATH'] = '' tmpdir = utils.mkdtemp(self) os.chdir(tmpdir) webpack = toolchain.WebpackToolchain() spec = Spec() with self.assertRaises(RuntimeError) as e: webpack.prepare(spec) # not fixed string because platform specific value. self.assertEqual(str(e.exception), "unable to locate '%s'" % (webpack.webpack_bin))
def test_cli_compile_explicit_service(self): utils.remember_cwd(self) working_dir = utils.mkdtemp(self) os.chdir(working_dir) # Trigger the compile using the module level compile function, # but without bundling spec = cli.compile_all( ['service'], source_registries=(self.registry_name,), bundle_map_method='none', source_map_method='explicit', ) service_js = join(working_dir, 'service.js') self.assertEqual(spec['export_target'], service_js) with open(service_js) as fd: self.assertIn('service/rpc/lib', fd.read()) # build its parent js separately, too spec = cli.compile_all( ['framework'], source_registries=(self.registry_name,), bundle_map_method='none', source_map_method='explicit', ) framework_js = join(working_dir, 'framework.js') self.assertEqual(spec['export_target'], framework_js) # verify that the bundle works with node. First change back to # directory with requirejs library installed. os.chdir(self._env_root) # The execution should then work as expected if we loaded both # bundles. stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var rpclib = requirejs("service/rpc/lib");\n' 'console.log(rpclib.Library);\n', framework_js, service_js, ) self.assertEqual(stderr, '') self.assertEqual(stdout, 'service.rpc.lib.Library\n')
def test_cli_compile_explicit_service(self): utils.remember_cwd(self) working_dir = utils.mkdtemp(self) os.chdir(working_dir) # Trigger the compile using the module level compile function, # but without bundling spec = cli.compile_all( ['service'], source_registries=(self.registry_name,), bundlepath_method='none', sourcepath_method='explicit', ) service_js = join(working_dir, 'service.js') self.assertEqual(spec['export_target'], service_js) with open(service_js) as fd: self.assertIn('service/rpc/lib', fd.read()) # build its parent js separately, too spec = cli.compile_all( ['framework'], source_registries=(self.registry_name,), bundlepath_method='none', sourcepath_method='explicit', ) framework_js = join(working_dir, 'framework.js') self.assertEqual(spec['export_target'], framework_js) # verify that the bundle works with node. First change back to # directory with requirejs library installed. os.chdir(self._env_root) # The execution should then work as expected if we loaded both # bundles. stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var rpclib = requirejs("service/rpc/lib");\n' 'console.log(rpclib.Library);\n', framework_js, service_js, ) self.assertEqual(stderr, '') self.assertEqual(stdout, 'service.rpc.lib.Library\n')
def test_find_node_module_pkg_name_full_suffix(self): remember_cwd(self) # this one is fully named 'text-loader' text_handler = loaderplugin.WebpackLoaderHandler(None, 'text-loader') 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)) # not affected by a prefix-free package create_mock_npm_package(working_dir, 'text', 'index.js') self.assertEqual( 'text-loader', text_handler.find_node_module_pkg_name(toolchain, spec))
def test_cli_compile_all_site(self): # create a new working directory to install our current site utils.remember_cwd(self) working_dir = utils.mkdtemp(self) os.chdir(working_dir) # Finally, install dependencies for site in the new directory # normally this might be done # npm = Driver() # npm.npm_install('site', production=True) # However, since we have our set of fake_modules, just install # by copying the fake_modules dir from dist_dir into the current # directory. copytree( join(self.dist_dir, 'fake_modules'), join(working_dir, 'fake_modules'), ) # Trigger the compile using the module level compile function spec = cli.compile_all( ['site'], source_registries=(self.registry_name,)) self.assertEqual( spec['export_target'], join(working_dir, 'site.js')) # verify that the bundle works with node. First change back to # directory with requirejs library installed. os.chdir(self._env_root) # The execution should then work as expected on the bundle we # have. stdout, stderr = run_node( 'var requirejs = require("requirejs");\n' 'var define = requirejs.define;\n' '%s\n' 'var datepicker = requirejs("widget/datepicker");\n' 'console.log(datepicker.DatePickerWidget);\n', spec['export_target'], ) self.assertEqual(stderr, '') self.assertEqual(stdout, 'widget.datepicker.DatePickerWidget\n')
def test_plugin_package_success_package_spec_missing_working_dir(self): remember_cwd(self) cwd = mkdtemp(self) chdir(cwd) base = NPMLoaderPluginHandler(None, 'base') base.node_module_pkg_name = 'dummy_pkg' toolchain = NullToolchain() spec = Spec() pkg_dir = join(cwd, 'node_modules', 'dummy_pkg') makedirs(pkg_dir) with open(join(pkg_dir, 'package.json'), 'w') as fd: fd.write('{"browser": "browser/base.js"}') with pretty_logging(stream=StringIO()) as stream: self.assertEqual( join(pkg_dir, 'browser', 'base.js'), base.generate_handler_sourcepath(toolchain, spec, {})['base'], ) self.assertIn("for loader plugin 'base'", stream.getvalue()) self.assertIn("missing working_dir", stream.getvalue())
def setUp(self): remember_cwd(self) app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~1.11.0'}, })), ), 'foo', '1.9.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) # Stub out the flatten_egginfo_json calls with one that uses our # custom working_set here. stub_item_attr_value(self, dist, 'default_working_set', working_set) # Quiet stdout from distutils logs stub_stdouts(self) # Force auto-detected interactive mode to True, because this is # typically executed within an interactive context. stub_check_interactive(self, True)
def test_prepare_spec_artifacts_order(self): remember_cwd(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) def touch(fn): with open(fn, 'w'): pass return fn names = ['art1.js', 'art2.js', 'art3.js'] paths = [touch(join(tmpdir, n)) for n in names] spec = Spec(artifact_paths=names) prepare_spec_artifacts(spec) self.assertEqual(spec['artifact_paths'], paths) spec = Spec(artifact_paths=['art2.js', 'art1.js', 'art3.js']) prepare_spec_artifacts(spec) self.assertEqual(spec['artifact_paths'], [ join(tmpdir, 'art2.js'), join(tmpdir, 'art1.js'), join(tmpdir, 'art3.js'), ])
def test_runtime_cli_compile_no_indent(self): utils.remember_cwd(self) target_dir = utils.mkdtemp(self) target_file = join(target_dir, 'bundle.js') # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'example.package', '--transpile-no-indent', '--export-target=' + target_file, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0) self.assertTrue(exists(target_file)) 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(true);\n', target_file, ) # The test should really test the files in the build directory, # but if we are doing this as an integration test, the bundle # should also at least maintain the same column when ran... patt = re.compile('%s:[0-9]+(:%d)?' % ( target_file.replace('\\', '\\\\'), self._bad_notdefinedsymbol[-1])) self.assertTrue(patt.search(stderr)) self.assertEqual(stdout, '2\n4\n') # ... or, just see that the bad line is there, too. with open(target_file) as fd: bundle_source = fd.read() self.assertIn('\nvar die = function() {\n', bundle_source)
def setUp(self): # so that yarn's lock file wouldn't be in this dir. remember_cwd(self) os.chdir(mkdtemp(self))
def setUp(self): remember_cwd(self) stub_os_environ(self) stub_check_interactive(self, True)
def setUp(self): self.cwd = mkdtemp(self) remember_cwd(self) os.chdir(self.cwd)