Skip to content

Commit

Permalink
Add more SDL_Process tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Sep 16, 2024
1 parent 5fc5793 commit f381d5f
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 204 deletions.
7 changes: 4 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,6 @@ add_sdl_test_executable(testmouse SOURCES testmouse.c)
add_sdl_test_executable(testoverlay NEEDS_RESOURCES TESTUTILS SOURCES testoverlay.c)
add_sdl_test_executable(testplatform NONINTERACTIVE SOURCES testplatform.c)
add_sdl_test_executable(testpower NONINTERACTIVE SOURCES testpower.c)
add_sdl_test_executable(testprocess NONINTERACTIVE THREADS NONINTERACTIVE_ARGS $<TARGET_FILE:childprocess> SOURCES testprocess.c)
add_sdl_test_executable(childprocess SOURCES childprocess.c)
add_dependencies(testprocess childprocess)
add_sdl_test_executable(testfilesystem NONINTERACTIVE SOURCES testfilesystem.c)
if(WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
add_sdl_test_executable(pretest SOURCES pretest.c NONINTERACTIVE NONINTERACTIVE_TIMEOUT 60)
Expand Down Expand Up @@ -405,6 +402,10 @@ add_sdl_test_executable(testtime SOURCES testtime.c)
add_sdl_test_executable(testmanymouse SOURCES testmanymouse.c)
add_sdl_test_executable(testmodal SOURCES testmodal.c)

add_sdl_test_executable(testprocess NONINTERACTIVE THREADS NONINTERACTIVE_ARGS $<TARGET_FILE:childprocess> SOURCES testprocess.c)
add_sdl_test_executable(childprocess SOURCES childprocess.c)
add_dependencies(testprocess childprocess)

if (HAVE_WAYLAND)
# Set the GENERATED property on the protocol file, since it is first created at build time
set_property(SOURCE ${SDL3_BINARY_DIR}/wayland-generated-protocols/xdg-shell-protocol.c PROPERTY GENERATED 1)
Expand Down
131 changes: 76 additions & 55 deletions test/childprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,83 @@
#include <stdio.h>
#include <errno.h>

#ifdef SDL_PLATFORM_WINDOWS
#include <windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif

int main(int argc, char *argv[]) {
SDLTest_CommonState *state;
int i;
const char *expect_environment = NULL;
SDL_bool expect_environment_match = SDL_FALSE;
SDL_bool print_arguments = SDL_FALSE;
SDL_bool print_environment = SDL_FALSE;
SDL_bool stdin_to_stdout = SDL_FALSE;
SDL_bool read_stdin = SDL_FALSE;
SDL_bool stdin_to_stderr = SDL_FALSE;
int exit_code = 0;

state = SDLTest_CommonCreateState(argv, 0);

for (i = 1; i < argc;) {
int consumed = SDLTest_CommonArg(state, i);
if (SDL_strcmp(argv[i], "--print-arguments") == 0) {
print_arguments = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--print-environment") == 0) {
print_environment = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--expect-env") == 0) {
if (i + 1 < argc) {
expect_environment = argv[i + 1];
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--stdin-to-stdout") == 0) {
stdin_to_stdout = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdin-to-stderr") == 0) {
stdin_to_stderr = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdout") == 0) {
if (i + 1 < argc) {
fprintf(stdout, "%s", argv[i + 1]);
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--stderr") == 0) {
if (i + 1 < argc) {
fprintf(stderr, "%s", argv[i + 1]);
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--exit-code") == 0) {
if (i + 1 < argc) {
char *endptr = NULL;
exit_code = SDL_strtol(argv[i + 1], &endptr, 0);
if (endptr && *endptr == '\0') {
if (!consumed) {
if (SDL_strcmp(argv[i], "--print-arguments") == 0) {
print_arguments = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--print-environment") == 0) {
print_environment = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdin-to-stdout") == 0) {
stdin_to_stdout = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdin-to-stderr") == 0) {
stdin_to_stderr = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdin") == 0) {
read_stdin = SDL_TRUE;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--stdout") == 0) {
if (i + 1 < argc) {
fprintf(stdout, "%s", argv[i + 1]);
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--stderr") == 0) {
if (i + 1 < argc) {
fprintf(stderr, "%s", argv[i + 1]);
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--exit-code") == 0) {
if (i + 1 < argc) {
char *endptr = NULL;
exit_code = SDL_strtol(argv[i + 1], &endptr, 0);
if (endptr && *endptr == '\0') {
consumed = 2;
}
}
} else if (SDL_strcmp(argv[i], "--version") == 0) {
int version = SDL_GetVersion();
fprintf(stdout, "SDL version %d.%d.%d",
SDL_VERSIONNUM_MAJOR(version),
SDL_VERSIONNUM_MINOR(version),
SDL_VERSIONNUM_MICRO(version));
fprintf(stderr, "SDL version %d.%d.%d",
SDL_VERSIONNUM_MAJOR(version),
SDL_VERSIONNUM_MINOR(version),
SDL_VERSIONNUM_MICRO(version));
consumed = 1;
break;
} else if (SDL_strcmp(argv[i], "--") == 0) {
i++;
break;
}
} else if (SDL_strcmp(argv[i], "--") == 0) {
i++;
break;
}
if (consumed <= 0) {
const char *args[] = {
"[--print-arguments]",
"[--print-environment]",
"[--expect-env KEY=VAL]",
"[--stdin]",
"[--stdin-to-stdout]",
"[--stdout TEXT]",
"[--stdin-to-stderr]",
Expand All @@ -86,48 +103,52 @@ int main(int argc, char *argv[]) {
}
}

if (print_environment || expect_environment) {
if (print_environment) {
char **env = SDL_GetEnvironmentVariables(SDL_GetEnvironment());
if (env) {
for (i = 0; env[i]; ++i) {
if (print_environment) {
fprintf(stdout, "%s\n", env[i]);
}
if (expect_environment) {
expect_environment_match |= SDL_strcmp(env[i], expect_environment) == 0;
}
fprintf(stdout, "%s\n", env[i]);
}
SDL_free(env);
}
}

if (stdin_to_stdout || stdin_to_stderr) {
#ifdef SDL_PLATFORM_WINDOWS
{
DWORD mode;
HANDLE stdout_handle = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(stdout_handle, &mode);
SetConsoleMode(stdout_handle, mode & ~(ENABLE_LINE_INPUT));
}
#else
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) & ~(O_NONBLOCK));
#endif

if (stdin_to_stdout || stdin_to_stderr || read_stdin) {
for (;;) {
int c;
c = fgetc(stdin);
if (c == EOF) {
char buffer[4 * 4096];
unsigned long result;

result = fread(buffer, 1, sizeof(buffer), stdin);
if (result == 0) {
if (errno == EAGAIN) {
clearerr(stdin);
SDL_Delay(10);
SDL_Delay(20);
continue;
}
break;
}
if (stdin_to_stdout) {
fputc(c, stdout);
fwrite(buffer, 1, result, stdout);
fflush(stdout);
}
if (stdin_to_stderr) {
fputc(c, stderr);
fwrite(buffer, 1, result, stderr);
}
}
}

SDLTest_CommonDestroyState(state);

if (expect_environment && !expect_environment_match) {
exit_code |= 0x1;
}
return exit_code;
}
Loading

0 comments on commit f381d5f

Please sign in to comment.