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