Пример #1
0
    def test_python_module_reload(self):
        search_path = abspath(join(FILES_DIR, 'imports'))
        pypath = abspath(join(search_path, 'A', 'a.py'))
        assert exists(pypath)

        pycpath = abspath(join(search_path, 'A', 'a.pyc'))
        pyopath = abspath(join(search_path, 'A', 'a.pyo'))
        if not exists(pycpath) or not exists(pyopath):
            compile_python_source(pypath)
            compile_python_source(pypath, optimization=True)
        assert exists(pycpath) and exists(pyopath)

        m = read_module_code(pypath, search_path=[search_path])
        old_imports = m.context['imports'][:]
        old_classdefs = m.context['classdefs'][:]

        for f in [pypath, pycpath, pyopath]:
            # ugly ...
            del m.context['imports'][:]
            del m.context['classdefs'][:]
            m.filename = f

            co = m.reload()
            self.assertNotNone(co)
            self.assertEqual(pypath, co.co_filename)
            self.assertEqual(old_imports, m.context['imports'])
            self.assertEqual(old_classdefs, m.context['classdefs'])
Пример #2
0
    def test_init(self):
        filepath = join(FILES_DIR, 'python', 'a.py')
        code = read_module_code(filepath, search_path=[FILES_DIR])

        self.assertNotNone(code)
        descriptor = ModuleDescriptor(code)

        self.assertEqual('python.a', descriptor.name)
        self.assertEqual(filepath, descriptor.filename)
Пример #3
0
    def read_module_code(self, modulename):
        items = [FILES_DIR] + list(modulename.split('.'))
        items[-1] += '.py'
        filepath = join(*items)
        module = read_module_code(filepath, search_path=[FILES_DIR])

        self.assertNotNone(module)
        self.assertEqual(modulename, module.name)
        self.assertEqual(filepath, module.filename)
        return module
Пример #4
0
    def test_autotest_plugin(self):
        monitor = Monitor(__file__)
        descriptor = ModuleDescriptor(read_module_code(__file__))
        event = Event(Event.MODULE_MODIFIED, descriptor)
        plugin = FakeAutotest(event, monitor, {});

        self.assertEqual(descriptor, plugin.descriptor)
        self.assertTrue(callable(plugin))

        plugin()
        self.assertEqual([descriptor], plugin.testables)
        self.assertEqual(['--loglevel', LOGGER.getEffectiveLevel()], plugin.extra_arguments)
Пример #5
0
    def refresh(self):
        assert isinstance(self.paths, (tuple, list))
        assert isinstance(self.__descriptors, dict)
        assert isinstance(self.__filenames, dict)
        assert isinstance(self.__failures, set)

        # localize variable access to minimize overhead
        # and to reduce the visual noise.
        descriptors = self.__descriptors
        filenames = self.__filenames
        failures = self.__failures

        # ``monitor()`` updates all entries and
        # removes deleted entries.
        for modified in self.monitor():
            yield modified

        # For now, only need to check new entries.
        resolver = ModuleNameResolver(self.search_path)
        newcomers = []
        for filename, typebits in collect_python_module_file(self.paths):
            if filename in filenames or filename in failures:
                continue
            try:
                mc = read_module_code(filename, typebits=typebits,
                        search_path=self.search_path,
                        resolver=resolver,
                        allow_compilation_failure=True,
                        allow_standalone=True)
            except ImportError:
                LOGGER.debug("Couldn't import file", exc_info=True)
                failures.add(filename)
                continue
            else:
                desc = ModuleDescriptor(mc)
                self.add(desc)
                # modifieds += new entries
                newcomers.append(desc)
                LOGGER.debug("Added: %s" % desc.describe())

        if newcomers:
            # Since there are some entries already refer new entry,
            # we need to update dependencies of all entries
            for desc in descriptors.itervalues():
                desc.update_dependencies(descriptors)

            # Notify caller what entries are appended
            for desc in newcomers:
                yield Event(Event.MODULE_CREATED, desc)