minishell/tests/expansion.c

215 lines
6.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 17:52/50 by khais #+# #+# */
/* Updated: 2025/03/20 10:36:23 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "testutil.h"
#include "../src/subst/replace_substr.h"
#include "../src/subst/subst.h"
#include "../src/env/env.h"
#include "../src/env/env_manip.h"
#include "../src/parser/wordsplit/wordsplit.h"
static t_worddesc *create_single_word(char *str)
{
t_wordlist *words = minishell_wordsplit(str);
assert(wordlist_get(words, 0) != NULL);
assert(wordlist_get(words, 1) == NULL);
t_worddesc *word = wordlist_pop(&words);
return (word);
}
/*
** Test file for the different expansion/substitution types of minishell.
*/
static void test_insert_instr(void)
{
char *line;
line = replace_in_str("abcdefghijk", 1, 4, "souris");
assert_strequal("asourisfghijk", line);
free(line);
line = replace_in_str("abcdefgh" , 2, 2, "non ce n'est pas ma faute");
assert_strequal("abnon ce n'est pas ma fautedefgh", line);
free(line);
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, "");
assert_strequal("le canari qui fait cuicui", line);
free(line);
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, NULL);
assert_strequal("le canari qui fait cuicui", line);
free(line);
line = replace_in_str("le canari qui fait cuicui", 2, 2, " petit ");
assert_strequal("le petit canari qui fait cuicui", line);
free(line);
do_leak_check();
}
/*
** NOTE/REMINDER: I currently replace $0 to $9 with nothing but I can change
** this behavior at any point if you would rather we ignored them and therefore
** did not replace those.
*/
void test_env_variable_expansion(void)
{
t_wordlist *list;
t_minishell *app;
char *value;
char *key;
value = ft_strdup("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin");
key = ft_strdup("PATH");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
key = ft_strdup("USER");
value = ft_strdup("jguelen");
app->env = env_set_entry(&(app->env), key, value);
list = minishell_wordsplit("$USER$USER $PATH \"'$USER'$USER\" a$test'b' '$USER'");
list = wordlist_var_expansion(list, app);
assert_strequal("jguelenjguelen", list->word->word);
assert_strequal("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin", list->next->word->word);
assert_strequal("\"'jguelen'jguelen\"", list->next->next->word->word);
assert_strequal("a'b'", list->next->next->next->word->word);
assert_strequal("'$USER'", list->next->next->next->next->word->word);
wordlist_destroy(list);
env_destroy(app->env);
free(app);
do_leak_check();
}
static void test_cmd_path_expansion(void)
{
t_minishell *app;
char *key;
char *value;
char *cmdpath;
key = ft_strdup("PATH");
value = ft_strdup("./port:/usr/bin");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app);
assert_strequal("./port/ls", cmdpath);
free(cmdpath);
value = ft_strdup(":/usr/bin");
key = ft_strdup("PATH");
app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app);
assert_strequal("./ls", cmdpath);
free(cmdpath);
cmdpath = get_cmdpath("peekaboo", app);
assert(cmdpath == NULL);
free(cmdpath);
env_destroy(app->env);
free(app);
do_leak_check();
}
static void test_filename_star_expansion(void)
{
t_worddesc *filepattern;
t_wordlist *expanded;
t_wordlist *tmp;
//test1 Everything except . and ..
ft_printf("test_filename_star_expansion\n");
filepattern = create_single_word("*");
expanded = expand_star(filepattern);
tmp = expanded;
assert_strequal("aba", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("abcda", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("aiia", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("axr", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("directory", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("exp", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("ls", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("port", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("yuhbqa", tmp->word->word);
tmp = tmp->next;
assert(!tmp);
wordlist_destroy(expanded);
worddesc_destroy(filepattern);
//test2
filepattern = create_single_word("**a*'b'*a*");
expanded = expand_star(filepattern);
assert(wordlist_size(expanded) == 2);
assert_strequal("aba", expanded->word->word);
assert_strequal("abcda", expanded->next->word->word);
worddesc_destroy(filepattern);
wordlist_destroy(expanded);
//test ., .. and .plop
filepattern = create_single_word(".*");
expanded = expand_star(filepattern);
//assert(wordlist_size(expanded) == 3);
//assert_strequal(".", expanded->word->word);
//assert_strequal("..", expanded->next->word->word);
//assert_strequal(".plop", expanded->next->next->word->word);
worddesc_destroy(filepattern);
wordlist_destroy(expanded);
//test zero result
filepattern = create_single_word("e*x***p*b");
expanded = expand_star(filepattern);
worddesc_destroy(filepattern);
assert(!expanded);
wordlist_destroy(expanded);
do_leak_check();
}
void simple_sub_test(void)
{
t_wordlist *list;
t_minishell *app;
char *value;
char *key;
value = ft_strdup("val");
key = ft_strdup("KEY");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
list = minishell_wordsplit("v$KEY");
list = wordlist_var_expansion(list, app);
assert_strequal("vval", list->word->word);
wordlist_destroy(list);
env_destroy(app->env);
free(app);
do_leak_check();
}
int main(void)
{
if (chdir("./expand_test") == -1)
assert("chdir failure" && false);
simple_sub_test();
test_insert_instr();
test_env_variable_expansion();
test_cmd_path_expansion();
test_filename_star_expansion();
return (0);
}