| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/xml_reporter.h> |
| 3 | |
| 4 | #include <cgreen/vector.h> |
| 5 | |
| 6 | #include "utils.h" |
| 7 | |
| 8 | #include <unistd.h> |
| 9 | #include <libgen.h> |
| 10 | |
| 11 | #include "gopt.h" |
| 12 | |
| 13 | #include "runner.h" |
| 14 | #include "discoverer.h" |
| 15 | |
| 16 | |
| 17 | /*----------------------------------------------------------------------*/ |
| 18 | static int file_exists(const char *filename) |
| 19 | { |
| 20 | if (! filename) return 0; |
| 21 | return (access(filename, F_OK) == 0); |
| 22 | } |
| 23 | |
| 24 | |
| 25 | /*----------------------------------------------------------------------*/ |
| 26 | static void version(void) { |
| 27 | printf("cgreen-runner for Cgreen unittest and mocking framework v%s\n", VERSION); |
| 28 | printf("Cgreen library version %s (%s) loaded\n", cgreen_library_version, cgreen_library_revision); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /*----------------------------------------------------------------------*/ |
| 33 | static void usage(const char *program_name) { |
| 34 | printf("cgreen-runner for Cgreen unittest and mocking framework v%s\n\n", VERSION); |
| 35 | printf("Usage:\n %s [--suite <name>] [--verbose] [--quiet] [--no-run] " |
| 36 | #if HAVE_XML_REPORTER |
| 37 | "[--xml <prefix>] " |
| 38 | #endif |
| 39 | #if HAVE_LIBXML2_REPORTER |
| 40 | "[--libxml2 <prefix>] " |
| 41 | #endif |
| 42 | "[--help] (<library> [<test>])+\n\n", program_name); |
| 43 | printf("Discover and run all or named cgreen test(s) from one or multiple\n"); |
| 44 | printf("dynamically loadable libraries.\n\n"); |
| 45 | printf("A single test can be run using the form [<context>:]<name> where <context> can\n"); |
| 46 | printf("be omitted if there is no context.\n\n"); |
| 47 | printf(" -c --colours/colors\t\tUse colours to emphasis result (requires ANSI-capable terminal)\n"); |
| 48 | printf(" -C --no-colours/no-colors\tDon't use colours\n"); |
| 49 | #if HAVE_XML_REPORTER |
| 50 | printf(" -x --xml <prefix>\t\tInstead of messages on stdout, write results into one XML-file\n"); |
| 51 | printf("\t\t\t\tper suite, compatible with Hudson/Jenkins CI. The filename(s)\n"); |
| 52 | printf("\t\t\t\twill be '<prefix>-<suite>.xml'\n"); |
| 53 | #endif |
| 54 | #if HAVE_LIBXML2_REPORTER |
| 55 | printf(" -X --libxml2 <prefix>\t\tFormat the test results using libxml2\n"); |
| 56 | #endif |
| 57 | printf(" -s --suite <name>\t\tName the top level suite\n"); |
| 58 | printf(" -n --no-run\t\t\tDon't run the tests\n"); |
| 59 | printf(" -v --verbose\t\t\tShow progress information\n"); |
| 60 | printf(" -q --quiet\t\t\tOutput a single character per context ('.'=pass, 'S'=suspended, 'F'=fail, 'X'=error), then a summary\n"); |
| 61 | printf(" --version\t\t\tShow version information\n"); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /*======================================================================*/ |
| 66 | |
| 67 | enum option_arg { |
| 68 | HELP_OPT, |
| 69 | XML_OPT, |
| 70 | LIBXML2_OPT, |
| 71 | SUITE_OPT, |
| 72 | VERBOSE_OPT, |
| 73 | QUITE_OPT, |
| 74 | VERSION_OPT, |
| 75 | COLORS_OPT, |
| 76 | COLOURS_OPT, |
| 77 | NO_COLORS_OPT, |
| 78 | NO_COLOURS_OPT, |
| 79 | NO_RUN_OPT, |
| 80 | LAST_OPT, |
| 81 | }; |
| 82 | |
| 83 | static struct option options[] = { |
| 84 | [HELP_OPT] = { |
| 85 | .short_name = 'h', |
| 86 | .long_name = "help", |
| 87 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 88 | }, |
| 89 | [XML_OPT] = { |
| 90 | #if HAVE_XML_REPORTER |
| 91 | .short_name = 'x', |
| 92 | .long_name = "xml", |
| 93 | .flags = GOPT_ARGUMENT_REQUIRED, |
| 94 | #endif |
| 95 | }, |
| 96 | [LIBXML2_OPT] = { |
| 97 | #if HAVE_LIBXML2_REPORTER |
| 98 | .short_name = 'X', |
| 99 | .long_name = "libxml2", |
| 100 | #endif |
| 101 | .flags = GOPT_ARGUMENT_REQUIRED, |
| 102 | }, |
| 103 | [SUITE_OPT] = { |
| 104 | .short_name = 's', |
| 105 | .long_name = "suite", |
| 106 | .flags = GOPT_ARGUMENT_REQUIRED, |
| 107 | }, |
| 108 | [VERBOSE_OPT] = { |
| 109 | .short_name = 'v', |
| 110 | .long_name = "verbose", |
| 111 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 112 | }, |
| 113 | [QUITE_OPT] = { |
| 114 | .short_name = 'q', |
| 115 | .long_name = "quiet", |
| 116 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 117 | }, |
| 118 | [VERSION_OPT] = { |
| 119 | .short_name = 'V', |
| 120 | .long_name = "version", |
| 121 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 122 | }, |
| 123 | [COLORS_OPT] = { |
| 124 | .short_name = 'c', |
| 125 | .long_name = "colors", |
| 126 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 127 | }, |
| 128 | [COLOURS_OPT] = { |
| 129 | .short_name = 'c', |
| 130 | .long_name = "colours", |
| 131 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 132 | }, |
| 133 | [NO_COLORS_OPT] = { |
| 134 | .short_name = 'C', |
| 135 | .long_name = "no-colors", |
| 136 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 137 | }, |
| 138 | [NO_COLOURS_OPT] = { |
| 139 | .short_name = 'C', |
| 140 | .long_name = "no-colours", |
| 141 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 142 | }, |
| 143 | [NO_RUN_OPT] = { |
| 144 | .short_name = 'n', |
| 145 | .long_name = "no-run", |
| 146 | .flags = GOPT_ARGUMENT_FORBIDDEN, |
| 147 | }, |
| 148 | [LAST_OPT] = { |
| 149 | .flags = GOPT_LAST, |
| 150 | }, |
| 151 | }; |
| 152 | static TestReporter *reporter = NULL; |
| 153 | static TextReporterOptions reporter_options; |
| 154 | |
| 155 | static void cleanup(void) |
| 156 | { |
| 157 | if (reporter) reporter->destroy(reporter); |
| 158 | } |
| 159 | |
| 160 | static char* get_a_suite_name(const char *suite_option, const char *test_library_name) { |
| 161 | if (suite_option == NULL) { |
| 162 | char *suite_name; |
| 163 | char *s; |
| 164 | /* basename can return the parameter or an internal static string. Work around this. */ |
| 165 | s = string_dup(test_library_name); |
| 166 | suite_name = string_dup(basename(s)); |
| 167 | free(s); |
| 168 | if ((s = strchr(suite_name, '.'))) { |
| 169 | *s = '\0'; |
| 170 | } |
| 171 | return suite_name; |
| 172 | } else { |
| 173 | return string_dup(suite_option); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /*----------------------------------------------------------------------*/ |
| 178 | static bool have_xml_option(void) { |
| 179 | return options[XML_OPT].count > 0 || options[LIBXML2_OPT].count > 0; |
| 180 | } |
| 181 | |
| 182 | static bool have_libxml2_option(void) { |
| 183 | return options[LIBXML2_OPT].count > 0; |
| 184 | } |
| 185 | |
| 186 | /*----------------------------------------------------------------------*/ |
| 187 | static bool run_tests_in_library(const char *suite_name_option, const char *test_name, |
| 188 | const char *test_library, bool verbose, bool no_run) { |
| 189 | int status; |
| 190 | char *suite_name; |
| 191 | |
| 192 | suite_name = get_a_suite_name(suite_name_option, test_library); |
| 193 | |
| 194 | status = runner(reporter, test_library, suite_name, test_name, verbose, no_run); |
| 195 | free((void*)suite_name); |
| 196 | |
| 197 | return status != 0; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /*----------------------------------------------------------------------*/ |
| 202 | static void print_common_header(const char *suite_name_option, int library_count, int test_count) { |
| 203 | char in_libraries_text[100] = ""; |
| 204 | sprintf(in_libraries_text, " in %d libraries", library_count); |
| 205 | |
| 206 | printf("Running \"%s\" (%d test%s%s)...\n", suite_name_option, |
| 207 | test_count, test_count>1?"s":"", |
| 208 | in_libraries_text); |
| 209 | } |
| 210 | |
| 211 | /*----------------------------------------------------------------------*/ |
| 212 | static void inhibit_appropriate_suite_message(int i, int library_count) { |
| 213 | /* If we are using the text_reporter && a common suite name |
| 214 | then inhibit the start suite message for all libraries and |
| 215 | for all except the last library inhibit the finish suite |
| 216 | message. NOTE that the last library might be the second to |
| 217 | last arg since there might be a testname pattern after it. |
| 218 | */ |
| 219 | reporter_options.inhibit_start_suite_message = true; |
| 220 | |
| 221 | if (i < library_count-1 && library_count > 1) { |
| 222 | reporter_options.inhibit_finish_suite_message = true; |
| 223 | } else |
| 224 | reporter_options.inhibit_finish_suite_message = false; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | /*======================================================================*/ |
| 230 | int main(int argc, char **argv) { |
| 231 | int i; |
| 232 | |
| 233 | bool verbose = false; |
| 234 | bool no_run = false; |
| 235 | |
| 236 | const char *suite_name_option = NULL; |
| 237 | |
| 238 | bool any_fail = false; |
| 239 | |
| 240 | atexit(cleanup); |
| 241 | |
| 242 | argc = gopt(argv, options); |
| 243 | gopt_errors(argv[0], options); |
| 244 | |
| 245 | if (have_libxml2_option()) { |
| 246 | #if HAVE_LIBXML2_REPORTER |
| 247 | reporter = create_libxml_reporter(options[LIBXML2_OPT].argument); |
| 248 | #else |
| 249 | printf("libxml2 reporter not available\n"); |
| 250 | return EXIT_FAILURE; |
| 251 | #endif |
| 252 | } else if (have_xml_option()) { |
| 253 | #if HAVE_XML_REPORTER |
| 254 | reporter = create_xml_reporter(options[XML_OPT].argument); |
| 255 | #else |
| 256 | printf("XML reporter not available\n"); |
| 257 | return EXIT_FAILURE; |
| 258 | #endif |
| 259 | } else |
| 260 | reporter = create_text_reporter(); |
| 261 | |
| 262 | suite_name_option = options[SUITE_OPT].argument; |
| 263 | |
| 264 | verbose = options[VERBOSE_OPT].count > 0; |
| 265 | no_run = options[NO_RUN_OPT].count > 0; |
| 266 | |
| 267 | reporter_options.use_colours = true; |
| 268 | if (isatty(fileno(stdout))) { |
| 269 | if (options[NO_COLORS_OPT].count || options[NO_COLOURS_OPT].count) |
| 270 | reporter_options.use_colours = false; |
| 271 | } else { |
| 272 | if (!options[COLORS_OPT].count && !options[COLOURS_OPT].count) |
| 273 | reporter_options.use_colours = false; |
| 274 | } |
| 275 | panic_use_colours = reporter_options.use_colours; |
| 276 | |
| 277 | reporter_options.quiet_mode = options[QUITE_OPT].count > 0; |
| 278 | |
| 279 | if (options[HELP_OPT].count) { |
| 280 | usage(argv[0]); |
| 281 | return EXIT_SUCCESS; |
| 282 | } |
| 283 | |
| 284 | if (options[VERSION_OPT].count) { |
| 285 | version(); |
| 286 | return EXIT_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | if (argc < 2) { |
| 290 | usage(argv[0]); |
| 291 | return EXIT_FAILURE; |
| 292 | } |
| 293 | |
| 294 | set_reporter_options(reporter, &reporter_options); |
| 295 | |
| 296 | /* Walk through all arguments and set up list of libraries and testnames */ |
| 297 | const char **libraries; /* Array of libraries to run tests in */ |
| 298 | libraries = malloc(sizeof(char *)*argc); |
| 299 | |
| 300 | const char **testname; /* Array of test name patterns, maybe NULL */ |
| 301 | testname = malloc(sizeof(char *)*argc); |
| 302 | |
| 303 | int library_count = 0; |
| 304 | |
| 305 | /* Find libraries */ |
| 306 | i = 1; |
| 307 | while(i < argc) { |
| 308 | libraries[library_count++] = argv[i++]; |
| 309 | |
| 310 | /* Check if the next argument is not a filename, thus a test name, |
| 311 | remember and move past it */ |
| 312 | if (!file_exists(argv[i])) { |
| 313 | testname[library_count-1] = argv[i++]; |
| 314 | } else |
| 315 | testname[library_count-1] = NULL; |
| 316 | } |
| 317 | |
| 318 | reporter_options.inhibit_start_suite_message = false; |
| 319 | reporter_options.inhibit_finish_suite_message = false; |
| 320 | |
| 321 | int test_count = 0; |
| 322 | if (!have_xml_option() && (suite_name_option != NULL)) { |
| 323 | |
| 324 | /* Count all tests */ |
| 325 | for (i = 0; i<library_count; i++) { |
| 326 | if (!file_exists(libraries[i])) { |
| 327 | printf("Couldn't find library: %s\n", libraries[i]); |
| 328 | any_fail = true; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | CgreenVector *discovered_tests = discover_tests_in(libraries[i], false); |
| 333 | test_count += cgreen_vector_size(discovered_tests); |
| 334 | destroy_cgreen_vector(discovered_tests); |
| 335 | } |
| 336 | print_common_header(suite_name_option, library_count, test_count); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | for (i = 0; i<library_count; i++) { |
| 341 | bool fail = false; |
| 342 | |
| 343 | if (!file_exists(libraries[i])) { |
| 344 | printf("Couldn't find library: %s\n", libraries[i]); |
| 345 | any_fail = true; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | if (!have_xml_option() && (suite_name_option != NULL)) |
| 350 | inhibit_appropriate_suite_message(i, library_count); |
| 351 | |
| 352 | fail = run_tests_in_library(suite_name_option, testname[i], libraries[i], |
| 353 | verbose, no_run); |
| 354 | if (fail) any_fail = true; |
| 355 | } |
| 356 | |
| 357 | if (!have_xml_option() && reporter_options.quiet_mode) |
| 358 | printf("\n"); |
| 359 | |
| 360 | free(libraries); |
| 361 | free(testname); |
| 362 | return any_fail?EXIT_FAILURE:EXIT_SUCCESS; |
| 363 | } |