BaseTools: Remove types.TypeType
[mirror_edk2.git] / BaseTools / Tests / TestTools.py
CommitLineData
72443dd2 1from __future__ import print_function\r
f51461c8
LG
2## @file\r
3# Utility functions and classes for BaseTools unit tests\r
4#\r
53fc4ba2 5# Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
f51461c8
LG
6#\r
7# This program and the accompanying materials\r
8# are licensed and made available under the terms and conditions of the BSD License\r
9# which accompanies this distribution. The full text of the license may be found at\r
10# http://opensource.org/licenses/bsd-license.php\r
11#\r
12# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14#\r
15\r
16##\r
17# Import Modules\r
18#\r
19import base64\r
20import os\r
21import os.path\r
22import random\r
23import shutil\r
24import subprocess\r
25import sys\r
f51461c8
LG
26import unittest\r
27\r
28TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])\r
29BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))\r
30CSourceDir = os.path.join(BaseToolsDir, 'Source', 'C')\r
31PythonSourceDir = os.path.join(BaseToolsDir, 'Source', 'Python')\r
32TestTempDir = os.path.join(TestsDir, 'TestTempDir')\r
33\r
53fc4ba2
JJ
34if PythonSourceDir not in sys.path:\r
35 #\r
36 # Allow unit tests to import BaseTools python modules. This is very useful\r
37 # for writing unit tests.\r
38 #\r
39 sys.path.append(PythonSourceDir)\r
40\r
f51461c8
LG
41def MakeTheTestSuite(localItems):\r
42 tests = []\r
43 for name, item in localItems.iteritems():\r
aaa4c651 44 if isinstance(item, type):\r
f51461c8
LG
45 if issubclass(item, unittest.TestCase):\r
46 tests.append(unittest.TestLoader().loadTestsFromTestCase(item))\r
47 elif issubclass(item, unittest.TestSuite):\r
48 tests.append(item())\r
49 return lambda: unittest.TestSuite(tests)\r
50\r
51def GetBaseToolsPaths():\r
52 if sys.platform in ('win32', 'win64'):\r
53 return [ os.path.join(BaseToolsDir, 'Bin', sys.platform.title()) ]\r
54 else:\r
55 uname = os.popen('uname -sm').read().strip()\r
56 for char in (' ', '/'):\r
57 uname = uname.replace(char, '-')\r
58 return [\r
59 os.path.join(BaseToolsDir, 'Bin', uname),\r
60 os.path.join(BaseToolsDir, 'BinWrappers', uname),\r
61 os.path.join(BaseToolsDir, 'BinWrappers', 'PosixLike')\r
62 ]\r
63\r
64BaseToolsBinPaths = GetBaseToolsPaths()\r
65\r
66class BaseToolsTest(unittest.TestCase):\r
67\r
68 def cleanOutDir(self, dir):\r
69 for dirItem in os.listdir(dir):\r
70 if dirItem in ('.', '..'): continue\r
71 dirItem = os.path.join(dir, dirItem)\r
72 self.RemoveFileOrDir(dirItem)\r
73\r
74 def CleanUpTmpDir(self):\r
75 if os.path.exists(self.testDir):\r
76 self.cleanOutDir(self.testDir)\r
77\r
78 def HandleTreeDeleteError(self, function, path, excinfo):\r
79 os.chmod(path, stat.S_IWRITE)\r
80 function(path)\r
81 \r
82 def RemoveDir(self, dir):\r
83 shutil.rmtree(dir, False, self.HandleTreeDeleteError)\r
84\r
85 def RemoveFileOrDir(self, path):\r
86 if not os.path.exists(path):\r
87 return\r
88 elif os.path.isdir(path):\r
89 self.RemoveDir(path)\r
90 else:\r
91 os.remove(path)\r
92\r
93 def DisplayBinaryData(self, description, data):\r
72443dd2 94 print(description, '(base64 encoded):')\r
f51461c8 95 b64data = base64.b64encode(data)\r
72443dd2 96 print(b64data)\r
f51461c8
LG
97\r
98 def DisplayFile(self, fileName):\r
99 sys.stdout.write(self.ReadTmpFile(fileName))\r
100 sys.stdout.flush()\r
101\r
102 def FindToolBin(self, toolName):\r
103 for binPath in BaseToolsBinPaths:\r
104 bin = os.path.join(binPath, toolName)\r
105 if os.path.exists(bin):\r
106 break\r
107 assert os.path.exists(bin)\r
108 return bin\r
109\r
110 def RunTool(self, *args, **kwd):\r
111 if 'toolName' in kwd: toolName = kwd['toolName']\r
112 else: toolName = None\r
113 if 'logFile' in kwd: logFile = kwd['logFile']\r
114 else: logFile = None\r
115\r
116 if toolName is None: toolName = self.toolName\r
117 bin = self.FindToolBin(toolName)\r
118 if logFile is not None:\r
119 logFile = open(os.path.join(self.testDir, logFile), 'w')\r
120 popenOut = logFile\r
121 else:\r
122 popenOut = subprocess.PIPE\r
123\r
124 args = [toolName] + list(args)\r
125\r
126 Proc = subprocess.Popen(\r
127 args, executable=bin,\r
128 stdout=popenOut, stderr=subprocess.STDOUT\r
129 )\r
130\r
131 if logFile is None:\r
132 Proc.stdout.read()\r
133\r
134 return Proc.wait()\r
135\r
136 def GetTmpFilePath(self, fileName):\r
137 return os.path.join(self.testDir, fileName)\r
138\r
139 def OpenTmpFile(self, fileName, mode = 'r'):\r
140 return open(os.path.join(self.testDir, fileName), mode)\r
141\r
142 def ReadTmpFile(self, fileName):\r
143 f = open(self.GetTmpFilePath(fileName), 'r')\r
144 data = f.read()\r
145 f.close()\r
146 return data\r
147\r
148 def WriteTmpFile(self, fileName, data):\r
149 f = open(self.GetTmpFilePath(fileName), 'w')\r
150 f.write(data)\r
151 f.close()\r
152\r
153 def GenRandomFileData(self, fileName, minlen = None, maxlen = None):\r
154 if maxlen is None: maxlen = minlen\r
155 f = self.OpenTmpFile(fileName, 'w')\r
156 f.write(self.GetRandomString(minlen, maxlen))\r
157 f.close()\r
158\r
159 def GetRandomString(self, minlen = None, maxlen = None):\r
160 if minlen is None: minlen = 1024\r
161 if maxlen is None: maxlen = minlen\r
162 return ''.join(\r
163 [chr(random.randint(0,255))\r
164 for x in xrange(random.randint(minlen, maxlen))\r
165 ])\r
166\r
167 def setUp(self):\r
168 self.savedEnvPath = os.environ['PATH']\r
169 self.savedSysPath = sys.path[:]\r
170\r
171 for binPath in BaseToolsBinPaths:\r
172 os.environ['PATH'] = \\r
173 os.path.pathsep.join((os.environ['PATH'], binPath))\r
174\r
175 self.testDir = TestTempDir\r
176 if not os.path.exists(self.testDir):\r
177 os.mkdir(self.testDir)\r
178 else:\r
179 self.cleanOutDir(self.testDir)\r
180\r
181 def tearDown(self):\r
182 self.RemoveFileOrDir(self.testDir)\r
183\r
184 os.environ['PATH'] = self.savedEnvPath\r
185 sys.path = self.savedSysPath\r
186\r