Exemple #1
0
    def test_artifact_test_simulation(self):
        # don't actually run karma, since we are not setting up the full
        # integration environment for this isolated test - also keep the
        # spec reference here and have the helper return it so the
        # simplified verification can be done.
        spec = Spec(karma_advice_group=None)

        def generic_tester(package_names, export_target):
            spec['export_target'] = export_target
            return KarmaToolchain(), spec,

        tester_mod = ModuleType('calmjs_dev_tester')
        tester_mod.generic = generic_tester

        self.addCleanup(sys.modules.pop, 'calmjs_dev_tester')
        sys.modules['calmjs_dev_tester'] = tester_mod

        working_dir = utils.mkdtemp(self)

        utils.make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                '[calmjs.artifacts.tests]',
                'artifact.js = calmjs_dev_tester:generic',
            ])),
        ), 'app', '1.0', working_dir=working_dir)

        mock_ws = WorkingSet([working_dir])
        utils.stub_item_attr_value(self, dist, 'default_working_set', mock_ws)
        registry = ArtifactTestRegistry(
            'calmjs.artifacts.tests', _working_set=mock_ws)

        artifact_name = registry.get_artifact_filename('app', 'artifact.js')

        with self.assertRaises(ToolchainCancel) as e:
            # file not exist yet will cancel the execution
            registry.prepare_export_location(artifact_name)

        self.assertIn("missing export_target '", str(e.exception))
        self.assertIn("artifact.js'", str(e.exception))

        mkdir(dirname(artifact_name))
        with open(artifact_name, 'w') as fd:
            fd.write('console.log("test artifact");\n')

        # no longer raise an exception
        registry.prepare_export_location(artifact_name)

        self.assertNotIn('before_prepare', spec._advices)
        registry.process_package('app')
        # cheat a bit by probing some private bits to see that the
        # relevant advice is planted but not executed
        self.assertEqual(1, len(spec._advices['before_prepare']))
        # for whatever reason, instance methods are not identities of
        # itself thus `is` cannot be used as the validation operator.
        self.assertEqual(
            spec._advices['before_prepare'][0][0],
            registry.prepare_export_location,
        )
Exemple #2
0
    def setup_karma_artifact_runtime(self):

        def export_target_only(package_names, export_target):
            spec = Spec(
                export_target=export_target,
                test_package_names=package_names,
                # typically, the toolchain test advice will be advised
                # to the spec which will then set these up.
                calmjs_test_registry_names=['calmjs.dev.module.tests'],
            )
            return KarmaToolchain(), spec,

        def generic_tester(package_names, export_target):
            toolchain, spec = export_target_only(package_names, export_target)
            spec['artifact_paths'] = [export_target]
            return toolchain, spec

        tester_mod = ModuleType('calmjs_dev_tester')
        tester_mod.generic = generic_tester
        tester_mod.export = export_target_only

        self.addCleanup(sys.modules.pop, 'calmjs_dev_tester')
        self.addCleanup(
            root_registry.records.pop, 'calmjs.dev.module.tests', None)
        sys.modules['calmjs_dev_tester'] = tester_mod

        working_dir = mkdtemp(self)

        make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                '[calmjs.artifacts.tests]',
                'artifact.js = calmjs_dev_tester:generic',
                'export_target.js = calmjs_dev_tester:export',
            ])),
        ), 'calmjs.dev', '1.0', working_dir=working_dir)

        make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                '[calmjs.artifacts.tests]',
                'missing.js = calmjs_dev_tester:generic',
            ])),
        ), 'missing', '1.0', working_dir=working_dir)

        make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                # this won't actually generate an artifact, but that is
                # not what is being tested.
                '[calmjs.artifacts]',
                'testless.js = calmjs_dev_tester:generic',
            ])),
        ), 'testless', '1.0', working_dir=working_dir)

        make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                # the tester is missing.
                '[calmjs.artifacts.tests]',
                'artifact.js = not_installed:tester',
            ])),
        ), 'depsmissing', '1.0', working_dir=working_dir)

        make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
            ])),
        ), 'nothing', '1.0', working_dir=working_dir)

        # simply inject the "artifact" for this package into the
        # registry
        mock_ws = WorkingSet([working_dir])
        registry = ArtifactTestRegistry(
            'calmjs.artifacts.tests', _working_set=mock_ws)

        # produce the "artifact" by simply the local main.js
        main_js = resource_filename('calmjs.dev', 'main.js')
        artifact_target = registry.get_artifact_filename(
            'calmjs.dev', 'artifact.js')
        export_target = registry.get_artifact_filename(
            'calmjs.dev', 'export_target.js')
        os.mkdir(dirname(artifact_target))
        with open(main_js) as reader:
            with open(artifact_target, 'w') as writer:
                writer.write(reader.read())
            reader.seek(0)
            with open(export_target, 'w') as writer:
                writer.write(reader.read())

        # assign this dummy registry to the root records with cleanup
        self.addCleanup(root_registry.records.pop, 'calmjs.artifacts.tests')
        root_registry.records['calmjs.artifacts.tests'] = registry

        # include the artifact registry, too.
        self.addCleanup(root_registry.records.pop, 'calmjs.artifacts')
        artifact_registry = ArtifactRegistry(
            'calmjs.artifacts', _working_set=mock_ws)
        root_registry.records['calmjs.artifacts'] = artifact_registry

        # use the verify artifact runtime directly
        return KarmaArtifactRuntime()