Skip to content

Commit

Permalink
completely broken mingw code
Browse files Browse the repository at this point in the history
Clueless I am, and not having a machine to test this one does not help.
  • Loading branch information
pks-t committed Sep 7, 2024
1 parent c3406d5 commit 1473032
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ jobs:
strategy:
matrix:
platform:
- os: ubuntu-latest
generator: Unix Makefiles
- os: macos-latest
generator: Unix Makefiles
- os: windows-latest
generator: Visual Studio 17 2022
- os: windows-latest
generator: MSYS Makefiles
- os: windows-latest
generator: MinGW Makefiles

runs-on: ${{ matrix.platform.os }}

steps:
- name: Check out
uses: actions/checkout@v2
- name: Build and test
- name: Build
shell: bash
run: |
mkdir build
cd build
cmake .. -G "${{matrix.platform.generator}}"
cmake --build .
CTEST_OUTPUT_ON_FAILURE=1 ctest .
cmake --build . --verbose
- name: Test
shell: bash
run: |
cd build
CTEST_OUTPUT_ON_FAILURE=1 ctest
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ if (NOT MSVC)
)
endif()

set(SELFTEST_BINARY_PATH $<TARGET_FILE:selftest>)

target_sources(clar_test PRIVATE
main.c
clar.c
"${CMAKE_CURRENT_BINARY_DIR}/clar.suite"
)
target_compile_definitions(clar_test PRIVATE
CLAR_FIXTURE_PATH="${CMAKE_CURRENT_SOURCE_DIR}/expected/"
SELFTEST_BINARY_PATH="${CMAKE_CURRENT_BINARY_DIR}/selftest/selftest"
SELFTEST_BINARY_PATH="${SELFTEST_BINARY_PATH}"
_DARWIN_C_SOURCE
_POSIX_C_SOURCE=200809L
)
Expand Down
101 changes: 100 additions & 1 deletion test/clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#if defined(__MSYS__) || defined(__MINGW32__)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <sys/wait.h>
#endif

#include "clar.h"

Expand Down Expand Up @@ -59,6 +64,99 @@ static char *read_file(const char *path)

static void run(const char *expected_output_file, int expected_error_code, ...)
{
#ifdef __MINGW32__
SECURITY_ATTRIBUTES sec_attributes = { 0 };
PROCESS_INFORMATION process_info = { 0 };
STARTUPINFO startup_info = { 0 };
char cmdline[4096] = { 0 };
char *expected_output = NULL;
char *output = NULL;
size_t output_size = 0;
HANDLE stdout_write;
HANDLE stdout_read;
HANDLE stdin_write;
HANDLE stdin_read;
DWORD exit_code;
va_list ap;

/*
* Assemble command line arguments. In theory we'd have to properly
* quote them. In practice none of our tests actually care.
*/
va_start(ap, expected_error_code);
while (1) {
size_t cmdline_len = strlen(cmdline);
const char *arg;

arg = va_arg(ap, const char *);
if (!arg)
break;

cl_assert(cmdline_len + strlen(arg) < sizeof(cmdline));
snprintf(cmdline + cmdline_len, sizeof(cmdline) - cmdline_len,
" %s", arg);
}
va_end(ap);

/*
* Create a pipe that we will use to read data from the child process.
* The writing side needs to be inheritable such that the child can use
* it as stdout and stderr. The reading side should only be used by the
* parent.
*/
sec_attributes.nLength = sizeof(sec_attributes);
sec_attributes.bInheritHandle = TRUE;
cl_assert_equal_b(1, CreatePipe(&stdout_read, &stdout_write, &sec_attributes, 0));
cl_assert_equal_b(1, SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0));
cl_assert_equal_b(1, CreatePipe(&stdin_read, &stdin_write, &sec_attributes, 0));
cl_assert_equal_b(1, SetHandleInformation(stdin_write, HANDLE_FLAG_INHERIT, 0));

fprintf(stderr, "binary: %s cmdline: %s\n", SELFTEST_BINARY_PATH, cmdline);

/*
* Create the child process with our pipe.
*/
startup_info.cb = sizeof(startup_info);
startup_info.hStdError = stdout_write;
startup_info.hStdOutput = stdout_write;
startup_info.hStdInput = stdin_read;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
cl_assert_equal_b(1, CreateProcess(SELFTEST_BINARY_PATH, cmdline, NULL, NULL, TRUE,
0, NULL, NULL, &startup_info, &process_info));

cl_assert_equal_b(1, CloseHandle(stdout_write));
cl_assert_equal_b(1, CloseHandle(stdin_write));
cl_assert_equal_b(1, CloseHandle(stdin_read));

while (1) {
CHAR buf[4096];
DWORD bytes_read;

if (!ReadFile(stdout_read, buf, sizeof(buf), &bytes_read, NULL)) {
cl_assert_equal_i(GetLastError(), ERROR_HANDLE_EOF);
break;
}

output = realloc(output, output_size + bytes_read);
cl_assert(output);
memcpy(output + output_size, buf, bytes_read);
output_size += bytes_read;
}

output = realloc(output, output_size + 1);
cl_assert(output);
output[output_size] = '\0';

cl_assert_equal_b(1, CloseHandle(stdout_read));
cl_assert_equal_b(1, GetExitCodeProcess(process_info.hProcess, &exit_code));

expected_output = read_file(cl_fixture(expected_output_file));
cl_assert_equal_s(output, expected_output);
cl_assert_equal_i(exit_code, expected_error_code);

free(expected_output);
free(output);
#else
const char *argv[16];
int pipe_fds[2];
va_list ap;
Expand Down Expand Up @@ -111,6 +209,7 @@ static void run(const char *expected_output_file, int expected_error_code, ...)
} else {
cl_fail("Fork failed.");
}
#endif
}

void test_clar__help(void)
Expand Down

0 comments on commit 1473032

Please sign in to comment.