Debian Codegraph @

src/runner.c

raw symbols SCIP
1 #include <cgreen/assertions.h>
2 #include <cgreen/mocks.h>
3 #include <cgreen/reporter.h>
4 #include <cgreen/suite.h>
5 #include <cgreen/internal/runner_platform.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <cgreen/internal/cgreen_time.h>
13
14 #include "runner.h"
15
16 #ifdef __ANDROID__
17 #include "cgreen/internal/android_headers/androidcompat.h"
18 #endif // #ifdef __ANDROID__
19
20 // Export the current running test
21 CgreenTest *current_test;
22
23 static const char *CGREEN_PER_TEST_TIMEOUT_ENVIRONMENT_VARIABLE = "CGREEN_PER_TEST_TIMEOUT";
24
25 static void run_every_test(TestSuite *suite, TestReporter *reporter);
26 static void run_named_test(TestSuite *suite, const char *name, TestReporter *reporter);
27
28 static void run_test_in_the_current_process(TestSuite *suite, CgreenTest *test, TestReporter *reporter);
29
30 static int per_test_timeout_defined(void);
31 static int per_test_timeout_value(void);
32 static void validate_per_test_timeout_value(void);
33
34 int run_test_suite(TestSuite *suite, TestReporter *reporter)
35 {
36 int success;
37 if (per_test_timeout_defined())
38 {
39 validate_per_test_timeout_value();
40 }
41
42 setup_reporting(reporter);
43 run_every_test(suite, reporter);
44 success = (reporter->total_failures == 0) && (reporter->total_exceptions == 0);
45 return success ? EXIT_SUCCESS : EXIT_FAILURE;
46 }
47
48 int run_single_test(TestSuite *suite, const char *name, TestReporter *reporter)
49 {
50 int success;
51 if (per_test_timeout_defined())
52 {
53 validate_per_test_timeout_value();
54 }
55
56 setup_reporting(reporter);
57 run_named_test(suite, name, reporter);
58 success = (reporter->total_failures == 0);
59 return success ? EXIT_SUCCESS : EXIT_FAILURE;
60 }
61
62 static void run_every_test(TestSuite *suite, TestReporter *reporter)
63 {
64 int i;
65
66 run_specified_test_if_child(suite, reporter);
67
68 uint32_t total_test_starting_milliseconds = cgreen_time_get_current_milliseconds();
69 (*reporter->start_suite)(reporter, suite->name, count_tests(suite));
70
71 // Run sub-suites first
72 for (i = 0; i < suite->size; i++)
73 {
74 if (suite->tests[i].type != test_function)
75 {
76 (*suite->setup)();
77 run_every_test(suite->tests[i].Runnable.suite, reporter);
78 (*suite->teardown)();
79 }
80 }
81
82 // Reset counters for top-level tests
83 reporter->passes = 0;
84 reporter->failures = 0;
85 reporter->skips = 0;
86 reporter->exceptions = 0;
87
88 uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds();
89
90 // Run top-level tests
91 for (i = 0; i < suite->size; i++)
92 {
93 if (suite->tests[i].type == test_function)
94 {
95 if (getenv("CGREEN_NO_FORK") == NULL)
96 run_test_in_its_own_process(suite, suite->tests[i].Runnable.test, reporter);
97 else
98 run_test_in_the_current_process(suite, suite->tests[i].Runnable.test, reporter);
99 }
100 }
101
102 reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds,
103 cgreen_time_get_current_milliseconds());
104 reporter->total_duration = cgreen_time_duration_in_milliseconds(total_test_starting_milliseconds,
105 cgreen_time_get_current_milliseconds());
106 send_reporter_completion_notification(reporter);
107 (*reporter->finish_suite)(reporter, suite->filename, suite->line);
108 }
109
110 static void run_named_test(TestSuite *suite, const char *name, TestReporter *reporter)
111 {
112 int i;
113
114 uint32_t total_test_starting_milliseconds = cgreen_time_get_current_milliseconds();
115
116 (*reporter->start_suite)(reporter, suite->name, count_tests(suite));
117 for (i = 0; i < suite->size; i++)
118 {
119 if (suite->tests[i].type != test_function &&
120 has_test(suite->tests[i].Runnable.suite, name))
121 {
122 (*suite->setup)();
123 run_named_test(suite->tests[i].Runnable.suite, name, reporter);
124 (*suite->teardown)();
125 }
126 }
127
128 // Reset counters for top-level tests
129 reporter->passes = 0;
130 reporter->failures = 0;
131 reporter->skips = 0;
132 reporter->exceptions = 0;
133
134 uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds();
135
136 for (i = 0; i < suite->size; i++)
137 {
138 if (suite->tests[i].type == test_function)
139 {
140 if (strcmp(suite->tests[i].name, name) == 0)
141 {
142 run_test_in_the_current_process(suite, suite->tests[i].Runnable.test, reporter);
143 }
144 }
145 }
146
147 reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds,
148 cgreen_time_get_current_milliseconds());
149 reporter->total_duration = cgreen_time_duration_in_milliseconds(total_test_starting_milliseconds,
150 cgreen_time_get_current_milliseconds());
151
152 send_reporter_completion_notification(reporter);
153 (*reporter->finish_suite)(reporter, suite->filename, suite->line);
154 }
155
156 static void run_test_in_the_current_process(TestSuite *suite, CgreenTest *test, TestReporter *reporter)
157 {
158 uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds();
159
160 (*reporter->start_test)(reporter, test->name);
161 if (test->skip)
162 {
163 send_reporter_skipped_notification(reporter);
164 }
165 else
166 {
167 run_the_test_code(suite, test, reporter);
168 reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds,
169 cgreen_time_get_current_milliseconds());
170
171 send_reporter_completion_notification(reporter);
172 }
173 (*reporter->finish_test)(reporter, test->filename, test->line, NULL);
174 }
175
176 static int per_test_timeout_defined(void)
177 {
178 return getenv(CGREEN_PER_TEST_TIMEOUT_ENVIRONMENT_VARIABLE) != NULL;
179 }
180
181 static int per_test_timeout_value(void)
182 {
183 char *timeout_string;
184 int timeout_value;
185
186 if (!per_test_timeout_defined())
187 {
188 die("attempt to fetch undefined value for %s\n", CGREEN_PER_TEST_TIMEOUT_ENVIRONMENT_VARIABLE);
189 }
190
191 timeout_string = getenv(CGREEN_PER_TEST_TIMEOUT_ENVIRONMENT_VARIABLE);
192 timeout_value = atoi(timeout_string);
193
194 return timeout_value;
195 }
196
197 static void validate_per_test_timeout_value(void)
198 {
199 int timeout = per_test_timeout_value();
200
201 if (timeout <= 0)
202 {
203 die("invalid value for %s environment variable: %d\n", CGREEN_PER_TEST_TIMEOUT_ENVIRONMENT_VARIABLE, timeout);
204 }
205 }
206
207 static void run_setup_for(CgreenTest *spec)
208 {
209 #ifdef __cplusplus
210 std::string message = "an exception was thrown during setup: ";
211 try
212 {
213 #endif
214 spec->context->setup();
215 #ifdef __cplusplus
216 return;
217 }
218 catch (const std::exception &exception)
219 {
220 message += '[';
221 message += exception.what();
222 message += ']';
223 }
224 catch (const std::exception *exception)
225 {
226 message += '[';
227 message += exception->what();
228 message += ']';
229 }
230 catch (const std::string &exception_message)
231 {
232 message += '[';
233 message += exception_message;
234 message += ']';
235 }
236 catch (const std::string *exception_message)
237 {
238 message += '[';
239 message += *exception_message;
240 message += ']';
241 }
242 catch (const char *exception_message)
243 {
244 message += '[';
245 message += exception_message;
246 message += ']';
247 }
248 catch (...)
249 {
250 message += "unknown exception type";
251 }
252 va_list no_arguments;
253 memset(&no_arguments, 0, sizeof(va_list));
254 TestReporter *reporter = get_test_reporter();
255 reporter->show_incomplete(reporter, spec->filename, spec->line, message.c_str(), no_arguments);
256 send_reporter_exception_notification(reporter);
257 #endif
258 }
259
260 static void run_teardown_for(CgreenTest *spec)
261 {
262 #ifdef __cplusplus
263 std::string message = "an exception was thrown during teardown: ";
264 try
265 {
266 #endif
267 spec->context->teardown();
268 #ifdef __cplusplus
269 return;
270 }
271 catch (const std::exception &exception)
272 {
273 message += '[';
274 message += exception.what();
275 message += ']';
276 }
277 catch (const std::exception *exception)
278 {
279 message += '[';
280 message += exception->what();
281 message += ']';
282 }
283 catch (const std::string &exception_message)
284 {
285 message += '[';
286 message += exception_message;
287 message += ']';
288 }
289 catch (const std::string *exception_message)
290 {
291 message += '[';
292 message += *exception_message;
293 message += ']';
294 }
295 catch (const char *exception_message)
296 {
297 message += '[';
298 message += exception_message;
299 message += ']';
300 }
301 catch (...)
302 {
303 message += "unknown exception type";
304 }
305 va_list no_arguments;
306 memset(&no_arguments, 0, sizeof(va_list));
307 TestReporter *reporter = get_test_reporter();
308 reporter->show_incomplete(reporter, spec->filename, spec->line, message.c_str(), no_arguments);
309 send_reporter_exception_notification(reporter);
310 #endif
311 }
312
313 /**
314 run()
315
316 N.B. Although this is neither an API or public function, it is
317 documented as a good place to put a breakpoint. Do not change the
318 name or semantics of this function, it should continue to be very
319 close to the test code. */
320 static void run(CgreenTest *spec)
321 {
322 #ifdef __cplusplus
323 std::string message = "an exception was thrown during test: ";
324 try
325 {
326 #endif
327 current_test = spec;
328 spec->run();
329 #ifdef __cplusplus
330 return;
331 }
332 catch (const std::exception &exception)
333 {
334 message += '[';
335 message += exception.what();
336 message += ']';
337 }
338 catch (const std::exception *exception)
339 {
340 message += '[';
341 message += exception->what();
342 message += ']';
343 }
344 catch (const std::string &exception_message)
345 {
346 message += '[';
347 message += exception_message;
348 message += ']';
349 }
350 catch (const std::string *exception_message)
351 {
352 message += '[';
353 message += *exception_message;
354 message += ']';
355 }
356 catch (const char *exception_message)
357 {
358 message += '[';
359 message += exception_message;
360 message += ']';
361 }
362 catch (...)
363 {
364 message += "unknown exception type";
365 }
366 va_list no_arguments;
367 memset(&no_arguments, 0, sizeof(va_list));
368 TestReporter *reporter = get_test_reporter();
369 reporter->show_incomplete(reporter, spec->filename, spec->line, message.c_str(), no_arguments);
370 send_reporter_exception_notification(reporter);
371 #endif
372 }
373
374 void run_the_test_code(TestSuite *suite, CgreenTest *spec, TestReporter *reporter)
375 {
376 significant_figures_for_assert_double_are(8);
377 clear_mocks();
378
379 if (per_test_timeout_defined())
380 {
381 validate_per_test_timeout_value();
382
383 die_in(per_test_timeout_value());
384 }
385
386 // for historical reasons the suite can have a setup
387 if (has_setup(suite))
388 {
389 (*suite->setup)();
390 }
391 else
392 {
393 if (spec->context->setup != NULL)
394 {
395 run_setup_for(spec);
396 }
397 }
398
399 run(spec);
400 // for historical reasons the suite can have a teardown
401 if (has_teardown(suite))
402 {
403 (*suite->teardown)();
404 }
405 else
406 {
407 if (spec->context->teardown != NULL)
408 {
409 run_teardown_for(spec);
410 }
411 }
412
413 tally_mocks(reporter);
414 }
415
416 void die(const char *message, ...)
417 {
418 va_list arguments;
419 va_start(arguments, message);
420 vprintf(message, arguments);
421 va_end(arguments);
422 exit(EXIT_FAILURE);
423 }
424
425 /* vim: set ts=4 sw=4 et cindent: */