1 | ## @file
|
---|
2 | # Utility functions and classes for BaseTools unit tests
|
---|
3 | #
|
---|
4 | # Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
|
---|
5 | #
|
---|
6 | # 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 GetBaseToolsPaths():
|
---|
45 | if sys.platform in ('win32', 'win64'):
|
---|
46 | return [ os.path.join(BaseToolsDir, 'Bin', sys.platform.title()) ]
|
---|
47 | else:
|
---|
48 | uname = os.popen('uname -sm').read().strip()
|
---|
49 | for char in (' ', '/'):
|
---|
50 | uname = uname.replace(char, '-')
|
---|
51 | return [
|
---|
52 | os.path.join(BaseToolsDir, 'Bin', uname),
|
---|
53 | os.path.join(BaseToolsDir, 'BinWrappers', uname),
|
---|
54 | os.path.join(BaseToolsDir, 'BinWrappers', 'PosixLike')
|
---|
55 | ]
|
---|
56 |
|
---|
57 | BaseToolsBinPaths = GetBaseToolsPaths()
|
---|
58 |
|
---|
59 | class BaseToolsTest(unittest.TestCase):
|
---|
60 |
|
---|
61 | def cleanOutDir(self, dir):
|
---|
62 | for dirItem in os.listdir(dir):
|
---|
63 | if dirItem in ('.', '..'): continue
|
---|
64 | dirItem = os.path.join(dir, dirItem)
|
---|
65 | self.RemoveFileOrDir(dirItem)
|
---|
66 |
|
---|
67 | def CleanUpTmpDir(self):
|
---|
68 | if os.path.exists(self.testDir):
|
---|
69 | self.cleanOutDir(self.testDir)
|
---|
70 |
|
---|
71 | def HandleTreeDeleteError(self, function, path, excinfo):
|
---|
72 | os.chmod(path, stat.S_IWRITE)
|
---|
73 | function(path)
|
---|
74 |
|
---|
75 | def RemoveDir(self, dir):
|
---|
76 | shutil.rmtree(dir, False, self.HandleTreeDeleteError)
|
---|
77 |
|
---|
78 | def RemoveFileOrDir(self, path):
|
---|
79 | if not os.path.exists(path):
|
---|
80 | return
|
---|
81 | elif os.path.isdir(path):
|
---|
82 | self.RemoveDir(path)
|
---|
83 | else:
|
---|
84 | os.remove(path)
|
---|
85 |
|
---|
86 | def DisplayBinaryData(self, description, data):
|
---|
87 | print description, '(base64 encoded):'
|
---|
88 | b64data = base64.b64encode(data)
|
---|
89 | print b64data
|
---|
90 |
|
---|
91 | def DisplayFile(self, fileName):
|
---|
92 | sys.stdout.write(self.ReadTmpFile(fileName))
|
---|
93 | sys.stdout.flush()
|
---|
94 |
|
---|
95 | def FindToolBin(self, toolName):
|
---|
96 | for binPath in BaseToolsBinPaths:
|
---|
97 | bin = os.path.join(binPath, toolName)
|
---|
98 | if os.path.exists(bin):
|
---|
99 | break
|
---|
100 | assert os.path.exists(bin)
|
---|
101 | return bin
|
---|
102 |
|
---|
103 | def RunTool(self, *args, **kwd):
|
---|
104 | if 'toolName' in kwd: toolName = kwd['toolName']
|
---|
105 | else: toolName = None
|
---|
106 | if 'logFile' in kwd: logFile = kwd['logFile']
|
---|
107 | else: logFile = None
|
---|
108 |
|
---|
109 | if toolName is None: toolName = self.toolName
|
---|
110 | bin = self.FindToolBin(toolName)
|
---|
111 | if logFile is not None:
|
---|
112 | logFile = open(os.path.join(self.testDir, logFile), 'w')
|
---|
113 | popenOut = logFile
|
---|
114 | else:
|
---|
115 | popenOut = subprocess.PIPE
|
---|
116 |
|
---|
117 | args = [toolName] + list(args)
|
---|
118 |
|
---|
119 | Proc = subprocess.Popen(
|
---|
120 | args, executable=bin,
|
---|
121 | stdout=popenOut, stderr=subprocess.STDOUT
|
---|
122 | )
|
---|
123 |
|
---|
124 | if logFile is None:
|
---|
125 | Proc.stdout.read()
|
---|
126 |
|
---|
127 | return Proc.wait()
|
---|
128 |
|
---|
129 | def GetTmpFilePath(self, fileName):
|
---|
130 | return os.path.join(self.testDir, fileName)
|
---|
131 |
|
---|
132 | def OpenTmpFile(self, fileName, mode = 'r'):
|
---|
133 | return open(os.path.join(self.testDir, fileName), mode)
|
---|
134 |
|
---|
135 | def ReadTmpFile(self, fileName):
|
---|
136 | f = open(self.GetTmpFilePath(fileName), 'r')
|
---|
137 | data = f.read()
|
---|
138 | f.close()
|
---|
139 | return data
|
---|
140 |
|
---|
141 | def WriteTmpFile(self, fileName, data):
|
---|
142 | f = open(self.GetTmpFilePath(fileName), 'w')
|
---|
143 | f.write(data)
|
---|
144 | f.close()
|
---|
145 |
|
---|
146 | def GenRandomFileData(self, fileName, minlen = None, maxlen = None):
|
---|
147 | if maxlen is None: maxlen = minlen
|
---|
148 | f = self.OpenTmpFile(fileName, 'w')
|
---|
149 | f.write(self.GetRandomString(minlen, maxlen))
|
---|
150 | f.close()
|
---|
151 |
|
---|
152 | def GetRandomString(self, minlen = None, maxlen = None):
|
---|
153 | if minlen is None: minlen = 1024
|
---|
154 | if maxlen is None: maxlen = minlen
|
---|
155 | return ''.join(
|
---|
156 | [chr(random.randint(0,255))
|
---|
157 | for x in xrange(random.randint(minlen, maxlen))
|
---|
158 | ])
|
---|
159 |
|
---|
160 | def setUp(self):
|
---|
161 | self.savedEnvPath = os.environ['PATH']
|
---|
162 | self.savedSysPath = sys.path[:]
|
---|
163 |
|
---|
164 | for binPath in BaseToolsBinPaths:
|
---|
165 | os.environ['PATH'] = \
|
---|
166 | os.path.pathsep.join((os.environ['PATH'], binPath))
|
---|
167 |
|
---|
168 | self.testDir = TestTempDir
|
---|
169 | if not os.path.exists(self.testDir):
|
---|
170 | os.mkdir(self.testDir)
|
---|
171 | else:
|
---|
172 | self.cleanOutDir(self.testDir)
|
---|
173 |
|
---|
174 | def tearDown(self):
|
---|
175 | self.RemoveFileOrDir(self.testDir)
|
---|
176 |
|
---|
177 | os.environ['PATH'] = self.savedEnvPath
|
---|
178 | sys.path = self.savedSysPath
|
---|
179 |
|
---|