Exemple #1
0
    def _addTestsToSuite(self, tests=()):
        """Callback called by the TestLoader as it finds sets of tests.
        """
        if not tests:
            return

        trace.write('Got %d tests' % len(tests), outputLevel=self.TRACE_LEVEL)

        for test in tests:
            #
            # Why are there null tests?
            #
            if not test:
                continue

            try:
                categories = test.PROCTOR_TEST_CATEGORIES
            except AttributeError:
                self._getTestSuite('Unspecified').addTest(test)
            else:
                for category in categories:
                    test_suite = self._getTestSuite(category)
                    test_suite.addTest(test)

            self._getTestSuite('All').addTest(test)
        #
        # Return a bogus suite so the TestLoader works
        # right.  This is all thrown away when the scan
        # is completed.
        #
        return unittest.TestSuite()
Exemple #2
0
 def _buildScanner(self):
     trace.into('ProctorGUI', '_buildScanner')
     scan_directory = os.path.join(self.scan_directory_parent,
                                   self.scan_directory.get())
     trace.write('variable: %s' % scan_directory)
     self.scanner = ModuleScanner()
     self.showMessage('busy', 'Scanning...')
     self.busyStart()
     self.scanner.scan(scan_directory)
     self.busyEnd()
     self.showMessage('busy', '')
     trace.outof()
     return
Exemple #3
0
    def _ignoreModule(self, module):
        trace.into('ModuleTree', '_ignoreModule', outputLevel=self.TRACE_LEVEL)

        if not module:
            trace.write('No module')
            return trace.outof(True, outputLevel=self.TRACE_LEVEL)

        if hasattr(module, '__proctor_ignore_module__'
                   ) and module.__proctor_ignore_module__:
            trace.write('Found ignore flag')
            return trace.outof(True, outputLevel=self.TRACE_LEVEL)

        return trace.outof(False, outputLevel=self.TRACE_LEVEL)
Exemple #4
0
 def _loadTests(self):
     trace.into('ModuleTree', '_loadTests', outputLevel=self.TRACE_LEVEL)
     self.test_suites = {}
     if self._ignoreModule(self.module):
         trace.write('No module', outputLevel=self.TRACE_LEVEL)
         self.test_loader = None
     else:
         trace.write('Loading tests', outputLevel=self.TRACE_LEVEL)
         self.test_loader = unittest.TestLoader()
         self.test_loader.suiteClass = self._addTestsToSuite
         self.test_loader.loadTestsFromModule(self.module)
         #trace.writeVar(self_test_suite=self.test_suite,
         #               outputLevel=self.TRACE_LEVEL)
     trace.outof(outputLevel=self.TRACE_LEVEL)
     return
Exemple #5
0
 def __setitem__(self, key, value):
     trace.into('ModuleTree',
                '__setitem__',
                key=key,
                value=value,
                outputLevel=self.TRACE_LEVEL)
     key_parts = self._getKeyParts(key)
     trace.writeVar(key_parts=key_parts, outputLevel=self.TRACE_LEVEL)
     #
     # Separate the part of the key that is the name of the
     # value from the path to the key.
     #
     value_name = key_parts[-1]
     key_parts = key_parts[:-1]
     trace.writeVar(value_name=value_name,
                    key_parts=key_parts,
                    outputLevel=self.TRACE_LEVEL)
     #
     # Create intermediate nodes, if necessary.
     #
     child_node = self
     for node_name in key_parts:
         trace.write('handling node name %s' % node_name,
                     outputLevel=self.TRACE_LEVEL)
         try:
             trace.write('looking for %s' % node_name,
                         outputLevel=self.TRACE_LEVEL)
             child_node = child_node[node_name]
             trace.write('got %s' % node_name, outputLevel=self.TRACE_LEVEL)
         except KeyError:
             trace.write('creating child node',
                         outputLevel=self.TRACE_LEVEL)
             new_child_node = child_node.newNode(name=node_name)
             child_node.data[node_name] = new_child_node
             child_node = new_child_node
             trace.write('created %s' % node_name,
                         outputLevel=self.TRACE_LEVEL)
     child_node.data[value_name] = child_node.newNode(value_name)
     trace.outof(outputLevel=self.TRACE_LEVEL)
     return
Exemple #6
0
    def _createAllTestButtons(self, moduleTree=None):
        trace.into('ProctorGUI', '_createAllTestButtons')
        all_tests = moduleTree.getTestSuite(1)
        num_tests = all_tests.countTestCases()

        self.showMessage('userevent', '%d tests' % num_tests)

        trace.writeVar(num_tests=num_tests)

        #
        # Get preferences
        #
        width = self.user_preferences['width']
        height = self.user_preferences['height']
        spacing = self.user_preferences['spacing']

        #
        # Do some math to figure out how big to make the buttons.
        # Lean towards a more horizontal layout.
        #
        canvas_height = self.canvas.winfo_height()
        canvas_width = self.canvas.winfo_width()

        trace.writeVar(
            canvas_height=canvas_height,
            canvas_width=canvas_width,
        )

        num_per_row = ((canvas_width / (width + spacing)) or 1)
        num_rows = (num_tests / num_per_row)
        if (num_tests % num_per_row):
            num_rows += 1

        #
        # Store values for use later
        #
        self._button_height = height
        self._button_width = width
        self._num_per_row = num_per_row
        self._row = 0
        self._col = 0

        #
        # Resize the canvas (height only)
        #
        required_height = num_rows * (self._button_height + spacing)
        trace.writeVar(required_height=required_height)
        if canvas_height > (required_height + self._button_height):
            trace.write('resizing down')
            do_resize = 1
        elif canvas_height < required_height:
            trace.write('resizing up')
            do_resize = 1
        else:
            do_resize = 0

        if do_resize:
            canvas = self.component('testcanvas')
            hull = canvas.component('hull')
            hull.configure(height=required_height)

        moduleTree.walk(self._makeTestButtons)

        trace.outof()
        return
Exemple #7
0
    def _importModule(self):
        trace.into('ModuleTree', '_importModule', outputLevel=self.TRACE_LEVEL)
        if self.name:
            try:
                full_name = self.getName()

                path_name = full_name.replace('.', '/')
                directory, module_base = os.path.split(path_name)

                trace.write('find_module(%s, [%s])' % (module_base, directory),
                            outputLevel=self.TRACE_LEVEL)

                module = None
                global _module_cache
                try:
                    module = _module_cache[(module_base, directory)]
                    trace.write('Found module %s.%s in cache' % (
                        directory,
                        module_base,
                    ))
                except KeyError:
                    if self.parent and self.parent.module:
                        trace.write('using parent search path')
                        search_path = self.parent.module.__path__
                    else:
                        trace.write(
                            'using directory from filename for search path')
                        search_path = [directory]
                    trace.write('Searching for %s in %s' %
                                (module_base, search_path))
                    fp, pathname, description = imp.find_module(
                        module_base, search_path)

                    trace.writeVar(module_base=module_base,
                                   fp=fp,
                                   pathname=pathname,
                                   description=description,
                                   outputLevel=self.TRACE_LEVEL)

                    try:
                        # HACK ALERT:
                        # Add a fake prefix to the module so we effectivly import it
                        # into our own namespace.  That prevents errors if the
                        # test module wants to import other modules from the
                        # same package which we might not have already imported,
                        # since the sys.modules lookup will not return the module
                        # we imported for testing purposes.
                        load_module_args = ('test: %s' % full_name, fp,
                                            pathname, description)
                        trace.write('load_module%s' % str(load_module_args),
                                    outputLevel=self.TRACE_LEVEL)
                        try:
                            module = imp.load_module(*load_module_args)
                        except Exception, msg:
                            trace.write('ImportError(%s)' % str(msg))
                            trace.showTraceback()
                            raise ImportError(str(msg))
                        else:
                            trace.write('Imported %s (%s)' %
                                        (str(module), id(module)))

                        trace.write('Updating cache')
                        _module_cache[(module_base, directory)] = module
                    finally:
                        if fp:
                            fp.close()
Exemple #8
0
    def walkCallback(self, arg, dirname, filenames):
        trace.into('ModuleScanner',
                   'walkCallback',
                   arg=arg,
                   dirname=dirname,
                   filenames=filenames,
                   outputLevel=self.TRACE_LEVEL)
        if self.verbose_level:
            print 'Scanning: %s' % (dirname or '.')
            sys.stdout.flush()
        #
        # Create a local copy of the names to skip, then add
        # .cvsignore content to it if the file exists.
        #
        skip_names = list(self.SKIP_NAMES[:])
        if '.cvsignore' in filenames:
            try:
                ignore_file = open(os.path.join(dirname, '.cvsignore'), 'r')
            except IOError:
                trace.write('Unable to open .cvsignore file in %s' % dirname)
            else:
                ignore_files = ignore_file.readlines()
                ignore_file.close()
                ignore_files = [f.strip() for f in ignore_files]
                skip_names = skip_names + ignore_files

        #
        # Update the local copy of the names to skip
        # with .proctor configuration if the file exists.
        #
        if '.proctor' in filenames:
            try:
                proctor_file = open(os.path.join(dirname, '.proctor'), 'r')
            except IOError:
                trace.write('Unable to open .proctor file in %s' % dirname)
            else:
                proctor_file_body = proctor_file.read()
                proctor_file.close()
                global_namespace = {}
                local_namespace = {}
                try:
                    exec proctor_file_body in global_namespace, local_namespace
                except:
                    import traceback
                    sys.stderr.write(
                        '\n--- Config File Error %s/.proctor ---\n' % dirname)
                    traceback.print_exc()
                    sys.stderr.write(
                        '--------------------------------------------\n\n')
                else:
                    skip_names = skip_names + list(local_namespace['ignore'])

        #
        # First, skip over directories we are not interested in
        # scanning.
        #
        for skip_name in skip_names:
            if skip_name in filenames:
                trace.write('Skipping %s' % skip_name,
                            outputLevel=self.TRACE_LEVEL)
                del filenames[filenames.index(skip_name)]
        #
        # Clean up the directory name
        #
        normalized_dirname = os.path.normpath(dirname)
        trace.write('normalized path=%s' % normalized_dirname,
                    outputLevel=self.TRACE_LEVEL)
        #
        # Get the relative path
        #
        common_prefix = os.path.commonprefix((os.getcwd(), normalized_dirname))
        if common_prefix:
            prefix_len = len(common_prefix) + len(os.sep)
            normalized_dirname = normalized_dirname[prefix_len:]
        #
        # Convert the directory name to a module path
        #
        dirname_parts = normalized_dirname.split(os.sep)
        package_path = '.'.join(dirname_parts)
        trace.writeVar(package_path=package_path, outputLevel=self.TRACE_LEVEL)
        for filename in filenames:
            #
            # Skip files with bad extensions or prefixes.
            #
            base_filename, extension = os.path.splitext(filename)
            extension = extension[1:]

            if extension in self.SKIP_FILE_EXTENSIONS:
                trace.write('Skipping file %s/%s' %
                            (normalized_dirname, filename),
                            outputLevel=self.TRACE_LEVEL)

            elif filename[0] == '.':
                trace.write('Skipping file %s/%s' %
                            (normalized_dirname, filename),
                            outputLevel=self.TRACE_LEVEL)

            elif extension == 'py':
                #
                # If we are looking in ., put the new module
                # at the root of the module tree.  Otherwise,
                # build the import path to the module as the
                # key.
                #
                if package_path == os.curdir:
                    module_path = base_filename
                else:
                    module_path = '%s.%s' % (package_path, base_filename)
                trace.write('Adding %s' % module_path,
                            outputLevel=self.TRACE_LEVEL)
                self.module_tree[module_path] = module_path

            else:
                trace.write('Skipping file %s/%s' %
                            (normalized_dirname, filename),
                            outputLevel=self.TRACE_LEVEL)

        trace.outof(outputLevel=self.TRACE_LEVEL)
        return