Update shell_commander to use Str8 instead of const char *

This commit is contained in:
2025-02-16 21:31:22 +00:00
parent 2c9e4c91a0
commit 11949e69be
4 changed files with 75 additions and 77 deletions

View File

@@ -1,5 +1,6 @@
#include "test_shell_commander.h"
#include "commander.h"
#include "str8.h"
#include "tester.h"
#include <stdbool.h>
#include <stdio.h>
@@ -7,7 +8,11 @@
#include <string.h>
TestFuncResult test_commander_cmd_success(void) {
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, "echo", "hello world");
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("hello world"));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
@@ -15,7 +20,10 @@ TestFuncResult test_commander_cmd_success(void) {
}
TestFuncResult test_commander_cmd_failure(void) {
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, "grep");
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("grep"));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd);
bool failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
@@ -23,27 +31,36 @@ TestFuncResult test_commander_cmd_failure(void) {
}
TestFuncResult test_commander_cmd_out_buf_success(void) {
char buf[64] = {0};
char expected_output[64] = {0};
const char *msg = "hello world";
u64 length = strlen(msg);
snprintf(expected_output, length + 2, "%s\n", msg);
Str8 buf = wapp_str8_buf(64);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
wapp_str8_concat_capped(&expected, &wapp_str8_lit_ro("\n"));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg);
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR &&
strncmp(buf, expected_output, length) == 0;
result.error == SHELL_ERR_NO_ERROR && wapp_str8_equal(&buf, &expected);
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_out_buf_failure(void) {
char buf[4] = {0};
const char *msg = "hello world";
u64 length = strlen(msg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg);
Str8 buf = wapp_str8_buf(4);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
Str8List cmd = {0};
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo"));
wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg));
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && strncmp(buf, msg, length) != 0;
result.error == SHELL_ERR_OUT_BUF_FULL && !wapp_str8_equal(&buf, &expected);
return wapp_tester_result(failed);
}