| 1 | #include "discoverer.h" |
| 2 | |
| 3 | #include "io.h" |
| 4 | |
| 5 | #include <string.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include <cgreen/internal/unit_implementation.h> |
| 10 | |
| 11 | #include "../src/utils.h" |
| 12 | |
| 13 | |
| 14 | #include "test_item.h" |
| 15 | |
| 16 | static const char *cgreen_spec_start_of(const char *line) { |
| 17 | return strstr(line, CGREEN_SPEC_PREFIX CGREEN_SEPARATOR); |
| 18 | } |
| 19 | |
| 20 | static bool contains_cgreen_spec(const char *line) { |
| 21 | return cgreen_spec_start_of(line) != NULL; |
| 22 | } |
| 23 | |
| 24 | static bool is_definition(const char *line) { |
| 25 | return strstr(line, " D ") != NULL; |
| 26 | } |
| 27 | |
| 28 | static bool complete_line_read(char line[]) { |
| 29 | return line[strlen(line)-1] == '\n'; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | static void strip_newline_from(char *name) { |
| 34 | if (name[strlen(name)-1] == '\n') |
| 35 | name[strlen(name)-1] = '\0'; |
| 36 | } |
| 37 | |
| 38 | static void add_all_tests_from(FILE *nm_output_pipe, CgreenVector *tests, bool verbose) { |
| 39 | char line[1000]; |
| 40 | int length = read_line(nm_output_pipe, line, sizeof(line)-1); |
| 41 | while (length > -1) { /* TODO: >0 ? */ |
| 42 | if (!complete_line_read(line)) |
| 43 | PANIC("Too long line in nm output"); |
| 44 | if (contains_cgreen_spec(line) && is_definition(line)) { |
| 45 | strip_newline_from(line); |
| 46 | TestItem *test_item = create_test_item_from(cgreen_spec_start_of(line)); |
| 47 | if (verbose) |
| 48 | printf("Discovered %s:%s (%s)\n", test_item->context_name, test_item->test_name, |
| 49 | test_item->specification_name); |
| 50 | cgreen_vector_add(tests, test_item); |
| 51 | } |
| 52 | length = read_line(nm_output_pipe, line, sizeof(line)-1); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | CgreenVector *discover_tests_in(const char *filename, bool verbose) { |
| 57 | FILE *library = open_file(filename, "r"); |
| 58 | if (library == NULL) |
| 59 | return NULL; |
| 60 | close_file(library); |
| 61 | |
| 62 | char nm_command[1000]; |
| 63 | sprintf(nm_command, "%s '%s' 2>&1", NM_EXECUTABLE, filename); |
| 64 | FILE *nm_output_pipe = open_process(nm_command, "r"); |
| 65 | if (nm_output_pipe == NULL) |
| 66 | return NULL; |
| 67 | |
| 68 | CgreenVector *tests = create_cgreen_vector((GenericDestructor)&destroy_test_item); |
| 69 | add_all_tests_from(nm_output_pipe, tests, verbose); |
| 70 | close_process(nm_output_pipe); |
| 71 | |
| 72 | return tests; |
| 73 | } |