Changed logic creating the coverage and profile data file names. unittest

Mon, 23 May 2022 16:50:39 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 23 May 2022 16:50:39 +0200
branch
unittest
changeset 9092
043848f65726
parent 9091
4231a14a89d7
child 9093
437bfe0c5793

Changed logic creating the coverage and profile data file names.

eric7/Project/Project.py file | annotate | diff | comparison | revisions
eric7/Project/ProjectSourcesBrowser.py file | annotate | diff | comparison | revisions
eric7/QScintilla/Editor.py file | annotate | diff | comparison | revisions
eric7/Utilities/__init__.py file | annotate | diff | comparison | revisions
--- a/eric7/Project/Project.py	Mon May 23 16:50:04 2022 +0200
+++ b/eric7/Project/Project.py	Mon May 23 16:50:39 2022 +0200
@@ -3367,8 +3367,9 @@
         the project path.
         
         @param normalized flag indicating a normalized filename is wanted
-            (boolean)
-        @return filename of the projects main script (string)
+        @type bool
+        @return filename of the projects main script
+        @rtype str
         """
         if self.pdata["MAINSCRIPT"]:
             if normalized:
@@ -3376,15 +3377,16 @@
             else:
                 return self.pdata["MAINSCRIPT"]
         else:
-            return None
+            return ""
         
     def getSources(self, normalized=False):
         """
         Public method to return the source script files.
         
         @param normalized flag indicating a normalized filename is wanted
-            (boolean)
-        @return list of the projects scripts (list of string)
+        @type bool
+        @return list of the projects scripts
+        @rtype list of str
         """
         return self.getProjectFiles("SOURCES", normalized=normalized)
     
@@ -5068,14 +5070,7 @@
                     " current project. Aborting"))
             return
         
-        # determine name of coverage file to be used
-        files = []
-        for filename in [fn] + Utilities.getTestFileNames(fn):
-            basename = os.path.splitext(filename)[0]
-            f = "{0}.coverage".format(basename)
-            if os.path.isfile(f):
-                files.append(f)
-        
+        files = Utilities.getCoverageFileNames(fn)
         if files:
             if len(files) > 1:
                 fn, ok = QInputDialog.getItem(
@@ -5113,14 +5108,7 @@
                     " current project. Aborting"))
             return
         
-        # determine name of profile file to be used
-        files = []
-        for filename in [fn] + Utilities.getTestFileNames(fn):
-            basename = os.path.splitext(filename)[0]
-            f = "{0}.profile".format(basename)
-            if os.path.isfile(f):
-                files.append(f)
-        
+        files = Utilities.getProfileFileNames(fn)
         if files:
             if len(files) > 1:
                 fn, ok = QInputDialog.getItem(
@@ -5146,22 +5134,17 @@
         Private slot called before the show menu is shown.
         """
         fn = self.getMainScript(True)
-        if fn is not None:
-            filenames = [os.path.splitext(f)[0]
-                         for f in [fn] + Utilities.getTestFileNames(fn)]
-            self.codeProfileAct.setEnabled(any([
-                os.path.isfile("{0}.profile".format(f))
-                for f in filenames
-            ]))
-            self.codeCoverageAct.setEnabled(
-                self.isPy3Project() and any([
-                    os.path.isfile("{0}.coverage".format(f))
-                    for f in filenames
-                ])
-            )
-        else:
-            self.codeProfileAct.setEnabled(False)
-            self.codeCoverageAct.setEnabled(False)
+        if not fn:
+            fn = self.getProjectPath()
+        
+        self.codeProfileAct.setEnabled(
+            self.isPy3Project() and
+            bool(Utilities.getProfileFileName(fn))
+        )
+        self.codeCoverageAct.setEnabled(
+            self.isPy3Project() and
+            bool(Utilities.getCoverageFileNames(fn))
+        )
         
         self.showMenu.emit("Show", self.menuShow)
     
--- a/eric7/Project/ProjectSourcesBrowser.py	Mon May 23 16:50:04 2022 +0200
+++ b/eric7/Project/ProjectSourcesBrowser.py	Mon May 23 16:50:39 2022 +0200
@@ -755,33 +755,26 @@
         # a project coverage file
         fn = self.project.getMainScript(True)
         if fn is not None:
-            filenames = [os.path.splitext(f)[0]
-                         for f in [fn] + Utilities.getTestFileNames(fn)]
-            prEnable = any([
-                os.path.isfile("{0}.profile".format(f))
-                for f in filenames
-            ])
+            prEnable = (
+                self.project.isPy3Project() and
+                bool(Utilities.getProfileFileNames(fn))
+            )
             coEnable = (
                 self.project.isPy3Project() and
-                any([
-                    os.path.isfile("{0}.coverage".format(f))
-                    for f in filenames
-                ])
+                bool(Utilities.getCoverageFileNames(fn))
             )
         
         # now check the selected item
         itm = self.model().item(self.currentIndex())
         fn = itm.fileName()
         if fn is not None:
-            basename = os.path.splitext(fn)[0]
-            prEnable = (
-                prEnable or
-                os.path.isfile("{0}.profile".format(basename))
+            prEnable |= (
+                itm.isPython3File() and
+                bool(Utilities.getProfileFileNames(fn))
             )
-            coEnable = (
-                (coEnable or
-                 os.path.isfile("{0}.coverage".format(basename))) and
-                itm.isPython3File()
+            coEnable |= (
+                itm.isPython3File() and
+                bool(Utilities.getCoverageFileName(fn))
             )
         
         self.profileMenuAction.setEnabled(prEnable)
@@ -985,25 +978,17 @@
         fn = itm.fileName()
         pfn = self.project.getMainScript(True)
         
-        files = []
+        files = set()
         
         if pfn is not None:
-            for filename in [pfn] + Utilities.getTestFileNames(pfn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.coverage".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
+            files |= set(Utilities.getCoverageFileNames(pfn))
         
         if fn is not None:
-            for filename in [fn] + Utilities.getTestFileNames(fn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.coverage".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
+            files |= set(Utilities.getCoverageFileNames(fn))
         
-        if files:
+        if list(files):
             if len(files) > 1:
-                pfn, ok = QInputDialog.getItem(
+                cfn, ok = QInputDialog.getItem(
                     None,
                     self.tr("Code Coverage"),
                     self.tr("Please select a coverage file"),
@@ -1012,14 +997,14 @@
                 if not ok:
                     return
             else:
-                pfn = files[0]
+                cfn = files[0]
         else:
             return
         
         from DataViews.PyCoverageDialog import PyCoverageDialog
         self.codecoverage = PyCoverageDialog()
         self.codecoverage.show()
-        self.codecoverage.start(pfn, fn)
+        self.codecoverage.start(cfn, fn)
     
     def __showProfileData(self):
         """
@@ -1029,23 +1014,15 @@
         fn = itm.fileName()
         pfn = self.project.getMainScript(True)
         
-        files = []
+        files = set()
         
         if pfn is not None:
-            for filename in [pfn] + Utilities.getTestFileNames(pfn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.profile".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
+            files |= set(Utilities.getProfileFileNames(pfn))
         
         if fn is not None:
-            for filename in [fn] + Utilities.getTestFileNames(fn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.profile".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
-                
-        if files:
+            files |= set(Utilities.getProfileFileNames(fn))
+        
+        if list(files):
             if len(files) > 1:
                 pfn, ok = QInputDialog.getItem(
                     None,
--- a/eric7/QScintilla/Editor.py	Mon May 23 16:50:04 2022 +0200
+++ b/eric7/QScintilla/Editor.py	Mon May 23 16:50:39 2022 +0200
@@ -5632,35 +5632,25 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                filenames = [os.path.splitext(f)[0]
-                             for f in [fn] + Utilities.getTestFileNames(fn)]
-                prEnable = any([
-                    os.path.isfile("{0}.profile".format(f))
-                    for f in filenames
-                ])
+                prEnable = (
+                    self.project.isPy3Project() and
+                    bool(Utilities.getProfileFileNames(fn))
+                )
                 coEnable = (
                     self.project.isPy3Project() and
-                    any([
-                        os.path.isfile("{0}.coverage".format(f))
-                        for f in filenames
-                    ])
+                    bool(Utilities.getCoverageFileNames(fn))
                 )
         
         # now check ourselves
         fn = self.getFileName()
         if fn is not None:
-            filenames = [os.path.splitext(f)[0]
-                         for f in [fn] + Utilities.getTestFileNames(fn)]
-            prEnable |= any([
-                os.path.isfile("{0}.profile".format(f))
-                for f in filenames
-            ])
+            prEnable |= (
+                self.project.isPy3Project() and
+                bool(Utilities.getProfileFileName(fn))
+            )
             coEnable |= (
                 self.project.isPy3Project() and
-                any([
-                    os.path.isfile("{0}.coverage".format(f))
-                    for f in filenames
-                ])
+                bool(Utilities.getCoverageFileName(fn))
             )
         
         coEnable |= bool(self.__coverageFile)
@@ -6051,9 +6041,10 @@
         Private method to get the file name of the file containing coverage
         info.
         
-        @return file name of the coverage file (string)
-        """
-        files = []
+        @return file name of the coverage file
+        @rtype str
+        """
+        files = set()
         
         if bool(self.__coverageFile):
             # return the path of a previously used coverage file
@@ -6065,29 +6056,19 @@
             self.project.isOpen() and
             self.project.isProjectSource(self.fileName)
         ):
-            fn = self.project.getMainScript(True)
-            if fn is not None:
-                for filename in [fn] + Utilities.getTestFileNames(fn):
-                    basename = os.path.splitext(filename)[0]
-                    f = "{0}.coverage".format(basename)
-                    if os.path.isfile(f):
-                        files.append(f)
+            pfn = self.project.getMainScript(True)
+            if pfn is not None:
+                files |= set(Utilities.getCoverageFileNames(pfn))
         
         # now check, if there are coverage files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            for filename in [fn] + Utilities.getTestFileNames(fn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.coverage".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
-        
-        # make the list unique
-        files = list(set(files))
-        
+            files |= set(Utilities.getCoverageFileNames(fn))
+        
+        files = list(files)
         if files:
             if len(files) > 1:
-                fn, ok = QInputDialog.getItem(
+                cfn, ok = QInputDialog.getItem(
                     self,
                     self.tr("Code Coverage"),
                     self.tr("Please select a coverage file"),
@@ -6096,11 +6077,11 @@
                 if not ok:
                     return ""
             else:
-                fn = files[0]
-        else:
-            fn = None
-        
-        return fn
+                cfn = files[0]
+        else:
+            cfn = None
+        
+        return cfn
         
     def __showCodeCoverage(self):
         """
@@ -6242,7 +6223,7 @@
         """
         Private method to handle the show profile data context menu action.
         """
-        files = []
+        files = set()
         
         # first check if the file belongs to a project and there is
         # a project profile file
@@ -6252,24 +6233,14 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                for filename in [fn] + Utilities.getTestFileNames(fn):
-                    basename = os.path.splitext(filename)[0]
-                    f = "{0}.profile".format(basename)
-                    if os.path.isfile(f):
-                        files.append(f)
+                files |= set(Utilities.getProfileFileNames(fn))
         
         # now check, if there are profile files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            for filename in [fn] + Utilities.getTestFileNames(fn):
-                basename = os.path.splitext(filename)[0]
-                f = "{0}.profile".format(basename)
-                if os.path.isfile(f):
-                    files.append(f)
-        
-        # make the list unique
-        files = list(set(files))
-        
+            files |= set(Utilities.getProfileFileNames(fn))
+        
+        files = list(files)
         if files:
             if len(files) > 1:
                 fn, ok = QInputDialog.getItem(
--- a/eric7/Utilities/__init__.py	Mon May 23 16:50:04 2022 +0200
+++ b/eric7/Utilities/__init__.py	Mon May 23 16:50:39 2022 +0200
@@ -1323,6 +1323,86 @@
     ]
 
 
+def getCoverageFileNames(fn):
+    """
+    Function to build a list of coverage data file names.
+    
+    @param fn file name basis to be used for the coverage data file
+    @type str
+    @return list of existing coverage data files
+    @rtype list of str
+    """
+    files = []
+    for filename in [fn, os.path.dirname(fn) + os.sep] + getTestFileNames(fn):
+        f = getCoverageFileName(filename)
+        if f:
+            files.append(f)
+    return files
+
+
+def getCoverageFileName(fn, mustExist=True):
+    """
+    Function to build a file name for a coverage data file.
+    
+    @param fn file name basis to be used for the coverage data file name
+    @type str
+    @param mustExist flag indicating to check that the file exists (defaults
+        to True)
+    @type bool (optional)
+    @return coverage data file name
+    @rtype str
+    """
+    basename = os.path.splitext(fn)[0]
+    filename = "{0}.coverage".format(basename)
+    if mustExist:
+        if os.path.isfile(filename):
+            return filename
+        else:
+            return ""
+    else:
+        return filename
+
+
+def getProfileFileNames(fn):
+    """
+    Function to build a list of profile data file names.
+    
+    @param fn file name basis to be used for the profile data file
+    @type str
+    @return list of existing profile data files
+    @rtype list of str
+    """
+    files = []
+    for filename in [fn, os.path.dirname(fn) + os.sep] + getTestFileNames(fn):
+        f = getProfileFileName(filename)
+        if f:
+            files.append(f)
+    return files
+
+
+def getProfileFileName(fn, mustExist=True):
+    """
+    Function to build a file name for a profile data file.
+    
+    @param fn file name basis to be used for the profile data file name
+    @type str
+    @param mustExist flag indicating to check that the file exists (defaults
+        to True)
+    @type bool (optional)
+    @return profile data file name
+    @rtype str
+    """
+    basename = os.path.splitext(fn)[0]
+    filename = "{0}.profile".format(basename)
+    if mustExist:
+        if os.path.isfile(filename):
+            return filename
+        else:
+            return ""
+    else:
+        return filename
+
+
 def parseOptionString(s):
     """
     Function used to convert an option string into a list of options.

eric ide

mercurial