tests/environment: Run some subtests in a subprocess

This commit is contained in:
Luca Bacci
2025-05-22 11:02:53 +02:00
parent 92411ba25e
commit 51c71ea6d2

View File

@@ -159,45 +159,53 @@ test_getenv (void)
static void static void
test_setenv (void) test_setenv (void)
{ {
const gchar *var, *value; if (g_test_subprocess ())
{
const char *var, *value;
var = "NOSUCHENVVAR"; var = "NOSUCHENVVAR";
value = "value1"; value = "value1";
g_assert_null (g_getenv (var)); g_assert_null (g_getenv (var));
g_setenv (var, value, FALSE); g_setenv (var, value, FALSE);
g_assert_cmpstr (g_getenv (var), ==, value); g_assert_cmpstr (g_getenv (var), ==, value);
g_assert_true (g_setenv (var, "value2", FALSE)); g_assert_true (g_setenv (var, "value2", FALSE));
g_assert_cmpstr (g_getenv (var), ==, value); g_assert_cmpstr (g_getenv (var), ==, value);
g_assert_true (g_setenv (var, "value2", TRUE)); g_assert_true (g_setenv (var, "value2", TRUE));
g_assert_cmpstr (g_getenv (var), ==, "value2"); g_assert_cmpstr (g_getenv (var), ==, "value2");
g_unsetenv (var); g_unsetenv (var);
g_assert_null (g_getenv (var)); g_assert_null (g_getenv (var));
g_assert_true (g_setenv ("EMPTY_VAR", "", TRUE)); g_assert_true (g_setenv ("EMPTY_VAR", "", TRUE));
g_assert_cmpstr (g_getenv ("EMPTY_VAR"), ==, ""); g_assert_cmpstr (g_getenv ("EMPTY_VAR"), ==, "");
g_assert_false (g_setenv ("", "value", TRUE)); g_assert_false (g_setenv ("", "value", TRUE));
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
{ {
wchar_t invalid_utf16[] = { wchar_t invalid_utf16[] = {
L't', L'e', L's', L't', L't', L'e', L's', L't',
LOW_SURROGATE_START, LOW_SURROGATE_START,
HIGH_SURROGATE_START, HIGH_SURROGATE_START,
L'\0' L'\0'
}; };
if (!SetEnvironmentVariable (L"INVALID_UTF16_VAR", invalid_utf16)) if (!SetEnvironmentVariable (L"INVALID_UTF16_VAR", invalid_utf16))
{ {
g_error ("%s failed with error code %u", g_error ("%s failed with error code %u",
"SetEnvironmentVariable", (unsigned int) GetLastError ()); "SetEnvironmentVariable", (unsigned int) GetLastError ());
} }
} }
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*invalid UTF-16*"); g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*invalid UTF-16*");
g_assert_null (g_getenv ("INVALID_UTF16_VAR")); g_assert_null (g_getenv ("INVALID_UTF16_VAR"));
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
#endif #endif
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
g_test_trap_assert_passed ();
} }
static void static void
@@ -346,33 +354,41 @@ test_expansion (void)
g_test_summary ("Test expansion of environment variable references."); g_test_summary ("Test expansion of environment variable references.");
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
const struct if (g_test_subprocess ())
{ {
const wchar_t *name; const struct
const wchar_t *value;
} tests[] = {
{ L"EMPTY_VAR", L"" },
{ L"NON_EXISTING_VAR", NULL },
{ L"HELLO_VAR", L"HELLO" },
{ L"TO_EXPAND_VAR1", L"%HELLO_VAR% WORLD" },
{ L"TO_EXPAND_VAR2", L"%EMPTY_VAR%" },
{ L"TO_EXPAND_VAR3", L"%NON_EXISTING_VAR%" },
{ L"VAR1", L"%VAR2%" },
{ L"VAR2", L"%VAR1%" },
};
for (size_t i = 0; i < G_N_ELEMENTS (tests); i++)
if (!SetEnvironmentVariable (tests[i].name, tests[i].value))
{ {
g_error ("%s failed with error code %u", const wchar_t *name;
"SetEnvironmentVariable", (unsigned int) GetLastError ()); const wchar_t *value;
} } tests[] = {
{ L"EMPTY_VAR", L"" },
{ L"NON_EXISTING_VAR", NULL },
{ L"HELLO_VAR", L"HELLO" },
{ L"TO_EXPAND_VAR1", L"%HELLO_VAR% WORLD" },
{ L"TO_EXPAND_VAR2", L"%EMPTY_VAR%" },
{ L"TO_EXPAND_VAR3", L"%NON_EXISTING_VAR%" },
{ L"VAR1", L"%VAR2%" },
{ L"VAR2", L"%VAR1%" },
};
g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR1"), ==, "HELLO WORLD"); for (size_t i = 0; i < G_N_ELEMENTS (tests); i++)
g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR2"), ==, ""); if (!SetEnvironmentVariable (tests[i].name, tests[i].value))
g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR3"), ==, "%NON_EXISTING_VAR%"); {
g_error ("%s failed with error code %u",
"SetEnvironmentVariable", (unsigned int) GetLastError ());
}
g_assert_nonnull (g_getenv ("VAR1")); g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR1"), ==, "HELLO WORLD");
g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR2"), ==, "");
g_assert_cmpstr (g_getenv ("TO_EXPAND_VAR3"), ==, "%NON_EXISTING_VAR%");
g_assert_nonnull (g_getenv ("VAR1"));
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
g_test_trap_assert_passed ();
#else #else
g_test_skip ("Environment variable expansion is only supported on Windows"); g_test_skip ("Environment variable expansion is only supported on Windows");
#endif #endif