Example #1
0
 def test_base_plugin_generate_handler_sourcepath(self):
     base = BaseLoaderPluginHandler(None, 'base')
     toolchain = NullToolchain()
     spec = Spec(working_dir=mkdtemp(self))
     self.assertEqual(
         base.generate_handler_sourcepath(toolchain, spec, {
             'base!bad': 'base!bad',
         }), {})
Example #2
0
 def test_plugin_generate_handler_sourcepath_default_registry(self):
     base = LoaderPluginHandler(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, {
                 'base!bad': 'base!bad',
             }), {})
     self.assertIn("using default loaderplugin registry", stream.getvalue())
Example #3
0
    def test_plugin_package_dynamic_selection(self):

        class CustomHandler(NPMLoaderPluginHandler):
            def find_node_module_pkg_name(self, toolchain, spec):
                return spec.get('loaderplugin')

        reg = LoaderPluginRegistry('lp.reg', _working_set=WorkingSet({}))
        base = CustomHandler(reg, 'base')
        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(
                {}, base.generate_handler_sourcepath(toolchain, spec, {}))
        self.assertIn(
            "no npm package name specified or could be resolved for "
            "loaderplugin 'base' of registry 'lp.reg'",
            stream.getvalue()
        )
        self.assertIn(
            "test_loaderplugin:CustomHandler may be at fault",
            stream.getvalue()
        )
        self.assertNotIn("for loader plugin 'base'", stream.getvalue())

        # plug the value into the spec to satisfy the condition for this
        # particular loader

        spec['loaderplugin'] = 'dummy_pkg'
        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual(
                join(pkg_dir, 'base.js'),
                base.generate_handler_sourcepath(toolchain, spec, {})['base'],
            )
        self.assertIn("base.js' for loader plugin 'base'", stream.getvalue())
Example #4
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 #5
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 #6
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 #7
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 #8
0
    def test_plugin_package_strip_broken_recursion_stop(self):
        class BadPluginHandler(LoaderPluginHandler):
            def unwrap(self, value):
                # return the identity
                return value

        base = BadPluginHandler(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, {
                    'base!bad': 'base!bad',
                }), {})

        self.assertIn(
            "loaderplugin 'base' extracted same sourcepath of",
            stream.getvalue())
Example #9
0
    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())
Example #10
0
    def test_plugin_package_missing_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('{}')

        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual(
                base.generate_handler_sourcepath(toolchain, spec, {}), {})

        self.assertIn(
            "calmjs.loaderplugin 'package.json' for the npm package "
            "'dummy_pkg' does not contain a valid entry point: sources "
            "required for loader plugin 'base' cannot be included "
            "automatically; the build process may fail",
            stream.getvalue(),
        )
Example #11
0
    def test_plugin_package_chained_loaders(self):
        working_dir = mkdtemp(self)
        reg, base, extra, base_dir, extra_dir = self.create_base_extra_plugins(
            working_dir)
        # standard case
        toolchain = NullToolchain()
        spec = Spec(working_dir=working_dir)
        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual(
                {'base': join(base_dir, 'base.js')},
                base.generate_handler_sourcepath(toolchain, spec, {
                    'base!fun.file': 'base!fun.file',
                }),
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())

        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual({
                'base': join(base_dir, 'base.js'),
                'extra': join(extra_dir, 'extra.js'),
            }, base.generate_handler_sourcepath(toolchain, spec, {
                    'base!fun.file': 'fun.file',
                    'base!extra!fun.file': 'fun.file',
                    'base!missing!fun.file': 'fun.file',
                    'base!extra!missing!fun.file': 'fun.file',
                }),
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())
        self.assertIn("for loader plugin 'extra'", stream.getvalue())
        self.assertNotIn("for loader plugin 'missing'", stream.getvalue())

        # for the outer one
        self.assertIn(
            "loaderplugin 'base' from registry 'simloaders' cannot find "
            "sibling loaderplugin handler for 'missing'; processing may fail "
            "for the following nested/chained sources: "
            "{'missing!fun.file': 'fun.file'}", stream.getvalue()
        )
        # for the inner one
        self.assertIn(
            "loaderplugin 'extra' from registry 'simloaders' cannot find "
            "sibling loaderplugin handler for 'missing'; processing may fail "
            "for the following nested/chained sources: "
            "{'missing!fun.file': 'fun.file'}", stream.getvalue()
        )

        # for repeat loaders
        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual({
                'base': join(base_dir, 'base.js'),
                'extra': join(extra_dir, 'extra.js'),
            }, base.generate_handler_sourcepath(toolchain, spec, {
                    'base!extra!base!extra!fun.file': 'fun.file',
                }),
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())
        self.assertIn("for loader plugin 'extra'", stream.getvalue())

        # for repeat loaders
        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual({
                'base': join(base_dir, 'base.js'),
            }, base.generate_handler_sourcepath(toolchain, spec, {
                    'base!base!base!fun.file': 'fun.file',
                }),
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())

        # for argument loaders
        with pretty_logging(stream=StringIO()) as stream:
            self.assertEqual({
                'base': join(base_dir, 'base.js'),
                'extra': join(extra_dir, 'extra.js'),
            }, base.generate_handler_sourcepath(toolchain, spec, {
                    'base?argument!extra?argument!fun.file': 'fun.file',
                }),
            )
        self.assertIn("for loader plugin 'base'", stream.getvalue())
        self.assertIn("for loader plugin 'extra'", stream.getvalue())