]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Tests/TestTools.py
BaseTools: Refactor python print statements
[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
26import types\r
27import unittest\r
28\r
29TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])\r
30BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))\r
31CSourceDir = os.path.join(BaseToolsDir, 'Source', 'C')\r
32PythonSourceDir = os.path.join(BaseToolsDir, 'Source', 'Python')\r
33TestTempDir = os.path.join(TestsDir, 'TestTempDir')\r
34\r
53fc4ba2
JJ
35if PythonSourceDir not in sys.path:\r
36 #\r
37 # Allow unit tests to import BaseTools python modules. This is very useful\r
38 # for writing unit tests.\r
39 #\r
40 sys.path.append(PythonSourceDir)\r
41\r
f51461c8
LG
42def MakeTheTestSuite(localItems):\r
43 tests = []\r
44 for name, item in localItems.iteritems():\r
45 if isinstance(item, types.TypeType):\r
46 if issubclass(item, unittest.TestCase):\r
47 tests.append(unittest.TestLoader().loadTestsFromTestCase(item))\r
48 elif issubclass(item, unittest.TestSuite):\r
49 tests.append(item())\r
50 return lambda: unittest.TestSuite(tests)\r
51\r
52def GetBaseToolsPaths():\r
53 if sys.platform in ('win32', 'win64'):\r
54 return [ os.path.join(BaseToolsDir, 'Bin', sys.platform.title()) ]\r
55 else:\r
56 uname = os.popen('uname -sm').read().strip()\r
57 for char in (' ', '/'):\r
58 uname = uname.replace(char, '-')\r
59 return [\r
60 os.path.join(BaseToolsDir, 'Bin', uname),\r
61 os.path.join(BaseToolsDir, 'BinWrappers', uname),\r
62 os.path.join(BaseToolsDir, 'BinWrappers', 'PosixLike')\r
63 ]\r
64\r
65BaseToolsBinPaths = GetBaseToolsPaths()\r
66\r
67class BaseToolsTest(unittest.TestCase):\r
68\r
69 def cleanOutDir(self, dir):\r
70 for dirItem in os.listdir(dir):\r
71 if dirItem in ('.', '..'): continue\r
72 dirItem = os.path.join(dir, dirItem)\r
73 self.RemoveFileOrDir(dirItem)\r
74\r
75 def CleanUpTmpDir(self):\r
76 if os.path.exists(self.testDir):\r
77 self.cleanOutDir(self.testDir)\r
78\r
79 def HandleTreeDeleteError(self, function, path, excinfo):\r
80 os.chmod(path, stat.S_IWRITE)\r
81 function(path)\r
82 \r
83 def RemoveDir(self, dir):\r
84 shutil.rmtree(dir, False, self.HandleTreeDeleteError)\r
85\r
86 def RemoveFileOrDir(self, path):\r
87 if not os.path.exists(path):\r
88 return\r
89 elif os.path.isdir(path):\r
90 self.RemoveDir(path)\r
91 else:\r
92 os.remove(path)\r
93\r
94 def DisplayBinaryData(self, description, data):\r
72443dd2 95 print(description, '(base64 encoded):')\r
f51461c8 96 b64data = base64.b64encode(data)\r
72443dd2 97 print(b64data)\r
f51461c8
LG
98\r
99 def DisplayFile(self, fileName):\r
100 sys.stdout.write(self.ReadTmpFile(fileName))\r
101 sys.stdout.flush()\r
102\r
103 def FindToolBin(self, toolName):\r
104 for binPath in BaseToolsBinPaths:\r
105 bin = os.path.join(binPath, toolName)\r
106 if os.path.exists(bin):\r
107 break\r
108 assert os.path.exists(bin)\r
109 return bin\r
110\r
111 def RunTool(self, *args, **kwd):\r
112 if 'toolName' in kwd: toolName = kwd['toolName']\r
113 else: toolName = None\r
114 if 'logFile' in kwd: logFile = kwd['logFile']\r
115 else: logFile = None\r
116\r
117 if toolName is None: toolName = self.toolName\r
118 bin = self.FindToolBin(toolName)\r
119 if logFile is not None:\r
120 logFile = open(os.path.join(self.testDir, logFile), 'w')\r
121 popenOut = logFile\r
122 else:\r
123 popenOut = subprocess.PIPE\r
124\r
125 args = [toolName] + list(args)\r
126\r
127 Proc = subprocess.Popen(\r
128 args, executable=bin,\r
129 stdout=popenOut, stderr=subprocess.STDOUT\r
130 )\r
131\r
132 if logFile is None:\r
133 Proc.stdout.read()\r
134\r
135 return Proc.wait()\r
136\r
137 def GetTmpFilePath(self, fileName):\r
138 return os.path.join(self.testDir, fileName)\r
139\r
140 def OpenTmpFile(self, fileName, mode = 'r'):\r
141 return open(os.path.join(self.testDir, fileName), mode)\r
142\r
143 def ReadTmpFile(self, fileName):\r
144 f = open(self.GetTmpFilePath(fileName), 'r')\r
145 data = f.read()\r
146 f.close()\r
147 return data\r
148\r
149 def WriteTmpFile(self, fileName, data):\r
150 f = open(self.GetTmpFilePath(fileName), 'w')\r
151 f.write(data)\r
152 f.close()\r
153\r
154 def GenRandomFileData(self, fileName, minlen = None, maxlen = None):\r
155 if maxlen is None: maxlen = minlen\r
156 f = self.OpenTmpFile(fileName, 'w')\r
157 f.write(self.GetRandomString(minlen, maxlen))\r
158 f.close()\r
159\r
160 def GetRandomString(self, minlen = None, maxlen = None):\r
161 if minlen is None: minlen = 1024\r
162 if maxlen is None: maxlen = minlen\r
163 return ''.join(\r
164 [chr(random.randint(0,255))\r
165 for x in xrange(random.randint(minlen, maxlen))\r
166 ])\r
167\r
168 def setUp(self):\r
169 self.savedEnvPath = os.environ['PATH']\r
170 self.savedSysPath = sys.path[:]\r
171\r
172 for binPath in BaseToolsBinPaths:\r
173 os.environ['PATH'] = \\r
174 os.path.pathsep.join((os.environ['PATH'], binPath))\r
175\r
176 self.testDir = TestTempDir\r
177 if not os.path.exists(self.testDir):\r
178 os.mkdir(self.testDir)\r
179 else:\r
180 self.cleanOutDir(self.testDir)\r
181\r
182 def tearDown(self):\r
183 self.RemoveFileOrDir(self.testDir)\r
184\r
185 os.environ['PATH'] = self.savedEnvPath\r
186 sys.path = self.savedSysPath\r
187\r