| 1 | #include <cgreen/breadcrumb.h> |
| 2 | #include <cgreen/reporter.h> |
| 3 | #include <errno.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <time.h> |
| 10 | |
| 11 | #ifdef __ANDROID__ |
| 12 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 13 | #endif // #ifdef __ANDROID__ |
| 14 | |
| 15 | #ifdef _MSC_VER |
| 16 | #include <wincompat.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <cgreen/cdash_reporter.h> |
| 20 | #include "cdash_reporter_internal.h" |
| 21 | |
| 22 | |
| 23 | typedef time_t Timer(char *strtime); |
| 24 | |
| 25 | typedef struct { |
| 26 | CDashInfo *info; |
| 27 | CDashPrinter *printer; |
| 28 | CDashVPrinter *vprinter; |
| 29 | FILE *stream; |
| 30 | time_t begin; |
| 31 | time_t startdatetime; |
| 32 | time_t enddatetime; |
| 33 | time_t teststarted; |
| 34 | time_t testfinished; |
| 35 | } CDashMemo; |
| 36 | |
| 37 | static void cdash_destroy_reporter(TestReporter *reporter); |
| 38 | static void cdash_reporter_start_suite(TestReporter *reporter, const char *name, const int number_of_tests); |
| 39 | static void cdash_reporter_start_test(TestReporter *reporter, const char *name); |
| 40 | |
| 41 | static void cdash_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments); |
| 42 | static void cdash_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments); |
| 43 | static void cdash_show_incomplete(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments); |
| 44 | |
| 45 | static void cdash_finish_test(TestReporter *reporter, const char *filename, int line, |
| 46 | const char *message); |
| 47 | static void cdash_finish_suite(TestReporter *reporter, const char *filename, int line); |
| 48 | |
| 49 | static time_t cdash_build_stamp(char *sbuildstamp, size_t sb); |
| 50 | static time_t cdash_current_time(char *strtime); |
| 51 | static double cdash_elapsed_time(time_t t1, time_t t2); |
| 52 | |
| 53 | |
| 54 | void set_cdash_reporter_printer(TestReporter *reporter, CDashPrinter *new_printer) { |
| 55 | CDashMemo *memo = (CDashMemo *)reporter->memo; |
| 56 | memo->printer = new_printer; |
| 57 | } |
| 58 | |
| 59 | void set_cdash_reporter_vprinter(TestReporter *reporter, CDashVPrinter *new_vprinter) { |
| 60 | CDashMemo *memo = (CDashMemo *)reporter->memo; |
| 61 | memo->vprinter = new_vprinter; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | TestReporter *create_cdash_reporter(CDashInfo *info) { |
| 66 | TestReporter *reporter; |
| 67 | CDashMemo *memo; |
| 68 | FILE *fd; |
| 69 | char sbuildstamp[15]; |
| 70 | char strstart[30]; |
| 71 | char reporter_path[255]; |
| 72 | int rep_dir, strsize; |
| 73 | |
| 74 | |
| 75 | if (!info) |
| 76 | return NULL; |
| 77 | |
| 78 | reporter = create_reporter(); |
| 79 | if (!reporter) |
| 80 | return NULL; |
| 81 | |
| 82 | memo = (CDashMemo *) calloc(1, sizeof(CDashMemo)); |
| 83 | if (!memo) |
| 84 | return NULL; |
| 85 | memo->info = info; |
| 86 | memo->begin = cdash_build_stamp(sbuildstamp, 15); |
| 87 | reporter->memo = memo; |
| 88 | |
| 89 | set_cdash_reporter_printer(reporter, fprintf); |
| 90 | set_cdash_reporter_vprinter(reporter, vfprintf); |
| 91 | |
| 92 | rep_dir = mkdir("./Testing", S_IXUSR|S_IRUSR|S_IWUSR|S_IXGRP|S_IRGRP|S_IXOTH); |
| 93 | |
| 94 | if (rep_dir) { |
| 95 | if (errno != EEXIST) { |
| 96 | free(memo); |
| 97 | return NULL; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fd = fopen("./Testing/TAG", "w+"); |
| 102 | if (fd == NULL) { |
| 103 | free(memo); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | fprintf(fd, "%s\n%s\n", sbuildstamp, memo->info->type); |
| 108 | |
| 109 | fclose(fd); |
| 110 | |
| 111 | strsize = snprintf(reporter_path, sizeof(reporter_path) - 1, "./Testing/%s", sbuildstamp); |
| 112 | |
| 113 | rep_dir = mkdir(reporter_path, S_IXUSR|S_IRUSR|S_IWUSR|S_IXGRP|S_IRGRP|S_IXOTH); |
| 114 | |
| 115 | if (rep_dir) { |
| 116 | if (errno != EEXIST) { |
| 117 | free(memo); |
| 118 | return NULL; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | snprintf( (char *) (reporter_path + strsize), (255 - strsize), "/Test.xml"); |
| 123 | |
| 124 | fd = fopen(reporter_path, "w+"); |
| 125 | if (fd == NULL) { |
| 126 | free(memo); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /* now the Test.xml is in place */ |
| 131 | memo->stream = fd; |
| 132 | |
| 133 | memo->startdatetime = cdash_current_time(strstart); |
| 134 | |
| 135 | memo->printer(memo->stream, |
| 136 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 137 | " <Site BuildName=\"%s\" BuildStamp=\"%s-%s\" Name=\"%s\" Generator=\"%s\"\n" |
| 138 | " OSName=\"%s\" Hostname=\"%s\" OSRelease=\"%s\"\n" |
| 139 | " OSVersion=\"%s\" OSPlatform=\"%s\"\n" |
| 140 | " Is64Bits=\"\" VendorString=\"\" VendorID=\"\"\n" |
| 141 | " FamilyID=\"\" ModelID=\"\" ProcessorCacheSize=\"\" NumberOfLogicalCPU=\"\"\n" |
| 142 | " NumberOfPhysicalCPU=\"\" TotalVirtualMemory=\"\" TotalPhysicalMemory=\"\"\n" |
| 143 | " LogicalProcessorsPerPhysical=\"\" ProcessorClockFrequency=\"\" >\n" |
| 144 | " <Testing>\n" |
| 145 | " <StartDateTime>%s</StartDateTime>\n" |
| 146 | " <TestList>\n" |
| 147 | " <Test></Test>\n" |
| 148 | " </TestList>\n", |
| 149 | memo->info->build, sbuildstamp, memo->info->type, memo->info->name, "Cgreen" VERSION, |
| 150 | memo->info->os_name, memo->info->hostname, memo->info->os_release, |
| 151 | memo->info->os_version, memo->info->os_platform, strstart); |
| 152 | fflush(memo->stream); |
| 153 | |
| 154 | reporter->destroy = &cdash_destroy_reporter; |
| 155 | reporter->start_suite = &cdash_reporter_start_suite; |
| 156 | reporter->start_test = &cdash_reporter_start_test; |
| 157 | reporter->show_fail = &cdash_show_fail; |
| 158 | reporter->show_pass = &cdash_show_pass; |
| 159 | reporter->show_incomplete = &cdash_show_incomplete; |
| 160 | reporter->finish_test = &cdash_finish_test; |
| 161 | reporter->finish_suite = &cdash_finish_suite; |
| 162 | reporter->memo = memo; |
| 163 | |
| 164 | return reporter; |
| 165 | } |
| 166 | |
| 167 | static void cdash_destroy_reporter(TestReporter *reporter) { |
| 168 | char endtime[30]; |
| 169 | |
| 170 | CDashMemo *memo = (CDashMemo *)reporter->memo; |
| 171 | |
| 172 | memo->enddatetime = cdash_current_time(endtime); |
| 173 | |
| 174 | memo->printer(memo->stream, " <EndDateTime>%s</EndDateTime>\n" |
| 175 | " <ElapsedMinutes>%.2f</ElapsedMinutes>\n" |
| 176 | " </Testing>\n" |
| 177 | "</Site>\n", endtime, cdash_elapsed_time(memo->startdatetime, memo->enddatetime)); |
| 178 | |
| 179 | destroy_reporter(reporter); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static void cdash_reporter_start_suite(TestReporter *reporter, const char *name, const int number_of_tests) { |
| 184 | (void)number_of_tests; |
| 185 | |
| 186 | reporter->passes = 0; |
| 187 | reporter->failures = 0; |
| 188 | reporter->skips = 0; |
| 189 | reporter->exceptions = 0; |
| 190 | |
| 191 | reporter_start_test(reporter, name); |
| 192 | } |
| 193 | |
| 194 | static void cdash_reporter_start_test(TestReporter *reporter, const char *name) { |
| 195 | CDashMemo *memo = (CDashMemo *)reporter->memo; |
| 196 | |
| 197 | memo->teststarted = cdash_current_time(NULL); |
| 198 | reporter_start_test(reporter, name); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static void print_test_header(CDashMemo *memo, const char* status, const char *name, const char* file, int line) { |
| 203 | memo->printer(memo->stream, |
| 204 | " <Test Status=\"%s\">\n" |
| 205 | " <Name>%s</Name>\n" |
| 206 | " <Path>%s</Path>\n" |
| 207 | " <FullName>%s</FullName>\n" |
| 208 | " <FullCommandLine>at [%s] line [%d]</FullCommandLine>\n", |
| 209 | status, name, file, file, file, line); |
| 210 | } |
| 211 | |
| 212 | static void print_results_header(CDashMemo *memo, const char *name, float exectime) { |
| 213 | memo->printer(memo->stream, |
| 214 | " <Results>\n" |
| 215 | " <NamedMeasurement type=\"numeric/double\" name=\"Execution Time\">\n" |
| 216 | " <Value>%f</Value>\n" |
| 217 | " </NamedMeasurement>\n" |
| 218 | " <NamedMeasurement type=\"text/string\" name=\"Completion Status\">\n" |
| 219 | " <Value>Completed</Value>\n" |
| 220 | " </NamedMeasurement>\n" |
| 221 | " <NamedMeasurement type=\"text/string\" name=\"Command Line\">\n" |
| 222 | " <Value>%s</Value>\n" |
| 223 | " </NamedMeasurement>\n", |
| 224 | exectime, name); |
| 225 | } |
| 226 | |
| 227 | static void print_measurement(CDashMemo *memo, const char* message, va_list arguments) { |
| 228 | memo->printer(memo->stream, |
| 229 | " <Measurement>\n" |
| 230 | " <Value>"); |
| 231 | if (message == NULL) { |
| 232 | memo->printer(memo->stream, |
| 233 | "Problem"); |
| 234 | } else { |
| 235 | memo->vprinter(memo->stream, |
| 236 | message, arguments); |
| 237 | } |
| 238 | memo->printer(memo->stream, |
| 239 | "</Value>\n" |
| 240 | " </Measurement>\n"); |
| 241 | } |
| 242 | |
| 243 | static void print_tail(CDashMemo *memo) { |
| 244 | memo->printer(memo->stream, |
| 245 | " </Results>\n" |
| 246 | " </Test>\n"); |
| 247 | } |
| 248 | |
| 249 | static void cdash_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { |
| 250 | const char *name; |
| 251 | float exectime; |
| 252 | CDashMemo *memo; |
| 253 | |
| 254 | memo = (CDashMemo *)reporter->memo; |
| 255 | |
| 256 | memo->testfinished = cdash_current_time(NULL); |
| 257 | |
| 258 | exectime = (float)cdash_elapsed_time(memo->teststarted, memo->testfinished); |
| 259 | |
| 260 | name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 261 | |
| 262 | print_test_header(memo, "failed", name, file, line); |
| 263 | print_results_header(memo, name, exectime); |
| 264 | print_measurement(memo, message, arguments); |
| 265 | print_tail(memo); |
| 266 | } |
| 267 | |
| 268 | static void cdash_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { |
| 269 | double exectime; |
| 270 | CDashMemo *memo; |
| 271 | const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 272 | (void)message; |
| 273 | memo = (CDashMemo *)reporter->memo; |
| 274 | |
| 275 | memo->testfinished = cdash_current_time(NULL); |
| 276 | exectime = cdash_elapsed_time(memo->teststarted, memo->testfinished); |
| 277 | |
| 278 | print_test_header(memo, "passed", name, file, line); |
| 279 | print_results_header(memo, name, exectime); |
| 280 | print_measurement(memo, "", arguments); |
| 281 | print_tail(memo); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | static void cdash_show_incomplete(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { |
| 287 | const char *name; |
| 288 | float exectime; |
| 289 | CDashMemo *memo; |
| 290 | |
| 291 | memo = (CDashMemo *)reporter->memo; |
| 292 | |
| 293 | memo->testfinished = cdash_current_time(NULL); |
| 294 | |
| 295 | exectime = (float)cdash_elapsed_time(memo->teststarted, memo->testfinished); |
| 296 | |
| 297 | name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 298 | |
| 299 | print_test_header(memo, "incomplete", name, file, line); |
| 300 | print_results_header(memo, name, exectime); |
| 301 | print_measurement(memo, message, arguments); |
| 302 | print_tail(memo); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | static void cdash_finish_test(TestReporter *reporter, const char *filename, int line, |
| 307 | const char *message) { |
| 308 | reporter_finish_test(reporter, filename, line, message); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | static void cdash_finish_suite(TestReporter *reporter, const char *filename, int line) { |
| 313 | reporter_finish_test(reporter, filename, line, NULL); |
| 314 | |
| 315 | reporter->total_passes += reporter->passes; |
| 316 | reporter->total_failures += reporter->failures; |
| 317 | reporter->total_skips += reporter->skips; |
| 318 | reporter->total_exceptions += reporter->exceptions; |
| 319 | } |
| 320 | |
| 321 | static time_t cdash_build_stamp(char *sbuildstamp, size_t sb) { |
| 322 | time_t t1; |
| 323 | struct tm d1; |
| 324 | char s[15]; |
| 325 | |
| 326 | t1 = time(0); |
| 327 | gmtime_r(&t1, &d1); |
| 328 | |
| 329 | strftime(s, sizeof(s), "%Y%m%d-%H%M", &d1); |
| 330 | snprintf(sbuildstamp, sb, "%s", s); |
| 331 | |
| 332 | return t1; |
| 333 | } |
| 334 | |
| 335 | static time_t cdash_current_time(char *strtime) { |
| 336 | time_t t1; |
| 337 | struct tm d1; |
| 338 | char s[20]; |
| 339 | size_t i; |
| 340 | |
| 341 | t1 = time(0); |
| 342 | gmtime_r(&t1, &d1); |
| 343 | |
| 344 | if(strtime == NULL) |
| 345 | return t1; |
| 346 | |
| 347 | i = strftime(s, 20, "%b %d %H:%M EDT", &d1); |
| 348 | |
| 349 | strncpy(strtime, s, i+1); |
| 350 | |
| 351 | return t1; |
| 352 | } |
| 353 | |
| 354 | static double cdash_elapsed_time(time_t t1, time_t t2) { |
| 355 | double diff; |
| 356 | |
| 357 | diff = difftime(t2, t1); |
| 358 | return (diff == 0 ? 0 : (diff / 60)); |
| 359 | } |