Skip to content

Commit

Permalink
main, win: create test runner in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
refack committed May 7, 2017
1 parent 22405a8 commit aa0e549
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/mini/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string.h>
#include <errno.h>

#define BUFSIZE 4096

#define CHECK(VALUE, MESSAGE) \
do { \
if ((VALUE)) break; \
Expand Down
70 changes: 68 additions & 2 deletions src/spawn-win.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <strsafe.h>

#include "include/mini/common.h"

static char executable_path[BUFSIZE];

int popen_complete(char* cmd)
{
STARTUPINFO StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);

PROCESS_INFORMATION ProcessInformation;
ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));

// BOOL WINAPI CreateProcess(
// _In_opt_ LPCTSTR lpApplicationName,
// _Inout_opt_ LPTSTR lpCommandLine,
// _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
// _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
// _In_ BOOL bInheritHandles,
// _In_ DWORD dwCreationFlags,
// _In_opt_ LPVOID lpEnvironment,
// _In_opt_ LPCTSTR lpCurrentDirectory,
// _In_ LPSTARTUPINFO lpStartupInfo,
// Out_ LPPROCESS_INFORMATION lpProcessInformation
// );
BOOL ret = CreateProcess(
NULL, // No module name (use command line)
cmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&StartupInfo, // Pointer to STARTUPINFO structure
&ProcessInformation // Pointer to PROCESS_INFORMATION structure
);
CHECK_EQ(ret, TRUE, "CreateProcess failed");

// Wait until child process exits.
DWORD wait_ret = WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
CHECK_EQ(wait_ret, WAIT_OBJECT_0, "Process ended strange");

// Get exit code (-1 is good)
DWORD lpExitCode = 0;
BOOL exit_ret = GetExitCodeProcess(ProcessInformation.hProcess, &lpExitCode);

// Close process and thread handles.
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);

return lpExitCode;
}


void mini_prepare_runner(const char* main) {
CHECK(0, "implement me");
TCHAR** lppPart = { NULL };
GetFullPathName(main, BUFSIZE, executable_path, lppPart);
CHECK_NE(executable_path, NULL, "realpath(argv[0])");
}


int mini_run_single(const char* test) {
CHECK(0, "implement me");
const char cmd[BUFSIZE];
int print_len = sprintf_s(cmd, BUFSIZE, "\"%s\" %s", executable_path, test);
CHECK_NE(print_len, -1, "sprintf_s to format cmd failed");
int err = popen_complete(cmd);
CHECK_NE(err, -1, "popen() failure");
return 0;
}

0 comments on commit aa0e549

Please sign in to comment.