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