2025-03-07 11:55:51 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* test_redirection_parsing.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-03-10 16:42:53 +01:00
|
|
|
/* Created: 2025/03/10 16:50/33 by khais #+# #+# */
|
|
|
|
|
/* Updated: 2025/03/10 16:50:33 by khais ### ########.fr */
|
2025-03-07 11:55:51 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include "../src/parser/simple_cmd/simple_cmd.h"
|
|
|
|
|
#include "../src/parser/wordsplit/wordsplit.h"
|
2025-03-07 13:34:47 +01:00
|
|
|
#include "../src/postprocess/redirections/redirection_parsing.h"
|
2025-03-07 13:34:47 +01:00
|
|
|
#include "../src/postprocess/redirections/redirection_parsing.h"
|
|
|
|
|
#include "../src/postprocess/redirections/redirection_list.h"
|
2025-03-10 16:40:02 +01:00
|
|
|
#include "../src/ft_errno.h"
|
2025-03-07 15:18:06 +01:00
|
|
|
#include "ft_printf.h"
|
2025-03-10 16:40:02 +01:00
|
|
|
#include "stdio.h"
|
2025-03-07 13:34:47 +01:00
|
|
|
#include "testutil.h"
|
2025-03-07 11:55:51 +01:00
|
|
|
|
|
|
|
|
static t_simple_cmd *parse_simple_cmd(char *input)
|
|
|
|
|
{
|
|
|
|
|
t_wordlist *words = minishell_wordsplit(input);
|
|
|
|
|
t_simple_cmd *cmd = simple_cmd_from_wordlist(words);
|
|
|
|
|
return (cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_redirection_parsing_no_redirections(void)
|
|
|
|
|
{
|
2025-03-07 15:18:06 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-07 11:55:51 +01:00
|
|
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello world");
|
|
|
|
|
assert(parse_redirections(cmd) != NULL);
|
|
|
|
|
assert(cmd->redirections == NULL);
|
|
|
|
|
simple_cmd_destroy(cmd);
|
2025-03-07 13:34:47 +01:00
|
|
|
do_leak_check();
|
2025-03-07 11:55:51 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-07 13:34:47 +01:00
|
|
|
static void test_redirection_parsing_output_at_start(void)
|
|
|
|
|
{
|
2025-03-07 15:18:06 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-07 13:34:47 +01:00
|
|
|
t_simple_cmd *cmd = parse_simple_cmd(">outfile echo hello world");
|
|
|
|
|
assert(parse_redirections(cmd) != NULL);
|
|
|
|
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
|
|
|
|
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
|
|
|
|
assert(cmd->redirections->next == NULL);
|
|
|
|
|
assert_strequal("echo", cmd->words->word->word);
|
|
|
|
|
assert_strequal("hello", cmd->words->next->word->word);
|
|
|
|
|
assert_strequal("world", cmd->words->next->next->word->word);
|
|
|
|
|
assert(NULL == cmd->words->next->next->next);
|
|
|
|
|
simple_cmd_destroy(cmd);
|
|
|
|
|
do_leak_check();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 15:18:06 +01:00
|
|
|
static void test_redirection_parsing_output_at_end(void)
|
|
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
|
|
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello world > outfile");
|
|
|
|
|
assert(parse_redirections(cmd) != NULL);
|
|
|
|
|
assert_strequal("echo", cmd->words->word->word);
|
|
|
|
|
assert_strequal("hello", cmd->words->next->word->word);
|
|
|
|
|
assert_strequal("world", cmd->words->next->next->word->word);
|
|
|
|
|
assert(NULL == cmd->words->next->next->next);
|
|
|
|
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
|
|
|
|
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
|
|
|
|
assert(cmd->redirections->next == NULL);
|
|
|
|
|
simple_cmd_destroy(cmd);
|
|
|
|
|
do_leak_check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_redirection_parsing_output_in_middle(void)
|
|
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
|
|
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello >outfile world");
|
|
|
|
|
assert(parse_redirections(cmd) != NULL);
|
|
|
|
|
assert_strequal("echo", cmd->words->word->word);
|
|
|
|
|
assert_strequal("hello", cmd->words->next->word->word);
|
|
|
|
|
assert_strequal("world", cmd->words->next->next->word->word);
|
|
|
|
|
assert(NULL == cmd->words->next->next->next);
|
|
|
|
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
|
|
|
|
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
|
|
|
|
assert(cmd->redirections->next == NULL);
|
|
|
|
|
simple_cmd_destroy(cmd);
|
|
|
|
|
do_leak_check();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:40:02 +01:00
|
|
|
static void test_redirection_parsing_output_no_filename_at_end(void)
|
|
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
|
|
|
ft_errno(FT_ESUCCESS);
|
|
|
|
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello world >");
|
|
|
|
|
assert(parse_redirections(cmd) == NULL);
|
|
|
|
|
assert(FT_EMALFORMED_REDIRECTION == ft_errno_get());
|
|
|
|
|
simple_cmd_destroy(cmd);
|
|
|
|
|
do_leak_check();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:42:53 +01:00
|
|
|
static void test_redirection_parsing_null(void)
|
|
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
|
|
|
assert(parse_redirections(NULL) == NULL);
|
|
|
|
|
do_leak_check();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 13:34:47 +01:00
|
|
|
|
2025-03-07 11:55:51 +01:00
|
|
|
int main(void) {
|
|
|
|
|
test_redirection_parsing_no_redirections();
|
2025-03-07 13:34:47 +01:00
|
|
|
test_redirection_parsing_output_at_start();
|
2025-03-07 15:18:06 +01:00
|
|
|
test_redirection_parsing_output_at_end();
|
|
|
|
|
test_redirection_parsing_output_in_middle();
|
2025-03-10 16:40:02 +01:00
|
|
|
test_redirection_parsing_output_no_filename_at_end();
|
2025-03-10 16:42:53 +01:00
|
|
|
test_redirection_parsing_null();
|
2025-03-07 11:55:51 +01:00
|
|
|
return (0);
|
|
|
|
|
}
|