예제 #1
0
    def test_compile_plugin_empty(self):
        build_dir = utils.mkdtemp(self)
        target = 'target.txt'

        rjs = toolchain.RJSToolchain()
        spec = {
            'build_dir': build_dir,
            toolchain.RJS_LOADER_PLUGIN_REGISTRY: rjs.loader_plugin_registry,
        }

        # Should result in no exceptions with either cases.
        # Normally, both source and modpath will become the same if the
        # value is `empty:`.
        rjs.compile_plugin(
            spec,
            [
                # modname, source, target, modpath
                ('text!target.txt', 'empty:', target, 'target.txt'),
                ('text!target.txt', 'source.txt', target, 'empty:'),
                ('text!target.txt', 'empty:', target, 'empty:'),
            ])

        # Nothing should have been written at the end of that.
        self.assertFalse(exists(join(build_dir, target)))
        self.assertFalse(exists(join(build_dir, 'empty:')))
예제 #2
0
    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')
예제 #3
0
    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'")
예제 #4
0
    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')
예제 #5
0
    def test_compile_plugin_base(self):
        build_dir = utils.mkdtemp(self)
        src_dir = utils.mkdtemp(self)
        src = join(src_dir, 'mod.js')

        with open(src, 'w') as fd:
            fd.write('hello world')

        # prepare targets
        target1 = 'mod1.txt'
        target2 = join('namespace', 'mod2.txt')
        target3 = join('nested', 'namespace', 'mod3.txt')
        target4 = 'namespace.mod4.txt'

        rjs = toolchain.RJSToolchain()
        spec = {
            'build_dir': build_dir,
            toolchain.RJS_LOADER_PLUGIN_REGISTRY: rjs.loader_plugin_registry,
        }
        rjs.compile_plugin(spec, [
            ('text!mod1.txt', src, target1, 'mod1'),
            ('text!namespace/mod2.txt', src, target2, 'mod2'),
            ('text!nested/namespace/mod3.txt', src, target3, 'mod3'),
            ('text!namespace.mod4.txt', src, target4, 'mod4'),
        ])

        self.assertTrue(exists(join(build_dir, target1)))
        self.assertTrue(exists(join(build_dir, target2)))
        self.assertTrue(exists(join(build_dir, target3)))
        self.assertTrue(exists(join(build_dir, target4)))
예제 #6
0
 def test_modname_source_target_to_modpath_empty(self):
     modname = 'module'
     source = 'empty:'
     rjs = toolchain.RJSToolchain()
     spec = Spec()
     self.assertEqual(
         rjs.modname_source_target_to_modpath(spec, modname, source, ''),
         'empty:',
     )
예제 #7
0
 def test_transpile_modname_source_target_empty(self):
     modname = 'module'
     src_file = 'empty:'
     tgt_dir = utils.mkdtemp(self)
     tgt_file = join(tgt_dir, 'module.js')
     spec = Spec(build_dir=tgt_dir)
     rjs = toolchain.RJSToolchain()
     rjs.transpile_modname_source_target(spec, modname, src_file, tgt_file)
     self.assertFalse(exists(tgt_file))
예제 #8
0
 def test_toolchain_instance_transpile_skip_on_amd(self):
     source = StringIO("define(['jquery'], function($) {\n"
                       "    'use strict';\n"
                       "    return {'testing': {}}\n"
                       "});\n")
     target = StringIO()
     spec = Spec()
     rjs = toolchain.RJSToolchain()
     rjs.transpiler(spec, source, target)
     self.assertEqual(source.getvalue(), target.getvalue())
예제 #9
0
    def test_prepare_failure_manual(self):
        rjs = toolchain.RJSToolchain()
        spec = Spec(rjs_bin='/no/such/path')
        with self.assertRaises(RuntimeError) as e:
            rjs.prepare(spec)

        self.assertEqual(
            str(e.exception),
            "'/no/such/path' does not exist; cannot be used as '%s' binary" %
            (rjs.rjs_bin),
        )
예제 #10
0
 def test_modname_source_target_to_modpath(self):
     rjs = toolchain.RJSToolchain()
     spec = Spec()
     self.assertEqual(
         rjs.modname_source_target_to_modpath(spec, 'example/module',
                                              '/tmp/src/example/module',
                                              '/tmp/build/example/module'),
         'example/module')
     self.assertEqual(
         rjs.modname_source_target_to_modpath(spec, 'example/module',
                                              'empty:',
                                              '/tmp/build/example/module'),
         'empty:')
예제 #11
0
    def test_build_bundle_with_data(self):
        bundle_dir = utils.mkdtemp(self)
        build_dir = utils.mkdtemp(self)
        transpile_source_map = {}
        transpile_source_map.update(self._example_package_map)
        # include custom loader and data
        transpile_source_map.update(self._example_package_loader)
        bundle_source_map = {}
        export_target = join(bundle_dir, 'example.package')
        requirejs_plugins = {
            'example/package/loader': self._example_package_data
        }

        custom_registry = LoaderPluginRegistry(
            'custom', _working_set=WorkingSet({
                'custom': [
                    'example/package/loader = calmjs.rjs.plugin:TextPlugin']})
        )
        rjs = toolchain.RJSToolchain()
        rjs.loader_plugin_registry = custom_registry
        spec = Spec(
            transpile_source_map=transpile_source_map,
            bundle_source_map=bundle_source_map,
            requirejs_plugins=requirejs_plugins,
            export_target=export_target,
            build_dir=build_dir,
        )

        with pretty_logging(stream=StringIO()):
            # to avoid logging the issue of mismatch map to extension
            # to stderr.
            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 result = requirejs(\n'
            '    "example/package/loader!example/package/data");\n'
            'process.stdout.write("" + result.results.item_count);\n',
            export_target,
        )

        self.assertEqual(stderr, '')
        self.assertEqual(stdout, '0')
예제 #12
0
    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))
예제 #13
0
    def test_transpile_modname_source_target_normal(self):
        modname = 'module'
        src_file = join(utils.mkdtemp(self), 'module.js')
        tgt_file = 'module.js'
        tgt_dir = utils.mkdtemp(self)
        spec = Spec(build_dir=tgt_dir)

        with open(src_file, 'w') as fd:
            fd.write('console.log("Hello");')

        rjs = toolchain.RJSToolchain()
        rjs.transpile_modname_source_target(spec, modname, src_file, tgt_file)

        with open(join(tgt_dir, tgt_file)) as fd:
            # The transpiler will mutate it.
            result = fd.read()

        self.assertNotEqual('console.log("Hello");', result)
        self.assertIn('console.log("Hello");', result)
예제 #14
0
    def test_prepare_rjs_plugin_key(self):
        tmpdir = utils.mkdtemp(self)
        rjs = toolchain.RJSToolchain()

        with open(join(tmpdir, 'r.js'), 'w'):
            # mock a r.js file.
            pass

        spec = Spec(
            # this is not written
            export_target=join(tmpdir, 'bundle.js'),
            build_dir=tmpdir,
            transpiled_modpaths={},
            bundled_modpaths={},
            export_module_names=[],
        )
        spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js')
        spec[toolchain.REQUIREJS_PLUGINS] = {
            'text': {
                'text!namespace/module/path.txt': '/namespace/module/path.txt',
            },
            'unsupported/unknown_plugin': {
                'also this is an invalid value': '/some/path',
            },
        }

        with pretty_logging(logger='calmjs.rjs', stream=mocks.StringIO()) as s:
            rjs.prepare(spec)

        self.assertEqual(
            spec['plugin_source_map'], {
                'text!namespace/module/path.txt': '/namespace/module/path.txt',
            })

        logs = s.getvalue()
        self.assertIn("DEBUG", logs)
        self.assertIn("found handler for 'text' loader plugin", logs)
        self.assertIn("WARNING", logs)
        self.assertIn(
            "handler for 'unsupported/unknown_plugin' loader plugin not found",
            logs)
        self.assertIn("also this is an invalid value", logs)
예제 #15
0
    def test_assemble_null(self):
        tmpdir = utils.mkdtemp(self)

        with open(join(tmpdir, 'r.js'), 'w'):
            # mock a r.js file.
            pass

        spec = Spec(
            # this is not written
            export_target=join(tmpdir, 'bundle.js'),
            build_dir=tmpdir,
            transpiled_modpaths={},
            bundled_modpaths={},
            plugins_modpaths={},
            transpiled_targets={},
            bundled_targets={},
            plugins_targets={},
            export_module_names=[],
        )

        rjs = toolchain.RJSToolchain()
        spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js')
        rjs.prepare(spec)
        rjs.assemble(spec)

        self.assertTrue(exists(join(tmpdir, 'build.js')))
        self.assertTrue(exists(join(tmpdir, 'config.js')))
        self.assertEqual(spec[CONFIG_JS_FILES], [join(tmpdir, 'config.js')])

        with open(join(tmpdir, 'build.js')) as fd:
            # strip off the header and footer
            build_js = json.loads(''.join(fd.readlines()[1:-1]))

        with open(join(tmpdir, 'config.js')) as fd:
            # strip off the header and footer
            config_js = json.loads(''.join(fd.readlines()[4:-10]))

        self.assertEqual(build_js['paths'], {})
        self.assertEqual(build_js['include'], [])

        self.assertEqual(config_js['paths'], {})
        self.assertEqual(config_js['include'], [])
예제 #16
0
    def test_compile_plugin_error(self):
        build_dir = utils.mkdtemp(self)
        src_dir = utils.mkdtemp(self)
        src = join(src_dir, 'mod.js')

        with open(src, 'w') as fd:
            fd.write('hello world')

        # prepare targets
        target = 'target.txt'

        rjs = toolchain.RJSToolchain()
        spec = {
            'build_dir': build_dir,
            toolchain.RJS_LOADER_PLUGIN_REGISTRY: rjs.loader_plugin_registry,
        }
        with self.assertRaises(TypeError):
            # This normally shouldn't happen, and for now the method
            # will not trap exceptions.
            rjs.compile_plugin(spec, [
                ('unregistered/mod!target.txt', src, target, 'target.txt'),
            ])
예제 #17
0
    def assemble_spec_config(self, **kw):
        # for the assemble related tests.
        tmpdir = utils.mkdtemp(self)
        build_dir = utils.mkdtemp(self)
        rjs = toolchain.RJSToolchain()

        export_target = join(build_dir, 'export.js')
        build_manifest_path = join(build_dir, 'build.js')
        node_config_js = join(build_dir, 'node_config.js')
        requirejs_config_js = join(build_dir, 'requirejs_config.js')

        with open(join(tmpdir, 'r.js'), 'w'):
            pass

        with open(join(build_dir, 'module1.js'), 'w') as fd:
            fd.write("define(['jquery', 'underscore', 'some.pylike.module'], "
                     "function(jquery, underscore, module) {"
                     "});")

        with open(join(build_dir, 'module2.js'), 'w') as fd:
            fd.write("define(['module1', 'underscore'], "
                     "function(module1, underscore) {"
                     "});")

        with open(join(build_dir, 'module3.js'), 'w') as fd:
            fd.write("'use strict';\n"
                     "var $ = require('jquery');\n"
                     "var _ = require('underscore');\n"
                     "var module2 = require('module2');\n")

        spec = Spec(
            build_dir=build_dir,
            export_target=export_target,
            build_manifest_path=build_manifest_path,
            node_config_js=node_config_js,
            requirejs_config_js=requirejs_config_js,
            transpiled_modpaths={
                'module1': 'module1',
                'module2': 'module2',
                'module3': 'module3',
            },
            # these are not actually transpiled sources, but will fit
            # with the purposes of this test.
            transpiled_targets={
                'module1': 'module1.js',
                'module2': 'module2.js',
                'module3': 'module3.js',
            },
            # the "bundled" names were specified to be omitted.
            bundled_modpaths={},
            bundled_targets={},
            plugins_modpaths={},
            plugins_targets={},
            export_module_names=['module1', 'module2', 'module3'],
            **kw)
        spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js')
        rjs.assemble(spec)

        # the main config file
        # check that they all exists
        self.assertTrue(exists(build_manifest_path))
        self.assertTrue(exists(node_config_js))
        self.assertTrue(exists(requirejs_config_js))

        # only checking the build_manifest version, as the node config
        # version is not that much different.
        with open(build_manifest_path) as fd:
            build_tree = parse(fd.read())

        # this is the node for the json in the build file
        build_js = json.loads(build_tree.children()[0].children()[0].to_ecma())

        with open(requirejs_config_js) as fd:
            config_tree = parse(fd.read())

        # this is the node for json in the config file
        config_js = json.loads(
            config_tree.children()[0].children()[0].children()[0].children()
            [2].children()[0].children()[1].to_ecma())

        return build_js, config_js
예제 #18
0
    def test_prepare_assemble(self):
        tmpdir = utils.mkdtemp(self)

        with open(join(tmpdir, 'r.js'), 'w'):
            # mock a r.js file.
            pass

        spec = Spec(
            # this is not written
            export_target=join(tmpdir, 'bundle.js'),
            build_dir=tmpdir,
            transpiled_modpaths={
                'example/module': '/path/to/src/example/module'
            },
            bundled_modpaths={
                'bundled_pkg': '/path/to/bundled/index',
                'bundled_empty': 'empty:',
            },
            plugins_modpaths={'loader/plugin!resource/name': '/resource/name'},
            transpiled_targets={
                'example/module': '/path/to/src/example/module.js',
            },
            bundled_targets={
                'bundled_pkg': '/path/to/bundled/index.js',
                'bundled_txt': '/path/to/bundled/txt',
                'bundled_dir': '/path/to/bundled/dir.js',
                'bundled_empty': 'empty:',
            },
            plugins_targets={
                'resource/name': '/resource/name',
            },
            export_module_names=[
                'example/module',
                'bundled_dir',
                'bundled_pkg',
                'bundled_txt',
                'bundled_empty',
                'loader/plugin!resource/name',
            ],
        )

        # we are going to fake the is_file checks
        utils.stub_item_attr_value(self, toolchain, 'isfile',
                                   lambda x: not x.endswith('dir.js'))

        rjs = toolchain.RJSToolchain()
        spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js')
        rjs.prepare(spec)

        # skip the compile step as those entries are manually applied.
        with pretty_logging(logger='calmjs.rjs', stream=mocks.StringIO()) as s:
            # the parser will try to load the file
            rjs.assemble(spec)

        self.assertIn('No such file or directory', s.getvalue())
        self.assertIn(
            join(*('path/to/src/example/module.js'.split('/'))),
            s.getvalue(),
        )

        self.assertTrue(exists(join(tmpdir, 'build.js')))
        self.assertTrue(exists(join(tmpdir, 'config.js')))

        with open(join(tmpdir, 'build.js')) as fd:
            # strip off the header and footer as this is for r.js
            build_js = json.loads(''.join(fd.readlines()[1:-1]))

        with open(join(tmpdir, 'config.js')) as fd:
            # strip off the header and footer as this is for r.js
            config_js = json.loads(''.join(fd.readlines()[4:-10]))

        self.assertEqual(build_js['paths'], {
            'bundled_empty': 'empty:',
        })
        self.assertEqual(build_js['include'], [
            'example/module',
            'bundled_dir',
            'bundled_pkg',
            'bundled_txt',
            'bundled_empty',
            'loader/plugin!resource/name',
        ])

        self.assertEqual(
            config_js['paths'], {
                'example/module': '/path/to/src/example/module.js?',
                'bundled_pkg': '/path/to/bundled/index.js?',
                'bundled_txt': '/path/to/bundled/txt',
                'bundled_dir': '/path/to/bundled/dir.js',
                'resource/name': '/resource/name',
            })
        self.assertEqual(config_js['include'], [])