1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012-2013 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"
53 #include <sys/types.h>
59 #ifndef __INTEL_COMPILER
60 #pragma GCC diagnostic ignored "-Wold-style-cast"
63 #if __cplusplus >= 201103L
64 # define UNIQUE_PTR std::unique_ptr
66 # define UNIQUE_PTR std::auto_ptr
69 using namespace libtest
;
71 static void stats_print(libtest::Framework
*frame
)
73 if (frame
->failed() == 0 and frame
->success() == 0)
79 Out
<< "Collections\t\t\t\t\t" << frame
->total();
80 Out
<< "\tFailed\t\t\t\t\t" << frame
->failed();
81 Out
<< "\tSkipped\t\t\t\t\t" << frame
->skipped();
82 Out
<< "\tSucceeded\t\t\t\t" << frame
->success();
84 Out
<< "Tests\t\t\t\t\t" << frame
->sum_total();
85 Out
<< "\tFailed\t\t\t\t" << frame
->sum_failed();
86 Out
<< "\tSkipped\t\t\t\t" << frame
->sum_skipped();
87 Out
<< "\tSucceeded\t\t\t" << frame
->sum_success();
93 int main(int argc
, char *argv
[])
95 bool opt_massive
= false;
96 unsigned long int opt_repeat
= 1; // Run all tests once
97 bool opt_quiet
= false;
98 std::string collection_to_run
;
100 std::string binary_name
;
102 const char *just_filename
= rindex(argv
[0], '/');
109 just_filename
= argv
[0];
112 if (just_filename
[0] == 'l' and just_filename
[1] == 't' and just_filename
[2] == '-')
116 binary_name
.append(just_filename
);
119 Valgrind does not currently work reliably, or sometimes at all, on OSX
120 - Fri Jun 15 11:24:07 EDT 2012
122 #if defined(__APPLE__) && __APPLE__
123 if (valgrind_is_caller())
133 OPT_LIBYATL_MATCH_COLLECTION
,
136 OPT_LIBYATL_MATCH_WILDCARD
,
140 static struct option long_options
[]=
142 { "version", no_argument
, NULL
, OPT_LIBYATL_VERSION
},
143 { "quiet", no_argument
, NULL
, OPT_LIBYATL_QUIET
},
144 { "repeat", required_argument
, NULL
, OPT_LIBYATL_REPEAT
},
145 { "collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
146 { "wildcard", required_argument
, NULL
, OPT_LIBYATL_MATCH_WILDCARD
},
147 { "massive", no_argument
, NULL
, OPT_LIBYATL_MASSIVE
},
154 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
162 case OPT_LIBYATL_VERSION
:
165 case OPT_LIBYATL_QUIET
:
169 case OPT_LIBYATL_REPEAT
:
171 opt_repeat
= strtoul(optarg
, (char **) NULL
, 10);
174 Error
<< "unknown value passed to --repeat: `" << optarg
<< "`";
179 case OPT_LIBYATL_MATCH_COLLECTION
:
180 collection_to_run
= optarg
;
183 case OPT_LIBYATL_MATCH_WILDCARD
:
187 case OPT_LIBYATL_MASSIVE
:
192 /* getopt_long already printed an error message. */
193 Error
<< "unknown option to getopt_long()";
202 srandom((unsigned int)time(NULL
));
205 if (bool(getenv("YATL_REPEAT")))
208 opt_repeat
= strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10);
211 Error
<< "ENV YATL_REPEAT passed an invalid value: `" << getenv("YATL_REPEAT") << "`";
216 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet
)
220 else if (getenv("JENKINS_URL"))
222 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
230 if ((bool(getenv("YATL_RUN_MASSIVE_TESTS"))) or opt_massive
)
237 close(STDOUT_FILENO
);
242 is_massive(opt_massive
);
245 libtest::vchar_t tmp_directory
;
246 tmp_directory
.resize(1024);
247 if (getenv("LIBTEST_TMP"))
249 snprintf(&tmp_directory
[0], tmp_directory
.size(), "%s", getenv("LIBTEST_TMP"));
253 snprintf(&tmp_directory
[0], tmp_directory
.size(), "%s", LIBTEST_TEMP
);
256 if (chdir(&tmp_directory
[0]) == -1)
258 libtest::vchar_t getcwd_buffer
;
259 getcwd_buffer
.resize(1024);
260 char *dir
= getcwd(&getcwd_buffer
[0], getcwd_buffer
.size());
262 Error
<< "Unable to chdir() from " << dir
<< " to " << &tmp_directory
[0] << " errno:" << strerror(errno
);
266 if (getenv("YATL_COLLECTION_TO_RUN"))
268 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
270 collection_to_run
= getenv("YATL_COLLECTION_TO_RUN");
274 if (collection_to_run
.compare("none") == 0)
279 if (collection_to_run
.empty() == false)
281 Out
<< "Only testing " << collection_to_run
;
290 exit_code
= EXIT_SUCCESS
;
291 fatal_assert(sigignore(SIGPIPE
) == 0);
293 libtest::SignalThread signal
;
294 if (signal
.setup() == false)
296 Error
<< "Failed to setup signals";
300 UNIQUE_PTR
<libtest::Framework
> frame(new libtest::Framework(signal
, binary_name
, collection_to_run
, wildcard
));
302 // Run create(), bail on error.
304 switch (frame
->create())
310 SKIP("SKIP was returned from framework create()");
314 std::cerr
<< "Could not call frame->create()" << std::endl
;
321 if (signal
.is_shutdown() == false)
323 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
326 shutdown_t status
= signal
.get_shutdown();
327 if (status
== SHUTDOWN_FORCED
)
329 Out
<< "Tests were aborted.";
330 exit_code
= EXIT_FAILURE
;
332 else if (frame
->failed())
334 Out
<< "Some test failed.";
335 exit_code
= EXIT_FAILURE
;
337 else if (frame
->skipped() and frame
->failed() and frame
->success())
339 Out
<< "Some tests were skipped.";
341 else if (frame
->success() and (frame
->failed() == 0))
344 Out
<< "All tests completed successfully.";
347 stats_print(frame
.get());
349 std::ofstream xml_file
;
350 std::string file_name
;
351 file_name
.append(&tmp_directory
[0]);
352 file_name
.append(frame
->name());
353 file_name
.append(".xml");
354 xml_file
.open(file_name
.c_str(), std::ios::trunc
);
355 libtest::Formatter::xml(*frame
, xml_file
);
357 Outn(); // Generate a blank to break up the messages if make check/test has been run
358 } while (exit_code
== EXIT_SUCCESS
and --opt_repeat
);
360 catch (const libtest::__skipped
& e
)
364 catch (const libtest::__failure
& e
)
366 libtest::stream::make_cout(e
.file(), e
.line(), e
.func()) << e
.what();
367 exit_code
= EXIT_FAILURE
;
369 catch (const libtest::fatal
& e
)
371 std::cerr
<< "FATAL:" << e
.what() << std::endl
;
372 exit_code
= EXIT_FAILURE
;
374 catch (const libtest::disconnected
& e
)
376 std::cerr
<< "Unhandled disconnection occurred:" << e
.what() << std::endl
;
377 exit_code
= EXIT_FAILURE
;
379 catch (const std::exception
& e
)
381 std::cerr
<< "std::exception:" << e
.what() << std::endl
;
382 exit_code
= EXIT_FAILURE
;
384 catch (char const* s
)
386 std::cerr
<< "Exception:" << s
<< std::endl
;
387 exit_code
= EXIT_FAILURE
;
391 std::cerr
<< "Unknown exception halted execution." << std::endl
;
392 exit_code
= EXIT_FAILURE
;