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