2026-01-12 19:30:11 +00:00
|
|
|
#include "../../../src/utils/args/args.h"
|
|
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
#include <criterion/new/assert.h>
|
|
|
|
|
#include <criterion/redirect.h>
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
#include "../../../src/utils/hash_map/hash_map.h"
|
|
|
|
|
#include "../../../src/utils/vars/vars.h"
|
|
|
|
|
|
2026-01-12 19:30:11 +00:00
|
|
|
TestSuite(utils_args);
|
|
|
|
|
|
|
|
|
|
Test(utils_args, basic_command)
|
|
|
|
|
{
|
|
|
|
|
int argc = 3;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program", "-c", "echo Hello, World!" };
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == false);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == false);
|
|
|
|
|
cr_expect(options.type == INPUT_CMD);
|
|
|
|
|
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, basic_command_with_flags)
|
|
|
|
|
{
|
|
|
|
|
int argc = 5;
|
|
|
|
|
struct args_options options;
|
2026-01-31 15:40:46 +01:00
|
|
|
/* char *input[] = { "program", "--pretty-print", "-c", "echo Hello,
|
|
|
|
|
World!",
|
|
|
|
|
"--verbose" };*/
|
|
|
|
|
char *input[] = { "program", "-c", "echo Hello, World!", "--verbose" };
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == true);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == true);
|
|
|
|
|
cr_expect(options.type == INPUT_CMD);
|
|
|
|
|
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, basic_file_input)
|
|
|
|
|
{
|
|
|
|
|
int argc = 2;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program", "input.txt" };
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == false);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == false);
|
|
|
|
|
cr_expect(options.type == INPUT_FILE);
|
|
|
|
|
cr_expect(eq(options.input_source, "input.txt"));
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, basic_file_input_with_flags)
|
|
|
|
|
{
|
|
|
|
|
int argc = 4;
|
|
|
|
|
struct args_options options;
|
2026-01-31 15:40:46 +01:00
|
|
|
// char *input[] = { "program", "--verbose", "input.txt", "--pretty-print"
|
|
|
|
|
// };
|
|
|
|
|
char *input[] = { "program", "--verbose", "input.txt" };
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == true);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == true);
|
|
|
|
|
cr_expect(options.type == INPUT_FILE);
|
|
|
|
|
cr_expect(eq(options.input_source, "input.txt"));
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, basic_stdin_input)
|
|
|
|
|
{
|
|
|
|
|
int argc = 1;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program" };
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == false);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == false);
|
|
|
|
|
cr_expect(options.type == INPUT_STDIN);
|
|
|
|
|
cr_expect(options.input_source == NULL);
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, pretty_print_and_verbose_flags)
|
|
|
|
|
{
|
|
|
|
|
int argc = 3;
|
|
|
|
|
struct args_options options;
|
2026-01-31 15:40:46 +01:00
|
|
|
// char *input[] = { "program", "--pretty-print", "--verbose" };
|
|
|
|
|
char *input[] = { "program", "--verbose" };
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
2026-01-31 15:40:46 +01:00
|
|
|
// cr_expect(options.pretty_print == true);
|
2026-01-12 19:30:11 +00:00
|
|
|
cr_expect(options.verbose == true);
|
|
|
|
|
cr_expect(options.type == INPUT_STDIN);
|
|
|
|
|
cr_expect(options.input_source == NULL);
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, missing_command_after_c, .init = cr_redirect_stderr)
|
|
|
|
|
{
|
|
|
|
|
int argc = 2;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program", "-c" };
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r != 0);
|
|
|
|
|
cr_assert_stderr_eq_str("No command provided after -c\n");
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, unknown_option, .init = cr_redirect_stderr)
|
|
|
|
|
{
|
|
|
|
|
int argc = 2;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program", "--unknown" };
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r != 0);
|
|
|
|
|
cr_assert_stderr_eq_str("Unknown option: --unknown\n");
|
2026-01-23 17:05:56 +00:00
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
Test(utils_args, multiple_command, .init = cr_redirect_stderr)
|
2026-01-12 19:30:11 +00:00
|
|
|
{
|
|
|
|
|
int argc = 4;
|
|
|
|
|
struct args_options options;
|
2026-01-23 17:05:56 +00:00
|
|
|
char *input[] = { "program", "exec.sh", "-c", "echo World" };
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
cr_expect(r != 0);
|
2026-01-23 17:05:56 +00:00
|
|
|
cr_assert_stderr_eq_str("Multiple input sources specified: echo World\n");
|
|
|
|
|
hash_map_free(&vars);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(utils_args, command_with_additional_arguments)
|
|
|
|
|
{
|
|
|
|
|
int argc = 5;
|
|
|
|
|
struct args_options options;
|
|
|
|
|
char *input[] = { "program", "-c", "echo Hello", "arg1", "arg2" };
|
|
|
|
|
|
|
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
int r = args_handler(argc, input, &options, vars);
|
|
|
|
|
|
|
|
|
|
cr_expect(r == 0);
|
|
|
|
|
cr_expect(options.type == INPUT_CMD);
|
|
|
|
|
cr_expect(eq(options.input_source, "echo Hello"));
|
|
|
|
|
cr_expect_str_eq(get_var(vars, "0"), "program");
|
|
|
|
|
cr_expect_str_eq(get_var(vars, "1"), "arg1");
|
|
|
|
|
cr_expect_str_eq(get_var(vars, "2"), "arg2");
|
|
|
|
|
hash_map_free(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|