70 lines
2.3 KiB
C++
70 lines
2.3 KiB
C++
#include "test_shell_commander.h"
|
|
#include "wapp.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
WpTestFuncResult test_commander_cmd_success(void) {
|
|
WpStr8List cmd = wpDblList(WpStr8);
|
|
WpStr8 echo = wpStr8Lit("echo");
|
|
WpStr8 msg = wpStr8Lit("hello world");
|
|
wpDblListPushBack(WpStr8, &cmd, &echo);
|
|
wpDblListPushBack(WpStr8, &cmd, &msg);
|
|
|
|
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
|
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
|
result.error == SHELL_ERR_NO_ERROR;
|
|
|
|
return wpTesterResult(succeeded);
|
|
}
|
|
|
|
WpTestFuncResult test_commander_cmd_failure(void) {
|
|
WpStr8List cmd = wpDblList(WpStr8);
|
|
WpStr8 grep = wpStr8Lit("grep");
|
|
wpDblListPushBack(WpStr8, &cmd, &grep);
|
|
|
|
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
|
|
b8 failed = result.exited && result.exit_code != EXIT_SUCCESS &&
|
|
result.error == SHELL_ERR_NO_ERROR;
|
|
|
|
return wpTesterResult(failed);
|
|
}
|
|
|
|
WpTestFuncResult test_commander_cmd_out_buf_success(void) {
|
|
WpStr8 buf = wpStr8Buf(64);
|
|
WpStr8 expected = wpStr8Buf(64);
|
|
char msg[] = "hello world";
|
|
wpStr8CopyCstrCapped(&expected, msg);
|
|
|
|
WpStr8List cmd = wpDblList(WpStr8);
|
|
WpStr8 echo = wpStr8Lit("echo");
|
|
WpStr8 arg = wpStr8Lit(msg);
|
|
wpDblListPushBack(WpStr8, &cmd, &echo);
|
|
wpDblListPushBack(WpStr8, &cmd, &arg);
|
|
|
|
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
|
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
|
|
result.error == SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
|
|
|
|
return wpTesterResult(succeeded);
|
|
}
|
|
|
|
WpTestFuncResult test_commander_cmd_out_buf_failure(void) {
|
|
WpStr8 buf = wpStr8Buf(4);
|
|
WpStr8 expected = wpStr8Buf(64);
|
|
char msg[] = "hello world";
|
|
wpStr8CopyCstrCapped(&expected, msg);
|
|
|
|
WpStr8List cmd = wpDblList(WpStr8);
|
|
WpStr8 echo = wpStr8Lit("echo");
|
|
WpStr8 arg = wpStr8Lit(msg);
|
|
wpDblListPushBack(WpStr8, &cmd, &echo);
|
|
wpDblListPushBack(WpStr8, &cmd, &arg);
|
|
|
|
WpCmdResult result = wpShellCommanderExecute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
|
|
b8 failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
|
|
result.error == SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
|
|
|
|
return wpTesterResult(failed);
|
|
}
|