source: mainline/uspace/lib/pcut/src/main.c@ 3f1d4d5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f1d4d5 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2012-2013 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 *
31 * The main control loop of the whole library.
32 */
33
34#include "internal.h"
35#include "report/report.h"
36#include <assert.h>
37#include <stdlib.h>
38#include <stdio.h>
39
40/** Current running mode. */
41int pcut_run_mode = PCUT_RUN_MODE_FORKING;
42
43/** Empty list to bypass special handling for NULL. */
44static pcut_main_extra_t empty_main_extra[] = {
45 PCUT_MAIN_EXTRA_SET_LAST
46};
47
48/** Helper for iteration over main extras. */
49#define FOR_EACH_MAIN_EXTRA(extras, it) \
50 for (it = extras; it->type != PCUT_MAIN_EXTRA_LAST; it++)
51
52/** Checks whether the argument is an option followed by a number.
53 *
54 * @param arg Argument from the user.
55 * @param opt Option, including the leading dashes.
56 * @param value Where to store the integer value.
57 * @return Whether @p arg is @p opt followed by a number.
58 */
59int pcut_is_arg_with_number(const char *arg, const char *opt, int *value)
60{
61 int opt_len = pcut_str_size(opt);
62 if (!pcut_str_start_equals(arg, opt, opt_len)) {
63 return 0;
64 }
65 *value = pcut_str_to_int(arg + opt_len);
66 return 1;
67}
68
69
70/** Find item by its id.
71 *
72 * @param first List to search.
73 * @param id Id to find.
74 * @return The item with given id.
75 * @retval NULL No item with such id exists in the list.
76 */
77static pcut_item_t *pcut_find_by_id(pcut_item_t *first, int id)
78{
79 pcut_item_t *it = pcut_get_real(first);
80 while (it != NULL) {
81 if (it->id == id) {
82 return it;
83 }
84 it = pcut_get_real_next(it);
85 }
86 return NULL;
87}
88
89/** Run the whole test suite.
90 *
91 * @param suite Suite to run.
92 * @param last Pointer to first item after this suite is stored here.
93 * @param prog_path Path to the current binary (used in forked mode).
94 * @return Error code.
95 */
96static int run_suite(pcut_item_t *suite, pcut_item_t **last, const char *prog_path)
97{
98 int is_first_test = 1;
99 int total_count = 0;
100 int ret_code = PCUT_OUTCOME_PASS;
101 int ret_code_tmp;
102
103 pcut_item_t *it = pcut_get_real_next(suite);
104 if ((it == NULL) || (it->kind == PCUT_KIND_TESTSUITE)) {
105 goto leave_no_print;
106 }
107
108 for (; it != NULL; it = pcut_get_real_next(it)) {
109 if (it->kind == PCUT_KIND_TESTSUITE) {
110 goto leave_ok;
111 }
112 if (it->kind != PCUT_KIND_TEST) {
113 continue;
114 }
115
116 if (is_first_test) {
117 pcut_report_suite_start(suite);
118 is_first_test = 0;
119 }
120
121 if (pcut_run_mode == PCUT_RUN_MODE_FORKING) {
122 ret_code_tmp = pcut_run_test_forking(prog_path, it);
123 } else {
124 ret_code_tmp = pcut_run_test_single(it);
125 }
126
127 /*
128 * Override final return code in case of failure.
129 *
130 * In this case we suppress any special error codes as
131 * to the outside, there was a failure.
132 */
133 if (ret_code_tmp != PCUT_OUTCOME_PASS) {
134 ret_code = PCUT_OUTCOME_FAIL;
135 }
136
137 total_count++;
138 }
139
140leave_ok:
141 if (total_count > 0) {
142 pcut_report_suite_done(suite);
143 }
144
145leave_no_print:
146 if (last != NULL) {
147 *last = it;
148 }
149
150 return ret_code;
151}
152
153/** Add direct pointers to set-up/tear-down functions to a suites.
154 *
155 * At start-up, set-up and tear-down functions are scattered in the
156 * list as siblings of suites and tests.
157 * This puts them into the structure describing the suite itself.
158 *
159 * @param first First item of the list.
160 */
161static void set_setup_teardown_callbacks(pcut_item_t *first)
162{
163 pcut_item_t *active_suite = NULL;
164 pcut_item_t *it;
165 for (it = first; it != NULL; it = pcut_get_real_next(it)) {
166 if (it->kind == PCUT_KIND_TESTSUITE) {
167 active_suite = it;
168 } else if (it->kind == PCUT_KIND_SETUP) {
169 if (active_suite != NULL) {
170 active_suite->setup_func = it->setup_func;
171 }
172 it->kind = PCUT_KIND_SKIP;
173 } else if (it->kind == PCUT_KIND_TEARDOWN) {
174 if (active_suite != NULL) {
175 active_suite->teardown_func = it->teardown_func;
176 }
177 it->kind = PCUT_KIND_SKIP;
178 } else {
179 /* Not interesting right now. */
180 }
181 }
182}
183
184/** The main function of PCUT.
185 *
186 * This function is expected to be called as the only function in
187 * normal main().
188 *
189 * @param last Pointer to the last item defined by PCUT_TEST macros.
190 * @param argc Original argc of the program.
191 * @param argv Original argv of the program.
192 * @return Program exit code.
193 */
194int pcut_main(pcut_item_t *last, int argc, char *argv[])
195{
196 pcut_item_t *items = pcut_fix_list_get_real_head(last);
197 pcut_item_t *it;
198 pcut_main_extra_t *main_extras = last->main_extras;
199 pcut_main_extra_t *main_extras_it;
200
201 int run_only_suite = -1;
202 int run_only_test = -1;
203
204 int rc, rc_tmp;
205
206 if (main_extras == NULL) {
207 main_extras = empty_main_extra;
208 }
209
210 pcut_report_register_handler(&pcut_report_tap);
211
212 FOR_EACH_MAIN_EXTRA(main_extras, main_extras_it) {
213 if (main_extras_it->type == PCUT_MAIN_EXTRA_REPORT_XML) {
214 pcut_report_register_handler(&pcut_report_xml);
215 }
216 if (main_extras_it->type == PCUT_MAIN_EXTRA_PREINIT_HOOK) {
217 main_extras_it->preinit_hook(&argc, &argv);
218 }
219 }
220
221 if (argc > 1) {
222 int i;
223 for (i = 1; i < argc; i++) {
224 pcut_is_arg_with_number(argv[i], "-s", &run_only_suite);
225 pcut_is_arg_with_number(argv[i], "-t", &run_only_test);
226 if (pcut_str_equals(argv[i], "-l")) {
227 pcut_print_tests(items);
228 return PCUT_OUTCOME_PASS;
229 }
230 if (pcut_str_equals(argv[i], "-x")) {
231 pcut_report_register_handler(&pcut_report_xml);
232 }
233#ifndef PCUT_NO_LONG_JUMP
234 if (pcut_str_equals(argv[i], "-u")) {
235 pcut_run_mode = PCUT_RUN_MODE_SINGLE;
236 }
237#endif
238 }
239 }
240
241 setvbuf(stdout, NULL, _IONBF, 0);
242 set_setup_teardown_callbacks(items);
243
244 FOR_EACH_MAIN_EXTRA(main_extras, main_extras_it) {
245 if (main_extras_it->type == PCUT_MAIN_EXTRA_INIT_HOOK) {
246 main_extras_it->init_hook();
247 }
248 }
249
250 PCUT_DEBUG("run_only_suite = %d run_only_test = %d", run_only_suite, run_only_test);
251
252 if ((run_only_suite >= 0) && (run_only_test >= 0)) {
253 printf("Specify either -s or -t!\n");
254 return PCUT_OUTCOME_BAD_INVOCATION;
255 }
256
257 if (run_only_suite > 0) {
258 pcut_item_t *suite = pcut_find_by_id(items, run_only_suite);
259 if (suite == NULL) {
260 printf("Suite not found, aborting!\n");
261 return PCUT_OUTCOME_BAD_INVOCATION;
262 }
263 if (suite->kind != PCUT_KIND_TESTSUITE) {
264 printf("Invalid suite id!\n");
265 return PCUT_OUTCOME_BAD_INVOCATION;
266 }
267
268 run_suite(suite, NULL, argv[0]);
269 return PCUT_OUTCOME_PASS;
270 }
271
272 if (run_only_test > 0) {
273 pcut_item_t *test = pcut_find_by_id(items, run_only_test);
274 if (test == NULL) {
275 printf("Test not found, aborting!\n");
276 return PCUT_OUTCOME_BAD_INVOCATION;
277 }
278 if (test->kind != PCUT_KIND_TEST) {
279 printf("Invalid test id!\n");
280 return PCUT_OUTCOME_BAD_INVOCATION;
281 }
282
283 if (pcut_run_mode == PCUT_RUN_MODE_SINGLE) {
284 rc = pcut_run_test_single(test);
285 } else {
286 rc = pcut_run_test_forked(test);
287 }
288
289 return rc;
290 }
291
292 /* Otherwise, run the whole thing. */
293 pcut_report_init(items);
294
295 rc = PCUT_OUTCOME_PASS;
296
297 it = items;
298 while (it != NULL) {
299 if (it->kind == PCUT_KIND_TESTSUITE) {
300 pcut_item_t *tmp;
301 rc_tmp = run_suite(it, &tmp, argv[0]);
302 if (rc_tmp != PCUT_OUTCOME_PASS) {
303 rc = rc_tmp;
304 }
305 it = tmp;
306 } else {
307 it = pcut_get_real_next(it);
308 }
309 }
310
311 pcut_report_done();
312
313 return rc;
314}
Note: See TracBrowser for help on using the repository browser.