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_runtime_cli_compile_explicit_registry_site(self): utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'site', '--source-registry-method=explicit', '--export-target=' + target_file, ]) self.assertEqual(e.exception.args[0], 0) with open(target_file) as fd: contents = fd.read() # As the registry is NOT declared for that package, it should # result in nothing. self.assertNotIn('framework/lib', contents) self.assertIn( 'no module registry declarations found using packages', sys.stderr.getvalue(), ) self.assertIn("'site'", sys.stderr.getvalue()) self.assertIn( "using acquisition method 'explicit'", sys.stderr.getvalue())
def test_runtime_cli_compile_explicit_site_legacy_flag(self): # same as previous test, but use the legacy flags. utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'site', '--source-map-method=explicit', '--bundle-map-method=none', '--export-target=' + target_file, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0) with open(target_file) as fd: contents = fd.read() # Since the package has no sources, and we disabled bundling of # sources (none works here because no code to automatically get # r.js to look for them), it should generate an empty bundle. self.assertEqual(contents, '(function () {}());') err = sys.stderr.getvalue() self.assertIn("flag '--source-map-method' is deprecated", err) self.assertIn("flag '--bundle-map-method' is deprecated", err)
def test_karma_test_runner_standalone_artifact(self): """ What's the purpose of tests if they can't be executed any time, anywhere, against anything? """ utils.stub_stdouts(self) current_dir = utils.mkdtemp(self) export_target = join(current_dir, 'example_package.js') # first, generate our bundle. with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'example.package', '--export-target', export_target]) self.assertTrue(exists(export_target)) # leverage the karma run command to run the tests provided by # the example.package against the resulting artifact. with self.assertRaises(SystemExit) as e: runtime.main([ 'karma', 'run', '--test-package', 'example.package', # TODO make this argument optional '--test-registry', self.registry_name + '.tests', '--artifact', export_target, # this is critical '--toolchain-package', 'calmjs.rjs', ]) # tests should pass against the resultant bundle self.assertEqual(e.exception.args[0], 0)
def test_runtime_cli_compile_framework_simple_invocation(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'framework', '--export-target=' + target_file, ]) self.assertEqual(e.exception.args[0], 0) self.assertTrue(exists(target_file)) # 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 lib = requirejs("framework/lib");\n' 'console.log(lib.Core);\n' '', target_file ) self.assertEqual(stderr, '') self.assertEqual(stdout, ( 'framework.lib.Core\n' ))
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 test_calmjs_main_runtime_console_version(self): stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['npm', '-V']) self.assertEqual(e.exception.args[0], 0) # reports both versions. value = sys.stdout.getvalue() self.assertEqual(2, len(value.strip().splitlines()))
def test_calmjs_artifact_package_generation(self): utils.stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['artifact', 'build', 'example.package']) self.assertEqual(e.exception.args[0], 0) registry = get_registry('calmjs.artifacts') for e, t, spec in registry.iter_builders_for('example.package'): self.assertTrue(exists(spec['export_target']))
def test_calmjs_main_console_version_broken(self): stub_stdouts(self) stub_item_attr_value(self, runtime, 'default_working_set', pkg_resources.WorkingSet([mkdtemp(self)])) # make sure the bad case doesn't just blow up... with self.assertRaises(SystemExit) as e: runtime.main(['-V']) self.assertEqual(e.exception.args[0], 0) self.assertIn('? ? from ?', sys.stdout.getvalue())
def test_karma_test_runner_basic(self): utils.stub_stdouts(self) current_dir = utils.mkdtemp(self) export_target = join(current_dir, 'example_package.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'karma', 'rjs', 'example.package', '--export-target=' + export_target, ]) self.assertEqual(e.exception.args[0], 0) self.assertTrue(exists(export_target))
def test_runtime_cli_bundle_method_force_empty(self): utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_slim_js = join(current_dir, 'widget_slim.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--empty', '--source-map-method=all', '--bundle-map-method=none', '--export-target=' + widget_slim_js, ]) self.assertEqual(e.exception.args[0], 0) # ensure that the bundled files are not copied self.assertFalse(exists(join(build_dir, 'underscore.js'))) self.assertFalse(exists(join(build_dir, 'jquery.js'))) with open(join(build_dir, 'build.js')) as fd: # strip off the header and footer build_js = json.loads(''.join(fd.readlines()[1:-1])) with open(join(build_dir, 'config.js')) as fd: # strip off the header and footer config_js = json.loads(''.join(fd.readlines()[4:-10])) self.assertEqual(build_js['paths'], { # this is missing because no sources actually poke into it, # whereas the previous test it showed up as extras_calmjs # 'jquery': 'empty:', 'underscore': 'empty:', }) self.assertEqual(sorted(build_js['include']), [ 'framework/lib', 'widget/core', 'widget/datepicker', 'widget/richedit', ]) self.assertEqual(config_js['paths'], { 'framework/lib': 'framework/lib.js?', 'widget/core': 'widget/core.js?', 'widget/datepicker': 'widget/datepicker.js?', 'widget/richedit': 'widget/richedit.js?', # this is picked up by the source analysis when empty option # is appied 'underscore': 'empty:', }) self.assertEqual(config_js['include'], [])
def test_runtime_cli_bundle_method_force_empty(self): utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_slim_js = join(current_dir, 'widget_slim.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--empty', '--sourcepath-method=all', '--bundlepath-method=none', '--export-target=' + widget_slim_js, ]) self.assertEqual(e.exception.args[0], 0) # ensure that the bundled files are not copied self.assertFalse(exists(join(build_dir, 'underscore.js'))) self.assertFalse(exists(join(build_dir, 'jquery.js'))) with open(join(build_dir, 'build.js')) as fd: # strip off the header and footer build_js = json.loads(''.join(fd.readlines()[1:-1])) with open(join(build_dir, 'config.js')) as fd: # strip off the header and footer config_js = json.loads(''.join(fd.readlines()[4:-10])) self.assertEqual(build_js['paths'], { # this is missing because no sources actually poke into it, # whereas the previous test it showed up as extras_calmjs # 'jquery': 'empty:', 'underscore': 'empty:', }) self.assertEqual(sorted(build_js['include']), [ 'framework/lib', 'widget/core', 'widget/datepicker', 'widget/richedit', ]) self.assertEqual(config_js['paths'], { 'framework/lib': 'framework/lib.js?', 'widget/core': 'widget/core.js?', 'widget/datepicker': 'widget/datepicker.js?', 'widget/richedit': 'widget/richedit.js?', # this is picked up by the source analysis when empty option # is appied 'underscore': 'empty:', }) self.assertEqual(config_js['include'], [])
def test_runtime_cli_help_text(self): utils.stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['rjs', '-h']) self.assertEqual(e.exception.args[0], 0) out = ' '.join(i.strip() for i in sys.stdout.getvalue().splitlines()) self.assertIn( '--export-target <export_target> output filename; ' 'defaults to last ${package_name}.js ', out) self.assertIn( '--working-dir <working_dir> the working directory; ' 'for this tool it will be used as the base directory to ' 'find source files declared for bundling; ', out) self.assertIn('default is current working directory', out)
def test_runtime_cli_help_text(self): utils.stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['rjs', '-h']) self.assertEqual(e.exception.args[0], 0) out = ' '.join(i.strip() for i in sys.stdout.getvalue().splitlines()) self.assertIn( '--export-target EXPORT_TARGET output filename; ' 'defaults to last ${package_name}.js ', out) self.assertIn( '--working-dir WORKING_DIR the working directory; ' 'for this tool it will be used as the base directory to ' 'find source files declared for bundling; ', out) self.assertIn('default is current working directory', out)
def test_runtime_cli_compile_all_service(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'service', 'site', '--export-target=' + target_file, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0) self.assertTrue(exists(target_file)) # 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 lib = requirejs("framework/lib");\n' 'console.log(lib.Core);\n' 'var datepicker = requirejs("widget/datepicker");\n' 'console.log(datepicker.DatePickerWidget);\n' 'var rpclib = requirejs("service/rpc/lib");\n' 'console.log(rpclib.Library);\n' 'var jquery = requirejs("jquery");\n' 'console.log(jquery);\n' 'var underscore = requirejs("underscore");\n' 'console.log(underscore);\n' '', target_file ) self.assertEqual(stderr, '') # note the names of the bundled files self.assertEqual(stdout, ( 'framework.lib.Core\n' 'widget.datepicker.DatePickerWidget\n' 'service.rpc.lib.Library\n' 'jquery/dist/jquery.js\n' 'underscore/underscore.js\n' ))
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_runtime_cli_bundle_method_standard(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_js = join(current_dir, 'widget_standard.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--source-map-method=all', '--bundle-map-method=all', '--export-target=' + widget_js, ]) self.assertEqual(e.exception.args[0], 0) # ensure that the bundled files are copied self.assertTrue(exists(join(build_dir, 'underscore.js'))) # even jquery.min.js is used, it's copied like this due to how # modules are renamed. self.assertTrue(exists(join(build_dir, 'jquery.js')))
def test_runtime_cli_bundle_method_standard(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_js = join(current_dir, 'widget_standard.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--sourcepath-method=all', '--bundlepath-method=all', '--export-target=' + widget_js, ]) self.assertEqual(e.exception.args[0], 0) # ensure that the bundled files are copied self.assertTrue(exists(join(build_dir, 'underscore.js'))) # even jquery.min.js is used, it's copied like this due to how # modules are renamed. self.assertTrue(exists(join(build_dir, 'jquery.js')))
def test_runtime_cli_bundle_method_explicit(self): utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_js = join(current_dir, 'widget_explicit.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--source-map-method=all', '--bundle-map-method=explicit', '--export-target=' + widget_js, ]) # as the explicit option only pulled dependencies from just # this file, the process does not actually complete self.assertNotEqual(e.exception.args[0], 0) # ensure that the explicitly defined bundled files are copied self.assertFalse(exists(join(build_dir, 'underscore.js'))) self.assertTrue(exists(join(build_dir, 'jquery.js')))
def test_runtime_cli_bundle_method_explicit(self): utils.stub_stdouts(self) current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) build_dir = utils.mkdtemp(self) widget_js = join(current_dir, 'widget_explicit.js') with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'widget', '--build-dir=' + build_dir, '--sourcepath-method=all', '--bundlepath-method=explicit', '--export-target=' + widget_js, ]) # as the explicit option only pulled dependencies from just # this file, the process does not actually complete self.assertNotEqual(e.exception.args[0], 0) # ensure that the explicitly defined bundled files are copied self.assertFalse(exists(join(build_dir, 'underscore.js'))) self.assertTrue(exists(join(build_dir, 'jquery.js')))
def test_runtime_cli_compile_explicit_site(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'site', '--sourcepath-method=explicit', '--bundlepath-method=none', '--export-target=' + target_file, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0) with open(target_file) as fd: contents = fd.read() # Since the package has no sources, and we disabled bundling of # sources (none works here because no code to automatically get # r.js to look for them), it should generate an empty bundle. self.assertEqual(contents, '(function () {}());')
def test_runtime_cli_compile_explicit_site(self): current_dir, target_file = self.setup_runtime_main_env() os.chdir(current_dir) # Invoke the thing through the main runtime with self.assertRaises(SystemExit) as e: runtime.main([ 'rjs', 'site', '--source-map-method=explicit', '--bundle-map-method=none', '--export-target=' + target_file, '--source-registry=' + self.registry_name, ]) self.assertEqual(e.exception.args[0], 0) with open(target_file) as fd: contents = fd.read() # Since the package has no sources, and we disabled bundling of # sources (none works here because no code to automatically get # r.js to look for them), it should generate an empty bundle. self.assertEqual(contents, '(function () {}());')
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 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 test_calmjs_artifact_test_verification(self): utils.stub_stdouts(self) artifact_path = join( self.dist_dir, 'example.package-1.0.egg-info', 'calmjs_artifacts', 'example.package.rjs.js', ) def clean_artifact(): if exists(artifact_path): unlink(artifact_path) self.addCleanup(clean_artifact) with self.assertRaises(SystemExit) as e: runtime.main(['artifact', 'karma', 'example.package']) # artifacts haven't been built yet? self.assertFalse(exists(artifact_path)) self.assertEqual(e.exception.args[0], 1) # so build the artifacts with self.assertRaises(SystemExit) as e: runtime.main(['artifact', 'build', 'example.package']) self.assertEqual(e.exception.args[0], 0) # should pass with self.assertRaises(SystemExit) as e: runtime.main(['artifact', 'karma', 'example.package']) self.assertEqual(e.exception.args[0], 0) with open(artifact_path, 'w') as fd: fd.write('// this should break the test.') # should fail again since the artifact is invalid with self.assertRaises(SystemExit) as e: runtime.main(['artifact', 'karma', 'example.package']) self.assertEqual(e.exception.args[0], 1)
from __future__ import absolute_import if __name__ == '__main__': from calmjs import runtime runtime.main()
def test_calmjs_main_console_entry_point(self): stub_stdouts(self) with self.assertRaises(SystemExit): runtime.main(['-h']) # ensure our base action module/class is registered. self.assertIn('bower', sys.stdout.getvalue())
def test_calmjs_main_runtime_bower_version(self): stub_stdouts(self) with self.assertRaises(SystemExit): runtime.main(['bower', '-V']) # reports both versions. self.assertIn('calmjs.bower', sys.stdout.getvalue())
def test_calmjs_main_console_version(self): stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['-V']) self.assertEqual(e.exception.args[0], 0) self.assertIn('calmjs', sys.stdout.getvalue())
def test_calmjs_main_console_entry_point_help(self): stub_stdouts(self) with self.assertRaises(SystemExit) as e: runtime.main(['-h']) self.assertIn('npm', sys.stdout.getvalue()) self.assertEqual(e.exception.args[0], 0)
def runtime_main(args, error_code=0): # Invoke the thing through the main runtime os.chdir(current_dir) with self.assertRaises(SystemExit) as e: runtime.main(args) self.assertEqual(e.exception.args[0], error_code)
def test_main_integration(self): stub_stdouts(self) with self.assertRaises(SystemExit): main(['karma', '-h']) self.assertIn('karma testrunner', sys.stdout.getvalue())