def test_assemble_explicit_entry(self):
        tmpdir = utils.mkdtemp(self)

        with open(join(tmpdir, 'webpack'), 'w'):
            # mock a webpack executable.
            pass

        spec = Spec(
            # this is not written
            export_target=join(tmpdir, 'bundle.js'),
            build_dir=tmpdir,
            transpiled_modpaths={'example/module': 'example/module'},
            bundled_modpaths={},
            transpiled_targetpaths={
                'example/module': 'example/module.js',
            },
            bundled_targetpaths={},
            export_module_names=[],
            webpack_entry_point='example/module',
        )

        webpack = toolchain.WebpackToolchain()
        spec[webpack.webpack_bin_key] = join(tmpdir, 'webpack')
        webpack.prepare(spec)
        with pretty_logging(stream=mocks.StringIO()) as s:
            webpack.assemble(spec)

        target = join(tmpdir, 'example', 'module.js')
        self.assertIn(
            "alias 'example/module' points to '%s' but file does not exist" %
            (target),
            s.getvalue(),
        )

        # no bootstrap module with an explicit entry point
        self.assertFalse(exists(join(tmpdir, '__calmjs_bootstrap__.js')))
        self.assertTrue(exists(join(tmpdir, 'config.js')))
        self.assertEqual(spec[CONFIG_JS_FILES], [join(tmpdir, 'config.js')])

        with open(join(tmpdir, 'config.js'), encoding='utf8') as fd:
            config_js = extract_webpack_config_object(fd)

        self.assertEqual(
            config_js['output'], {
                "path": tmpdir,
                "filename": "bundle.js",
                "libraryTarget": "umd",
                "umdNamedDefine": True,
            })
        module_fn = join(tmpdir, 'example', 'module.js')
        self.assertEqual(config_js['resolve']['alias'], {
            'example/module': module_fn,
        })
        self.assertEqual(config_js['entry'], module_fn)
Example #2
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),
        )
    def test_prepare_failure_manual(self):
        webpack = toolchain.WebpackToolchain()
        spec = Spec(toolchain_bin_path='/no/such/path')
        with self.assertRaises(RuntimeError) as e:
            webpack.prepare(spec)

        self.assertEqual(
            str(e.exception),
            "'/no/such/path' does not exist; cannot be used as '%s' binary" %
            (webpack.webpack_bin),
        )
Example #4
0
 def test_coverage_generation_targets(self):
     spec = Spec(test_covered_test_paths=[
         'some/test/file',
         'some/other/file',
     ])
     loader = dev._generate_coverage_loader(None, spec)
     self.assertEqual(
         {
             "loader": "sourcemap-istanbul-instrumenter-loader",
             "include": ['some/test/file', 'some/other/file'],
         }, loader)
Example #5
0
    def test_missing_keys(self):
        build_dir = mkdtemp(self)
        spec = Spec(build_dir=build_dir, )

        with self.assertRaises(AdviceAbort) as e:
            precompile_nunja(spec, False, 'myplugin_sourcepath',
                             'mybundle_sourcepath')

        self.assertEqual(
            e.exception.args[0],
            'cannot precompile_nunja if spec is missing keys '
            '{mybundle_sourcepath, myplugin_sourcepath}')
Example #6
0
 def test_nunjucks_slim_empty_unspecified(self):
     nunjucks_path = 'empty:'
     build_dir = mkdtemp(self)
     spec = Spec(
         build_dir=build_dir,
         plugin_sourcepath={},
         bundle_sourcepath={
             'nunjucks': nunjucks_path,
         },
     )
     precompile_nunja(spec, True, 'plugin_sourcepath', 'bundle_sourcepath')
     self.assertNotEqual(spec['bundle_sourcepath']['nunjucks'], 'empty:')
Example #7
0
 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'])
Example #8
0
    def test_toolchain_calf_with_build_dir_null(self):
        spec = Spec(build_dir=None)

        with self.assertRaises(NotImplementedError):
            self.toolchain(spec)

        # While build_dir is defined, no value was assigned.  See that
        # the process will give it a new one.
        self.assertTrue(spec['build_dir'].startswith(
            realpath(tempfile.gettempdir())))
        # Also that it got deleted properly.
        self.assertFalse(exists(spec['build_dir']))
    def test_prepare_assemble_calmjs_bootstrap_explicit(self):
        tmpdir = utils.mkdtemp(self)

        with open(join(tmpdir, 'webpack'), 'w'):
            # mock a webpack executable.
            pass

        # note that all *_targetpaths are relative to the build dir.
        spec = Spec(
            # export_target will not be written.
            export_target=join(tmpdir, 'bundle.js'),
            build_dir=tmpdir,
            transpiled_modpaths={'example/module': 'example/module'},
            bundled_modpaths={
                'bundled_pkg': 'bundled_pkg',
            },
            transpiled_targetpaths={
                'example/module': 'example/module.js',
            },
            bundled_targetpaths={
                'bundled_pkg': 'bundled_pkg.js',
                # note that this is probably meaningless in the context
                # of webpack.
                'bundled_dir': 'bundled_dir',
            },
            export_module_names=[
                'example/module',
                'bundled_dir',
                'bundled_pkg',
            ],
            # note that webpack_output_library is defined to use the
            # complete module without the externals being defined, this
            # will trigger an exception
            webpack_output_library='__calmjs__',
        )

        webpack = toolchain.WebpackToolchain()
        spec[webpack.webpack_bin_key] = join(tmpdir, 'webpack')
        webpack.prepare(spec)
        # skip the compile step as those entries are manually applied.

        with pretty_logging(logger='calmjs.webpack',
                            stream=mocks.StringIO()) as s:
            with self.assertRaises(ValueError):
                webpack.assemble(spec)

        self.assertIn(
            "webpack.externals does not have '__calmjs__' defined for "
            "the complete calmjs webpack bootstrap module", s.getvalue())
        self.assertIn(
            "aborting export to webpack.output.library as '__calmjs__'",
            s.getvalue())
Example #10
0
 def test_plugin_package_base(self):
     base = NPMLoaderPluginHandler(None, 'base')
     toolchain = NullToolchain()
     spec = Spec(working_dir=mkdtemp(self))
     with pretty_logging(stream=StringIO()) as stream:
         self.assertEqual(
             base.generate_handler_sourcepath(toolchain, spec, {}), {})
     self.assertIn(
         "no npm package name specified or could be resolved for "
         "loaderplugin 'base' of registry '<invalid_registry/handler>'; "
         "please subclass calmjs.loaderplugin:NPMLoaderPluginHandler such "
         "that the npm package name become specified", stream.getvalue(),
     )
Example #11
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:')
Example #12
0
    def test_update_spec_webpack_loaders_modules(self):
        spec = Spec(calmjs_webpack_modname_loader_map={
            'some/style.css': ['style', 'css'],
        }, )
        alias = {
            'some/style.css': '/path/to/some/style.css',
        }
        update_spec_webpack_loaders_modules(spec, alias)

        self.assertEqual([{
            'test': '/path/to/some/style.css',
            'loaders': ['style', 'css'],
        }], spec['webpack_module_rules'])
Example #13
0
    def test_toolchain_standard_good(self):
        # good, with a mock
        called = []

        def mockcall(spec):
            called.append(True)

        spec = Spec()
        self.toolchain.assemble = mockcall
        self.toolchain.link = mockcall

        self.toolchain(spec)

        self.assertEqual(len(called), 2)
Example #14
0
 def test_plugin_generate_handler_sourcepath_resolved_registry(self):
     base = LoaderPluginHandler(None, 'base')
     reg = LoaderPluginRegistry('loaders', _working_set=WorkingSet({}))
     toolchain = NullToolchain()
     spec = Spec(
         working_dir=mkdtemp(self), calmjs_loaderplugin_registry=reg)
     with pretty_logging(stream=StringIO()) as stream:
         self.assertEqual(
             base.generate_handler_sourcepath(toolchain, spec, {
                 'base!bad': 'base!bad',
             }), {})
     self.assertIn(
         "loaderplugin registry 'loaders' already assigned to spec",
         stream.getvalue())
Example #15
0
    def test_nunjucks(self):
        nunjucks_path = join('node_modules', 'nunjucks', 'nunjucks.js'),
        build_dir = mkdtemp(self)
        spec = Spec(
            build_dir=build_dir,
            plugin_sourcepath={},
            bundle_sourcepath={
                'nunjucks': nunjucks_path,
            },
        )

        precompile_nunja(spec, False, 'plugin_sourcepath', 'bundle_sourcepath')
        self.assertFalse(exists(join(build_dir, '__nunja_precompiled__.js')))
        self.assertEqual(spec['bundle_sourcepath']['nunjucks'], nunjucks_path)
Example #16
0
 def test_coverage_generation_all(self):
     spec = Spec(
         build_dir=mkdtemp(self),
         test_covered_build_dir_paths=['afile.js'],
         test_covered_test_paths=['some/test/file'],
     )
     loader = dev._generate_coverage_loader(None, spec)
     self.assertEqual(
         {
             "loader": "sourcemap-istanbul-instrumenter-loader",
             "include":
             ['some/test/file',
              join(spec['build_dir'], 'afile.js')],
         }, loader)
Example #17
0
    def test_plugin_loaders_modname_source_to_target_identity(self):
        # manually create a registry
        reg = LoaderPluginRegistry('simloaders', _working_set=WorkingSet({}))
        base = reg.records['local/dev'] = LoaderPluginHandler(reg, 'local/dev')
        toolchain = NullToolchain()
        spec = Spec()

        self.assertEqual('fun.file', base.modname_source_to_target(
            toolchain, spec, 'local/dev!fun.file',
            '/some/path/fun.file'))
        # a redundant usage test
        self.assertEqual('local/dev', base.modname_source_to_target(
            toolchain, spec, 'local/dev',
            '/some/path/to/the/plugin'))
Example #18
0
 def test_plugin_package_missing_dir(self):
     base = NPMLoaderPluginHandler(None, 'base')
     base.node_module_pkg_name = 'dummy_pkg'
     toolchain = NullToolchain()
     spec = Spec(working_dir=mkdtemp(self))
     with pretty_logging(stream=StringIO()) as stream:
         self.assertEqual(
             base.generate_handler_sourcepath(toolchain, spec, {}), {})
     self.assertIn(
         "could not locate 'package.json' for the npm package 'dummy_pkg' "
         "which was specified to contain the loader plugin 'base' in the "
         "current working directory '%s'" % spec['working_dir'],
         stream.getvalue(),
     )
Example #19
0
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
Example #20
0
 def test_process_test_files_empty(self):
     from calmjs.webpack.cli import default_toolchain as toolchain
     # the bare minimum spec
     spec = Spec(build_dir=mkdtemp(self),
                 karma_config={
                     'webpack': {
                         'resolve': {
                             'alias': {},
                         },
                     },
                     'preprocessors': {},
                 })
     test_files, loaderspath = dev._process_test_files(toolchain, spec)
     self.assertEqual(set(), test_files)
     self.assertEqual({}, loaderspath)
Example #21
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')
Example #22
0
    def test_plugin_package_success_main(self):
        base = NPMLoaderPluginHandler(None, 'base')
        base.node_module_pkg_name = 'dummy_pkg'
        toolchain = NullToolchain()
        spec = Spec(working_dir=mkdtemp(self))
        pkg_dir = join(spec['working_dir'], 'node_modules', 'dummy_pkg')
        makedirs(pkg_dir)
        with open(join(pkg_dir, 'package.json'), 'w') as fd:
            fd.write('{"main": "base.js"}')

        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual(
                join(pkg_dir, 'base.js'),
                base.generate_handler_sourcepath(toolchain, spec, {})['base'],
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())
Example #23
0
    def test_karma_setup_basic_file(self):
        karma_config = karma.build_base_config()
        # this should be purged.
        karma_config['files'] = ['example/package/lib.js']
        spec = Spec(
            karma_config=karma_config,
            build_dir=mkdtemp(self),
            toolchain_bin_path=self.setup_fake_webpack(),
        )

        with pretty_logging(stream=StringIO()) as s:
            karma_webpack(spec)

        self.assertEqual(spec['karma_config']['files'],
                         [join(spec['build_dir'], '__calmjs_tests__.js')])
        self.assertNotIn('unsupported', s.getvalue())
Example #24
0
 def test_nunjucks_slim_empty_specified(self):
     nunjucks_path = 'about:blank'
     build_dir = mkdtemp(self)
     spec = Spec(
         build_dir=build_dir,
         plugin_sourcepath={},
         bundle_sourcepath={
             'nunjucks': nunjucks_path,
         },
     )
     precompile_nunja(spec,
                      True,
                      'plugin_sourcepath',
                      'bundle_sourcepath',
                      omit_paths=('about:blank', ))
     self.assertEqual(spec['bundle_sourcepath']['nunjucks'], 'about:blank')
Example #25
0
 def test_plugin_loaders_modname_source_to_target(self):
     working_dir = mkdtemp(self)
     reg, base, extra, base_dir, extra_dir = self.create_base_extra_plugins(
         working_dir)
     toolchain = NullToolchain()
     spec = Spec(working_dir=working_dir)
     self.assertEqual('fun.file', base.modname_source_to_target(
         toolchain, spec, 'base!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'))
Example #26
0
    def test_update_spec_webpack_loaders_modules_missing_alias(self):
        spec = Spec(calmjs_webpack_modname_loader_map={
            'some/style.css': ['style', 'css'],
        }, )
        alias = {}
        with pretty_logging(stream=StringIO()) as s:
            update_spec_webpack_loaders_modules(spec, alias)

        self.assertIn(
            "WARNING modname 'some/style.css' requires loader chain "
            "['style', 'css'] but it does not have a corresponding webpack "
            "resolve.alias; webpack build failure may result as loaders are "
            "not configured for this modname",
            s.getvalue(),
        )

        self.assertEqual([], spec['webpack_module_rules'])
Example #27
0
    def test_null_transpiler(self):
        # a kind of silly test but shows concept
        tmpdir = mkdtemp(self)
        js_code = 'var dummy = function () {};\n'
        source = join(tmpdir, 'source.js')
        target = join(tmpdir, 'target.js')

        with open(source, 'w') as fd:
            fd.write(js_code)

        spec = Spec()
        self.toolchain.compile(spec, source, target)

        with open(target) as fd:
            result = fd.read()

        self.assertEqual(js_code, result)
Example #28
0
    def test_toolchain_standard_not_implemented(self):
        spec = Spec()

        with self.assertRaises(NotImplementedError):
            self.toolchain(spec)

        with self.assertRaises(NotImplementedError):
            self.toolchain.assemble(spec)

        with self.assertRaises(NotImplementedError):
            self.toolchain.link(spec)

        # Check that the build_dir is set on the spec based on tempfile
        self.assertTrue(spec['build_dir'].startswith(
            realpath(tempfile.gettempdir())))
        # Also that it got deleted properly.
        self.assertFalse(exists(spec['build_dir']))
    def test_assemble_alias_malformed_check_skipped(self):
        tmpdir = utils.mkdtemp(self)
        build_dir = utils.mkdtemp(self)
        webpack = toolchain.WebpackToolchain()

        export_target = join(build_dir, 'export.js')
        config_js = join(build_dir, 'config.js')

        with open(join(tmpdir, 'webpack'), 'w'):
            pass

        with open(join(build_dir, 'underscore.js'), 'w') as fd:
            # somehow this is malformed.
            fd.write("function() {});")

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

        spec = Spec(
            build_dir=build_dir,
            export_target=export_target,
            webpack_config_js=config_js,
            transpiled_modpaths={
                'module1': 'module1',
            },
            transpiled_targetpaths={
                'module1': 'module1.js',
            },
            bundled_modpaths={
                'underscore': 'underscore',
            },
            bundled_targetpaths={
                'underscore': 'underscore.js',
            },
            export_module_names=['module1'],
            verify_imports=False,
        )
        spec[webpack.webpack_bin_key] = join(tmpdir, 'webpack')

        with pretty_logging(logger='calmjs.webpack', stream=mocks.StringIO()):
            webpack.assemble(spec)

        # the main config file will be created as the same check that
        # caused the failure will no longer be triggered.
        self.assertTrue(exists(config_js))
Example #30
0
    def test_null_toolchain_transpile_js_ns_directory_sources(self):
        """
        Ensure that directory structures are copied, if needed, because
        JavaScript uses directories for namespaces, too, however the
        names are verbatim from directories and `.`s are valid module
        names which can result in some really hilarious side effects
        when combined with its completely transparent model on top of
        the filesystem (think ``..``), but that's for another time.
        """

        source_dir = mkdtemp(self)
        build_dir = mkdtemp(self)

        namespace_root = join(source_dir, 'namespace', 'dummy')
        makedirs(namespace_root)
        source_file = join(namespace_root, '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')))