Esempio n. 1
0
 def create(self, modulename, module_source):
     if modulename not in self.recorder.metadata:
         pragmas = PragmaFinder().find_pragmas(module_source)
         self.recorder.metadata[modulename] = (
             MetadataGatheringVisitor.analyze(self.config, modulename,
                                              module_source, pragmas))
     return CoverageAnnotator(self.config, modulename, self.recorder)
Esempio n. 2
0
    def test_load_package(self):
        from instrumental.instrument import AnnotatorFactory
        from instrumental.metadata import MetadataGatheringVisitor
        from instrumental.monkey import load_module_factory
        from instrumental.pragmas import PragmaFinder
        from instrumental.recorder import ExecutionRecorder

        import instrumental.test.samples
        samples_directory = os.path.dirname(instrumental.test.samples.__file__)
        simple_name = 'instrumental.test.samples.package'
        simple_path = os.path.join(samples_directory, 'package')

        source = open(os.path.join(simple_path, '__init__.py'), "r").read()
        pragmas = PragmaFinder().find_pragmas(source)
        metadata = MetadataGatheringVisitor.analyze(self.config, simple_name,
                                                    source, pragmas)

        recorder = ExecutionRecorder.get()
        recorder.add_metadata(metadata)
        visitor_factory = AnnotatorFactory(self.config, recorder)
        load_module = load_module_factory(
            ['instrumental.test.samples.package'], [], visitor_factory)

        load_module(simple_name, None, simple_path,
                    (None, None, imp.PKG_DIRECTORY))

        assert simple_name in sys.modules
Esempio n. 3
0
    def test_Module_with_docstring_and_future_import(self):
        from instrumental.instrument import CoverageAnnotator
        from instrumental.metadata import MetadataGatheringVisitor
        from instrumental.pragmas import PragmaFinder

        source = """'a docstring am I'
from __future__ import with_statement
if u:
    return u
else:
    return 'else'
"""
        module = ast.parse(source)
        pragmas = PragmaFinder().find_pragmas(source)
        config = DummyConfig()
        metadata = MetadataGatheringVisitor.analyze(config, 'somemodule',
                                                    source, pragmas)
        self.recorder.add_metadata(metadata)
        transformer = CoverageAnnotator(config, 'somemodule', self.recorder)
        inst_module = transformer.visit(module)

        assert isinstance(inst_module.body[1], ast.ImportFrom)
        assert module.body[1].module == '__future__'
        assert isinstance(module.body[1].names[0], ast.alias)
        assert module.body[1].names[0].name == 'with_statement'
        self._assert_recorder_setup(inst_module, 2)
Esempio n. 4
0
 def _instrument_module(self, module_func):
     from instrumental.metadata import MetadataGatheringVisitor
     from instrumental.pragmas import PragmaFinder
     module, source = load_module(module_func)
     pragmas = PragmaFinder().find_pragmas(source)
     metadata = MetadataGatheringVisitor.analyze(self.config,
                                                 module_func.__name__,
                                                 source, pragmas)
     self.recorder.add_metadata(metadata)
     transformer = CoverageAnnotator(self.config, module_func.__name__,
                                     self.recorder)
     inst_module = transformer.visit(module)
     # print renderer.render(inst_module)
     return inst_module
Esempio n. 5
0
 def _load_and_compile_module(self, module_func):
     module, source = load_module(module_func)
     from instrumental.pragmas import PragmaFinder
     pragmas = PragmaFinder().find_pragmas(source)
     from instrumental.metadata import MetadataGatheringVisitor
     config = DummyConfig()
     self.recorder.add_metadata(
         MetadataGatheringVisitor.analyze(config, module_func.__name__,
                                          source, pragmas))
     # self.recorder.add_source(module_func.__name__, source)
     transformer = CoverageAnnotator(config, module_func.__name__,
                                     self.recorder)
     inst_module = transformer.visit(module)
     sys.stdout.write(renderer.render(inst_module) + "\n")
     code = compile(inst_module, '<string>', 'exec')
     return code
 def _makeOne(self):
     from instrumental.metadata import MetadataGatheringVisitor
     metadata = MetadataGatheringVisitor.analyze(DummyConfig(),
                                                 self.modulename,
                                                 self.source, {6: set()})
     return metadata.constructs[self.label]