mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
215 lines
6.6 KiB
C
215 lines
6.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* expansion.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/19 17:52/50 by khais #+# #+# */
|
|
/* Updated: 2025/03/19 17:52:50 by khais ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.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("/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("/usr/bin/ls", cmdpath); */ // FIXME: does not work on nixos
|
|
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); */ // FIXME: is not portable
|
|
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;
|
|
|
|
return ;
|
|
//test1
|
|
ft_printf("test_filename_star_expansion\n");
|
|
filepattern = create_single_word("*");
|
|
expanded = expand_star(filepattern);
|
|
tmp = expanded;
|
|
assert_strequal("blabla", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("exp", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("expansion.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("flagadaPATH", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("ls", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("Makefile", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("metacharacters.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("parse_pipelines.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("parse_simple_cmds.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("test_env_manip.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("testutil.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("testutil.h", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(tmp);
|
|
assert_strequal("word_splitting.c", tmp->word->word);
|
|
tmp = tmp->next;
|
|
assert(!tmp);
|
|
wordlist_destroy(expanded);
|
|
worddesc_destroy(filepattern);
|
|
//test2
|
|
filepattern = create_single_word("**bla'b'*la*");
|
|
expanded = expand_star(filepattern);
|
|
assert(wordlist_size(expanded) == 1);
|
|
assert_strequal("blabla", expanded->word->word);
|
|
worddesc_destroy(filepattern);
|
|
wordlist_destroy(expanded);
|
|
//test zero résultat
|
|
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)
|
|
{
|
|
simple_sub_test();
|
|
test_insert_instr();
|
|
test_env_variable_expansion();
|
|
test_cmd_path_expansion();
|
|
test_filename_star_expansion();
|
|
return (0);
|
|
}
|