minishell/tests/test_env_manip.c
Khaïs COLIN dff68d5a8b
env refactor: split imports over multiple files
This allows for potentially faster recompilation, by not triggering
recompilation for all env files if env_manip changes
2025-02-19 16:43:37 +01:00

155 lines
5.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_env_manip.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
/* Updated: 2025/02/19 16:41:53 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "../src/env/env.h"
#include "../src/env/env_convert.h"
#include "../src/env/env_manip.h"
#include "../src/env/envp.h"
#include "../src/ft_errno.h"
#include "libft.h"
#include "unistd.h"
#include <assert.h>
static void test_envp_parsing(char *line, char *expected_key, char *expected_value)
{
char *got_key = envp_get_key(line);
char *got_value = envp_get_val(line);
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, got_key);
assert(expected_key == got_key || ft_strcmp(expected_key, got_key) == 0);
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting value to eq '%s', and got '%s'\n", line, expected_value, got_value);
assert(expected_value == got_value || ft_strcmp(expected_value, got_value) == 0);
free(got_key);
free(got_value);
}
static void assert_env_value(t_env *env, char *key, char *value)
{
char *got_value = env_get_val(env, key);
ft_dprintf(STDERR_FILENO, "expecting %s=%s to exist, and got %s=%s\n", key, value, key, got_value);
assert(ft_strcmp(value, got_value) == 0);
}
static void test_env_set_entry_empty_env(void)
{
t_env *env;
env = NULL;
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
assert_env_value(env, "VAR", "hello");
assert(1 == env_get_size(env));
env_destroy(env);
}
static void test_env_set_entry_existing_value(void)
{
t_env *env;
env = NULL;
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("there")) != NULL);
assert_env_value(env, "VAR", "there");
assert(1 == env_get_size(env));
env_destroy(env);
}
static void test_env_set_entry_multiple(void)
{
t_env *env;
env = NULL;
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
assert(env_set_entry(&env, ft_strdup("SHELL"), ft_strdup("/bin/bash")) != NULL);
assert(env_set_entry(&env, ft_strdup("TERM"), ft_strdup("xterm-kitty")) != NULL);
assert_env_value(env, "VAR", "hello");
assert_env_value(env, "SHELL", "/bin/bash");
assert_env_value(env, "TERM", "xterm-kitty");
assert(3 == env_get_size(env));
env_destroy(env);
}
static void test_env_set_entry_nullargs(void)
{
t_env *env;
env = NULL;
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
ft_errno(FT_ESUCCESS);
assert(env_set_entry(&env, NULL, ft_strdup("hello")) == NULL);
assert(ft_errno_get() == FT_EINVAL);
assert(env_set_entry(&env, ft_strdup("VAR"), NULL) == NULL);
ft_errno(FT_ESUCCESS);
assert(env_set_entry(&env, ft_strdup(""), ft_strdup("value")) == NULL);
assert(ft_errno_get() == FT_EBADID);
assert_env_value(env, "VAR", "hello");
assert(1 == env_get_size(env));
env_destroy(env);
}
static void test_env_from_envp(void)
{
t_env *env;
char *envp[] = {"VAR=hello", "SHELL=/bin/bash", "TERM=xterm-kitty", NULL};
env = env_from_envp(envp);
assert(env != NULL);
assert_env_value(env, "VAR", "hello");
assert_env_value(env, "SHELL", "/bin/bash");
assert_env_value(env, "TERM", "xterm-kitty");
assert(3 == env_get_size(env));
env_destroy(env);
}
static void test_env_from_envp_invalid(void)
{
t_env *env;
char *envp[] = {"VAR=hello", "", "TERM=xterm-kitty", NULL};
ft_dprintf(STDERR_FILENO, "test_env_from_envp\n");
env = env_from_envp(envp);
assert(env == NULL);
}
static void test_envp_from_env(void)
{
t_env *env;
char *envp[] = {"VAR=hello", "SHELL=/bin/bash", "TERM=xterm-kitty", NULL};
env = env_from_envp(envp);
char **got_envp = envp_from_env(env);
t_env *got_env = env_from_envp(got_envp);
assert(got_env != NULL);
assert_env_value(got_env, "VAR", "hello");
assert_env_value(got_env, "SHELL", "/bin/bash");
assert_env_value(got_env, "TERM", "xterm-kitty");
assert(3 == env_get_size(got_env));
env_destroy(env);
env_destroy(got_env);
envp_destroy(got_envp);
}
int main(void) {
test_envp_parsing("SHELL=/bin/fish", "SHELL", "/bin/fish");
test_envp_parsing("=/bin/fish", "", "/bin/fish");
test_envp_parsing(NULL, NULL, NULL);
test_envp_parsing("", NULL, NULL);
test_envp_parsing("VARNAME", "VARNAME", "");
test_env_set_entry_empty_env();
test_env_set_entry_existing_value();
test_env_set_entry_multiple();
test_env_set_entry_nullargs();
test_env_from_envp();
test_env_from_envp_invalid();
test_envp_from_env();
return (0);
}