]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Tests/TestTools.py
1 from __future__
import print_function
3 # Utility functions and classes for BaseTools unit tests
5 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
7 # This program and the accompanying materials
8 # are licensed and made available under the terms and conditions of the BSD License
9 # which accompanies this distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
29 TestsDir
= os
.path
.realpath(os
.path
.split(sys
.argv
[0])[0])
30 BaseToolsDir
= os
.path
.realpath(os
.path
.join(TestsDir
, '..'))
31 CSourceDir
= os
.path
.join(BaseToolsDir
, 'Source', 'C')
32 PythonSourceDir
= os
.path
.join(BaseToolsDir
, 'Source', 'Python')
33 TestTempDir
= os
.path
.join(TestsDir
, 'TestTempDir')
35 if PythonSourceDir
not in sys
.path
:
37 # Allow unit tests to import BaseTools python modules. This is very useful
38 # for writing unit tests.
40 sys
.path
.append(PythonSourceDir
)
42 def MakeTheTestSuite(localItems
):
44 for name
, item
in localItems
.items():
45 if isinstance(item
, type):
46 if issubclass(item
, unittest
.TestCase
):
47 tests
.append(unittest
.TestLoader().loadTestsFromTestCase(item
))
48 elif issubclass(item
, unittest
.TestSuite
):
50 return lambda: unittest
.TestSuite(tests
)
52 def GetBaseToolsPaths():
53 if sys
.platform
in ('win32', 'win64'):
54 return [ os
.path
.join(BaseToolsDir
, 'Bin', sys
.platform
.title()) ]
56 uname
= os
.popen('uname -sm').read().strip()
57 for char
in (' ', '/'):
58 uname
= uname
.replace(char
, '-')
60 os
.path
.join(BaseToolsDir
, 'Bin', uname
),
61 os
.path
.join(BaseToolsDir
, 'BinWrappers', uname
),
62 os
.path
.join(BaseToolsDir
, 'BinWrappers', 'PosixLike')
65 BaseToolsBinPaths
= GetBaseToolsPaths()
67 class BaseToolsTest(unittest
.TestCase
):
69 def cleanOutDir(self
, dir):
70 for dirItem
in os
.listdir(dir):
71 if dirItem
in ('.', '..'): continue
72 dirItem
= os
.path
.join(dir, dirItem
)
73 self
.RemoveFileOrDir(dirItem
)
75 def CleanUpTmpDir(self
):
76 if os
.path
.exists(self
.testDir
):
77 self
.cleanOutDir(self
.testDir
)
79 def HandleTreeDeleteError(self
, function
, path
, excinfo
):
80 os
.chmod(path
, stat
.S_IWRITE
)
83 def RemoveDir(self
, dir):
84 shutil
.rmtree(dir, False, self
.HandleTreeDeleteError
)
86 def RemoveFileOrDir(self
, path
):
87 if not os
.path
.exists(path
):
89 elif os
.path
.isdir(path
):
94 def DisplayBinaryData(self
, description
, data
):
95 print(description
, '(base64 encoded):')
96 b64data
= base64
.b64encode(data
)
99 def DisplayFile(self
, fileName
):
100 sys
.stdout
.write(self
.ReadTmpFile(fileName
))
103 def FindToolBin(self
, toolName
):
104 for binPath
in BaseToolsBinPaths
:
105 bin
= os
.path
.join(binPath
, toolName
)
106 if os
.path
.exists(bin
):
108 assert os
.path
.exists(bin
)
111 def RunTool(self
, *args
, **kwd
):
112 if 'toolName' in kwd
: toolName
= kwd
['toolName']
113 else: toolName
= None
114 if 'logFile' in kwd
: logFile
= kwd
['logFile']
117 if toolName
is None: toolName
= self
.toolName
118 bin
= self
.FindToolBin(toolName
)
119 if logFile
is not None:
120 logFile
= open(os
.path
.join(self
.testDir
, logFile
), 'w')
123 popenOut
= subprocess
.PIPE
125 args
= [toolName
] + list(args
)
127 Proc
= subprocess
.Popen(
128 args
, executable
=bin
,
129 stdout
=popenOut
, stderr
=subprocess
.STDOUT
137 def GetTmpFilePath(self
, fileName
):
138 return os
.path
.join(self
.testDir
, fileName
)
140 def OpenTmpFile(self
, fileName
, mode
= 'r'):
141 return open(os
.path
.join(self
.testDir
, fileName
), mode
)
143 def ReadTmpFile(self
, fileName
):
144 f
= open(self
.GetTmpFilePath(fileName
), 'r')
149 def WriteTmpFile(self
, fileName
, data
):
150 if isinstance(data
, bytes
):
151 with
open(self
.GetTmpFilePath(fileName
), 'wb') as f
:
154 with codecs
.open(self
.GetTmpFilePath(fileName
), 'w', encoding
='utf-8') as f
:
157 def GenRandomFileData(self
, fileName
, minlen
= None, maxlen
= None):
158 if maxlen
is None: maxlen
= minlen
159 f
= self
.OpenTmpFile(fileName
, 'w')
160 f
.write(self
.GetRandomString(minlen
, maxlen
))
163 def GetRandomString(self
, minlen
= None, maxlen
= None):
164 if minlen
is None: minlen
= 1024
165 if maxlen
is None: maxlen
= minlen
167 [chr(random
.randint(0, 255))
168 for x
in range(random
.randint(minlen
, maxlen
))
172 self
.savedEnvPath
= os
.environ
['PATH']
173 self
.savedSysPath
= sys
.path
[:]
175 for binPath
in BaseToolsBinPaths
:
176 os
.environ
['PATH'] = \
177 os
.path
.pathsep
.join((os
.environ
['PATH'], binPath
))
179 self
.testDir
= TestTempDir
180 if not os
.path
.exists(self
.testDir
):
181 os
.mkdir(self
.testDir
)
183 self
.cleanOutDir(self
.testDir
)
186 self
.RemoveFileOrDir(self
.testDir
)
188 os
.environ
['PATH'] = self
.savedEnvPath
189 sys
.path
= self
.savedSysPath