WIP: parse command list tests: assert that invalid pipeline returns null

This commit is contained in:
Khaïs COLIN 2025-02-26 14:07:55 +01:00 committed by Khaïs COLIN
parent bccd68b11f
commit c57a4a69a7
4 changed files with 54 additions and 6 deletions

View file

@ -6,13 +6,16 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */ /* Created: 2025/02/24 17:49:46 by khais #+# #+# */
/* Updated: 2025/02/26 13:56:45 by khais ### ########.fr */ /* Updated: 2025/02/26 16:53:55 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "command_list.h" #include "command_list.h"
#include "command_list_builder.h" #include "command_list_builder.h"
#include "operator.h" #include "operator.h"
#include "libft.h"
#include "../../ft_errno.h"
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -35,6 +38,22 @@ static bool cmdlist_builder_at_last_pipeline(t_cmdlist_builder *builder)
return (builder->idx == builder->cmdlist->num_pipelines - 1); return (builder->idx == builder->cmdlist->num_pipelines - 1);
} }
/*
** Frees all memory associated with this builder and set error to true.
*/
static void cmdlist_builder_error(t_cmdlist_builder *builder)
{
if (builder == NULL)
return ;
cmdlist_destroy(builder->cmdlist);
ft_dprintf(STDERR_FILENO, "current wordlist: %p\n", builder->current_wordlist);
ft_dprintf(STDERR_FILENO, "current wordlist->word: %p\n", builder->current_wordlist->word);
wordlist_destroy(builder->current_wordlist);
ft_dprintf(STDERR_FILENO, "current wordlist has been freed: %p\n", builder->current_wordlist);
worddesc_destroy(builder->current_word);
builder->error = true;
}
/* /*
** Delimit the current pipeline, creating a new t_pipeline object. ** Delimit the current pipeline, creating a new t_pipeline object.
** **
@ -46,8 +65,17 @@ static void cmdlist_builder_delimit(
t_cmdlist_builder *builder, t_cmdlist_builder *builder,
t_wordlist **words) t_wordlist **words)
{ {
ft_dprintf(STDERR_FILENO, "delimit: ");
wordlist_debug(builder->current_wordlist);
builder->cmdlist->pipelines[builder->idx] builder->cmdlist->pipelines[builder->idx]
= pipeline_from_wordlist(builder->current_wordlist); = pipeline_from_wordlist(builder->current_wordlist);
if (builder->cmdlist->pipelines[builder->idx] == NULL)
{
ft_perror("invalid pipeline");
cmdlist_builder_error(builder);
ft_dprintf(STDERR_FILENO, "destroyed builder\n");
return ;
}
if (cmdlist_builder_at_last_pipeline(builder)) if (cmdlist_builder_at_last_pipeline(builder))
builder->cmdlist->operators[builder->idx] = OP_END; builder->cmdlist->operators[builder->idx] = OP_END;
else else
@ -68,13 +96,16 @@ t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
if (setup_cmdlist_builder(&builder, &words) == NULL) if (setup_cmdlist_builder(&builder, &words) == NULL)
return (NULL); return (NULL);
while (builder.current_word != NULL) while (builder.error == false && builder.current_word != NULL)
{ {
ft_dprintf(STDERR_FILENO, "current word: %s\n", builder.current_word->word);
if (match_op(builder.current_word->word) == OP_INVALID) if (match_op(builder.current_word->word) == OP_INVALID)
cmdlist_builder_next_word(&builder, &words); cmdlist_builder_next_word(&builder, &words);
else else
cmdlist_builder_delimit(&builder, &words); cmdlist_builder_delimit(&builder, &words);
} }
if (builder.error)
return (NULL);
if (builder.current_wordlist != NULL) if (builder.current_wordlist != NULL)
cmdlist_builder_delimit(&builder, &words); cmdlist_builder_delimit(&builder, &words);
return (builder.cmdlist); return (builder.cmdlist);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 13:51:18 by khais #+# #+# */ /* Created: 2025/02/26 13:51:18 by khais #+# #+# */
/* Updated: 2025/02/26 13:52:39 by khais ### ########.fr */ /* Updated: 2025/02/26 17:02:05 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,7 +78,10 @@ t_cmdlist_builder *setup_cmdlist_builder(
if (builder->cmdlist == NULL) if (builder->cmdlist == NULL)
return (NULL); return (NULL);
builder->current_wordlist = NULL; builder->current_wordlist = NULL;
ft_dprintf(STDERR_FILENO, "words before pop in setup: %p\n", *words);
builder->current_word = wordlist_pop(words); builder->current_word = wordlist_pop(words);
ft_dprintf(STDERR_FILENO, "builder->current_word after pop in setup: %p\n", builder->current_word);
ft_dprintf(STDERR_FILENO, "words after pop in setup: %p\n", *words);
builder->idx = 0; builder->idx = 0;
return (builder); return (builder);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 13:50:25 by khais #+# #+# */ /* Created: 2025/02/26 13:50:25 by khais #+# #+# */
/* Updated: 2025/02/26 13:54:55 by khais ### ########.fr */ /* Updated: 2025/02/26 14:05:36 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,6 +33,11 @@ typedef struct s_cmdlist_builder
** index of the pipeline that is about to be inserted into the cmdlist ** index of the pipeline that is about to be inserted into the cmdlist
*/ */
int idx; int idx;
/*
** set to true in an error was encountered that requires stopping the
** processing.
*/
bool error;
} t_cmdlist_builder; } t_cmdlist_builder;
t_cmdlist_builder *setup_cmdlist_builder( t_cmdlist_builder *setup_cmdlist_builder(

View file

@ -6,13 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:40:48 by khais #+# #+# */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */
/* Updated: 2025/02/25 15:25:14 by khais ### ########.fr */ /* Updated: 2025/02/26 17:29:15 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h"
#include "../src/parser/command_list/command_list.h" #include "../src/parser/command_list/command_list.h"
#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/wordsplit/wordsplit.h"
#include "ft_printf.h"
#include "testutil.h" #include "testutil.h"
#include "unistd.h" #include "unistd.h"
#include <assert.h> #include <assert.h>
@ -159,8 +160,16 @@ static void test_parse_command_list_quad_both_operators(void)
cmdlist_destroy(cmd); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_invalid_pipeline(void)
{
t_cmdlist *cmd = parse_command_list("echo this | | cat -e || echo does not work");
assert(cmd == NULL);
}
int main(void) int main(void)
{ {
test_parse_command_list_invalid_pipeline();
test_parse_command_list_empty(); test_parse_command_list_empty();
test_parse_command_list_single_pipeline(); test_parse_command_list_single_pipeline();
test_parse_command_list_simple_and(); test_parse_command_list_simple_and();