feat(expansion): add special var expansion tests $$ and $?

This commit is contained in:
william.valenduc 2026-01-19 18:05:42 +00:00
parent 868b1cae17
commit 913e3bb5f5

View file

@ -3,6 +3,7 @@
#include <criterion/new/assert.h>
#include <criterion/redirect.h>
#include <stdlib.h>
#include <unistd.h>
#include "../../../src/expansion/expansion.h"
#include "../../../src/utils/ast/ast.h"
@ -234,3 +235,43 @@ Test(expand, random)
"RANDOM variable should expand to a value between 0 and 32767");
ast_free(&ast);
}
Test(expand, pid)
{
char str[] = "$$";
char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap);
struct ast *ast = ast_create_command(list);
struct ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init();
vars_default(vars);
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
int pid = atoi((char *)command2->command->data);
cr_assert(pid == getpid(),
"$$ variable should expand to the pid of the process");
ast_free(&ast);
hash_map_free(&vars);
}
Test(expand, default_last_exit_code)
{
char str[] = "$?";
char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap);
struct ast *ast = ast_create_command(list);
struct ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init();
vars_default(vars);
struct ast_command *command2 = expand(ast_command, vars);
cr_assert_not_null(command2, "Expansion returned NULL");
int code = atoi((char *)command2->command->data);
cr_assert(code == 0,
"$? variable should expand to the last exit code (default 0)");
ast_free(&ast);
hash_map_free(&vars);
}