]>
Commit | Line | Data |
---|---|---|
72443dd2 | 1 | from __future__ import print_function\r |
f51461c8 LG |
2 | ## @file\r |
3 | # Utility functions and classes for BaseTools unit tests\r | |
4 | #\r | |
f7496d71 | 5 | # Copyright (c) 2008 - 2018, 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 | |
19 | import base64\r | |
20 | import os\r | |
21 | import os.path\r | |
22 | import random\r | |
23 | import shutil\r | |
24 | import subprocess\r | |
25 | import sys\r | |
f51461c8 | 26 | import unittest\r |
31e3eeb5 | 27 | import codecs\r |
f51461c8 LG |
28 | \r |
29 | TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])\r | |
30 | BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))\r | |
31 | CSourceDir = os.path.join(BaseToolsDir, 'Source', 'C')\r | |
32 | PythonSourceDir = os.path.join(BaseToolsDir, 'Source', 'Python')\r | |
33 | TestTempDir = os.path.join(TestsDir, 'TestTempDir')\r | |
34 | \r | |
53fc4ba2 JJ |
35 | if 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 |
42 | def MakeTheTestSuite(localItems):\r |
43 | tests = []\r | |
fe906312 | 44 | for name, item in localItems.items():\r |
aaa4c651 | 45 | if isinstance(item, type):\r |
f51461c8 LG |
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 | |
52 | def 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 | |
65 | BaseToolsBinPaths = GetBaseToolsPaths()\r | |
66 | \r | |
67 | class 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 | |
f7496d71 | 82 | \r |
f51461c8 LG |
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 | |
fe906312 ZF |
150 | if isinstance(data, bytes):\r |
151 | with open(self.GetTmpFilePath(fileName), 'wb') as f:\r | |
152 | f.write(data)\r | |
153 | else:\r | |
31e3eeb5 | 154 | with codecs.open(self.GetTmpFilePath(fileName), 'w', encoding='utf-8') as f:\r |
fe906312 | 155 | f.write(data)\r |
f51461c8 LG |
156 | \r |
157 | def GenRandomFileData(self, fileName, minlen = None, maxlen = None):\r | |
158 | if maxlen is None: maxlen = minlen\r | |
159 | f = self.OpenTmpFile(fileName, 'w')\r | |
160 | f.write(self.GetRandomString(minlen, maxlen))\r | |
161 | f.close()\r | |
162 | \r | |
163 | def GetRandomString(self, minlen = None, maxlen = None):\r | |
164 | if minlen is None: minlen = 1024\r | |
165 | if maxlen is None: maxlen = minlen\r | |
166 | return ''.join(\r | |
ccaa7754 | 167 | [chr(random.randint(0, 255))\r |
fe906312 | 168 | for x in range(random.randint(minlen, maxlen))\r |
f51461c8 LG |
169 | ])\r |
170 | \r | |
171 | def setUp(self):\r | |
172 | self.savedEnvPath = os.environ['PATH']\r | |
173 | self.savedSysPath = sys.path[:]\r | |
174 | \r | |
175 | for binPath in BaseToolsBinPaths:\r | |
176 | os.environ['PATH'] = \\r | |
177 | os.path.pathsep.join((os.environ['PATH'], binPath))\r | |
178 | \r | |
179 | self.testDir = TestTempDir\r | |
180 | if not os.path.exists(self.testDir):\r | |
181 | os.mkdir(self.testDir)\r | |
182 | else:\r | |
183 | self.cleanOutDir(self.testDir)\r | |
184 | \r | |
185 | def tearDown(self):\r | |
186 | self.RemoveFileOrDir(self.testDir)\r | |
187 | \r | |
188 | os.environ['PATH'] = self.savedEnvPath\r | |
189 | sys.path = self.savedSysPath\r | |
1ccc4d89 | 190 | \r |