Beispiel #1
0
    def __annotate(self):
        """
        Private slot to handle the annotate context menu action.
        
        This method produce an annotated coverage file of the
        selected file.
        """
        itm = self.resultList.currentItem()
        fn = itm.text(0)

        cover = Coverage(data_file=self.cfn)
        cover.exclude(self.excludeList[0])
        cover.load()
        cover.annotate([fn], None, True)
Beispiel #2
0
    def __annotateAll(self):
        """
        Private slot to handle the annotate all context menu action.
        
        This method produce an annotated coverage file of every
        file listed in the listview.
        """
        amount = self.resultList.topLevelItemCount()
        if amount == 0:
            return

        # get list of all filenames
        files = []
        for index in range(amount):
            itm = self.resultList.topLevelItem(index)
            files.append(itm.text(0))

        cover = Coverage(data_file=self.cfn)
        cover.exclude(self.excludeList[0])
        cover.load()

        # now process them
        progress = E5ProgressDialog(self.tr("Annotating files..."),
                                    self.tr("Abort"), 0, len(files),
                                    self.tr("%v/%m Files"), self)
        progress.setMinimumDuration(0)
        progress.setWindowTitle(self.tr("Coverage"))
        count = 0

        for file in files:
            progress.setValue(count)
            if progress.wasCanceled():
                break
            cover.annotate([file], None)  # , True)
            count += 1

        progress.setValue(len(files))
Beispiel #3
0
    def start(self, cfn, fn):
        """
        Public slot to start the coverage data evaluation.
        
        @param cfn basename of the coverage file (string)
        @param fn file or list of files or directory to be checked
                (string or list of strings)
        """
        self.__cfn = cfn
        self.__fn = fn

        self.basename = os.path.splitext(cfn)[0]

        self.cfn = "{0}.coverage".format(self.basename)

        if isinstance(fn, list):
            files = fn
            self.path = os.path.dirname(cfn)
        elif os.path.isdir(fn):
            files = Utilities.direntries(fn, True, '*.py', False)
            self.path = fn
        else:
            files = [fn]
            self.path = os.path.dirname(cfn)
        files.sort()

        cover = Coverage(data_file=self.cfn)
        cover.load()

        # set the exclude pattern
        self.excludeCombo.clear()
        self.excludeCombo.addItems(self.excludeList)

        self.checkProgress.setMaximum(len(files))
        QApplication.processEvents()

        total_statements = 0
        total_executed = 0
        total_exceptions = 0

        cover.exclude(self.excludeList[0])
        progress = 0

        try:
            # disable updates of the list for speed
            self.resultList.setUpdatesEnabled(False)
            self.resultList.setSortingEnabled(False)

            # now go through all the files
            for file in files:
                if self.cancelled:
                    return

                try:
                    statements, excluded, missing, readable = (
                        cover.analysis2(file)[1:])
                    readableEx = (excluded and self.__format_lines(excluded)
                                  or '')
                    n = len(statements)
                    m = n - len(missing)
                    if n > 0:
                        pc = 100.0 * m / n
                    else:
                        pc = 100.0
                    self.__createResultItem(file, str(n), str(m), pc,
                                            readableEx, readable)

                    total_statements = total_statements + n
                    total_executed = total_executed + m
                except CoverageException:
                    total_exceptions += 1

                progress += 1
                self.checkProgress.setValue(progress)
                QApplication.processEvents()
        finally:
            # reenable updates of the list
            self.resultList.setSortingEnabled(True)
            self.resultList.setUpdatesEnabled(True)
            self.checkProgress.reset()

        # show summary info
        if len(files) > 1:
            if total_statements > 0:
                pc = 100.0 * total_executed / total_statements
            else:
                pc = 100.0
            itm = QTreeWidgetItem(self.summaryList, [
                str(total_statements),
                str(total_executed), "{0:.0f}%".format(pc)
            ])
            for col in range(0, 3):
                itm.setTextAlignment(col, Qt.AlignRight)
        else:
            self.summaryGroup.hide()

        if total_exceptions:
            E5MessageBox.warning(
                self, self.tr("Parse Error"),
                self.tr(
                    """%n file(s) could not be parsed. Coverage"""
                    """ info for these is not available.""", "",
                    total_exceptions))

        self.__finish()
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        # 1. 设置coverage
        opts = {
            'auto_data': settings.COVERAGE_USE_CACHE,
        }
        if settings.COVERAGE_SOURCE:
            opts['source'] = settings.COVERAGE_SOURCE
        if settings.COVERAGE_CONFIG_FILE:
            opts['config_file'] = settings.COVERAGE_CONFIG_FILE
        coverage = Coverage(**opts)
        for e in settings.COVERAGE_CODE_EXCLUDES:
            coverage.exclude(e)
        coverage.start()
        results = super(CoverageRunner,
                        self).run_tests(test_labels, extra_tests, **kwargs)
        coverage.stop()

        # 2. 添加coverage_modules
        coverage_modules = []
        if test_labels:
            for label in test_labels:
                label = label.split('.')[0].rstrip('/')
                app = get_app(label)
                coverage_modules.append(self._get_app_package(app))
        else:
            for app in get_apps():
                coverage_modules.append(self._get_app_package(app))

        coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES)

        # 加载所有的packages, modules等
        packages, modules, excludes, errors = get_all_modules(
            coverage_modules, settings.COVERAGE_MODULE_EXCLUDES,
            settings.COVERAGE_PATH_EXCLUDES)

        if settings.COVERAGE_USE_STDOUT:
            coverage.report(modules.values(), show_missing=1)
            if excludes:
                message = "The following packages or modules were excluded:"
                print("")
                print(message)
                for e in excludes:
                    print(e)
                print("")
            if errors:
                message = "There were problems with the following packages "
                message += "or modules:"
                print("")
                print(message)
                for e in errors:
                    print(e)
                print("")

        # 输出Html格式的数据
        # modules: module_name --> module
        #
        outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
        if outdir:
            outdir = os.path.abspath(outdir)
            # 默认使用55minutes的html表
            if settings.COVERAGE_CUSTOM_REPORTS:
                html_report(coverage, outdir, modules, excludes, errors)
            else:
                coverage.html_report(modules.values(), outdir)
            print("")
            print("HTML reports were output to '%s'" % outdir)

        return results