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