Modified the 'getTestFileName()' function of the Utilities module to allow for additional test file name patterns (currently 'test...' and 'test_...'). unittest

Wed, 18 May 2022 08:43:36 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 18 May 2022 08:43:36 +0200
branch
unittest
changeset 9072
8d3ae97ee666
parent 9070
eab09a1ab8ce
child 9073
d88e53da6e66

Modified the 'getTestFileName()' function of the Utilities module to allow for additional test file name patterns (currently 'test...' and 'test_...').

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/Testing/TestingWidget.py file | annotate | diff | comparison | revisions
eric7/UI/UserInterface.py file | annotate | diff | comparison | revisions
eric7/Utilities/__init__.py file | annotate | diff | comparison | revisions
--- a/eric7/Project/Project.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/Project/Project.py	Wed May 18 08:43:36 2022 +0200
@@ -5068,18 +5068,13 @@
                     " current project. Aborting"))
             return
         
-        tfn = Utilities.getTestFileName(fn)
-        basename = os.path.splitext(fn)[0]
-        tbasename = os.path.splitext(tfn)[0]
-        
         # determine name of coverage file to be used
         files = []
-        f = "{0}.coverage".format(basename)
-        tf = "{0}.coverage".format(tbasename)
-        if os.path.isfile(f):
-            files.append(f)
-        if os.path.isfile(tf):
-            files.append(tf)
+        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)
         
         if files:
             if len(files) > 1:
@@ -5118,18 +5113,13 @@
                     " current project. Aborting"))
             return
         
-        tfn = Utilities.getTestFileName(fn)
-        basename = os.path.splitext(fn)[0]
-        tbasename = os.path.splitext(tfn)[0]
-        
         # determine name of profile file to be used
         files = []
-        f = "{0}.profile".format(basename)
-        tf = "{0}.profile".format(tbasename)
-        if os.path.isfile(f):
-            files.append(f)
-        if os.path.isfile(tf):
-            files.append(tf)
+        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:
             if len(files) > 1:
@@ -5157,16 +5147,18 @@
         """
         fn = self.getMainScript(True)
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            self.codeProfileAct.setEnabled(
-                os.path.isfile("{0}.profile".format(basename)) or
-                os.path.isfile("{0}.profile".format(tbasename)))
+            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
-                (os.path.isfile("{0}.coverage".format(basename)) or
-                 os.path.isfile("{0}.coverage".format(tbasename))))
+                self.isPy3Project() and any([
+                    os.path.isfile("{0}.coverage".format(f))
+                    for f in filenames
+                ])
+            )
         else:
             self.codeProfileAct.setEnabled(False)
             self.codeCoverageAct.setEnabled(False)
--- a/eric7/Project/ProjectSourcesBrowser.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/Project/ProjectSourcesBrowser.py	Wed May 18 08:43:36 2022 +0200
@@ -755,19 +755,18 @@
         # a project coverage file
         fn = self.project.getMainScript(True)
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            prEnable = (
-                prEnable or
-                os.path.isfile("{0}.profile".format(basename)) or
-                os.path.isfile("{0}.profile".format(tbasename))
-            )
+            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
+            ])
             coEnable = (
-                (coEnable or
-                 os.path.isfile("{0}.coverage".format(basename)) or
-                 os.path.isfile("{0}.coverage".format(tbasename))) and
-                self.project.isPy3Project()
+                self.project.isPy3Project() and
+                any([
+                    os.path.isfile("{0}.coverage".format(f))
+                    for f in filenames
+                ])
             )
         
         # now check the selected item
@@ -989,28 +988,18 @@
         files = []
         
         if pfn is not None:
-            tpfn = Utilities.getTestFileName(pfn)
-            basename = os.path.splitext(pfn)[0]
-            tbasename = os.path.splitext(tpfn)[0]
-            
-            f = "{0}.coverage".format(basename)
-            tf = "{0}.coverage".format(tbasename)
-            if os.path.isfile(f):
-                files.append(f)
-            if os.path.isfile(tf):
-                files.append(tf)
+            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)
         
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.coverage".format(basename)
-            tf = "{0}.coverage".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
+            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)
         
         if files:
             if len(files) > 1:
@@ -1043,28 +1032,18 @@
         files = []
         
         if pfn is not None:
-            tpfn = Utilities.getTestFileName(pfn)
-            basename = os.path.splitext(pfn)[0]
-            tbasename = os.path.splitext(tpfn)[0]
-            
-            f = "{0}.profile".format(basename)
-            tf = "{0}.profile".format(tbasename)
-            if os.path.isfile(f):
-                files.append(f)
-            if os.path.isfile(tf):
-                files.append(tf)
+            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)
         
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.profile".format(basename)
-            tf = "{0}.profile".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
+            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:
             if len(files) > 1:
--- a/eric7/QScintilla/Editor.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/QScintilla/Editor.py	Wed May 18 08:43:36 2022 +0200
@@ -5632,37 +5632,35 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
-                prEnable = (
-                    prEnable or
-                    os.path.isfile("{0}.profile".format(basename)) or
-                    os.path.isfile("{0}.profile".format(tbasename))
-                )
+                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
+                ])
                 coEnable = (
-                    (coEnable or
-                     os.path.isfile("{0}.coverage".format(basename)) or
-                     os.path.isfile("{0}.coverage".format(tbasename))) and
-                    self.project.isPy3Project()
+                    self.project.isPy3Project() and
+                    any([
+                        os.path.isfile("{0}.coverage".format(f))
+                        for f in filenames
+                    ])
                 )
         
         # now check ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            prEnable = (
-                prEnable or
-                os.path.isfile("{0}.profile".format(basename)) or
-                os.path.isfile("{0}.profile".format(tbasename))
-            )
-            coEnable = (
-                (coEnable or
-                 os.path.isfile("{0}.coverage".format(basename)) or
-                 os.path.isfile("{0}.coverage".format(tbasename))) and
-                self.isPyFile()
+            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
+            ])
+            coEnable |= (
+                self.project.isPy3Project() and
+                any([
+                    os.path.isfile("{0}.coverage".format(f))
+                    for f in filenames
+                ])
             )
         
         coEnable |= bool(self.__coverageFile)
@@ -6069,30 +6067,23 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
-                
-                f = "{0}.coverage".format(basename)
-                tf = "{0}.coverage".format(tbasename)
-                if os.path.isfile(f):
-                    files.append(f)
-                if os.path.isfile(tf):
-                    files.append(tf)
+                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)
         
         # now check, if there are coverage files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.coverage".format(basename)
-            tf = "{0}.coverage".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
+            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))
         
         if files:
             if len(files) > 1:
@@ -6261,30 +6252,23 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
-                
-                f = "{0}.profile".format(basename)
-                tf = "{0}.profile".format(tbasename)
-                if os.path.isfile(f):
-                    files.append(f)
-                if os.path.isfile(tf):
-                    files.append(tf)
+                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)
         
         # now check, if there are profile files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.profile".format(basename)
-            tf = "{0}.profile".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
+            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))
         
         if files:
             if len(files) > 1:
--- a/eric7/Testing/TestingWidget.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/Testing/TestingWidget.py	Wed May 18 08:43:36 2022 +0200
@@ -315,17 +315,20 @@
                              start)
     
     @pyqtSlot(str)
-    def setTestFile(self, testFile):
+    def setTestFile(self, testFile, forProject=False):
         """
         Public slot to set the given test file as the current one.
         
         @param testFile path of the test file
         @type str
+        @param forProject flag indicating that this call is for a project
+            (defaults to False)
+        @type bool (optional)
         """
         if testFile:
             self.__insertTestFile(testFile)
         
-        self.discoverCheckBox.setChecked(not bool(testFile))
+        self.discoverCheckBox.setChecked(forProject or not bool(testFile))
         
         self.tabWidget.setCurrentIndex(0)
     
--- a/eric7/UI/UserInterface.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/UI/UserInterface.py	Wed May 18 08:43:36 2022 +0200
@@ -5373,14 +5373,13 @@
         if testFile is None:
             aw = self.viewmanager.activeWindow()
             fn = aw.getFileName()
-            tfn = Utilities.getTestFileName(fn)
-            if os.path.exists(tfn):
-                testFile = tfn
-            else:
-                testFile = fn
+            testFile = [
+                f for f in Utilities.getTestFileNames(fn) + [fn]
+                if os.path.exists(f)
+            ][0]
         
         self.__startTesting()
-        self.__testingWidget.setTestFile(testFile)
+        self.__testingWidget.setTestFile(testFile, forProject=False)
         self.restartTestAct.setEnabled(False)
         self.rerunFailedTestsAct.setEnabled(False)
     
@@ -5393,12 +5392,13 @@
         testFile = None
         fn = self.project.getMainScript(True)
         if fn:
-            tfn = Utilities.getTestFileName(fn)
-            if os.path.exists(tfn):
-                testFile = tfn
+            testFile = [
+                f for f in Utilities.getTestFileNames(fn) + [fn]
+                if os.path.exists(f)
+            ][0]
         
         self.__startTesting()
-        self.__testingWidget.setTestFile(testFile)
+        self.__testingWidget.setTestFile(testFile, forProject=True)
         self.restartTestAct.setEnabled(False)
         self.rerunFailedTestsAct.setEnabled(False)
         
--- a/eric7/Utilities/__init__.py	Tue May 17 17:23:07 2022 +0200
+++ b/eric7/Utilities/__init__.py	Wed May 18 08:43:36 2022 +0200
@@ -1304,21 +1304,23 @@
         return volumeDirectory
 
 
-# TODO: rename to getTestFileNames and add a name beginning with 'test_'
-def getTestFileName(fn):
+def getTestFileNames(fn):
     """
-    Function to build the filename of a test file.
+    Function to build the potential file names of a test file.
     
-    The filename for the test file is built by prepending
-    the string "test" to the file name passed into this function.
+    The file names for the test file is built by prepending the string
+    "test" and "test_" to the file name passed into this function.
     
-    @param fn file name basis to be used for the test file name
+    @param fn file name basis to be used for the test file names
     @type str
-    @return file name of the corresponding test file
-    @rtype str
+    @return file names of the corresponding test file
+    @rtype list of str
     """
     dn, fn = os.path.split(fn)
-    return os.path.join(dn, "test{0}".format(fn))
+    prefixes = ["test", "test_"]
+    return [
+        os.path.join(dn, "{0}{1}".format(prefix, fn)) for prefix in prefixes
+    ]
 
 
 def parseOptionString(s):

eric ide

mercurial