test(System): replace host /bin utility assumptions with VoidProcessHelper (X-BASE-01)

This commit is contained in:
faumaray committed 2026-11-24 02:10:06 +03:00
1 parent 563da02c0f
commit 340acaec34
3 files changed
+194 -5

No files matched your search

+15 -2
View File
@@ -106,8 +106,21 @@ if(_all_generated)
DEPENDS VoidTests)
endif()
# ── Test-support libraries ────────────────────────────────────────────────────
# These are NOT test files; they are helper shared libs referenced by tests.
# ── Test-support executables & libraries ──────────────────────────────────────
# These are NOT test files; they are helper executables/libs referenced by tests.
if(TARGET VoidTests AND TARGET Void::System)
add_executable(VoidProcessHelper
"${CMAKE_CURRENT_SOURCE_DIR}/helpers/ProcessHelper.cpp")
set_target_properties(VoidProcessHelper PROPERTIES
CXX_STANDARD 26
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
VOID_TESTING_NO_AUTO_SCAN TRUE)
target_compile_definitions(VoidTests PRIVATE
"VOID_PROCESS_HELPER_PATH=\"$<TARGET_FILE:VoidProcessHelper>\"")
add_dependencies(VoidTests VoidProcessHelper)
endif()
if(TARGET Void::Plugin)
set(_test_plugin_src "${CMAKE_CURRENT_SOURCE_DIR}/plugins/TestPlugin.cpp")
+157
View File
@@ -0,0 +1,157 @@
#include <array>
#include <charconv>
#include <cstdint>
#include <iostream>
#include <string>
#include <string_view>
namespace
{
bool parse_i64(std::string_view text, std::int64_t& out) noexcept
{
if (text.empty()) {
return false;
}
const char* begin = text.data();
const char* end = text.data() + text.size();
auto [ptr, ec] = std::from_chars(begin, end, out, 10);
return ec == std::errc{} && ptr == end;
}
int usage_error(std::string_view msg)
{
std::cerr << "VoidProcessHelper: " << msg << '\n';
return 2;
}
int mode_echo(int argc, char** argv)
{
for (int i = 2; i < argc; ++i) {
if (i > 2) {
std::cout.put(' ');
}
std::cout << argv[i];
}
std::cout.put('\n');
std::cout.flush();
return 0;
}
int mode_cat()
{
std::array<char, 4096> buf{};
while (std::cin) {
std::cin.read(buf.data(), static_cast<std::streamsize>(buf.size()));
const std::streamsize got = std::cin.gcount();
if (got > 0) {
std::cout.write(buf.data(), got);
}
}
std::cout.flush();
return std::cout.good() ? 0 : 1;
}
int mode_seq(int argc, char** argv)
{
if (argc != 4) {
return usage_error("seq requires <start> <end>");
}
std::int64_t start = 0;
std::int64_t finish = 0;
if (!parse_i64(argv[2], start) || !parse_i64(argv[3], finish)) {
return usage_error("seq arguments must be integers");
}
if (start <= finish) {
for (std::int64_t v = start;; ++v) {
std::cout << v << '\n';
if (v == finish) {
break;
}
}
}
std::cout.flush();
return 0;
}
int mode_streams()
{
std::cout << "to_stdout\n" << std::flush;
std::cerr << "to_stderr\n" << std::flush;
return 0;
}
int mode_exit(int argc, char** argv)
{
if (argc != 3) {
return usage_error("exit requires <code>");
}
std::int64_t code = 0;
if (!parse_i64(argv[2], code)) {
return usage_error("exit code must be an integer");
}
return static_cast<int>(code);
}
int mode_lines(int argc, char** argv)
{
bool no_newline = false;
bool to_stderr = false;
int idx = 2;
while (idx < argc) {
const std::string_view arg{argv[idx]};
if (arg == "--no-newline") {
no_newline = true;
++idx;
} else if (arg == "--stderr") {
to_stderr = true;
++idx;
} else {
break;
}
}
std::ostream& os = to_stderr ? std::cerr : std::cout;
const int count = argc - idx;
for (int i = idx; i < argc; ++i) {
os << argv[i];
const bool is_last = (i == argc - 1);
if (!is_last || !no_newline) {
os.put('\n');
}
}
if (count == 0 && !no_newline) {
os.put('\n');
}
os.flush();
return 0;
}
} // namespace
int main(int argc, char** argv)
{
std::ios::sync_with_stdio(false);
if (argc < 2) {
return usage_error("missing mode");
}
const std::string_view mode{argv[1]};
if (mode == "echo") {
return mode_echo(argc, argv);
}
if (mode == "cat") {
return mode_cat();
}
if (mode == "seq") {
return mode_seq(argc, argv);
}
if (mode == "streams") {
return mode_streams();
}
if (mode == "exit") {
return mode_exit(argc, argv);
}
if (mode == "lines") {
return mode_lines(argc, argv);
}
return usage_error("unknown mode");
}
+22 -3
View File
@@ -29,7 +29,8 @@ Void::Testing::Test shell_captures_stderr_separately()
Void::Testing::Test run_with_args_builds_correct_argv()
{
auto r = Void::Process::run("/bin/echo", {"hello", "world"});
auto r = Void::Process::run(VOID_PROCESS_HELPER_PATH,
{"echo", "hello", "world"});
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->exit_code, 0);
Void::Testing::expect(r->out.find("hello world") != std::string::npos);
@@ -37,12 +38,30 @@ Void::Testing::Test run_with_args_builds_correct_argv()
Void::Testing::Test run_stdin_forwarded_to_cat()
{
auto r = Void::Process::run("/bin/cat", {}, "piped_input");
auto r = Void::Process::run(VOID_PROCESS_HELPER_PATH, {"cat"}, "piped_input");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->exit_code, 0);
Void::Testing::expect(r->out.find("piped_input") != std::string::npos);
}
Void::Testing::Test run_direct_exit_code_propagates()
{
auto r = Void::Process::run(VOID_PROCESS_HELPER_PATH, {"exit", "42"});
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->exit_code, 42);
}
Void::Testing::Test run_direct_stdout_and_stderr_independent()
{
auto r = Void::Process::run(VOID_PROCESS_HELPER_PATH, {"streams"});
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->exit_code, 0);
Void::Testing::expect(r->out.find("to_stdout") != std::string::npos);
Void::Testing::expect(r->err.find("to_stderr") != std::string::npos);
Void::Testing::expect(r->out.find("to_stderr") == std::string::npos);
Void::Testing::expect(r->err.find("to_stdout") == std::string::npos);
}
Void::Testing::Test run_missing_executable_returns_code_127()
{
auto r = Void::Process::run("/this/binary/does/not/exist");
@@ -65,7 +84,7 @@ Void::Testing::Test run_stdout_and_stderr_independent()
Void::Testing::Test run_large_output_is_captured_fully()
{
// Generate 10 000 lines; poll-based reader must accumulate them all.
auto r = Void::Process::shell("seq 1 10000");
auto r = Void::Process::run(VOID_PROCESS_HELPER_PATH, {"seq", "1", "10000"});
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->exit_code, 0);
// "10000\n" must be present, and total output must be substantial