diff options
Diffstat (limited to 'tests/test_environment.c')
-rw-r--r-- | tests/test_environment.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/test_environment.c b/tests/test_environment.c new file mode 100644 index 0000000..3418800 --- /dev/null +++ b/tests/test_environment.c @@ -0,0 +1,76 @@ +#include "testing.h" + +extern char **environ; + +void test_runtime_copy() { + RuntimeEnv *env = runtime_copy(environ); + STASIS_ASSERT(env->data != environ, "copied array is not unique"); + int difference = 0; + for (size_t i = 0; i < strlist_count(env); i++) { + char *item = strlist_item(env, i); + if (!strstr_array(environ, item)) { + difference++; + } + } + STASIS_ASSERT(difference == 0, "copied environment is incomplete"); + runtime_free(env); +} + +void test_runtime_copy_empty() { + char **empty_env = calloc(1, sizeof(empty_env)); + RuntimeEnv *env = runtime_copy(empty_env); + STASIS_ASSERT(env->num_inuse == 0, "copied array isn't empty"); + GENERIC_ARRAY_FREE(empty_env); + runtime_free(env); +} + +void test_runtime() { + RuntimeEnv *env = runtime_copy(environ); + runtime_set(env, "CUSTOM_KEY", "Very custom"); + ssize_t idx = -1; + STASIS_ASSERT((idx = runtime_contains(env, "CUSTOM_KEY")) >= 0, "CUSTOM_KEY should exist in object"); + STASIS_ASSERT(strcmp(strlist_item(env, idx), "CUSTOM_KEY=Very custom") == 0, "Incorrect index returned by runtime_contains"); + + const char *custom_value = runtime_get(env, "CUSTOM_KEY"); + STASIS_ASSERT_FATAL(custom_value != NULL, "CUSTOM_KEY should not be NULL"); + STASIS_ASSERT(strcmp(custom_value, "Very custom") == 0, "CUSTOM_KEY has incorrect data"); + STASIS_ASSERT(strstr_array(environ, "CUSTOM_KEY") == NULL, "CUSTOM_KEY should not exist in the global environment"); + + runtime_apply(env); + const char *global_custom_value = getenv("CUSTOM_KEY"); + STASIS_ASSERT_FATAL(global_custom_value != NULL, "CUSTOM_KEY must exist in global environment after calling runtime_apply()"); + STASIS_ASSERT(strcmp(global_custom_value, custom_value) == 0, "local and global CUSTOM_KEY variable are supposed to be identical"); + + STASIS_ASSERT(setenv("CUSTOM_KEY", "modified", 1) == 0, "modifying global CUSTOM_KEY failed"); + global_custom_value = getenv("CUSTOM_KEY"); + STASIS_ASSERT(strcmp(global_custom_value, custom_value) != 0, "local and global CUSTOM_KEY variable are supposed to be different"); + + runtime_replace(&env, environ); + runtime_apply(env); + + global_custom_value = getenv("CUSTOM_KEY"); + custom_value = runtime_get(env, "CUSTOM_KEY"); + STASIS_ASSERT_FATAL(custom_value != NULL, "CUSTOM_KEY must exist in local environment after calling runtime_replace()"); + STASIS_ASSERT_FATAL(global_custom_value != NULL, "CUSTOM_KEY must exist in global environment after calling runtime_replace()"); + STASIS_ASSERT(strcmp(global_custom_value, custom_value) == 0, "local and global CUSTOM_KEY variable are supposed to be identical"); + + char output_truth[BUFSIZ] = {0}; + sprintf(output_truth, "Your PATH is '%s'.", runtime_get(env, "PATH")); + const char *output_expanded = runtime_expand_var(env, "Your PATH is '${PATH}'."); + STASIS_ASSERT(output_expanded != NULL, "expansion of PATH should not fail"); + STASIS_ASSERT(strcmp(output_expanded, output_truth) == 0, "the expansion, and the expected result should be identical"); + + // TODO: runtime_export() + // requires dumping stdout to a file and comparing it with the current environment array +} + +int main(int argc, char *argv[], char *arge[]) { + STASIS_TEST_BEGIN_MAIN(); + STASIS_TEST_FUNC *tests[] = { + test_runtime_copy, + test_runtime_copy_empty, + test_runtime, + }; + STASIS_TEST_RUN(tests); + STASIS_TEST_END_MAIN(); +}
\ No newline at end of file |