Expansion: one little problem remaining with * expansion and .

This commit is contained in:
Jérôme Guélen 2025-03-20 10:37:50 +01:00
parent 2da7f03a66
commit da06c0d4e0
No known key found for this signature in database
4 changed files with 42 additions and 41 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* wordlist_quicksort.c :+: :+: :+: */ /* wordlist_quicksort.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 17:29:05 by jguelen #+# #+# */ /* Created: 2025/03/08 17:29:05 by jguelen #+# #+# */
/* Updated: 2025/03/19 16:29:58 by jguelen ### ########.fr */ /* Updated: 2025/03/20 09:41:04 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,7 @@ static int wordlist_quicksort_partition(t_wordlist *list, int start, int end)
pivot = wordlist_get_elem(list, end); pivot = wordlist_get_elem(list, end);
while (j < end) while (j < end)
{ {
if (ft_strcmp(list_j->word->word, pivot->word->word) > 0) if (ft_strcmp(list_j->word->word, pivot->word->word) < 0)
{ {
ft_swap_wordlist_contents(list_i, list_j); ft_swap_wordlist_contents(list_i, list_j);
i++; i++;

0
tests/expand_test/.plop Normal file
View file

1
tests/expand_test/port/ls Executable file
View file

@ -0,0 +1 @@
echo hello

View file

@ -6,12 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 17:52/50 by khais #+# #+# */ /* Created: 2025/03/19 17:52/50 by khais #+# #+# */
/* Updated: 2025/03/19 17:52:50 by khais ### ########.fr */ /* Updated: 2025/03/20 10:36:23 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "testutil.h" #include "testutil.h"
#include "../src/subst/replace_substr.h" #include "../src/subst/replace_substr.h"
#include "../src/subst/subst.h" #include "../src/subst/subst.h"
@ -19,7 +21,6 @@
#include "../src/env/env_manip.h" #include "../src/env/env_manip.h"
#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/wordsplit/wordsplit.h"
static t_worddesc *create_single_word(char *str) static t_worddesc *create_single_word(char *str)
{ {
t_wordlist *words = minishell_wordsplit(str); t_wordlist *words = minishell_wordsplit(str);
@ -95,17 +96,17 @@ static void test_cmd_path_expansion(void)
char *cmdpath; char *cmdpath;
key = ft_strdup("PATH"); key = ft_strdup("PATH");
value = ft_strdup("/usr/bin"); value = ft_strdup("./port:/usr/bin");
app = ft_calloc(1, sizeof(t_minishell)); app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value); app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app); cmdpath = get_cmdpath("ls", app);
/* assert_strequal("/usr/bin/ls", cmdpath); */ // FIXME: does not work on nixos assert_strequal("./port/ls", cmdpath);
free(cmdpath); free(cmdpath);
value = ft_strdup(":/usr/bin"); value = ft_strdup(":/usr/bin");
key = ft_strdup("PATH"); key = ft_strdup("PATH");
app->env = env_set_entry(&(app->env), key, value); app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app); cmdpath = get_cmdpath("ls", app);
/* assert_strequal("./ls", cmdpath); */ // FIXME: is not portable assert_strequal("./ls", cmdpath);
free(cmdpath); free(cmdpath);
cmdpath = get_cmdpath("peekaboo", app); cmdpath = get_cmdpath("peekaboo", app);
assert(cmdpath == NULL); assert(cmdpath == NULL);
@ -121,61 +122,58 @@ static void test_filename_star_expansion(void)
t_wordlist *expanded; t_wordlist *expanded;
t_wordlist *tmp; t_wordlist *tmp;
return ; //test1 Everything except . and ..
//test1
ft_printf("test_filename_star_expansion\n"); ft_printf("test_filename_star_expansion\n");
filepattern = create_single_word("*"); filepattern = create_single_word("*");
expanded = expand_star(filepattern); expanded = expand_star(filepattern);
tmp = expanded; tmp = expanded;
assert_strequal("blabla", tmp->word->word); 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; tmp = tmp->next;
assert(tmp); assert(tmp);
assert_strequal("exp", tmp->word->word); assert_strequal("exp", tmp->word->word);
tmp = tmp->next; tmp = tmp->next;
assert(tmp); 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); assert_strequal("ls", tmp->word->word);
tmp = tmp->next; tmp = tmp->next;
assert(tmp); assert(tmp);
assert_strequal("Makefile", tmp->word->word); assert_strequal("port", tmp->word->word);
tmp = tmp->next; tmp = tmp->next;
assert(tmp); assert(tmp);
assert_strequal("metacharacters.c", tmp->word->word); assert_strequal("yuhbqa", 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; tmp = tmp->next;
assert(!tmp); assert(!tmp);
wordlist_destroy(expanded); wordlist_destroy(expanded);
worddesc_destroy(filepattern); worddesc_destroy(filepattern);
//test2 //test2
filepattern = create_single_word("**bla'b'*la*"); filepattern = create_single_word("**a*'b'*a*");
expanded = expand_star(filepattern); expanded = expand_star(filepattern);
assert(wordlist_size(expanded) == 1); assert(wordlist_size(expanded) == 2);
assert_strequal("blabla", expanded->word->word); assert_strequal("aba", expanded->word->word);
assert_strequal("abcda", expanded->next->word->word);
worddesc_destroy(filepattern); worddesc_destroy(filepattern);
wordlist_destroy(expanded); wordlist_destroy(expanded);
//test zero résultat //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"); filepattern = create_single_word("e*x***p*b");
expanded = expand_star(filepattern); expanded = expand_star(filepattern);
worddesc_destroy(filepattern); worddesc_destroy(filepattern);
@ -206,6 +204,8 @@ void simple_sub_test(void)
int main(void) int main(void)
{ {
if (chdir("./expand_test") == -1)
assert("chdir failure" && false);
simple_sub_test(); simple_sub_test();
test_insert_instr(); test_insert_instr();
test_env_variable_expansion(); test_env_variable_expansion();