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 15, 2024
1 parent 72eb5dc commit 5af657b
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 200 deletions.
16 changes: 13 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,19 @@ 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> --child-with-missing-libs $<TARGET_FILE:childprocess_missing_libs> SOURCES testprocess.c)
add_sdl_test_executable(childprocess SOURCES childprocess.c)
add_library(childlibrary SHARED childlibrary.c)
set_property(TARGET childlibrary PROPERTY DEFINE_SYMBOL "DLL_EXPORT")
set_property(TARGET childlibrary PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/subdir")
set_property(TARGET childlibrary PROPERTY LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/subdir")
target_link_libraries(childlibrary PUBLIC SDL3::Headers)
add_sdl_test_executable(childprocess_missing_libs SOURCES childprocess.c)
set_property(TARGET childprocess_missing_libs PROPERTY SKIP_BUILD_RPATH "1")
target_compile_definitions(childprocess_missing_libs PRIVATE WITH_CHILDLIBRARY)
target_link_libraries(childprocess_missing_libs PRIVATE childlibrary)
add_dependencies(testprocess childprocess childprocess_missing_libs)

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
6 changes: 6 additions & 0 deletions test/childlibrary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "childlibrary.h"

SDL_bool ChildLibrary_GetSecret(int *secret)
{
*secret = 42;
}
8 changes: 8 additions & 0 deletions test/childlibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CHILDLIBRARY_H
#define CHILDLIBRARY_H

#include <SDL3/SDL.h>

extern SDL_DECLSPEC SDL_bool SDLCALL ChildLibrary_GetSecret(int *secret);

#endif /* CHILDLIBRARY_H */
109 changes: 59 additions & 50 deletions test/childprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,87 @@
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>

#ifdef WITH_CHILDLIBRARY
#include "childlibrary.h"
#endif

#include <stdio.h>
#include <errno.h>


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], "--") == 0) {
i++;
break;
#ifdef WITH_CHILDLIBRARY
} else if (SDL_strcmp(argv[i], "--test-secret") == 0) {
if (i + 1 < argc) {
int arg_secret;
char *endptr = NULL;
arg_secret = SDL_strtol(argv[i + 1], &endptr, 0);
if (endptr && *endptr == '\0') {
int secret;

consumed = 2;
ChildLibrary_GetSecret(&secret);
return arg_secret == secret ? 0 : 1;
}
}
#endif
}
} 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,23 +104,17 @@ 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) {

if (stdin_to_stdout || stdin_to_stderr || read_stdin) {
for (;;) {
int c;
c = fgetc(stdin);
Expand All @@ -126,8 +138,5 @@ int main(int argc, char *argv[]) {

SDLTest_CommonDestroyState(state);

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

0 comments on commit 5af657b

Please sign in to comment.