Beispiel #1
0
class ModuleRegistryTestCase(unittest.TestCase):
    """
    Test the JavaScript module registry.
    """
    def setUp(self):
        self.registry = ModuleRegistry(__name__)

    def test_module_registry_empty(self):
        with self.assertRaises(StopIteration):
            next(self.registry.iter_records())

    def test_module_registry_standard(self):
        with pretty_logging(stream=mocks.StringIO()):
            self.registry.register_entry_points([
                EntryPoint.parse(
                    'calmjs.testing.module1 = calmjs.testing.module1')
            ])
        self.assertEqual(
            sorted(key for key, value in self.registry.iter_records()), [
                'calmjs.testing.module1',
            ])

        module1 = self.registry.get_record('calmjs.testing.module1')
        key = 'calmjs/testing/module1/hello'
        self.assertEqual(sorted(module1.keys()), [key])

    def test_module_registry_pythonic(self):
        registry = PythonicModuleRegistry(__name__)
        with pretty_logging(stream=mocks.StringIO()):
            registry.register_entry_points([
                EntryPoint.parse(
                    'calmjs.testing.module1 = calmjs.testing.module1')
            ])
        self.assertEqual(sorted(key for key, value in registry.iter_records()),
                         [
                             'calmjs.testing.module1',
                         ])

        module1 = registry.get_record('calmjs.testing.module1')
        key = 'calmjs.testing.module1.hello'
        self.assertEqual(sorted(module1.keys()), [key])
Beispiel #2
0
class ModuleRegistryTestCase(unittest.TestCase):
    """
    Test the JavaScript module registry.
    """

    def setUp(self):
        self.registry = ModuleRegistry(__name__)

    def test_module_registry_empty(self):
        with self.assertRaises(StopIteration):
            next(self.registry.iter_records())

    def test_module_registry_standard(self):
        with pretty_logging(stream=mocks.StringIO()):
            self.registry.register_entry_points([EntryPoint.parse(
                'calmjs.testing.module1 = calmjs.testing.module1')])
        self.assertEqual(sorted(
            key for key, value in self.registry.iter_records()
        ), [
            'calmjs.testing.module1',
        ])

        module1 = self.registry.get_record('calmjs.testing.module1')
        key = 'calmjs/testing/module1/hello'
        self.assertEqual(sorted(module1.keys()), [key])

    def test_module_registry_pythonic(self):
        registry = PythonicModuleRegistry(__name__)
        with pretty_logging(stream=mocks.StringIO()):
            registry.register_entry_points([EntryPoint.parse(
                'calmjs.testing.module1 = calmjs.testing.module1')])
        self.assertEqual(sorted(
            key for key, value in registry.iter_records()
        ), [
            'calmjs.testing.module1',
        ])

        module1 = registry.get_record('calmjs.testing.module1')
        key = 'calmjs.testing.module1.hello'
        self.assertEqual(sorted(module1.keys()), [key])
    def test_module_loader_registry_multiple_loaders(self):
        working_set = WorkingSet({
            'calmjs.module': [
                'module4 = calmjs.testing.module4',
            ],
            'calmjs.module.loader': [
                'css = css[style,css]',
                'json = json[json]',
                'empty = empty[]',
            ],
            __name__: [
                'calmjs.module = calmjs.module:ModuleRegistry',
                'calmjs.module.loader = '
                'calmjs.loaderplugin:ModuleLoaderRegistry',
            ]},
            # use a real distribution instead for this case
            dist=root_working_set.find(Requirement.parse('calmjs')),
        )

        registry = ModuleRegistry('calmjs.module', _working_set=working_set)
        loader_registry = ModuleLoaderRegistry(
            'calmjs.module.loader', _working_set=working_set, _parent=registry)
        self.assertEqual({
            'calmjs': ['calmjs.testing.module4'],
        }, loader_registry.package_module_map)

        self.assertEqual(
            ['css', 'empty', 'json'],
            sorted(loader_registry.get_loaders_for_package('calmjs'))
        )

        self.assertEqual([
            'css!calmjs/testing/module4/other.css',
            'css!calmjs/testing/module4/widget.style',
            'json!calmjs/testing/module4/data.json',
        ], sorted(loader_registry.get_records_for_package('calmjs').keys()))

        # was not registered to calmjs.testing
        self.assertEqual([], loader_registry.get_loaders_for_package(
            'calmjs.testing'))
        self.assertEqual({}, loader_registry.get_records_for_package(
            'calmjs.testing'))
Beispiel #4
0
    def test_module_loader_registry_multiple_loaders(self):
        working_set = WorkingSet(
            {
                'calmjs.module': [
                    'module4 = calmjs.testing.module4',
                ],
                'calmjs.module.webpackloader': [
                    'style!css = css[css]',
                    'json = json[json]',
                ],
                __name__: [
                    'calmjs.module = calmjs.module:ModuleRegistry',
                    'calmjs.module.webpackloader = '
                    'calmjs.webpack.loaderplugin:WebpackModuleLoaderRegistry',
                ]
            },
            # use a real distribution instead for this case
            dist=root_working_set.find(Requirement.parse('calmjs')),
        )

        registry = ModuleRegistry('calmjs.module', _working_set=working_set)
        loader_registry = WebpackModuleLoaderRegistry(
            'calmjs.module.webpackloader',
            _working_set=working_set,
            _parent=registry)
        self.assertEqual({
            'calmjs': ['calmjs.testing.module4'],
        }, loader_registry.package_module_map)

        self.assertEqual(
            ['json', 'style!css'],
            sorted(loader_registry.get_loaders_for_package('calmjs')))

        self.assertEqual([
            WebpackModuleLoaderRegistryKey(
                loader='json', modname='calmjs/testing/module4/data.json'),
            WebpackModuleLoaderRegistryKey(
                loader='style!css',
                modname='calmjs/testing/module4/other.css'),
        ], sorted(loader_registry.get_records_for_package('calmjs').keys()))
Beispiel #5
0
    def test_module_registry_dependencies_success(self):
        from calmjs.registry import _inst

        make_dummy_dist(self, (('requires.txt', '\n'.join([])), ), 'security',
                        '9999')

        make_dummy_dist(self, (('requires.txt', '\n'.join([
            'security',
        ])), ), 'framework', '2.4')

        make_dummy_dist(self, (('requires.txt', '\n'.join([
            'framework>=2.1',
        ])), ), 'widget', '1.1')

        make_dummy_dist(self, (('requires.txt', '\n'.join([
            'framework>=2.2',
            'widget>=1.0',
        ])), ), 'forms', '1.6')

        make_dummy_dist(self, (('requires.txt', '\n'.join([
            'framework>=2.1',
        ])), ), 'service', '1.1')

        make_dummy_dist(self, (('requires.txt', '\n'.join([
            'framework>=2.1',
            'widget>=1.1',
            'forms>=1.6',
            'service>=1.1',
        ])), ), 'site', '2.0')

        working_set = pkg_resources.WorkingSet([self._calmjs_testing_tmpdir])

        dummy_regid = 'calmjs.module.dummy.test'

        # ensure the dummy record we adding will be cleaned up.
        def cleanup():
            _inst.records.pop(dummy_regid, None)

        self.addCleanup(cleanup)

        # set up/register a dummy registry with dummy records.
        dummy_reg = _inst.records[dummy_regid] = ModuleRegistry(dummy_regid)
        dummy_reg.records = {
            'site': {
                'site/config': '/home/src/site/config.js',
            },
            'widget': {
                'widget/ui': '/home/src/widget/ui.js',
                'widget/widget': '/home/src/widget/widget.js',
            },
            'forms': {
                'forms/ui': '/home/src/forms/ui.js',
            },
            'service': {
                'service/lib': '/home/src/forms/lib.js',
            },
        }
        dummy_reg.package_module_map = {
            'site': ['site'],
            'widget': ['widget'],
            'forms': ['forms'],
            'service': ['service'],
        }

        site = calmjs_dist.flatten_module_registry_dependencies(
            ['site'], registry_name=dummy_regid, working_set=working_set)
        self.assertEqual(
            site, {
                'site/config': '/home/src/site/config.js',
                'widget/ui': '/home/src/widget/ui.js',
                'widget/widget': '/home/src/widget/widget.js',
                'service/lib': '/home/src/forms/lib.js',
                'forms/ui': '/home/src/forms/ui.js',
            })

        self.assertEqual(
            calmjs_dist.flatten_parents_module_registry_dependencies(
                ['site'], registry_name=dummy_regid, working_set=working_set),
            {
                'widget/ui': '/home/src/widget/ui.js',
                'widget/widget': '/home/src/widget/widget.js',
                'service/lib': '/home/src/forms/lib.js',
                'forms/ui': '/home/src/forms/ui.js',
            })

        service = calmjs_dist.flatten_module_registry_dependencies(
            ['service'], registry_name=dummy_regid, working_set=working_set)
        self.assertEqual(service, {
            'service/lib': '/home/src/forms/lib.js',
        })

        forms = calmjs_dist.flatten_module_registry_dependencies(
            ['forms'], registry_name=dummy_regid, working_set=working_set)
        self.assertEqual(
            forms, {
                'forms/ui': '/home/src/forms/ui.js',
                'widget/ui': '/home/src/widget/ui.js',
                'widget/widget': '/home/src/widget/widget.js',
            })

        # merger
        merged = calmjs_dist.flatten_module_registry_dependencies(
            ['forms', 'service'],
            registry_name=dummy_regid,
            working_set=working_set)
        self.assertEqual(
            merged, {
                'forms/ui': '/home/src/forms/ui.js',
                'widget/ui': '/home/src/widget/ui.js',
                'widget/widget': '/home/src/widget/widget.js',
                'service/lib': '/home/src/forms/lib.js',
            })

        self.assertEqual(
            calmjs_dist.flatten_parents_module_registry_dependencies(
                ['forms', 'service', 'app'],
                registry_name=dummy_regid,
                working_set=working_set), {
                    'widget/ui': '/home/src/widget/ui.js',
                    'widget/widget': '/home/src/widget/widget.js',
                })

        # no declared exports/registry entries in security.
        security = calmjs_dist.flatten_module_registry_dependencies(
            ['security'], registry_name=dummy_regid, working_set=working_set)
        self.assertEqual(security, {})

        # package not even in working set
        missing_pkg = calmjs_dist.flatten_module_registry_dependencies(
            ['missing_pkg'],
            registry_name=dummy_regid,
            working_set=working_set)
        self.assertEqual(missing_pkg, {})

        # singlular methods
        self.assertEqual(
            calmjs_dist.get_module_registry_dependencies(
                ['site'], registry_name=dummy_regid, working_set=working_set),
            {'site/config': '/home/src/site/config.js'})

        self.assertEqual(
            calmjs_dist.get_module_registry_dependencies(
                ['security'],
                registry_name=dummy_regid,
                working_set=working_set), {})

        self.assertEqual(
            calmjs_dist.get_module_registry_dependencies(
                ['missing'],
                registry_name=dummy_regid,
                working_set=working_set), {})
Beispiel #6
0
 def setUp(self):
     self.registry = ModuleRegistry(__name__)
Beispiel #7
0
def generate_integration_environment(working_dir,
                                     registry_id='calmjs.module.simulated',
                                     pkgman_filename='package.json',
                                     extras_calmjs_key='fake_modules'):
    """
    Generate a comprehensive integration testing environment for test
    cases in other packages that integrates with calmjs.

    Arguments:

    working_dir
        The working directory to write all the distribution information
        and dummy test scripts to.

    registry_id
        The registry id to be used for the dummy module registry.
        Default is 'calmjs.module.simulated'

    pkgman_filename
        The package manager's expected filename.  Defaults to the npm
        default of 'package.json'.

    extras_calmjs_key
        The extras keys for the extras_calmjs definition.  Defaults to
        fake_modules.

    Returns a tuple of the mock working set and the registry.
    """

    from calmjs.module import ModuleRegistry
    from calmjs.dist import EXTRAS_CALMJS_JSON

    def make_entry_points(registry_id, *raw):
        return '\n'.join(['[%s]' % registry_id] + list(raw))

    make_dummy_dist(None, (('entry_points.txt',
                            make_entry_points(
                                'calmjs.extras_keys',
                                '%s = enabled' % extras_calmjs_key,
                            )), ),
                    'calmjs.simulated',
                    '420',
                    working_dir=working_dir)

    make_dummy_dist(None, (('requires.txt', '\n'.join([])), ),
                    'security',
                    '9999',
                    working_dir=working_dir)

    make_dummy_dist(None, (
        ('requires.txt', '\n'.join([
            'security',
        ])),
        (pkgman_filename,
         json.dumps({
             'dependencies': {
                 'left-pad': '~1.1.1',
             },
             'devDependencies': {
                 'sinon': '~1.15.0',
             },
         })),
        ('entry_points.txt',
         make_entry_points(
             registry_id,
             'framework = framework',
         )),
        (EXTRAS_CALMJS_JSON,
         json.dumps({
             extras_calmjs_key: {
                 'jquery': 'jquery/dist/jquery.min.js',
                 'underscore': 'underscore/underscore-min.js',
             },
         })),
    ),
                    'framework',
                    '2.4',
                    working_dir=working_dir)

    make_dummy_dist(None, (
        ('requires.txt', '\n'.join([
            'framework>=2.1',
        ])),
        (pkgman_filename,
         json.dumps({
             'dependencies': {
                 'jquery': '~2.0.0',
                 'underscore': '~1.7.0',
             },
         })),
        (EXTRAS_CALMJS_JSON,
         json.dumps({
             extras_calmjs_key: {
                 'jquery': 'jquery/dist/jquery.min.js',
             },
         })),
        ('entry_points.txt', make_entry_points(
            registry_id,
            'widget = widget',
        )),
    ),
                    'widget',
                    '1.1',
                    working_dir=working_dir)

    make_dummy_dist(None, (
        ('requires.txt', '\n'.join([
            'framework>=2.2',
            'widget>=1.0',
        ])),
        (pkgman_filename,
         json.dumps({
             'dependencies': {
                 'backbone': '~1.3.0',
                 'jquery-ui': '~1.12.0',
             },
         })),
        ('entry_points.txt', make_entry_points(
            registry_id,
            'forms = forms',
        )),
    ),
                    'forms',
                    '1.6',
                    working_dir=working_dir)

    make_dummy_dist(None, (
        ('requires.txt', '\n'.join([
            'framework>=2.1',
        ])),
        (pkgman_filename,
         json.dumps({
             'dependencies': {
                 'underscore': '~1.8.0',
             },
             'devDependencies': {
                 'sinon': '~1.17.0',
             },
         })),
        (EXTRAS_CALMJS_JSON,
         json.dumps({
             extras_calmjs_key: {
                 'underscore': 'underscore/underscore.js',
             },
             '_bad_dir_': {
                 'unsupported': 'unsupported',
             },
         })),
        ('entry_points.txt',
         make_entry_points(
             registry_id,
             'service = service',
             'service.rpc = service.rpc',
         )),
    ),
                    'service',
                    '1.1',
                    working_dir=working_dir)

    make_dummy_dist(None, (
        ('requires.txt', '\n'.join([
            'framework>=2.1',
            'widget>=1.1',
            'forms>=1.6',
        ])),
        (pkgman_filename,
         json.dumps({
             'name': 'site',
             'dependencies': {
                 'underscore': '~1.8.0',
                 'jquery': '~3.0.0',
             },
         })),
        (EXTRAS_CALMJS_JSON,
         json.dumps({
             extras_calmjs_key: {
                 'jquery': 'jquery/dist/jquery.js',
                 'underscore': 'underscore/underscore.js',
             },
         })),
    ),
                    'site',
                    '2.0',
                    working_dir=working_dir)

    # The mocked mock_working_set
    mock_working_set = WorkingSet([working_dir])

    contents = (
        (('framework', 'lib.js'), '''
            exports.Core = 'framework.lib.Core';
        '''),
        (('widget', 'core.js'), '''
            var framework_lib = require('framework/lib');
            var Core = framework_lib.Core;
            exports.Core = Core + '/' + 'widget.core.Core';
        '''),
        (('widget', 'richedit.js'), '''
            var core = require('widget/core');
            exports.RichEditWidget = 'widget.richedit.RichEditWidget';
        '''),
        (('widget', 'datepicker.js'), '''
            var _ = require('underscore');
            var core = require('widget/core');
            exports.DatePickerWidget = 'widget.datepicker.DatePickerWidget';
        '''),
        (('forms', 'ui.js'), '''
            var $ = require('jquery');
            var richedit = require('widget/richedit');
            var datepicker = require('widget/datepicker');
            exports.RichForm = [
                'forms.ui.RichForm',
                richedit.RichEditWidget,
                datepicker.DatePickerWidget,
            ];
        '''),
        (('service', 'endpoint.js'), '''
            var framework_lib = require('framework/lib');
            var Core = framework_lib.Core;
            exports.Endpoint = 'service.endpoint.Endpoint';
        '''),
        (('service', 'rpc', 'lib.js'), '''
            var framework_lib = require('framework/lib');
            var Core = framework_lib.Core;
            exports.Library = 'service.rpc.lib.Library';
        '''),
    )

    records = {}
    package_module_map = {}

    # I kind of want to do something like
    # registry = ModuleRegistry(registry_id, _working_set=mock_working_set)
    # However, this requires actually stubbing out a bunch of other
    # stuff and I really don't want to muck about with imports for a
    # setup... so we are going to mock the registry like so:

    for ep in mock_working_set.iter_entry_points(registry_id):
        package_module_map[ep.dist.project_name] = package_module_map.get(
            ep.dist.project_name, [])
        package_module_map[ep.dist.project_name].append(ep.module_name)

    for fn, content in contents:
        target = join(working_dir, *fn)
        modname = '/'.join(fn)[:-3]
        record_key = '.'.join(fn[:-1])
        records[record_key] = records.get(record_key, {})
        records[record_key][modname] = target
        base = dirname(target)
        if not isdir(base):
            makedirs(base)
        with open(target, 'w') as fd:
            fd.write(textwrap.dedent(content).lstrip())

    extras_sources = [
        'jquery/dist/jquery.js',
        'jquery/dist/jquery.min.js',
        'underscore/underscore.js',
        'underscore/underscore-min.js',
    ]

    # Generate the extras, too
    for source in extras_sources:
        fn = source.split('/')
        target = join(working_dir, extras_calmjs_key, *fn)
        base = dirname(target)
        if not isdir(base):
            makedirs(base)
        with open(target, 'w') as fd:
            # return a module that returns the name of the file.
            fd.write("define([], function () { return '%s' });" % source)

    makedirs(join(working_dir, '_bad_dir_'))
    with open(join(working_dir, '_bad_dir_', 'unsupported'), 'w') as fd:
        pass

    # Now create and assign the registry with our things
    registry = ModuleRegistry(registry_id)
    registry.records = records
    registry.package_module_map = package_module_map

    # Return dummy working set (for dist resolution) and the registry
    return mock_working_set, registry
Beispiel #8
0
 def setUp(self):
     self.registry = ModuleRegistry(__name__)