aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-08-29 09:43:32 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-08-29 09:44:15 -0400
commit7d8b260671a7a27e34bee4995bbbdc623ce85689 (patch)
tree237d438efa64505400ed2a6dfb567fad3eaf55fc
parentb625cf2045a73b9477abe3cc25f38bf34c8c483b (diff)
downloadstasis-more-tests.tar.gz
Add test_environment.cmore-tests
-rw-r--r--tests/test_environment.c76
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