sl@0
|
1 |
# Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
# All rights reserved.
|
sl@0
|
3 |
# This component and the accompanying materials are made available
|
sl@0
|
4 |
# under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
# which accompanies this distribution, and is available
|
sl@0
|
6 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
#
|
sl@0
|
8 |
# Initial Contributors:
|
sl@0
|
9 |
# Nokia Corporation - initial contribution.
|
sl@0
|
10 |
#
|
sl@0
|
11 |
# Contributors:
|
sl@0
|
12 |
#
|
sl@0
|
13 |
# Description:
|
sl@0
|
14 |
# Test code for MbcUtils
|
sl@0
|
15 |
#
|
sl@0
|
16 |
|
sl@0
|
17 |
from MbcUtils import *
|
sl@0
|
18 |
import testDataMbcUtils as testdata
|
sl@0
|
19 |
import unittest
|
sl@0
|
20 |
import tempfile
|
sl@0
|
21 |
import StringIO
|
sl@0
|
22 |
|
sl@0
|
23 |
class TestParent(unittest.TestCase):
|
sl@0
|
24 |
"""Potential parent test"""
|
sl@0
|
25 |
|
sl@0
|
26 |
def TempFile(self, str):
|
sl@0
|
27 |
"""Create a temp file. Write given string and return filename.
|
sl@0
|
28 |
Note caller is responsible for deleting the file"""
|
sl@0
|
29 |
tfile = tempfile.mkstemp()
|
sl@0
|
30 |
file = os.fdopen(tfile[0],"w")
|
sl@0
|
31 |
print >> file, str
|
sl@0
|
32 |
file.close() # close the file - we just want the filename then
|
sl@0
|
33 |
return tfile[1] # return filename. Note client expected to delete
|
sl@0
|
34 |
|
sl@0
|
35 |
def fixupDirsInExpectedList(self, expectedToFixUp):
|
sl@0
|
36 |
"""Convert relative filenames to absolute.
|
sl@0
|
37 |
The returned list returns absolute filenames. Need to convert relelative
|
sl@0
|
38 |
names to absolute so can easily compare."""
|
sl@0
|
39 |
|
sl@0
|
40 |
for tupEle in expectedToFixUp:
|
sl@0
|
41 |
mbcFile = tupEle[0]
|
sl@0
|
42 |
baseDir = os.path.dirname(mbcFile)
|
sl@0
|
43 |
for i in range(1,3):
|
sl@0
|
44 |
# tupEle[1] is list of required folders
|
sl@0
|
45 |
# tupEle[2] is list of optional folders
|
sl@0
|
46 |
for j in range(0, len(tupEle[i])):
|
sl@0
|
47 |
absDir = os.path.abspath(os.path.join(baseDir, tupEle[i][j]))
|
sl@0
|
48 |
tupEle[i][j] = absDir
|
sl@0
|
49 |
# tupEle[3] is list of tuples of dir and command
|
sl@0
|
50 |
for j in range(0, len(tupEle[3])):
|
sl@0
|
51 |
# note need to tuple to list and back to do this
|
sl@0
|
52 |
lst = list(tupEle[3][j])
|
sl@0
|
53 |
absDir = os.path.abspath(os.path.join(baseDir, lst[0]))
|
sl@0
|
54 |
lst[0] = absDir
|
sl@0
|
55 |
tupEle[3][j] = tuple(lst)
|
sl@0
|
56 |
return expectedToFixUp
|
sl@0
|
57 |
|
sl@0
|
58 |
class SimpleTest(TestParent):
|
sl@0
|
59 |
"""Open and return basic case"""
|
sl@0
|
60 |
|
sl@0
|
61 |
def __init__(self, sample, expected):
|
sl@0
|
62 |
super(SimpleTest,self).__init__()
|
sl@0
|
63 |
self.sample = sample
|
sl@0
|
64 |
self.expected = expected
|
sl@0
|
65 |
self.tfileName = None
|
sl@0
|
66 |
|
sl@0
|
67 |
def fixExpected(self, fname):
|
sl@0
|
68 |
"""change the filename slot in self.expected"""
|
sl@0
|
69 |
# expected is a tuple. Need to convert to list and back.
|
sl@0
|
70 |
# then turn into a single element list so can run through
|
sl@0
|
71 |
# fixupDirsInExpectedList()
|
sl@0
|
72 |
tempList = list(self.expected)
|
sl@0
|
73 |
tempList[0] = fname
|
sl@0
|
74 |
newTuple = tuple(tempList)
|
sl@0
|
75 |
fixedList = self.fixupDirsInExpectedList([newTuple])
|
sl@0
|
76 |
self.expected = fixedList[0]
|
sl@0
|
77 |
|
sl@0
|
78 |
def runTest(self):
|
sl@0
|
79 |
"""SimpleTest.runTest
|
sl@0
|
80 |
|
sl@0
|
81 |
Create temp file with known content. Run parser and compare result
|
sl@0
|
82 |
"""
|
sl@0
|
83 |
self.tfileName = self.TempFile(self.sample)
|
sl@0
|
84 |
parser = MbcParser(self.tfileName)
|
sl@0
|
85 |
parser.execute()
|
sl@0
|
86 |
self.fixExpected(self.tfileName)
|
sl@0
|
87 |
result = parser()
|
sl@0
|
88 |
self.assertEquals(result, self.expected,
|
sl@0
|
89 |
"Result(%s) not same as Expected(%s)"%(result,self.expected))
|
sl@0
|
90 |
|
sl@0
|
91 |
def tearDown(self):
|
sl@0
|
92 |
if self.tfileName:
|
sl@0
|
93 |
os.unlink(self.tfileName)
|
sl@0
|
94 |
|
sl@0
|
95 |
class BadDataTest(SimpleTest):
|
sl@0
|
96 |
"""Bad data so should get an exception"""
|
sl@0
|
97 |
|
sl@0
|
98 |
def __init__(self, sample, testName):
|
sl@0
|
99 |
super(BadDataTest,self).__init__(sample, [])
|
sl@0
|
100 |
self.__testName = testName
|
sl@0
|
101 |
|
sl@0
|
102 |
def runTest(self):
|
sl@0
|
103 |
"""BadDataTest.runTest
|
sl@0
|
104 |
|
sl@0
|
105 |
Use SimpleTest.runTest. Should throw an exception"""
|
sl@0
|
106 |
self.assertRaises(InvalidInput, SimpleTest.runTest, self)
|
sl@0
|
107 |
|
sl@0
|
108 |
def shortDescription(self):
|
sl@0
|
109 |
"One line description - additionally giving test name"
|
sl@0
|
110 |
parentStr = super(BadDataTest, self).shortDescription()
|
sl@0
|
111 |
return "%s-%s" % (parentStr, self.__testName)
|
sl@0
|
112 |
|
sl@0
|
113 |
class ListTest(TestParent):
|
sl@0
|
114 |
"""Open and return basic case, but with list input"""
|
sl@0
|
115 |
|
sl@0
|
116 |
def __init__(self, samples, expected, secondStageExpected=None, filenameToLookFor=None):
|
sl@0
|
117 |
super(ListTest,self).__init__()
|
sl@0
|
118 |
self.samples = samples
|
sl@0
|
119 |
self.expected = expected
|
sl@0
|
120 |
self.secondStageExpected = secondStageExpected
|
sl@0
|
121 |
self.filenameToLookFor = filenameToLookFor
|
sl@0
|
122 |
self.tfileNames = []
|
sl@0
|
123 |
|
sl@0
|
124 |
def fixExpected(self, indx, fname):
|
sl@0
|
125 |
"""change the filename slot in self.expected"""
|
sl@0
|
126 |
# expected is a tuple. Need to convert to list and back
|
sl@0
|
127 |
tempList = list(self.expected[indx])
|
sl@0
|
128 |
tempList[0] = fname
|
sl@0
|
129 |
self.expected[indx] = tuple(tempList)
|
sl@0
|
130 |
|
sl@0
|
131 |
def runTest(self):
|
sl@0
|
132 |
"""ListTest.runTest
|
sl@0
|
133 |
|
sl@0
|
134 |
Create temp files with known content, one per list element. Run parser and compare result
|
sl@0
|
135 |
"""
|
sl@0
|
136 |
for indx in range(0, len(self.samples)):
|
sl@0
|
137 |
tfileName = self.TempFile(self.samples[indx])
|
sl@0
|
138 |
self.tfileNames.append(tfileName)
|
sl@0
|
139 |
self.fixExpected(indx, tfileName)
|
sl@0
|
140 |
self.expected = self.fixupDirsInExpectedList(self.expected)
|
sl@0
|
141 |
parser = MbcParser(self.tfileNames)
|
sl@0
|
142 |
parser.execute()
|
sl@0
|
143 |
result = parser()
|
sl@0
|
144 |
self.assertEquals(result, self.expected,
|
sl@0
|
145 |
"Result(%s) not same as Expected(%s)"%(result,self.expected))
|
sl@0
|
146 |
if self.secondStageExpected:
|
sl@0
|
147 |
getFolderList = GetFolderList(result)
|
sl@0
|
148 |
getFolderList.execute()
|
sl@0
|
149 |
folderList = getFolderList()
|
sl@0
|
150 |
self.assertEquals(folderList, self.secondStageExpected,
|
sl@0
|
151 |
"Result2(%s) not same as Expected2(%s)"%(folderList,self.secondStageExpected))
|
sl@0
|
152 |
|
sl@0
|
153 |
def tearDown(self):
|
sl@0
|
154 |
if self.tfileNames:
|
sl@0
|
155 |
for fname in self.tfileNames:
|
sl@0
|
156 |
os.unlink(fname)
|
sl@0
|
157 |
|
sl@0
|
158 |
class FolderListUnitTest(TestParent):
|
sl@0
|
159 |
"""Test GetFolderList with on-the-fly generated data.
|
sl@0
|
160 |
|
sl@0
|
161 |
Create a list on fly with folder of one temp file and a non-existant one. Treatement of second
|
sl@0
|
162 |
is optional."""
|
sl@0
|
163 |
|
sl@0
|
164 |
def __init__(self, fussyOnNonExist):
|
sl@0
|
165 |
super(FolderListUnitTest,self).__init__()
|
sl@0
|
166 |
self.__fussyOnNonExist = fussyOnNonExist
|
sl@0
|
167 |
self.__tempFile = None
|
sl@0
|
168 |
|
sl@0
|
169 |
def shortDescription(self):
|
sl@0
|
170 |
"""One line description - additionally giving option"""
|
sl@0
|
171 |
parentStr = super(FolderListUnitTest, self).shortDescription()
|
sl@0
|
172 |
return "%s-%s" % (parentStr, str(self.__fussyOnNonExist))
|
sl@0
|
173 |
|
sl@0
|
174 |
def __realTest(self):
|
sl@0
|
175 |
"real test run - separate so can be trapped if required"
|
sl@0
|
176 |
|
sl@0
|
177 |
self.__tempFile = tempfile.NamedTemporaryFile() # used to create list and check
|
sl@0
|
178 |
|
sl@0
|
179 |
(input,tempFileName,expected) = self.__createDataLists()
|
sl@0
|
180 |
getFolderList = GetFolderList(input, nameToCheck=tempFileName)
|
sl@0
|
181 |
getFolderList.execute()
|
sl@0
|
182 |
result = getFolderList()
|
sl@0
|
183 |
self.assertEquals(result, expected,
|
sl@0
|
184 |
"Result2(%s) not same as Expected2(%s)(filename=%s)"%(result,expected,tempFileName))
|
sl@0
|
185 |
|
sl@0
|
186 |
def runTest(self):
|
sl@0
|
187 |
"FolderListUnitTest.runTest"
|
sl@0
|
188 |
if not self.__fussyOnNonExist:
|
sl@0
|
189 |
self.__realTest()
|
sl@0
|
190 |
else:
|
sl@0
|
191 |
# should fail
|
sl@0
|
192 |
self.assertRaises(MissingFile, self.__realTest)
|
sl@0
|
193 |
|
sl@0
|
194 |
def tearDown(self):
|
sl@0
|
195 |
"Stop using temp file, so will be deleted"
|
sl@0
|
196 |
self.__tempFile = None
|
sl@0
|
197 |
|
sl@0
|
198 |
def __createDataLists(self):
|
sl@0
|
199 |
"""Create input and output for test run as lists.
|
sl@0
|
200 |
|
sl@0
|
201 |
input will be one element with <tempdir> and second with "nonexistant",
|
sl@0
|
202 |
but which element is used depends on fussyOnNonExist"""
|
sl@0
|
203 |
input = []
|
sl@0
|
204 |
tempFileName = None
|
sl@0
|
205 |
expected = []
|
sl@0
|
206 |
|
sl@0
|
207 |
(tempDir,tempFileName) = os.path.split(self.__tempFile.name) # the dir and name of the temporary file we are using
|
sl@0
|
208 |
input += [("f1", [tempDir], [], [])] # the existing file
|
sl@0
|
209 |
expected += [(True, tempDir, 'From f1')]
|
sl@0
|
210 |
if self.__fussyOnNonExist:
|
sl@0
|
211 |
input += [("f2", ["nonexistant"], [], [])]
|
sl@0
|
212 |
else:
|
sl@0
|
213 |
input += [("f2", [], ["nonexistant"], [])]
|
sl@0
|
214 |
expected += [(True, None, 'Skip "nonexistant" from f2')] # always return the non-fussy version, as with fussy we expect exception raised
|
sl@0
|
215 |
|
sl@0
|
216 |
return (input,tempFileName,expected)
|
sl@0
|
217 |
|
sl@0
|
218 |
class FolderListIntTest(TestParent):
|
sl@0
|
219 |
"""Integration test combining MbcParser and FolderList
|
sl@0
|
220 |
|
sl@0
|
221 |
Create a temporary folder. Add the following contents:
|
sl@0
|
222 |
|
sl@0
|
223 |
x/bld.inf
|
sl@0
|
224 |
y/bld.inf
|
sl@0
|
225 |
z/bld.inf
|
sl@0
|
226 |
mbc containing "./x, ./y, [optional] ./z"
|
sl@0
|
227 |
|
sl@0
|
228 |
z/bld.inf and y.bld are optional
|
sl@0
|
229 |
option="x" no z or y. Should raise exception.
|
sl@0
|
230 |
option="xy" no z but y. Should get list of ../x and ../y
|
sl@0
|
231 |
option="xz" no y but z. Should raise exception.
|
sl@0
|
232 |
option="xyz" both y and z. Should get list of ../x, ../y and ../z
|
sl@0
|
233 |
|
sl@0
|
234 |
"""
|
sl@0
|
235 |
|
sl@0
|
236 |
def __init__(self, option):
|
sl@0
|
237 |
super(FolderListIntTest,self).__init__()
|
sl@0
|
238 |
self.__option = option
|
sl@0
|
239 |
self.__testDir = None
|
sl@0
|
240 |
self.__testDirs = [] # list of dirs we create, so we can delete them
|
sl@0
|
241 |
self.__testFiles = [] # list of files we create, so we can delete them
|
sl@0
|
242 |
self.__fullMbcFileName = None
|
sl@0
|
243 |
self.__expectedResult = []
|
sl@0
|
244 |
|
sl@0
|
245 |
def shortDescription(self):
|
sl@0
|
246 |
"One line description - additionally giving option"
|
sl@0
|
247 |
parentStr = super(FolderListIntTest, self).shortDescription()
|
sl@0
|
248 |
return "%s-%s" % (parentStr, str(self.__option))
|
sl@0
|
249 |
|
sl@0
|
250 |
def tearDown(self):
|
sl@0
|
251 |
for fle in self.__testFiles:
|
sl@0
|
252 |
try:
|
sl@0
|
253 |
os.unlink(fle)
|
sl@0
|
254 |
except:
|
sl@0
|
255 |
pass # ignore any error. assume means we didn't create the file in the end
|
sl@0
|
256 |
# delete folders in reverse, so child directories deleted after parent
|
sl@0
|
257 |
for dir in self.__testDirs[len(self.__testDirs)-1::-1]:
|
sl@0
|
258 |
try:
|
sl@0
|
259 |
os.rmdir(dir)
|
sl@0
|
260 |
except:
|
sl@0
|
261 |
pass # ignore any error. assume means we didn't create the file in the end
|
sl@0
|
262 |
|
sl@0
|
263 |
def runTest(self):
|
sl@0
|
264 |
"FolderListIntTest.runTest"
|
sl@0
|
265 |
self.__setup()
|
sl@0
|
266 |
if "y" in self.__option:
|
sl@0
|
267 |
self.__realTest()
|
sl@0
|
268 |
else:
|
sl@0
|
269 |
# expected to raise exception as y/bld.inf does not exist
|
sl@0
|
270 |
self.assertRaises(MissingFile, self.__realTest)
|
sl@0
|
271 |
|
sl@0
|
272 |
def __realTest(self):
|
sl@0
|
273 |
parser = MbcParser(self.__fullMbcFileName)
|
sl@0
|
274 |
parser.execute()
|
sl@0
|
275 |
folderList = GetFolderList(parser())
|
sl@0
|
276 |
folderList.execute()
|
sl@0
|
277 |
result = folderList()
|
sl@0
|
278 |
self.assertEquals(result, self.__expectedResult,
|
sl@0
|
279 |
"Result (%s) not that expected (%s)" % (str(result), str(self.__expectedResult)))
|
sl@0
|
280 |
|
sl@0
|
281 |
def __setup(self):
|
sl@0
|
282 |
self.__testDir = tempfile.mkdtemp() # __testDir is name of temp folder
|
sl@0
|
283 |
self.__testDirs.append(self.__testDir)
|
sl@0
|
284 |
self.__fullMbcFileName = os.path.join(self.__testDir, "test.mbc")
|
sl@0
|
285 |
|
sl@0
|
286 |
for name in ["x","y","z"]:
|
sl@0
|
287 |
fullpath = os.path.join(self.__testDir, name)
|
sl@0
|
288 |
if name in self.__option:
|
sl@0
|
289 |
os.mkdir(fullpath)
|
sl@0
|
290 |
self.__testDirs.append(fullpath)
|
sl@0
|
291 |
filepath = os.path.join(fullpath, "bld.inf")
|
sl@0
|
292 |
fileToWrite = file(filepath, "w")
|
sl@0
|
293 |
self.__testFiles.append(filepath)
|
sl@0
|
294 |
print >>fileToWrite, "//generated" # 9.5 syntax
|
sl@0
|
295 |
fileToWrite.close()
|
sl@0
|
296 |
expected = (True, fullpath, "From %s" % self.__fullMbcFileName)
|
sl@0
|
297 |
self.__expectedResult.append(expected)
|
sl@0
|
298 |
else:
|
sl@0
|
299 |
expected = (True, None, """Skip "%s" from %s""" % (fullpath, self.__fullMbcFileName))
|
sl@0
|
300 |
self.__expectedResult.append(expected)
|
sl@0
|
301 |
mbcFile = file(self.__fullMbcFileName, "w")
|
sl@0
|
302 |
self.__testFiles.append(self.__fullMbcFileName)
|
sl@0
|
303 |
print >>mbcFile, testdata.intTestMbcFile
|
sl@0
|
304 |
mbcFile.close()
|
sl@0
|
305 |
|
sl@0
|
306 |
class ConfigFileUnitTest(unittest.TestCase):
|
sl@0
|
307 |
"""ConfigFile UnitTest"""
|
sl@0
|
308 |
|
sl@0
|
309 |
def __init__(self, inputData, expected):
|
sl@0
|
310 |
super(ConfigFileUnitTest,self).__init__()
|
sl@0
|
311 |
self.__inputData = inputData
|
sl@0
|
312 |
self.__expected = expected
|
sl@0
|
313 |
|
sl@0
|
314 |
def runTest(self):
|
sl@0
|
315 |
"""ConfigFileUnitTest.runTest
|
sl@0
|
316 |
|
sl@0
|
317 |
Take dummy folder list and generate XML file. Output goes to string. Compare with expected
|
sl@0
|
318 |
"""
|
sl@0
|
319 |
|
sl@0
|
320 |
outputStream = StringIO.StringIO()
|
sl@0
|
321 |
generator = ConfigFileGenerator(self.__inputData, outputStream)
|
sl@0
|
322 |
generator.write()
|
sl@0
|
323 |
outputString = outputStream.getvalue()
|
sl@0
|
324 |
## print ("output=%s" % str(outputString))
|
sl@0
|
325 |
## print ("expected=%s" % str(self.__expected))
|
sl@0
|
326 |
self.assertEquals(outputString, self.__expected,
|
sl@0
|
327 |
"Generated output (%s) not same as expected (%s)" % (outputString, self.__expected))
|
sl@0
|
328 |
|
sl@0
|
329 |
def suite():
|
sl@0
|
330 |
"""TestSuite"""
|
sl@0
|
331 |
|
sl@0
|
332 |
tests = [
|
sl@0
|
333 |
SimpleTest(testdata.import1, testdata.result1),
|
sl@0
|
334 |
ListTest(testdata.import2, testdata.result2),
|
sl@0
|
335 |
BadDataTest(testdata.badImport1, "bad1"),
|
sl@0
|
336 |
BadDataTest(testdata.badImport2, "bad2"),
|
sl@0
|
337 |
FolderListUnitTest(False),
|
sl@0
|
338 |
FolderListUnitTest(True),
|
sl@0
|
339 |
FolderListIntTest("x"),
|
sl@0
|
340 |
FolderListIntTest("xy"),
|
sl@0
|
341 |
FolderListIntTest("xz"),
|
sl@0
|
342 |
FolderListIntTest("xyz"),
|
sl@0
|
343 |
ConfigFileUnitTest(testdata.testFolderList1, testdata.testXmlFile1),
|
sl@0
|
344 |
]
|
sl@0
|
345 |
|
sl@0
|
346 |
return unittest.TestSuite(tests)
|
sl@0
|
347 |
|
sl@0
|
348 |
|
sl@0
|
349 |
if __name__ == "__main__":
|
sl@0
|
350 |
unittest.TextTestRunner(verbosity=2).run(suite())
|