| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/xml_reporter.h> |
| 3 | #include <cgreen/vector.h> |
| 4 | |
| 5 | #include <dlfcn.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | #include <fnmatch.h> |
| 12 | |
| 13 | #include "../src/utils.h" |
| 14 | #include "runner.h" |
| 15 | #include "test_item.h" |
| 16 | |
| 17 | #include "discoverer.h" |
| 18 | |
| 19 | /* The ContextSuite is a datastructure created to partion the tests in |
| 20 | suites according to the contexts, one suite per context. It |
| 21 | consists of a list of nodes, each mapping a context name to a |
| 22 | TestSuite. If there where a public way to navigate the structure of |
| 23 | suites, we could name the TestSuites after the context and then |
| 24 | find the appropriate suite by navigating in the TestSuite |
| 25 | structure, but this seems impossible for now. */ |
| 26 | |
| 27 | typedef struct ContextSuite { |
| 28 | char *context_name; |
| 29 | TestSuite *suite; |
| 30 | struct ContextSuite *next; |
| 31 | } ContextSuite; |
| 32 | |
| 33 | /*----------------------------------------------------------------------*/ |
| 34 | static void destroy_context_suites(ContextSuite *context_suite) { |
| 35 | if (context_suite != NULL) { |
| 36 | if (context_suite->next != NULL) |
| 37 | destroy_context_suites(context_suite->next); |
| 38 | free(context_suite->context_name); |
| 39 | free(context_suite); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #define CGREEN_DEFAULT_SUITE "default" |
| 44 | |
| 45 | |
| 46 | /*----------------------------------------------------------------------*/ |
| 47 | static char *context_name_of(const char* symbolic_name) { |
| 48 | char *context_name; |
| 49 | |
| 50 | if (strchr(symbolic_name, ':')) { |
| 51 | context_name = string_dup(symbolic_name); |
| 52 | *strchr(context_name, ':') = '\0'; |
| 53 | } else { |
| 54 | context_name = string_dup(CGREEN_DEFAULT_SUITE); |
| 55 | } |
| 56 | |
| 57 | return context_name; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /*----------------------------------------------------------------------*/ |
| 62 | static char *test_name_of(const char *symbolic_name) { |
| 63 | const char *colon = strchr(symbolic_name, ':'); |
| 64 | if (colon) { |
| 65 | return string_dup(colon+1); |
| 66 | } |
| 67 | |
| 68 | return string_dup(symbolic_name); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /*----------------------------------------------------------------------*/ |
| 73 | static bool test_matches_pattern(const char *symbolic_name_pattern, |
| 74 | TestItem *test_item) { |
| 75 | char* context_name = context_name_of(symbolic_name_pattern); |
| 76 | int context_name_matches_test = fnmatch(context_name, test_item->context_name, 0) == 0; |
| 77 | char* test_name = test_name_of(symbolic_name_pattern); |
| 78 | int test_name_matches_test = fnmatch(test_name, test_item->test_name, 0) == 0; |
| 79 | |
| 80 | free(context_name); |
| 81 | free(test_name); |
| 82 | |
| 83 | return context_name_matches_test && test_name_matches_test; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /*----------------------------------------------------------------------*/ |
| 88 | static TestSuite *find_suite_for_context(ContextSuite *suites, const char *context_name) { |
| 89 | ContextSuite *suite; |
| 90 | |
| 91 | for (suite = suites; suite != NULL; suite = suite->next) { |
| 92 | if (strcmp(suite->context_name, context_name) == 0) |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | if (suite != NULL) { |
| 97 | return suite->suite; |
| 98 | } |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /*----------------------------------------------------------------------*/ |
| 105 | static ContextSuite *add_new_context_suite(TestSuite *parent, const char* context_name, |
| 106 | ContextSuite *next) { |
| 107 | ContextSuite *new_context_suite = (ContextSuite *)calloc(1, sizeof(ContextSuite)); |
| 108 | new_context_suite->context_name = string_dup(context_name); |
| 109 | new_context_suite->suite = create_named_test_suite(context_name); |
| 110 | new_context_suite->next = next; |
| 111 | add_suite_(parent, context_name, new_context_suite->suite); |
| 112 | return new_context_suite; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /*----------------------------------------------------------------------*/ |
| 117 | static void add_test_to_context(TestSuite *parent, ContextSuite **context_suites, |
| 118 | TestItem *test_item, CgreenTest *test) { |
| 119 | TestSuite *suite_for_context = find_suite_for_context(*context_suites, test_item->context_name); |
| 120 | |
| 121 | if (suite_for_context == NULL) { |
| 122 | *context_suites = add_new_context_suite(parent, test_item->context_name, *context_suites); |
| 123 | suite_for_context = (*context_suites)->suite; |
| 124 | } |
| 125 | add_test_(suite_for_context, test_item->test_name, test); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /*----------------------------------------------------------------------*/ |
| 130 | static TestItem *get_item_from(CgreenVector *tests, int i) { |
| 131 | return (TestItem*)cgreen_vector_get(tests, i); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /*----------------------------------------------------------------------*/ |
| 136 | static int add_matching_tests_to_suite(void *handle, |
| 137 | const char *symbolic_name_pattern, |
| 138 | CgreenVector *tests, TestSuite *suite, |
| 139 | ContextSuite **context_suites) |
| 140 | { |
| 141 | int count = 0; |
| 142 | |
| 143 | for (int i = 0; i<cgreen_vector_size(tests); i++) { |
| 144 | if (symbolic_name_pattern == NULL || test_matches_pattern(symbolic_name_pattern, |
| 145 | get_item_from(tests, i))) { |
| 146 | char *error; |
| 147 | const char *specification_name = get_item_from(tests, i)->specification_name; |
| 148 | CgreenTest *test_function = (CgreenTest *)(dlsym(handle, |
| 149 | specification_name)); |
| 150 | if ((error = dlerror()) != NULL) { |
| 151 | PANIC("Could not find specification_name symbol '%s'", |
| 152 | specification_name); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | add_test_to_context(suite, context_suites, get_item_from(tests, i), test_function); |
| 157 | count++; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return count; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /*----------------------------------------------------------------------*/ |
| 166 | static bool matching_test_exists(const char *test_name, CgreenVector *tests) { |
| 167 | for (int i = 0; i<cgreen_vector_size(tests); i++) |
| 168 | if (test_matches_pattern(test_name, get_item_from(tests, i))) { |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /*----------------------------------------------------------------------*/ |
| 176 | static int count(CgreenVector *vector) { |
| 177 | return cgreen_vector_size(vector); |
| 178 | } |
| 179 | |
| 180 | /*----------------------------------------------------------------------*/ |
| 181 | static bool error_when_matching(int number_of_matches) { |
| 182 | return number_of_matches < 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /*----------------------------------------------------------------------*/ |
| 187 | static int run_tests(TestReporter *reporter, |
| 188 | const char *suite_name, |
| 189 | const char *symbolic_name, |
| 190 | void *test_library_handle, |
| 191 | CgreenVector *tests, |
| 192 | bool verbose) { |
| 193 | int status; |
| 194 | ContextSuite *context_suites = NULL; |
| 195 | TestSuite *suite = create_named_test_suite(suite_name); |
| 196 | |
| 197 | const int number_of_matches = add_matching_tests_to_suite(test_library_handle, |
| 198 | symbolic_name, |
| 199 | tests, |
| 200 | suite, |
| 201 | &context_suites); |
| 202 | if (error_when_matching(number_of_matches)) |
| 203 | return EXIT_FAILURE; |
| 204 | |
| 205 | if (symbolic_name != NULL && number_of_matches == 1) { |
| 206 | bool found = matching_test_exists(symbolic_name, tests); |
| 207 | if (verbose) |
| 208 | printf(" to only run one test: '%s' ...\n", symbolic_name); |
| 209 | if (!found) { |
| 210 | fprintf(stderr, "ERROR: No such test: '%s' in '%s'\n", symbolic_name, suite_name); |
| 211 | return EXIT_FAILURE; |
| 212 | } |
| 213 | char *test_name = test_name_of(symbolic_name); |
| 214 | status = run_single_test(suite, test_name, reporter); |
| 215 | free(test_name); |
| 216 | } else { |
| 217 | if (verbose) { |
| 218 | if (number_of_matches != count(tests)) |
| 219 | printf(" to run %d matching tests ...\n", number_of_matches); |
| 220 | else |
| 221 | printf(" to run all %d discovered tests ...\n", count(tests)); |
| 222 | } |
| 223 | |
| 224 | if (number_of_matches > 0) |
| 225 | status = run_test_suite(suite, reporter); |
| 226 | else { |
| 227 | fprintf(stderr, "ERROR: No such test: '%s' in '%s'\n", symbolic_name, suite_name); |
| 228 | status = EXIT_FAILURE; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | destroy_test_suite(suite); |
| 233 | destroy_context_suites(context_suites); |
| 234 | return(status); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /*----------------------------------------------------------------------*/ |
| 239 | const char *test_name_of_element(CgreenVector *tests, int index) { |
| 240 | return get_item_from(tests, index)->test_name; |
| 241 | } |
| 242 | |
| 243 | /*----------------------------------------------------------------------*/ |
| 244 | static CgreenVector *sorted_test_items_from(CgreenVector *test_items) { |
| 245 | CgreenVector *sorted = create_cgreen_vector((GenericDestructor)destroy_test_item); |
| 246 | while (cgreen_vector_size(test_items) > 1) { |
| 247 | int smallest = 0; |
| 248 | const char *smallest_test_name = test_name_of_element(test_items, smallest); |
| 249 | for (int i=1; i<cgreen_vector_size(test_items); i++) { |
| 250 | const char *current_test_name = test_name_of_element(test_items, i); |
| 251 | if (strcmp(smallest_test_name, current_test_name) > 0) { |
| 252 | smallest = i; |
| 253 | smallest_test_name = test_name_of_element(test_items, smallest); |
| 254 | } |
| 255 | } |
| 256 | cgreen_vector_add(sorted, cgreen_vector_remove(test_items, smallest)); |
| 257 | } |
| 258 | if (cgreen_vector_size(test_items) == 1) { |
| 259 | cgreen_vector_add(sorted, cgreen_vector_remove(test_items, 0)); |
| 260 | destroy_cgreen_vector(test_items); |
| 261 | } |
| 262 | return sorted; |
| 263 | } |
| 264 | |
| 265 | /*----------------------------------------------------------------------*/ |
| 266 | static char *absolute(const char *file_path) { |
| 267 | if (strchr("./", file_path[0]) != NULL) |
| 268 | return strdup(file_path); |
| 269 | else { |
| 270 | char *tmp = malloc(strlen(file_path)+3); |
| 271 | strcpy(tmp, "./"); |
| 272 | strcat(tmp, file_path); |
| 273 | return tmp; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /*======================================================================*/ |
| 279 | int runner(TestReporter *reporter, const char *test_library_name, |
| 280 | const char *suite_name, const char *test_name, |
| 281 | bool verbose, bool dont_run) { |
| 282 | int status = 0; |
| 283 | void *test_library_handle = NULL; |
| 284 | |
| 285 | CgreenVector *tests = discover_tests_in(test_library_name, verbose); |
| 286 | |
| 287 | if (count(tests) == 0) { |
| 288 | printf("No tests found in '%s'.\n", test_library_name); |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | if (verbose) |
| 293 | printf("Discovered %d test(s)\n", count(tests)); |
| 294 | |
| 295 | char *absolute_library_name = absolute(test_library_name); |
| 296 | tests = sorted_test_items_from(tests); |
| 297 | if (verbose) |
| 298 | printf("Opening [%s]", test_library_name); |
| 299 | test_library_handle = dlopen(absolute_library_name, RTLD_NOW); |
| 300 | if (test_library_handle == NULL) { |
| 301 | PANIC("dlopen failure when trying to run '%s' (error: %s)\n", |
| 302 | absolute_library_name, dlerror()); |
| 303 | status = 2; |
| 304 | } else { |
| 305 | if (!dont_run) { |
| 306 | status = run_tests(reporter, suite_name, test_name, test_library_handle, |
| 307 | tests, verbose); |
| 308 | } |
| 309 | dlclose(test_library_handle); |
| 310 | } |
| 311 | free(absolute_library_name); |
| 312 | |
| 313 | destroy_cgreen_vector(tests); |
| 314 | |
| 315 | return status; |
| 316 | } |