VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/BaseTools/Tests/TestTools.py@ 107675

最後變更 在這個檔案從107675是 101291,由 vboxsync 提交於 18 月 前

EFI/FirmwareNew: Make edk2-stable202308 build on all supported platforms (using gcc at least, msvc not tested yet), bugref:4643

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette