Merge branch 'testsuite' into dev

This commit is contained in:
matteo 2026-01-24 17:49:07 +01:00
commit 668bba5b02
6 changed files with 96 additions and 85 deletions

View file

@ -35,7 +35,13 @@ check_PROGRAMS = testsuite
#testsuite_CFLAGS += $(CRITERION_CFLAGS)
testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \
../tests/unit/utils/utils_tests.c
../tests/unit/utils/utils_tests.c \
../tests/unit/expansion/expand.c \
../tests/unit/expansion/parse_var.c \
../tests/unit/io_backend/io_backend.c \
../tests/unit/utils/args.c \
../tests/unit/utils/hash_map.c \
../tests/unit/utils/insert_into.c
testsuite_CPPFLAGS = $(42sh_CPPFLAGS)

View file

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stddef.h>
#include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.h"
/**

View file

@ -20,9 +20,9 @@ Test(expand, no_expansion)
struct ast *ast = ast_create_command(list);
struct ast_command *ast_command = ast_get_command(ast);
struct ast_command *command2 = expand(ast_command, NULL);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo something",
bool ret = expand(ast_command, NULL);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo something",
"String without variables should remain unchanged");
ast_free(&ast);
}
@ -38,9 +38,9 @@ Test(expand, single_quotes_no_expansion)
struct hash_map *vars = vars_init();
set_var_copy(vars, "VAR", "expanded");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo $VAR",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo $VAR",
"Variable should not expand inside single quotes");
ast_free(&ast);
hash_map_free(&vars);
@ -57,9 +57,9 @@ Test(expand, single_dollar)
struct hash_map *vars = vars_init();
set_var_copy(vars, "VAR", "expanded");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo $ sign",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo $ sign",
"Variable should not expand inside single quotes");
ast_free(&ast);
hash_map_free(&vars);
@ -76,8 +76,8 @@ Test(expand, empty_braces_no_expansion)
struct hash_map *vars = vars_init();
set_var_copy(vars, "VAR", "expanded");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_null(command2, "Expansion should fail on empty braces");
bool ret = expand(ast_command, vars);
cr_expect(ret == false, "expansion should fail with %s", str);
ast_free(&ast);
hash_map_free(&vars);
}
@ -93,9 +93,9 @@ Test(expand, basic_expansion)
struct hash_map *vars = vars_init();
set_var_copy(vars, "VAR", "expanded");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo expanded",
"Variable should expand correctly");
ast_free(&ast);
hash_map_free(&vars);
@ -114,9 +114,9 @@ Test(expand, multiple_expansion)
set_var_copy(vars, "VAR2", "values");
set_var_copy(vars, "VAR3", "here");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data,
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data,
"echo expanded values here",
"Multiple variables should expand correctly");
ast_free(&ast);
@ -133,9 +133,9 @@ Test(expand, env_variable)
setenv("MY_ENV_VAR", "environment", 0);
struct ast_command *command2 = expand(ast_command, NULL);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo environment",
bool ret = expand(ast_command, NULL);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo environment",
"Environment variable should expand correctly");
ast_free(&ast);
}
@ -150,9 +150,9 @@ Test(expand, undefined_variable)
struct hash_map *vars = vars_init();
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo ",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo ",
"Undefined variable should expand to empty string");
ast_free(&ast);
hash_map_free(&vars);
@ -170,9 +170,9 @@ Test(expand, nested_expansion)
set_var_copy(vars, "A", "expanded");
set_var_copy(vars, "B", "$A");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo expanded",
"Nested variable should expand correctly");
ast_free(&ast);
hash_map_free(&vars);
@ -190,9 +190,9 @@ Test(expand, mixed_quotes_expansion)
set_var_copy(vars, "VAR1", "expanded");
set_var_copy(vars, "VAR2", "not_expanded");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data,
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data,
"echo \"expanded and $VAR2\"",
"Variable in double quotes should expand, while variable "
"in single quotes should not");
@ -212,9 +212,9 @@ Test(expand, adjacent_variables)
set_var_copy(vars, "VAR1", "hello");
set_var_copy(vars, "VAR2", "world");
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
cr_assert_str_eq((char *)command2->command->data, "echo helloworld",
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
cr_expect_str_eq((char *)ast_command->command->data, "echo helloworld",
"Adjacent variables should expand correctly");
ast_free(&ast);
hash_map_free(&vars);
@ -228,9 +228,9 @@ Test(expand, random)
struct ast *ast = ast_create_command(list);
struct ast_command *ast_command = ast_get_command(ast);
struct ast_command *command2 = expand(ast_command, NULL);
cr_assert_not_null(command2, "Expansion returned NULL");
int rnd = atoi((char *)command2->command->data);
bool ret = expand(ast_command, NULL);
cr_expect(ret, "expansion failed with %s", str);
int rnd = atoi((char *)ast_command->command->data);
cr_assert(rnd >= 0 && rnd <= 32767,
"RANDOM variable should expand to a value between 0 and 32767");
ast_free(&ast);
@ -245,9 +245,9 @@ Test(expand, pid)
struct ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init();
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
int pid = atoi((char *)command2->command->data);
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
int pid = atoi((char *)ast_command->command->data);
cr_assert(pid == getpid(),
"$$ variable should expand to the pid of the process");
ast_free(&ast);
@ -263,9 +263,9 @@ Test(expand, default_last_exit_code)
struct ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init();
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
int code = atoi((char *)command2->command->data);
bool ret = expand(ast_command, vars);
cr_expect(ret, "expansion failed with %s", str);
int code = atoi((char *)ast_command->command->data);
cr_assert(code == 0,
"$? variable should expand to the last exit code (default 0)");
ast_free(&ast);

View file

@ -11,10 +11,10 @@ Test(IO_Backend, init_null)
{
struct iob_context ctx =
{
.iob_mode = IOB_MODE_NULL;
.args = NULL;
.mode = IOB_MODE_NULL,
.args = NULL
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
@ -23,10 +23,10 @@ Test(IO_Backend, init_stdin)
{
struct iob_context ctx =
{
.iob_mode = IOB_MODE_STDIN;
.args = NULL;
.mode = IOB_MODE_STDIN,
.args = NULL
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
@ -36,15 +36,16 @@ iob_close();
// Same applies for other tests
Test(IO_Backend, init_script)
{
char *script_name = "script.tmp" struct iob_context ctx = {
.iob_mode = IOB_MODE_SCRIPT;
.args = script_name;
char *script_name = "script.tmp";
struct iob_context ctx = {
.mode = IOB_MODE_SCRIPT,
.args = script_name
};
// Create file
FILE *f = fopen(script_name, "w");
fclose(f);
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
@ -53,11 +54,12 @@ remove(script_name);
Test(IO_Backend, init_script_not_a_file)
{
char *script_name = "not_a_file.tmp" struct iob_context ctx = {
.iob_mode = IOB_MODE_SCRIPT;
.args = script_name;
char *script_name = "not_a_file.tmp";
struct iob_context ctx = {
.mode = IOB_MODE_SCRIPT,
.args = script_name
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
@ -66,21 +68,22 @@ Test(IO_Backend, init_script_null)
{
struct iob_context ctx =
{
.iob_mode = IOB_MODE_SCRIPT;
.args = NULL;
.mode = IOB_MODE_SCRIPT,
.args = NULL
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_cmd)
{
char *cmd = "iamacommand --yesido" struct iob_context ctx = {
.iob_mode = IOB_MODE_CMD;
.args = cmd;
char *cmd = "iamacommand --yesido";
struct iob_context ctx = {
.mode = IOB_MODE_CMD,
.args = cmd
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
@ -90,23 +93,24 @@ Test(IO_Backend, init_cmd_null)
{
struct iob_context ctx =
{
.iob_mode = IOB_MODE_CMD;
.args = NULL;
.mode = IOB_MODE_CMD,
.args = NULL
};
int actual = iob_init(ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_already_init)
{
char *cmd = "iamacommand --yesido" struct iob_context ctx = {
.iob_mode = IOB_MODE_CMD;
.args = cmd;
char *cmd = "iamacommand --yesido";
struct iob_context ctx = {
.mode = IOB_MODE_CMD,
.args = cmd
};
iob_init(ctx);
int actual = iob_init(ctx);
int expected = IOB_ERROR_ALREADY_INITIALIZED;
iob_init(&ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_MODULE_ALREADY_INITIALIZED;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}

View file

@ -3,7 +3,7 @@ then export BIN_PATH="$(pwd)/42sh"
fi
if [ "$COVERAGE" = "yes" ]; #coverage
then ./testsuite && ../tests/functional/run-tests.sh
then (./testsuite || true) && ../tests/functional/run-tests.sh
else ../tests/functional/run-tests.sh
fi
echo bin path: "$BIN_PATH"