1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import os |
---|
12 | import shutil |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | |
---|
16 | from bitten.build import ctools |
---|
17 | from bitten.build.tests import dummy |
---|
18 | from bitten.recipe import Context, Recipe |
---|
19 | |
---|
20 | |
---|
21 | class CppUnitTestCase(unittest.TestCase): |
---|
22 | |
---|
23 | def setUp(self): |
---|
24 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
25 | self.ctxt = Context(self.basedir) |
---|
26 | |
---|
27 | def tearDown(self): |
---|
28 | shutil.rmtree(self.basedir) |
---|
29 | |
---|
30 | def test_missing_param_file(self): |
---|
31 | self.assertRaises(AssertionError, ctools.cppunit, self.ctxt) |
---|
32 | |
---|
33 | def test_empty_summary(self): |
---|
34 | cppunit_xml = file(self.ctxt.resolve('cppunit.xml'), 'w') |
---|
35 | cppunit_xml.write("""<?xml version="1.0" encoding='utf-8' ?> |
---|
36 | <TestRun> |
---|
37 | <FailedTests> |
---|
38 | <FailedTest id="2"> |
---|
39 | <Name>HelloTest::secondTest</Name> |
---|
40 | <FailureType>Assertion</FailureType> |
---|
41 | <Location> |
---|
42 | <File>HelloTest.cxx</File> |
---|
43 | <Line>95</Line> |
---|
44 | </Location> |
---|
45 | <Message>assertion failed |
---|
46 | - Expression: 2 == 3 |
---|
47 | </Message> |
---|
48 | </FailedTest> |
---|
49 | </FailedTests> |
---|
50 | <SuccessfulTests> |
---|
51 | <Test id="1"> |
---|
52 | <Name>HelloTest::firstTest</Name> |
---|
53 | </Test> |
---|
54 | <Test id="3"> |
---|
55 | <Name>HelloTest::thirdTest</Name> |
---|
56 | </Test> |
---|
57 | </SuccessfulTests> |
---|
58 | <Statistics> |
---|
59 | <Tests>3</Tests> |
---|
60 | <FailuresTotal>1</FailuresTotal> |
---|
61 | <Errors>0</Errors> |
---|
62 | <Failures>1</Failures> |
---|
63 | </Statistics> |
---|
64 | </TestRun>""") |
---|
65 | cppunit_xml.close() |
---|
66 | ctools.cppunit(self.ctxt, file_='cppunit.xml') |
---|
67 | type, category, generator, xml = self.ctxt.output.pop() |
---|
68 | self.assertEqual(Recipe.REPORT, type) |
---|
69 | self.assertEqual('test', category) |
---|
70 | |
---|
71 | tests = list(xml.children) |
---|
72 | self.assertEqual(3, len(tests)) |
---|
73 | self.assertEqual('HelloTest', tests[0].attr['fixture']) |
---|
74 | self.assertEqual('secondTest', tests[0].attr['name']) |
---|
75 | self.assertEqual('failure', tests[0].attr['status']) |
---|
76 | self.assertEqual('HelloTest.cxx', tests[0].attr['file']) |
---|
77 | self.assertEqual('95', tests[0].attr['line']) |
---|
78 | |
---|
79 | self.assertEqual('HelloTest', tests[1].attr['fixture']) |
---|
80 | self.assertEqual('firstTest', tests[1].attr['name']) |
---|
81 | self.assertEqual('success', tests[1].attr['status']) |
---|
82 | |
---|
83 | self.assertEqual('HelloTest', tests[2].attr['fixture']) |
---|
84 | self.assertEqual('thirdTest', tests[2].attr['name']) |
---|
85 | self.assertEqual('success', tests[2].attr['status']) |
---|
86 | |
---|
87 | |
---|
88 | class GCovTestCase(unittest.TestCase): |
---|
89 | |
---|
90 | def setUp(self): |
---|
91 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
92 | self.ctxt = Context(self.basedir) |
---|
93 | |
---|
94 | def tearDown(self): |
---|
95 | shutil.rmtree(self.basedir) |
---|
96 | |
---|
97 | def _create_file(self, *path, **kwargs): |
---|
98 | filename = os.path.join(self.basedir, *path) |
---|
99 | dirname = os.path.dirname(filename) |
---|
100 | if not os.path.isdir(dirname): |
---|
101 | os.makedirs(dirname) |
---|
102 | fd = file(filename, 'w') |
---|
103 | if 'content' in kwargs: |
---|
104 | fd.write(kwargs['content']) |
---|
105 | fd.close() |
---|
106 | return filename[len(self.basedir) + 1:] |
---|
107 | |
---|
108 | def test_no_file(self): |
---|
109 | ctools.CommandLine = dummy.CommandLine() |
---|
110 | ctools.gcov(self.ctxt) |
---|
111 | type, category, generator, xml = self.ctxt.output.pop() |
---|
112 | self.assertEqual('log', type) |
---|
113 | type, category, generator, xml = self.ctxt.output.pop() |
---|
114 | self.assertEqual('report', type) |
---|
115 | self.assertEqual('coverage', category) |
---|
116 | self.assertEqual(0, len(xml.children)) |
---|
117 | |
---|
118 | def test_single_file(self): |
---|
119 | self._create_file('foo.c') |
---|
120 | self._create_file('foo.o') |
---|
121 | self._create_file('foo.gcno') |
---|
122 | self._create_file('foo.gcda') |
---|
123 | |
---|
124 | ctools.CommandLine = dummy.CommandLine(stdout=""" |
---|
125 | File `foo.c' |
---|
126 | Lines executed:45.81% of 884 |
---|
127 | Branches executed:54.27% of 398 |
---|
128 | Taken at least once:36.68% of 398 |
---|
129 | Calls executed:48.19% of 249 |
---|
130 | |
---|
131 | File `foo.h' |
---|
132 | Lines executed:50.00% of 4 |
---|
133 | No branches |
---|
134 | Calls executed:100.00% of 1 |
---|
135 | """) |
---|
136 | ctools.gcov(self.ctxt) |
---|
137 | type, category, generator, xml = self.ctxt.output.pop() |
---|
138 | self.assertEqual('log', type) |
---|
139 | type, category, generator, xml = self.ctxt.output.pop() |
---|
140 | self.assertEqual('report', type) |
---|
141 | self.assertEqual('coverage', category) |
---|
142 | self.assertEqual(1, len(xml.children)) |
---|
143 | elem = xml.children[0] |
---|
144 | self.assertEqual('coverage', elem.name) |
---|
145 | self.assertEqual('foo.c', elem.attr['file']) |
---|
146 | self.assertEqual('foo.c', elem.attr['name']) |
---|
147 | self.assertEqual(888, elem.attr['lines']) |
---|
148 | self.assertEqual(45, elem.attr['percentage']) |
---|
149 | |
---|
150 | def test_unreached_exceptional_path_is_zero(self): |
---|
151 | self._create_file('foo.cpp') |
---|
152 | self._create_file('foo.o') |
---|
153 | self._create_file('foo.gcno') |
---|
154 | self._create_file('foo.gcda') |
---|
155 | self._create_file('foo.cpp.gcov', content=""" -: 0:Source:foo.cpp |
---|
156 | -: 0:Graph:./foo.gcno |
---|
157 | -: 0:Data:./foo.gcda |
---|
158 | -: 0:Runs:1 |
---|
159 | -: 0:Programs:1 |
---|
160 | -: 1:#include <iostream> |
---|
161 | -: 2: |
---|
162 | 1: 3:int main() { |
---|
163 | -: 4: try { |
---|
164 | 1: 5: std::cout << "No exception!"; |
---|
165 | =====: 6: } catch(const std::exception &) { |
---|
166 | =====: 7: std::cout << "Exception!"; |
---|
167 | -: 8: } |
---|
168 | 4: 9:} |
---|
169 | """) |
---|
170 | ctools.CommandLine = dummy.CommandLine(stdout=""" |
---|
171 | File 'foo.cpp' |
---|
172 | Lines executed:60.00% of 5 |
---|
173 | Creating 'foo.cpp.gcov' |
---|
174 | |
---|
175 | File '/usr/include/c++/4.9.1/iostream' |
---|
176 | Lines executed:100.00% of 1 |
---|
177 | Creating 'iostream.gcov' |
---|
178 | |
---|
179 | File '/usr/include/c++/4.9.1/bits/basic_ios.h' |
---|
180 | No executable lines |
---|
181 | Removing 'basic_ios.h.gcov' |
---|
182 | |
---|
183 | File '/usr/include/c++/4.9.1/ostream' |
---|
184 | No executable lines |
---|
185 | Removing 'ostream.gcov' |
---|
186 | |
---|
187 | File '/usr/include/c++/4.9.1/bits/ios_base.h' |
---|
188 | Lines executed:0.00% of 2 |
---|
189 | Creating 'ios_base.h.gcov' |
---|
190 | |
---|
191 | File '/usr/include/c++/4.9.1/bits/char_traits.h' |
---|
192 | Lines executed:0.00% of 2 |
---|
193 | Creating 'char_traits.h.gcov' |
---|
194 | """) |
---|
195 | ctools.gcov(self.ctxt) |
---|
196 | type, category, generator, xml = self.ctxt.output.pop() |
---|
197 | self.assertEqual('log', type) |
---|
198 | type, category, generator, xml = self.ctxt.output.pop() |
---|
199 | self.assertEqual('report', type) |
---|
200 | self.assertEqual('coverage', category) |
---|
201 | self.assertEqual(1, len(xml.children)) |
---|
202 | elem = xml.children[0] |
---|
203 | self.assertEqual('coverage', elem.name) |
---|
204 | self.assertEqual('- - 1 - 1 0 0 - 4', elem.attr['line_hits']) |
---|
205 | |
---|
206 | def test_mark_unknown_lines(self): |
---|
207 | self._create_file('foo.cpp') |
---|
208 | self._create_file('foo.o') |
---|
209 | self._create_file('foo.gcno') |
---|
210 | self._create_file('foo.gcda') |
---|
211 | # changed line 2 to include unknown text |
---|
212 | self._create_file('foo.cpp.gcov', content=""" -: 0:Source:foo.cpp |
---|
213 | -: 0:Graph:./foo.gcno |
---|
214 | -: 0:Data:./foo.gcda |
---|
215 | -: 0:Runs:1 |
---|
216 | -: 0:Programs:1 |
---|
217 | -: 1:#include <iostream> |
---|
218 | x: 2: |
---|
219 | 1: 3:int main() { |
---|
220 | -: 4: try { |
---|
221 | 1: 5: std::cout << "No exception!"; |
---|
222 | =====: 6: } catch(const std::exception &) { |
---|
223 | =====: 7: std::cout << "Exception!"; |
---|
224 | -: 8: } |
---|
225 | 4: 9:} |
---|
226 | """) |
---|
227 | ctools.CommandLine = dummy.CommandLine(stdout=""" |
---|
228 | File 'foo.cpp' |
---|
229 | Lines executed:60.00% of 5 |
---|
230 | Creating 'foo.cpp.gcov' |
---|
231 | |
---|
232 | File '/usr/include/c++/4.9.1/iostream' |
---|
233 | Lines executed:100.00% of 1 |
---|
234 | Creating 'iostream.gcov' |
---|
235 | |
---|
236 | File '/usr/include/c++/4.9.1/bits/basic_ios.h' |
---|
237 | No executable lines |
---|
238 | Removing 'basic_ios.h.gcov' |
---|
239 | |
---|
240 | File '/usr/include/c++/4.9.1/ostream' |
---|
241 | No executable lines |
---|
242 | Removing 'ostream.gcov' |
---|
243 | |
---|
244 | File '/usr/include/c++/4.9.1/bits/ios_base.h' |
---|
245 | Lines executed:0.00% of 2 |
---|
246 | Creating 'ios_base.h.gcov' |
---|
247 | |
---|
248 | File '/usr/include/c++/4.9.1/bits/char_traits.h' |
---|
249 | Lines executed:0.00% of 2 |
---|
250 | Creating 'char_traits.h.gcov' |
---|
251 | """) |
---|
252 | ctools.gcov(self.ctxt) |
---|
253 | type, category, generator, xml = self.ctxt.output.pop() |
---|
254 | self.assertEqual('log', type) |
---|
255 | type, category, generator, xml = self.ctxt.output.pop() |
---|
256 | self.assertEqual('report', type) |
---|
257 | self.assertEqual('coverage', category) |
---|
258 | self.assertEqual(1, len(xml.children)) |
---|
259 | elem = xml.children[0] |
---|
260 | self.assertEqual('coverage', elem.name) |
---|
261 | self.assertEqual('- ??? 1 - 1 0 0 - 4', elem.attr['line_hits']) |
---|
262 | |
---|
263 | def suite(): |
---|
264 | suite = unittest.TestSuite() |
---|
265 | suite.addTest(unittest.makeSuite(CppUnitTestCase, 'test')) |
---|
266 | suite.addTest(unittest.makeSuite(GCovTestCase, 'test')) |
---|
267 | return suite |
---|
268 | |
---|
269 | if __name__ == '__main__': |
---|
270 | unittest.main(defaultTest='suite') |
---|