#include #include #include #include #include namespace { using Void::Testing::Test; struct ProcessStream : Void::Testing::Suite { // stdout lines are delivered to the callback as they arrive, and // the full output is still captured. Test streams_stdout_lines() { std::vector lines; Void::Process::StreamCallbacks cb; cb.on_stdout = [&](std::string_view l) { lines.emplace_back(l); }; auto r = Void::Process::run_streaming( "/bin/sh", {"-c", "printf 'a\\nb\\nc\\n'"}, cb); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(r->exit_code, 0); const std::vector expected{"a", "b", "c"}; Void::Testing::expect(lines == expected); Void::Testing::expect_eq(r->out, std::string{"a\nb\nc\n"}); } // A trailing partial line (no newline) is flushed at EOF. Test flushes_trailing_partial_line() { std::vector lines; Void::Process::StreamCallbacks cb; cb.on_stdout = [&](std::string_view l) { lines.emplace_back(l); }; auto r = Void::Process::run_streaming( "/bin/sh", {"-c", "printf 'no-newline'"}, cb); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(lines.size(), std::size_t{1}); Void::Testing::expect_eq(lines.at(0), std::string{"no-newline"}); } // stderr is routed to its own callback. Test streams_stderr_separately() { std::vector errs; Void::Process::StreamCallbacks cb; cb.on_stderr = [&](std::string_view l) { errs.emplace_back(l); }; auto r = Void::Process::run_streaming( "/bin/sh", {"-c", "printf 'oops\\n' 1>&2"}, cb); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(errs.size(), std::size_t{1}); Void::Testing::expect_eq(errs.at(0), std::string{"oops"}); } }; } // namespace