| 1 |
#ifdef WIN32 |
| 2 |
|
| 3 |
#include "config.h" |
| 4 |
#include "runner.h" |
| 5 |
#include "cgreen/internal/runner_platform.h" |
| 6 |
#include "cgreen/messaging.h" |
| 7 |
#include "wincompat.h" |
| 8 |
#include "cgreen/internal/cgreen_time.h" |
| 9 |
|
| 10 |
#include "win32_cgreen.h" |
| 11 |
|
| 12 |
#include "Strsafe.h" |
| 13 |
#define SECOND (1000) |
| 14 |
static void run_named_test_child(TestSuite *suite, const char *name, TestReporter *reporter); |
| 15 |
|
| 16 |
|
| 17 |
static void CALLBACK stop(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 18 |
{ |
| 19 |
#ifdef CGREEN_INTERNAL_WITH_GCOV |
| 20 |
if (1) |
| 21 |
#else |
| 22 |
if (NULL != getenv("CGREEN_CHILD_EXIT_WITH_FLUSH")) |
| 23 |
#endif |
| 24 |
exit(EXIT_SUCCESS); |
| 25 |
else |
| 26 |
_exit(EXIT_SUCCESS); |
| 27 |
} |
| 28 |
|
| 29 |
void die_in(unsigned int seconds) { |
| 30 |
MMRESULT signal_result = timeSetEvent(seconds*SECOND,SECOND,&stop,0,TIME_ONESHOT); |
| 31 |
if (0 == signal_result){ |
| 32 |
fprintf(stderr, "could not set alarm signal handler\n"); |
| 33 |
return; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void run_test_in_the_current_process_child(TestSuite *suite, CgreenTest *test, TestReporter *reporter) { |
| 38 |
(*reporter->start_test)(reporter, test->name); |
| 39 |
run_the_test_code(suite, test, reporter); |
| 40 |
send_reporter_completion_notification(reporter); |
| 41 |
stop(0, 0, 0, 0, 0); |
| 42 |
} |
| 43 |
|
| 44 |
static void run_named_test_child(TestSuite *suite, const char *name, TestReporter *reporter) { |
| 45 |
int i; |
| 46 |
|
| 47 |
for (i = 0; i < suite->size; i++) { |
| 48 |
if (suite->tests[i].type == test_function) { |
| 49 |
if (strcmp(suite->tests[i].name, name) == 0) { |
| 50 |
run_test_in_the_current_process_child(suite, suite->tests[i].Runnable.test, reporter); |
| 51 |
} |
| 52 |
} else if (has_test(suite->tests[i].Runnable.suite, name)) { |
| 53 |
TestSuite* newSuite=suite->tests[i].Runnable.suite; |
| 54 |
(*suite->setup)(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
(*reporter->start_suite)(reporter, newSuite->name, count_tests(newSuite)); |
| 59 |
|
| 60 |
uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds(); |
| 61 |
|
| 62 |
run_named_test_child(newSuite, name, reporter); |
| 63 |
|
| 64 |
reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds, |
| 65 |
cgreen_time_get_current_milliseconds()); |
| 66 |
|
| 67 |
(*reporter->finish_suite)(reporter, newSuite->filename, newSuite->line, test_duration); |
| 68 |
(*suite->teardown)(); |
| 69 |
} |
| 70 |
} |
| 71 |
send_reporter_completion_notification(reporter); |
| 72 |
} |
| 73 |
|
| 74 |
void run_specified_test_if_child(TestSuite *suite, TestReporter *reporter){ |
| 75 |
|
| 76 |
char testName[256]; |
| 77 |
DWORD result = GetEnvironmentVariableA(CGREEN_TEST_TO_RUN,testName,sizeof(testName)); |
| 78 |
|
| 79 |
if (result) |
| 80 |
{ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
reporter_start_test(reporter, testName); |
| 86 |
|
| 87 |
uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds(); |
| 88 |
|
| 89 |
run_named_test_child(suite, testName, reporter); |
| 90 |
|
| 91 |
reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds, |
| 92 |
cgreen_time_get_current_milliseconds()); |
| 93 |
|
| 94 |
reporter_finish_test(reporter, suite->filename, suite->line, NULL, test_duration); |
| 95 |
|
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
struct environment |
| 102 |
{ |
| 103 |
char env[MAX_PATH]; |
| 104 |
char *p_head; |
| 105 |
}; |
| 106 |
|
| 107 |
struct environment* create_environment() |
| 108 |
{ |
| 109 |
struct environment* p = (struct environment*)malloc(sizeof(struct environment)); |
| 110 |
memset(p->env, 0, sizeof(p->env)); |
| 111 |
p->p_head = p->env; |
| 112 |
return p; |
| 113 |
} |
| 114 |
|
| 115 |
static void AddEnvironmentVariable(struct environment* env,const char* varName, const char* valueString) |
| 116 |
{ |
| 117 |
size_t len; |
| 118 |
size_t envSize = MAX_PATH - (env->p_head-env->env); |
| 119 |
StringCbCopyA(env->p_head, envSize, varName); |
| 120 |
StringCbCatA(env->p_head, envSize, "="); |
| 121 |
StringCbCatA(env->p_head, envSize, valueString); |
| 122 |
StringCbCatA(env->p_head, envSize, "\0"); |
| 123 |
len = strnlen_s(env->p_head, envSize); |
| 124 |
env->p_head += (len + 1); |
| 125 |
} |
| 126 |
|
| 127 |
static const char* get_environment_block(const struct environment* env) |
| 128 |
{ |
| 129 |
return env->env; |
| 130 |
} |
| 131 |
|
| 132 |
static void dispose_environment(struct environment* env) |
| 133 |
{ |
| 134 |
free(env); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void run_test_in_its_own_process(TestSuite *suite, CgreenTest *test, TestReporter *reporter) { |
| 139 |
#define COMMAND_LINE_LENGTH (2*MAX_PATH) |
| 140 |
BOOL success; |
| 141 |
char fname[MAX_PATH+1]; |
| 142 |
struct environment* p_environment = create_environment(); |
| 143 |
char handleString[256]; |
| 144 |
|
| 145 |
STARTUPINFOA siStartupInfo; |
| 146 |
PROCESS_INFORMATION piProcessInfo; |
| 147 |
|
| 148 |
|
| 149 |
GetModuleFileNameA(NULL, fname,MAX_PATH); |
| 150 |
|
| 151 |
|
| 152 |
memset(&siStartupInfo, 0, sizeof(siStartupInfo)); |
| 153 |
memset(&piProcessInfo, 0, sizeof(piProcessInfo)); |
| 154 |
siStartupInfo.cb = sizeof(siStartupInfo); |
| 155 |
|
| 156 |
sprintf_s(handleString,sizeof(handleString), "%i", get_pipe_read_handle()); |
| 157 |
AddEnvironmentVariable(p_environment, CGREEN_READ_HANDLE, handleString); |
| 158 |
|
| 159 |
sprintf_s(handleString,sizeof(handleString), "%i", get_pipe_write_handle()); |
| 160 |
AddEnvironmentVariable(p_environment, CGREEN_WRITE_HANDLE, handleString); |
| 161 |
|
| 162 |
|
| 163 |
AddEnvironmentVariable(p_environment, CGREEN_TEST_TO_RUN, test->name); |
| 164 |
|
| 165 |
(*reporter->start_test)(reporter, test->name); |
| 166 |
|
| 167 |
uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds(); |
| 168 |
|
| 169 |
|
| 170 |
success = CreateProcessA(fname, NULL, NULL, NULL, true, NORMAL_PRIORITY_CLASS , p_environment, NULL, &siStartupInfo, &piProcessInfo); |
| 171 |
dispose_environment(p_environment); |
| 172 |
WaitForSingleObject(piProcessInfo.hProcess,INFINITE); |
| 173 |
|
| 174 |
reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds, |
| 175 |
cgreen_time_get_current_milliseconds()); |
| 176 |
|
| 177 |
(*reporter->finish_test)(reporter, test->filename, test->line, NULL, test_duration); |
| 178 |
|
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
#endif |
| 183 |
|