Compare commits

..

2 Commits

Author SHA1 Message Date
8a91a0ec6b Add tests for the shell commander 2024-06-17 00:15:10 +01:00
802b70f5ee Add option to discard command output 2024-06-17 00:14:34 +01:00
5 changed files with 85 additions and 7 deletions

View File

@ -101,7 +101,7 @@ internal inline CMDError get_command_output(FILE *fp,
} }
strcat(out_buf, out); strcat(out_buf, out);
} else { } else if (out_handling == SHELL_OUTPUT_PRINT) {
printf("%s", out); printf("%s", out);
} }
} }

View File

@ -10,11 +10,12 @@ extern "C" {
#endif // __cplusplus #endif // __cplusplus
#define CMD_NO_EXIT(ERR) \ #define CMD_NO_EXIT(ERR) \
((CMDResult){.exited = false, .exit_code = EXIT_FAILURE}) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR})
#define wapp_shell_commander_execute(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, ...) \ #define wapp_shell_commander_execute(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, ...) \
run_command(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, __VA_ARGS__, NULL) run_command(HANDLE_OUTPUT, OUT_BUF, BUF_SIZE, __VA_ARGS__, NULL)
typedef enum { typedef enum {
SHELL_OUTPUT_DISCARD,
SHELL_OUTPUT_PRINT, SHELL_OUTPUT_PRINT,
SHELL_OUTPUT_CAPTURE, SHELL_OUTPUT_CAPTURE,
} CMDOutHandling; } CMDOutHandling;

View File

@ -0,0 +1,55 @@
#include "test_shell_commander.h"
#include "commander.h"
#include "tester.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
TestFuncResult test_commander_cmd_success(void) {
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0,
"echo", "hello world");
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_failure(void) {
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0,
"grep", "2>&1");
bool failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(failed);
}
TestFuncResult test_commander_cmd_out_buf_success(void) {
char buf[64] = {0};
char expected_output[64] = {0};
const char *msg = "hello world";
sprintf(expected_output, "%s\n", msg);
CMDResult result =
wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR &&
strcmp(buf, expected_output) == 0;
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_out_buf_failure(void) {
char buf[4] = {0};
const char *msg = "hello world";
CMDResult result =
wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg);
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0;
return wapp_tester_result(failed);
}

View File

@ -0,0 +1,19 @@
#ifndef TEST_SHELL_COMMANDER_H
#define TEST_SHELL_COMMANDER_H
#include "tester.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
TestFuncResult test_commander_cmd_success(void);
TestFuncResult test_commander_cmd_failure(void);
TestFuncResult test_commander_cmd_out_buf_success(void);
TestFuncResult test_commander_cmd_out_buf_failure(void);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !TEST_SHELL_COMMANDER_H

View File

@ -1,13 +1,16 @@
#include "test_arena.h" #include "test_arena.h"
#include "test_shell_commander.h"
#include "tester.h" #include "tester.h"
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
wapp_tester_run_tests(test_arena_init, wapp_tester_run_tests(
test_arena_init_succeeds_when_reserving_very_large_size, test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size,
test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_succeeds_when_within_capacity,
test_arena_alloc_fails_when_over_capacity, test_arena_alloc_fails_when_over_capacity, test_arena_clear,
test_arena_clear, test_arena_destroy); test_arena_destroy, test_commander_cmd_success,
test_commander_cmd_failure, test_commander_cmd_out_buf_success,
test_commander_cmd_out_buf_failure);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }