コード例 #1
0
    def test_registry_dynamic_template_addition(self):
        module_map = {'tmp': mkdtemp_singleton(self)}
        stub_mod_mock_resources_filename(self, pkg_resources, module_map)
        (working_set, main_template,
         sub_template) = setup_tmp_mold_templates(self)
        registry = MoldRegistry.create(_working_set=working_set,
                                       auto_reload=True)

        path = registry.lookup_path('tmp/mold/template.nja')
        self.assertEqual(main_template, path)

        tmpdir = mkdtemp_singleton(self)
        mold_root = join(tmpdir, 'tmp', 'root')
        mold_base = join(mold_root, 'new_mold')
        mkdir(mold_base)
        path = registry.lookup_path('tmp/new_mold/template.nja')
        new_tmpl = join(mold_base, 'template.nja')
        self.assertEqual(path, new_tmpl)

        with self.assertRaises(OSError):
            registry.verify_path('tmp/new_mold/template.nja')

        with open(new_tmpl, 'w') as fd:
            fd.write('<div>New Template</div>')

        self.assertEqual(registry.verify_path('tmp/new_mold/template.nja'),
                         new_tmpl)
コード例 #2
0
    def test_registry_load_entry_point_missing_attrs(self):
        working_set = mocks.WorkingSet(
            {
                'nunja.mold': [
                    'nunja.testing.mold1 = nunja.testing',
                    'nunja.testing.mold2 = nunja:testing.mold',
                ]
            },
            dist=Distribution(project_name='nunjatesting', version='0.0'))

        with pretty_logging(logger='nunja', stream=mocks.StringIO()) as stream:
            registry = MoldRegistry('nunja.mold', _working_set=working_set)

        msg = stream.getvalue()
        self.assertIn(
            "entry_point 'nunja.testing.mold1 = nunja.testing' "
            "from package 'nunjatesting 0.0' incompatible ",
            msg,
        )
        self.assertIn(
            "entry_point 'nunja.testing.mold2 = nunja:testing.mold' "
            "from package 'nunjatesting 0.0' incompatible ",
            msg,
        )

        records = registry.get_records_for_package('nunjatesting')
        self.assertEqual(records, {})
コード例 #3
0
    def test_registry_entry_point_to_path(self):
        module_map = {'tmp': mkdtemp_singleton(self)}
        stub_mod_mock_resources_filename(self, pkg_resources, module_map)

        (working_set, main_template,
         sub_template) = setup_tmp_mold_templates(self)
        registry = MoldRegistry.create(_working_set=working_set,
                                       auto_reload=True)

        # poking internals to test internals
        tmpdir = mkdtemp_singleton(self)
        tmp_entry_point = registry.tracked_entry_points['tmp']
        self.assertEqual(registry._entry_point_to_path(tmp_entry_point),
                         join(tmpdir, 'tmp', 'root'))

        # Using the API quickly
        self.assertEqual(registry.mold_id_to_path('tmp/mold'),
                         join(tmpdir, 'tmp', 'root', 'mold'))
        self.assertEqual(registry.mold_id_to_path('tmp/new_mold'),
                         join(tmpdir, 'tmp', 'root', 'new_mold'))

        with self.assertRaises(KeyError):
            registry.mold_id_to_path('no_such_entry/point')

        with self.assertRaises(KeyError):
            registry.mold_id_to_path('mold_id_not_like_this')
コード例 #4
0
ファイル: mocks.py プロジェクト: metatoaster/nunja
def setup_tmp_mold_templates_registry(testcase_inst):
    from nunja.registry import MoldRegistry

    (working_set, main_template,
     sub_template) = setup_tmp_mold_templates(testcase_inst)

    registry = MoldRegistry('nunja.mold', _working_set=working_set)
    return registry, main_template, sub_template
コード例 #5
0
    def mk_test_registry(self, entry_points=None):
        if entry_points is None:
            entry_points = ['nunja.testing.mold = nunja.testing:mold']

        working_set = mocks.WorkingSet({'nunja.mold': entry_points},
                                       dist=Distribution(
                                           project_name='nunjatesting',
                                           version='0.0'))
        return MoldRegistry.create(_working_set=working_set)
コード例 #6
0
    def test_registry_load_working_set(self):
        # do note these mocking sets are for the registry; actual
        # filenames is not overridden (uses pkg_resources directly)
        working_set = mocks.WorkingSet(
            {'nunja.mold': [
                'nunja.testing.molds = nunja.testing:mold',
            ]},
            dist=Distribution(project_name='nunjatesting', version='0.0'))

        with pretty_logging(logger='nunja', stream=mocks.StringIO()) as stream:
            registry = MoldRegistry('nunja.mold', _working_set=working_set)

        records = registry.get_records_for_package('nunjatesting')
        keys = [
            'nunja.testing.molds/include_by_name/index',
            'nunja.testing.molds/include_by_value/index',
            'nunja.testing.molds/itemlist/index',
            'nunja.testing.molds/noinit/index',
            'nunja.testing.molds/problem/index',
            'text!nunja.testing.molds/basic/template.nja',
            'text!nunja.testing.molds/include_by_name/empty.nja',
            'text!nunja.testing.molds/include_by_name/template.nja',
            'text!nunja.testing.molds/include_by_value/template.nja',
            'text!nunja.testing.molds/itemlist/template.nja',
            'text!nunja.testing.molds/noinit/template.nja',
            'text!nunja.testing.molds/problem/template.nja',
        ]

        self.assertEqual(sorted(records.keys()), keys)

        self.assertIn('7 templates', stream.getvalue())
        self.assertIn('5 scripts', stream.getvalue())
        self.assertIn('generated 6 molds', stream.getvalue())

        # select directly by mold_id through get_record
        self.assertEqual(
            sorted(registry.get_record('nunja.testing.molds/basic').keys()),
            ['text!nunja.testing.molds/basic/template.nja'],
        )
        self.assertEqual(
            sorted(registry.get_record('nunja.testing.molds/itemlist').keys()),
            [
                'nunja.testing.molds/itemlist/index',
                'text!nunja.testing.molds/itemlist/template.nja',
            ],
        )
        self.assertEqual(
            sorted(
                registry.get_record(
                    'nunja.testing.molds/include_by_name').keys()),
            [
                'nunja.testing.molds/include_by_name/index',
                'text!nunja.testing.molds/include_by_name/empty.nja',
                'text!nunja.testing.molds/include_by_name/template.nja',
            ],
        )
コード例 #7
0
    def test_registry_autoreload_base_support(self):
        setup_tmp_module(self)

        entry_points = ['tmp = tmp:mold']

        working_set = mocks.WorkingSet({'nunja.mold': entry_points},
                                       dist=Distribution(
                                           project_name='nunjatesting',
                                           version='0.0'))

        registry = MoldRegistry.create(_working_set=working_set,
                                       auto_reload=False)
        self.assertFalse(registry.tracked_entry_points)
        with self.assertRaises(KeyError):
            registry.mold_id_to_path('tmp/new_mold')

        registry = MoldRegistry.create(_working_set=working_set,
                                       auto_reload=True)
        self.assertEqual(str(registry.tracked_entry_points['tmp']),
                         entry_points[0])
コード例 #8
0
ファイル: mocks.py プロジェクト: metatoaster/nunja
def setup_testing_mold_templates_registry(testcase_inst):
    from nunja.registry import MoldRegistry

    # first create a dummy of this package; we need the actual
    # version number
    make_dummy_dist(testcase_inst, (
        (
            'namespace_packages.txt',
            'nunja\n',
        ),
        (
            'entry_points.txt',
            '[nunja.mold]\n'
            '_core_ = nunja:_core_\n',
        ),
    ), 'nunja',
                    pkg_resources.get_distribution('nunja').version)

    # then make the one for the testing molds
    make_dummy_dist(testcase_inst, (
        (
            'namespace_packages.txt',
            'nunja\n',
        ),
        (
            'entry_points.txt',
            '[nunja.mold]\n'
            'nunja.testing.mold = nunja.testing:mold',
        ),
    ), 'nunja.testing', '0.0.0.dummy')

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

    # Ensure that both the "stubbed" modules can be found
    stub_mod_mock_resources_filename(
        testcase_inst, pkg_resources, {
            'nunja': resource_filename(Requirement.parse('nunja'), ''),
            'nunja.testing': resource_filename(Requirement.parse('nunja'), ''),
        })

    return MoldRegistry('nunja.mold', _working_set=working_set)
コード例 #9
0
    def test_no_valid_file(self):
        working_set = mocks.WorkingSet(
            {
                'nunja.mold': [
                    'nunja.testing.badmold = nunja.testing:badmold',
                ]
            },
            dist=Distribution(project_name='nunjatesting', version='0.0'))

        with pretty_logging(logger='nunja', stream=mocks.StringIO()) as stream:
            registry = MoldRegistry('nunja.mold', _working_set=working_set)

        records = registry.get_records_for_package('nunjatesting')

        self.assertEqual(
            sorted(records.keys()),
            ['text!nunja.testing.badmold/nomold/empty.nja'],
        )

        self.assertIn('1 templates', stream.getvalue())
        self.assertIn('0 scripts', stream.getvalue())
        self.assertIn('generated 0 molds', stream.getvalue())