mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
This seems appropriate since worddescs are getting more and more complex: markers, flags, tokentypes, ...
265 lines
8.1 KiB
C
265 lines
8.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* expansion.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/04/09 15:50/06 by khais #+# #+# */
|
|
/* Updated: 2025/04/09 15:50:06 by khais ### ########.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"
|
|
#include "../src/parser/wordlist/wordlist.h"
|
|
|
|
/*
|
|
** Test file for the different expansion/substitution types of minishell.
|
|
*/
|
|
|
|
static void test_replace_in_str_insert(void)
|
|
{
|
|
char *line;
|
|
|
|
// insertion is not supported, we must replace at least one char
|
|
line = replace_in_str("le canari qui fait cuicui", 3, 3, "petit ");
|
|
assert_strequal("le petit anari qui fait cuicui", line);
|
|
free(line);
|
|
}
|
|
|
|
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);
|
|
// premier charactere debut du remplacement
|
|
line = replace_in_str("le petit canari qui fait cuicui", 0, 1, "Le");
|
|
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.
|
|
*/
|
|
static 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_env_var_expansion_with_invalid_ident(void)
|
|
{
|
|
t_wordlist *list;
|
|
t_minishell *app;
|
|
char *value;
|
|
char *key;
|
|
|
|
value = ft_strdup("value");
|
|
key = ft_strdup("VAR");
|
|
app = ft_calloc(1, sizeof(t_minishell));
|
|
app->env = env_set_entry(&(app->env), key, value);
|
|
list = minishell_wordsplit("$''VAR");
|
|
list = wordlist_var_expansion(list, app);
|
|
assert_strequal("''VAR", list->word->word);
|
|
wordlist_destroy(list);
|
|
env_destroy(app->env);
|
|
free(app);
|
|
do_leak_check();
|
|
}
|
|
|
|
static void test_env_var_expansion_with_trailing_dollar(void)
|
|
{
|
|
t_wordlist *list;
|
|
t_minishell *app;
|
|
char *value;
|
|
char *key;
|
|
|
|
value = ft_strdup("value");
|
|
key = ft_strdup("VAR");
|
|
app = ft_calloc(1, sizeof(t_minishell));
|
|
app->env = env_set_entry(&(app->env), key, value);
|
|
list = minishell_wordsplit("$VAR$");
|
|
list = wordlist_var_expansion(list, app);
|
|
assert_strequal("value$", list->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);
|
|
assert(expanded);
|
|
assert_strequal(filepattern->word, expanded->word->word);
|
|
worddesc_destroy(filepattern);
|
|
wordlist_destroy(expanded);
|
|
do_leak_check();
|
|
}
|
|
|
|
static 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)
|
|
{
|
|
test_env_var_expansion_with_trailing_dollar();
|
|
test_env_var_expansion_with_invalid_ident();
|
|
test_replace_in_str_insert();
|
|
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);
|
|
}
|