From 8f76bb66807f692f90e7e27c568d3b0266a35ac4 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Mon, 26 Aug 2024 09:09:27 +0200 Subject: [PATCH] Clean up test code --- tests/native_binary/assertenv.cc | 61 +++++++++++++------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/tests/native_binary/assertenv.cc b/tests/native_binary/assertenv.cc index d4d7cf6..0dd51f3 100644 --- a/tests/native_binary/assertenv.cc +++ b/tests/native_binary/assertenv.cc @@ -1,44 +1,31 @@ #include +#include #include +bool check(const char *var, const char *expected = nullptr); + +int main() { + bool ok = true; + ok = ok && check("TEST_ENV_VAR", "test_env_var_value"); #ifdef _WIN32 -#define OS_VAR "APPDATA" -#define strncasecmp _strnicmp + ok = ok && check("APPDATA"); #else -#define OS_VAR "HOME" + ok = ok && check("HOME"); #endif - -enum vars_to_be_found { - test_env_var = 0, - inherited, - test_env_var_with_expansion, - last, -}; - -int main(int argc, char **argv, char **envp) { - const char* expected[last] = { - "TEST_ENV_VAR=test_env_var_value", - OS_VAR "=", - "TEST_ENV_VAR_WITH_EXPANSION=|tests/native_binary/BUILD|", - }; - for (char **env = envp; *env != NULL; ++env) { - printf("%s\n", *env); - if (expected[test_env_var] && strcmp(*env, expected[test_env_var]) == 0) { - expected[test_env_var] = nullptr; - } - if (expected[inherited] && strncasecmp(*env, expected[inherited], strlen(expected[inherited])) == 0) { - expected[inherited] = nullptr; - } - if (expected[test_env_var_with_expansion] && strcmp(*env, expected[test_env_var_with_expansion]) == 0) { - expected[test_env_var_with_expansion] = nullptr; - } - } - auto return_status = 0; - for (auto still_expected : expected) { - if (still_expected) { - fprintf(stderr, "expected %s\n", still_expected); - return_status = 1; - } - } - return return_status; + ok = + ok && check("TEST_ENV_VAR_WITH_EXPANSION", "|tests/native_binary/BUILD|"); + return ok ? 0 : 1; +} + +bool check(const char *var, const char *expected) { + const char *actual = getenv(var); + if (actual == nullptr) { + fprintf(stderr, "expected %s\n", var); + return false; + } + if (expected && strcmp(actual, expected) != 0) { + fprintf(stderr, "expected %s=%s, got %s\n", var, expected, actual); + return false; + } + return true; }