Clean up test code

This commit is contained in:
Paolo Tranquilli 2024-08-26 09:09:27 +02:00
parent bcbc4fd8a3
commit 8f76bb6680
1 changed files with 24 additions and 37 deletions

View File

@ -1,44 +1,31 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
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 #ifdef _WIN32
#define OS_VAR "APPDATA" ok = ok && check("APPDATA");
#define strncasecmp _strnicmp
#else #else
#define OS_VAR "HOME" ok = ok && check("HOME");
#endif #endif
ok =
enum vars_to_be_found { ok && check("TEST_ENV_VAR_WITH_EXPANSION", "|tests/native_binary/BUILD|");
test_env_var = 0, return ok ? 0 : 1;
inherited, }
test_env_var_with_expansion,
last, bool check(const char *var, const char *expected) {
}; const char *actual = getenv(var);
if (actual == nullptr) {
int main(int argc, char **argv, char **envp) { fprintf(stderr, "expected %s\n", var);
const char* expected[last] = { return false;
"TEST_ENV_VAR=test_env_var_value", }
OS_VAR "=", if (expected && strcmp(actual, expected) != 0) {
"TEST_ENV_VAR_WITH_EXPANSION=|tests/native_binary/BUILD|", fprintf(stderr, "expected %s=%s, got %s\n", var, expected, actual);
}; return false;
for (char **env = envp; *env != NULL; ++env) { }
printf("%s\n", *env); return true;
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;
} }