1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <libtest/common.h>
34 #include <sys/types.h>
40 #ifndef __INTEL_COMPILER
41 #pragma GCC diagnostic ignored "-Wold-style-cast"
44 using namespace libtest
;
46 static void stats_print(Stats
*stats
)
48 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
53 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
54 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
55 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
56 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
58 Out
<< "Total\t\t\t\t" << stats
->total
;
59 Out
<< "\tFailed\t\t\t" << stats
->failed
;
60 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
61 Out
<< "\tSucceeded\t\t" << stats
->success
;
64 static long int timedif(struct timeval a
, struct timeval b
)
68 us
= (long)(a
.tv_usec
- b
.tv_usec
);
70 s
= (long)(a
.tv_sec
- b
.tv_sec
);
75 static test_return_t
runner_code(Framework
* frame
,
81 struct timeval start_time
, end_time
;
83 gettimeofday(&start_time
, NULL
);
84 assert(frame
->runner());
87 test_return_t return_code
;
90 return_code
= frame
->runner()->run(run
->test_fn
, creators_ptr
);
92 // Special case where check for the testing of the exception
94 catch (libtest::fatal
&e
)
96 if (fatal::is_disabled())
98 fatal::increment_disabled_counter();
99 return_code
= TEST_SUCCESS
;
107 gettimeofday(&end_time
, NULL
);
108 load_time
= timedif(end_time
, start_time
);
116 int main(int argc
, char *argv
[])
118 bool opt_massive
= false;
119 unsigned long int opt_repeat
= 1; // Run all tests once
120 bool opt_quiet
= false;
121 std::string collection_to_run
;
127 OPT_LIBYATL_MATCH_COLLECTION
,
133 static struct option long_options
[]=
135 { "version", no_argument
, NULL
, OPT_LIBYATL_VERSION
},
136 { "quiet", no_argument
, NULL
, OPT_LIBYATL_QUIET
},
137 { "repeat", no_argument
, NULL
, OPT_LIBYATL_REPEAT
},
138 { "collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
139 { "massive", no_argument
, NULL
, OPT_LIBYATL_MASSIVE
},
146 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
154 case OPT_LIBYATL_VERSION
:
157 case OPT_LIBYATL_QUIET
:
161 case OPT_LIBYATL_REPEAT
:
162 opt_repeat
= strtoul(optarg
, (char **) NULL
, 10);
165 case OPT_LIBYATL_MATCH_COLLECTION
:
166 collection_to_run
= optarg
;
169 case OPT_LIBYATL_MASSIVE
:
174 /* getopt_long already printed an error message. */
175 Error
<< "unknown option to getopt_long()";
184 srandom((unsigned int)time(NULL
));
186 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10) > 1))
188 opt_repeat
= strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10);
191 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet
)
195 else if (getenv("JENKINS_URL"))
197 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
207 close(STDOUT_FILENO
);
211 if (getenv("LIBTEST_TMP"))
213 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
217 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
220 if (chdir(buffer
) == -1)
222 char getcwd_buffer
[1024];
223 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
225 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
229 if (libtest::libtool() == NULL
)
231 Error
<< "Failed to locate libtool";
241 exit_code
= EXIT_SUCCESS
;
242 std::auto_ptr
<Framework
> frame(new Framework
);
244 fatal_assert(sigignore(SIGPIPE
) == 0);
246 libtest::SignalThread signal
;
247 if (signal
.setup() == false)
249 Error
<< "Failed to setup signals";
255 get_world(frame
.get());
258 void *creators_ptr
= frame
->create(error
);
266 Out
<< "SKIP " << argv
[0];
273 if (getenv("YATL_COLLECTION_TO_RUN"))
275 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
277 collection_to_run
= getenv("YATL_COLLECTION_TO_RUN");
281 if (collection_to_run
.compare("none") == 0)
286 if (collection_to_run
.empty() == false)
288 Out
<< "Only testing " << collection_to_run
;
291 char *wildcard
= NULL
;
297 for (collection_st
*next
= frame
->collections
; next
and next
->name
and (not signal
.is_shutdown()); next
++)
299 if (collection_to_run
.empty() == false and fnmatch(collection_to_run
.c_str(), next
->name
, 0))
304 stats
.collection_total
++;
308 test_return_t collection_rc
;
309 if (test_success(collection_rc
= frame
->runner()->pre(next
->pre
, creators_ptr
)))
311 Out
<< "Collection: " << next
->name
;
313 for (test_st
*run
= next
->tests
; run
->name
; run
++)
315 long int load_time
= 0;
317 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
322 test_return_t return_code
;
325 if (run
->requires_flush
)
327 if (test_failed(frame
->runner()->flush(creators_ptr
)))
329 Error
<< "frame->runner()->flush(creators_ptr)";
334 return_code
= runner_code(frame
.get(), run
, creators_ptr
, load_time
);
336 if (return_code
== TEST_SKIPPED
)
338 else if (return_code
== TEST_FAILURE
)
341 Error
<< " frame->runner()->run(failure)";
342 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
347 catch (libtest::fatal
&e
)
349 Error
<< "Fatal exception was thrown: " << e
.what();
350 return_code
= TEST_FAILURE
;
353 catch (std::exception
&e
)
355 Error
<< "Exception was thrown: " << e
.what();
356 return_code
= TEST_FAILURE
;
361 Error
<< "Unknown exception occurred";
362 return_code
= TEST_FAILURE
;
371 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
378 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
384 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
388 fatal_message("invalid return code");
391 @TODO add code here to allow
for a collection to define a method to reset to allow tests to
continue.
395 (void) frame
->runner()->post(next
->post
, creators_ptr
);
397 else if (collection_rc
== TEST_FAILURE
)
399 Out
<< next
->name
<< " [ failed ]";
402 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
405 else if (collection_rc
== TEST_SKIPPED
)
407 Out
<< next
->name
<< " [ skipping ]";
411 if (failed
== false and skipped
== false)
413 stats
.collection_success
++;
418 stats
.collection_failed
++;
423 stats
.collection_skipped
++;
429 if (signal
.is_shutdown() == false)
431 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
434 shutdown_t status
= signal
.get_shutdown();
435 if (status
== SHUTDOWN_FORCED
)
437 Out
<< "Tests were aborted.";
438 exit_code
= EXIT_FAILURE
;
440 else if (stats
.collection_failed
)
442 Out
<< "Some test failed.";
443 exit_code
= EXIT_FAILURE
;
445 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
447 Out
<< "Some tests were skipped.";
449 else if (stats
.collection_success
and (stats
.collection_failed
== 0))
451 Out
<< "All tests completed successfully.";
456 Outn(); // Generate a blank to break up the messages if make check/test has been run
457 } while (exit_code
== EXIT_SUCCESS
and --opt_repeat
);
459 catch (libtest::fatal
& e
)
461 std::cerr
<< e
.what() << std::endl
;
463 catch (libtest::disconnected
& e
)
465 std::cerr
<< "Unhandled disconnection occurred:" << e
.what() << std::endl
;
467 catch (std::exception
& e
)
469 std::cerr
<< e
.what() << std::endl
;
473 std::cerr
<< "Unknown exception halted execution." << std::endl
;