mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
ENV: A small set of functions for internal env
This commit is contained in:
parent
18a2835a7c
commit
660d785237
8 changed files with 447 additions and 41 deletions
|
|
@ -1,5 +1,7 @@
|
|||
# make gets confused if a file with the same name exists in the sources, so some
|
||||
# file are prefixed with test_
|
||||
rawtests = \
|
||||
env_manip \
|
||||
test_env_manip \
|
||||
metacharacters \
|
||||
|
||||
tests = $(addprefix test_,$(rawtests))
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env_manip.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
|
||||
/* Updated: 2025/02/18 16:12:23 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../src/env_manip.h"
|
||||
#include "libft.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);
|
||||
}
|
||||
|
||||
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", "");
|
||||
return (0);
|
||||
}
|
||||
148
tests/test_env_manip.c
Normal file
148
tests/test_env_manip.c
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test_env_manip.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
|
||||
/* Updated: 2025/02/19 12:23:50 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../src/env_manip.h"
|
||||
#include "libft.h"
|
||||
#include "testutil.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);
|
||||
assert(env_set_entry(&env, NULL, ft_strdup("hello")) == NULL);
|
||||
assert(env_set_entry(&env, ft_strdup("VAR"), NULL) == NULL);
|
||||
assert(env_set_entry(&env, ft_strdup(""), ft_strdup("value")) == env);
|
||||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue