diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b381b8f..4d6fa33 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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=\"$\"") + add_dependencies(VoidTests VoidProcessHelper) +endif() if(TARGET Void::Plugin) set(_test_plugin_src "${CMAKE_CURRENT_SOURCE_DIR}/plugins/TestPlugin.cpp") diff --git a/tests/helpers/ProcessHelper.cpp b/tests/helpers/ProcessHelper.cpp new file mode 100644 index 0000000..e1e568c --- /dev/null +++ b/tests/helpers/ProcessHelper.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include + +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 buf{}; + while (std::cin) { + std::cin.read(buf.data(), static_cast(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 "); + } + 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 "); + } + std::int64_t code = 0; + if (!parse_i64(argv[2], code)) { + return usage_error("exit code must be an integer"); + } + return static_cast(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"); +} diff --git a/tests/standalone/System/Process.cpp b/tests/standalone/System/Process.cpp index 919f902..78b970f 100644 --- a/tests/standalone/System/Process.cpp +++ b/tests/standalone/System/Process.cpp @@ -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