]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Tests/TestTools.py
BaseTools: Remove types.TypeType
[mirror_edk2.git] / BaseTools / Tests / TestTools.py
index e838ae4294797a1be9f6edf7739858fed127086b..20a4ea28aa11b0622429cd222b932d94e92b146c 100644 (file)
-## @file
-# Utility functions and classes for BaseTools unit tests
-#
-#  Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
-#
-#  This program and the accompanying materials
-#  are licensed and made available under the terms and conditions of the BSD License
-#  which accompanies this distribution.  The full text of the license may be found at
-#  http://opensource.org/licenses/bsd-license.php
-#
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-
-##
-# Import Modules
-#
-import base64
-import os
-import os.path
-import random
-import shutil
-import subprocess
-import sys
-import types
-import unittest
-
-TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])
-BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))
-CSourceDir = os.path.join(BaseToolsDir, 'Source', 'C')
-PythonSourceDir = os.path.join(BaseToolsDir, 'Source', 'Python')
-TestTempDir = os.path.join(TestsDir, 'TestTempDir')
-
-def MakeTheTestSuite(localItems):
-    tests = []
-    for name, item in localItems.iteritems():
-        if isinstance(item, types.TypeType):
-            if issubclass(item, unittest.TestCase):
-                tests.append(unittest.TestLoader().loadTestsFromTestCase(item))
-            elif issubclass(item, unittest.TestSuite):
-                tests.append(item())
-    return lambda: unittest.TestSuite(tests)
-
-def GetBaseToolsPaths():
-    if sys.platform in ('win32', 'win64'):
-        return [ os.path.join(BaseToolsDir, 'Bin', sys.platform.title()) ]
-    else:
-        uname = os.popen('uname -sm').read().strip()
-        for char in (' ', '/'):
-            uname = uname.replace(char, '-')
-        return [
-                os.path.join(BaseToolsDir, 'Bin', uname),
-                os.path.join(BaseToolsDir, 'BinWrappers', uname),
-                os.path.join(BaseToolsDir, 'BinWrappers', 'PosixLike')
-            ]
-
-BaseToolsBinPaths = GetBaseToolsPaths()
-
-class BaseToolsTest(unittest.TestCase):
-
-    def cleanOutDir(self, dir):
-        for dirItem in os.listdir(dir):
-            if dirItem in ('.', '..'): continue
-            dirItem = os.path.join(dir, dirItem)
-            self.RemoveFileOrDir(dirItem)
-
-    def CleanUpTmpDir(self):
-        if os.path.exists(self.testDir):
-            self.cleanOutDir(self.testDir)
-
-    def HandleTreeDeleteError(self, function, path, excinfo):
-        os.chmod(path, stat.S_IWRITE)
-        function(path)
-    
-    def RemoveDir(self, dir):
-        shutil.rmtree(dir, False, self.HandleTreeDeleteError)
-
-    def RemoveFileOrDir(self, path):
-        if not os.path.exists(path):
-            return
-        elif os.path.isdir(path):
-            self.RemoveDir(path)
-        else:
-            os.remove(path)
-
-    def DisplayBinaryData(self, description, data):
-        print description, '(base64 encoded):'
-        b64data = base64.b64encode(data)
-        print b64data
-
-    def DisplayFile(self, fileName):
-        sys.stdout.write(self.ReadTmpFile(fileName))
-        sys.stdout.flush()
-
-    def FindToolBin(self, toolName):
-        for binPath in BaseToolsBinPaths:
-            bin = os.path.join(binPath, toolName)
-            if os.path.exists(bin):
-                break
-        assert os.path.exists(bin)
-        return bin
-
-    def RunTool(self, *args, **kwd):
-        if 'toolName' in kwd: toolName = kwd['toolName']
-        else: toolName = None
-        if 'logFile' in kwd: logFile = kwd['logFile']
-        else: logFile = None
-
-        if toolName is None: toolName = self.toolName
-        bin = self.FindToolBin(toolName)
-        if logFile is not None:
-            logFile = open(os.path.join(self.testDir, logFile), 'w')
-            popenOut = logFile
-        else:
-            popenOut = subprocess.PIPE
-
-        args = [toolName] + list(args)
-
-        Proc = subprocess.Popen(
-            args, executable=bin,
-            stdout=popenOut, stderr=subprocess.STDOUT
-            )
-
-        if logFile is None:
-            Proc.stdout.read()
-
-        return Proc.wait()
-
-    def GetTmpFilePath(self, fileName):
-        return os.path.join(self.testDir, fileName)
-
-    def OpenTmpFile(self, fileName, mode = 'r'):
-        return open(os.path.join(self.testDir, fileName), mode)
-
-    def ReadTmpFile(self, fileName):
-        f = open(self.GetTmpFilePath(fileName), 'r')
-        data = f.read()
-        f.close()
-        return data
-
-    def WriteTmpFile(self, fileName, data):
-        f = open(self.GetTmpFilePath(fileName), 'w')
-        f.write(data)
-        f.close()
-
-    def GenRandomFileData(self, fileName, minlen = None, maxlen = None):
-        if maxlen is None: maxlen = minlen
-        f = self.OpenTmpFile(fileName, 'w')
-        f.write(self.GetRandomString(minlen, maxlen))
-        f.close()
-
-    def GetRandomString(self, minlen = None, maxlen = None):
-        if minlen is None: minlen = 1024
-        if maxlen is None: maxlen = minlen
-        return ''.join(
-            [chr(random.randint(0,255))
-             for x in xrange(random.randint(minlen, maxlen))
-            ])
-
-    def setUp(self):
-        self.savedEnvPath = os.environ['PATH']
-        self.savedSysPath = sys.path[:]
-
-        for binPath in BaseToolsBinPaths:
-            os.environ['PATH'] = \
-                os.path.pathsep.join((os.environ['PATH'], binPath))
-
-        self.testDir = TestTempDir
-        if not os.path.exists(self.testDir):
-            os.mkdir(self.testDir)
-        else:
-            self.cleanOutDir(self.testDir)
-
-    def tearDown(self):
-        self.RemoveFileOrDir(self.testDir)
-
-        os.environ['PATH'] = self.savedEnvPath
-        sys.path = self.savedSysPath
-
+from __future__ import print_function\r
+## @file\r
+# Utility functions and classes for BaseTools unit tests\r
+#\r
+#  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+import base64\r
+import os\r
+import os.path\r
+import random\r
+import shutil\r
+import subprocess\r
+import sys\r
+import unittest\r
+\r
+TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])\r
+BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))\r
+CSourceDir = os.path.join(BaseToolsDir, 'Source', 'C')\r
+PythonSourceDir = os.path.join(BaseToolsDir, 'Source', 'Python')\r
+TestTempDir = os.path.join(TestsDir, 'TestTempDir')\r
+\r
+if PythonSourceDir not in sys.path:\r
+    #\r
+    # Allow unit tests to import BaseTools python modules. This is very useful\r
+    # for writing unit tests.\r
+    #\r
+    sys.path.append(PythonSourceDir)\r
+\r
+def MakeTheTestSuite(localItems):\r
+    tests = []\r
+    for name, item in localItems.iteritems():\r
+        if isinstance(item, type):\r
+            if issubclass(item, unittest.TestCase):\r
+                tests.append(unittest.TestLoader().loadTestsFromTestCase(item))\r
+            elif issubclass(item, unittest.TestSuite):\r
+                tests.append(item())\r
+    return lambda: unittest.TestSuite(tests)\r
+\r
+def GetBaseToolsPaths():\r
+    if sys.platform in ('win32', 'win64'):\r
+        return [ os.path.join(BaseToolsDir, 'Bin', sys.platform.title()) ]\r
+    else:\r
+        uname = os.popen('uname -sm').read().strip()\r
+        for char in (' ', '/'):\r
+            uname = uname.replace(char, '-')\r
+        return [\r
+                os.path.join(BaseToolsDir, 'Bin', uname),\r
+                os.path.join(BaseToolsDir, 'BinWrappers', uname),\r
+                os.path.join(BaseToolsDir, 'BinWrappers', 'PosixLike')\r
+            ]\r
+\r
+BaseToolsBinPaths = GetBaseToolsPaths()\r
+\r
+class BaseToolsTest(unittest.TestCase):\r
+\r
+    def cleanOutDir(self, dir):\r
+        for dirItem in os.listdir(dir):\r
+            if dirItem in ('.', '..'): continue\r
+            dirItem = os.path.join(dir, dirItem)\r
+            self.RemoveFileOrDir(dirItem)\r
+\r
+    def CleanUpTmpDir(self):\r
+        if os.path.exists(self.testDir):\r
+            self.cleanOutDir(self.testDir)\r
+\r
+    def HandleTreeDeleteError(self, function, path, excinfo):\r
+        os.chmod(path, stat.S_IWRITE)\r
+        function(path)\r
+    \r
+    def RemoveDir(self, dir):\r
+        shutil.rmtree(dir, False, self.HandleTreeDeleteError)\r
+\r
+    def RemoveFileOrDir(self, path):\r
+        if not os.path.exists(path):\r
+            return\r
+        elif os.path.isdir(path):\r
+            self.RemoveDir(path)\r
+        else:\r
+            os.remove(path)\r
+\r
+    def DisplayBinaryData(self, description, data):\r
+        print(description, '(base64 encoded):')\r
+        b64data = base64.b64encode(data)\r
+        print(b64data)\r
+\r
+    def DisplayFile(self, fileName):\r
+        sys.stdout.write(self.ReadTmpFile(fileName))\r
+        sys.stdout.flush()\r
+\r
+    def FindToolBin(self, toolName):\r
+        for binPath in BaseToolsBinPaths:\r
+            bin = os.path.join(binPath, toolName)\r
+            if os.path.exists(bin):\r
+                break\r
+        assert os.path.exists(bin)\r
+        return bin\r
+\r
+    def RunTool(self, *args, **kwd):\r
+        if 'toolName' in kwd: toolName = kwd['toolName']\r
+        else: toolName = None\r
+        if 'logFile' in kwd: logFile = kwd['logFile']\r
+        else: logFile = None\r
+\r
+        if toolName is None: toolName = self.toolName\r
+        bin = self.FindToolBin(toolName)\r
+        if logFile is not None:\r
+            logFile = open(os.path.join(self.testDir, logFile), 'w')\r
+            popenOut = logFile\r
+        else:\r
+            popenOut = subprocess.PIPE\r
+\r
+        args = [toolName] + list(args)\r
+\r
+        Proc = subprocess.Popen(\r
+            args, executable=bin,\r
+            stdout=popenOut, stderr=subprocess.STDOUT\r
+            )\r
+\r
+        if logFile is None:\r
+            Proc.stdout.read()\r
+\r
+        return Proc.wait()\r
+\r
+    def GetTmpFilePath(self, fileName):\r
+        return os.path.join(self.testDir, fileName)\r
+\r
+    def OpenTmpFile(self, fileName, mode = 'r'):\r
+        return open(os.path.join(self.testDir, fileName), mode)\r
+\r
+    def ReadTmpFile(self, fileName):\r
+        f = open(self.GetTmpFilePath(fileName), 'r')\r
+        data = f.read()\r
+        f.close()\r
+        return data\r
+\r
+    def WriteTmpFile(self, fileName, data):\r
+        f = open(self.GetTmpFilePath(fileName), 'w')\r
+        f.write(data)\r
+        f.close()\r
+\r
+    def GenRandomFileData(self, fileName, minlen = None, maxlen = None):\r
+        if maxlen is None: maxlen = minlen\r
+        f = self.OpenTmpFile(fileName, 'w')\r
+        f.write(self.GetRandomString(minlen, maxlen))\r
+        f.close()\r
+\r
+    def GetRandomString(self, minlen = None, maxlen = None):\r
+        if minlen is None: minlen = 1024\r
+        if maxlen is None: maxlen = minlen\r
+        return ''.join(\r
+            [chr(random.randint(0,255))\r
+             for x in xrange(random.randint(minlen, maxlen))\r
+            ])\r
+\r
+    def setUp(self):\r
+        self.savedEnvPath = os.environ['PATH']\r
+        self.savedSysPath = sys.path[:]\r
+\r
+        for binPath in BaseToolsBinPaths:\r
+            os.environ['PATH'] = \\r
+                os.path.pathsep.join((os.environ['PATH'], binPath))\r
+\r
+        self.testDir = TestTempDir\r
+        if not os.path.exists(self.testDir):\r
+            os.mkdir(self.testDir)\r
+        else:\r
+            self.cleanOutDir(self.testDir)\r
+\r
+    def tearDown(self):\r
+        self.RemoveFileOrDir(self.testDir)\r
+\r
+        os.environ['PATH'] = self.savedEnvPath\r
+        sys.path = self.savedSysPath\r
+\r