1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "libtest/yatlcon.h"
38 #include <libtest/common.h>
50 #include <sys/types.h>
56 #ifndef __INTEL_COMPILER
57 #pragma GCC diagnostic ignored "-Wold-style-cast"
60 using namespace libtest
;
62 static void stats_print(libtest::Framework
*frame
)
64 if (frame
->failed() == 0 and frame
->success() == 0)
70 Out
<< "Collections\t\t\t\t\t" << frame
->total();
71 Out
<< "\tFailed\t\t\t\t\t" << frame
->failed();
72 Out
<< "\tSkipped\t\t\t\t\t" << frame
->skipped();
73 Out
<< "\tSucceeded\t\t\t\t" << frame
->success();
75 Out
<< "Tests\t\t\t\t\t" << frame
->sum_total();
76 Out
<< "\tFailed\t\t\t\t" << frame
->sum_failed();
77 Out
<< "\tSkipped\t\t\t\t" << frame
->sum_skipped();
78 Out
<< "\tSucceeded\t\t\t" << frame
->sum_success();
84 int main(int argc
, char *argv
[])
86 bool opt_massive
= false;
87 unsigned long int opt_repeat
= 1; // Run all tests once
88 bool opt_quiet
= false;
89 std::string collection_to_run
;
91 std::string binary_name
;
93 const char *just_filename
= rindex(argv
[0], '/');
100 just_filename
= argv
[0];
103 if (just_filename
[0] == 'l' and just_filename
[1] == 't' and just_filename
[2] == '-')
107 binary_name
.append(just_filename
);
110 Valgrind does not currently work reliably, or sometimes at all, on OSX
111 - Fri Jun 15 11:24:07 EDT 2012
113 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
114 if (valgrind_is_caller())
124 OPT_LIBYATL_MATCH_COLLECTION
,
127 OPT_LIBYATL_MATCH_WILDCARD
,
131 static struct option long_options
[]=
133 { "version", no_argument
, NULL
, OPT_LIBYATL_VERSION
},
134 { "quiet", no_argument
, NULL
, OPT_LIBYATL_QUIET
},
135 { "repeat", required_argument
, NULL
, OPT_LIBYATL_REPEAT
},
136 { "collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
137 { "wildcard", required_argument
, NULL
, OPT_LIBYATL_MATCH_WILDCARD
},
138 { "massive", no_argument
, NULL
, OPT_LIBYATL_MASSIVE
},
145 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
153 case OPT_LIBYATL_VERSION
:
156 case OPT_LIBYATL_QUIET
:
160 case OPT_LIBYATL_REPEAT
:
161 opt_repeat
= strtoul(optarg
, (char **) NULL
, 10);
164 case OPT_LIBYATL_MATCH_COLLECTION
:
165 collection_to_run
= optarg
;
168 case OPT_LIBYATL_MATCH_WILDCARD
:
172 case OPT_LIBYATL_MASSIVE
:
177 /* getopt_long already printed an error message. */
178 Error
<< "unknown option to getopt_long()";
187 srandom((unsigned int)time(NULL
));
189 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10) > 1))
191 opt_repeat
= strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10);
194 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet
)
198 else if (getenv("JENKINS_URL"))
200 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
208 if ((bool(getenv("YATL_RUN_MASSIVE_TESTS"))) or opt_massive
)
215 close(STDOUT_FILENO
);
220 is_massive(opt_massive
);
223 libtest::vchar_t tmp_directory
;
224 tmp_directory
.resize(1024);
225 if (getenv("LIBTEST_TMP"))
227 snprintf(&tmp_directory
[0], tmp_directory
.size(), "%s", getenv("LIBTEST_TMP"));
231 snprintf(&tmp_directory
[0], tmp_directory
.size(), "%s", LIBTEST_TEMP
);
234 if (chdir(&tmp_directory
[0]) == -1)
236 libtest::vchar_t getcwd_buffer
;
237 getcwd_buffer
.resize(1024);
238 char *dir
= getcwd(&getcwd_buffer
[0], getcwd_buffer
.size());
240 Error
<< "Unable to chdir() from " << dir
<< " to " << &tmp_directory
[0] << " errno:" << strerror(errno
);
244 if (libtest::libtool() == NULL
)
246 Error
<< "Failed to locate libtool";
250 if (getenv("YATL_COLLECTION_TO_RUN"))
252 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
254 collection_to_run
= getenv("YATL_COLLECTION_TO_RUN");
258 if (collection_to_run
.compare("none") == 0)
263 if (collection_to_run
.empty() == false)
265 Out
<< "Only testing " << collection_to_run
;
274 exit_code
= EXIT_SUCCESS
;
275 fatal_assert(sigignore(SIGPIPE
) == 0);
277 libtest::SignalThread signal
;
278 if (signal
.setup() == false)
280 Error
<< "Failed to setup signals";
284 std::auto_ptr
<libtest::Framework
> frame(new libtest::Framework(signal
, binary_name
, collection_to_run
, wildcard
));
286 // Run create(), bail on error.
289 switch (frame
->create())
298 std::cerr
<< "Could not call frame->create()" << std::endl
;
302 catch (libtest::__skipped e
)
309 if (signal
.is_shutdown() == false)
311 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
314 shutdown_t status
= signal
.get_shutdown();
315 if (status
== SHUTDOWN_FORCED
)
317 Out
<< "Tests were aborted.";
318 exit_code
= EXIT_FAILURE
;
320 else if (frame
->failed())
322 Out
<< "Some test failed.";
323 exit_code
= EXIT_FAILURE
;
325 else if (frame
->skipped() and frame
->failed() and frame
->success())
327 Out
<< "Some tests were skipped.";
329 else if (frame
->success() and (frame
->failed() == 0))
332 Out
<< "All tests completed successfully.";
335 stats_print(frame
.get());
337 std::ofstream xml_file
;
338 std::string file_name
;
339 file_name
.append(&tmp_directory
[0]);
340 file_name
.append(frame
->name());
341 file_name
.append(".xml");
342 xml_file
.open(file_name
.c_str(), std::ios::trunc
);
343 libtest::Formatter::xml(*frame
, xml_file
);
345 Outn(); // Generate a blank to break up the messages if make check/test has been run
346 } while (exit_code
== EXIT_SUCCESS
and --opt_repeat
);
348 catch (libtest::__skipped e
)
352 catch (libtest::__failure e
)
354 libtest::stream::make_cout(e
.file(), e
.line(), e
.func()) << e
.what();
355 exit_code
= EXIT_FAILURE
;
357 catch (libtest::fatal
& e
)
359 std::cerr
<< "FATAL:" << e
.what() << std::endl
;
360 exit_code
= EXIT_FAILURE
;
362 catch (libtest::disconnected
& e
)
364 std::cerr
<< "Unhandled disconnection occurred:" << e
.what() << std::endl
;
365 exit_code
= EXIT_FAILURE
;
367 catch (std::exception
& e
)
369 std::cerr
<< "std::exception:" << e
.what() << std::endl
;
370 exit_code
= EXIT_FAILURE
;
374 std::cerr
<< "Exception:" << std::endl
;
375 exit_code
= EXIT_FAILURE
;
379 std::cerr
<< "Unknown exception halted execution." << std::endl
;
380 exit_code
= EXIT_FAILURE
;