2026-06-18 19:32:27 -05:00
|
|
|
#include "test_support.hpp"
|
|
|
|
|
|
2026-06-18 19:40:23 -05:00
|
|
|
#include <izo/Process.hpp>
|
2026-06-18 19:32:27 -05:00
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
std::string marker;
|
2026-06-18 19:40:23 -05:00
|
|
|
auto result = izo::LaunchProcess({});
|
2026-06-18 19:32:27 -05:00
|
|
|
CHECK(!result);
|
2026-06-18 19:40:23 -05:00
|
|
|
CHECK(!result.errorMessage.empty());
|
2026-06-18 19:32:27 -05:00
|
|
|
|
2026-06-18 19:40:23 -05:00
|
|
|
result = izo::LaunchProcess({"/izo/missing-executable", {}, {}, false});
|
2026-06-18 19:32:27 -05:00
|
|
|
CHECK(!result);
|
2026-06-18 19:40:23 -05:00
|
|
|
CHECK(!result.errorMessage.empty());
|
2026-06-18 19:32:27 -05:00
|
|
|
|
|
|
|
|
const auto directory = test_directory("process");
|
2026-06-18 19:40:23 -05:00
|
|
|
result = izo::LaunchProcess({"/bin/sh", {"-c", "printf launched > marker.txt"}, directory, false});
|
2026-06-18 19:32:27 -05:00
|
|
|
CHECK(result);
|
2026-06-18 19:40:23 -05:00
|
|
|
CHECK(result.processId > 0);
|
2026-06-18 19:32:27 -05:00
|
|
|
CHECK(wait_until([&] { return std::filesystem::exists(directory / "marker.txt"); }));
|
|
|
|
|
std::ifstream(directory / "marker.txt") >> marker;
|
|
|
|
|
CHECK(marker == "launched");
|
|
|
|
|
|
2026-06-18 19:40:23 -05:00
|
|
|
result = izo::LaunchProcess({"/bin/true", {}, directory / "missing", false});
|
2026-06-18 19:32:27 -05:00
|
|
|
CHECK(!result);
|
2026-06-18 19:40:23 -05:00
|
|
|
CHECK(!result.errorMessage.empty());
|
2026-06-18 21:16:23 -05:00
|
|
|
|
|
|
|
|
izo::ProcessOptions run_options;
|
|
|
|
|
run_options.executable = "/bin/sh";
|
|
|
|
|
run_options.arguments = {"-c", "printf stdout; printf stderr >&2; exit 7"};
|
|
|
|
|
run_options.captureOutput = true;
|
|
|
|
|
run_options.timeoutMs = 5000;
|
|
|
|
|
const auto run = izo::RunProcess(run_options);
|
|
|
|
|
CHECK(run.started);
|
|
|
|
|
CHECK(!run.timedOut);
|
|
|
|
|
CHECK(run.exitCode == 7);
|
|
|
|
|
CHECK(run.stdoutText == "stdout");
|
|
|
|
|
CHECK(run.stderrText == "stderr");
|
|
|
|
|
|
|
|
|
|
izo::ProcessOptions pipe_options;
|
|
|
|
|
pipe_options.executable = "/bin/cat";
|
|
|
|
|
pipe_options.captureOutput = true;
|
|
|
|
|
pipe_options.pipeStdin = true;
|
|
|
|
|
auto process = izo::StartProcess(pipe_options);
|
|
|
|
|
CHECK(process);
|
|
|
|
|
CHECK(process.Id() > 0);
|
|
|
|
|
CHECK(process.IsRunning());
|
|
|
|
|
CHECK(izo::IsProcessRunning(process.Id()));
|
|
|
|
|
CHECK(process.WriteStdin("input through pipe"));
|
|
|
|
|
CHECK(process.CloseStdin());
|
|
|
|
|
CHECK(process.Wait(5000));
|
|
|
|
|
CHECK(process.ExitCode() == 0);
|
|
|
|
|
CHECK(process.Stdout() == "input through pipe");
|
|
|
|
|
CHECK(process.Stderr().empty());
|
|
|
|
|
CHECK(!process.IsRunning());
|
|
|
|
|
|
|
|
|
|
izo::ProcessOptions timeout_options;
|
|
|
|
|
timeout_options.executable = "/bin/sh";
|
|
|
|
|
timeout_options.arguments = {"-c", "sleep 10"};
|
|
|
|
|
timeout_options.timeoutMs = 20;
|
|
|
|
|
const auto timeout = izo::RunProcess(timeout_options);
|
|
|
|
|
CHECK(timeout.started);
|
|
|
|
|
CHECK(timeout.timedOut);
|
|
|
|
|
CHECK(timeout.exitCode != 0);
|
|
|
|
|
|
|
|
|
|
izo::ProcessOptions output_options;
|
|
|
|
|
output_options.executable = "/bin/sh";
|
|
|
|
|
output_options.arguments = {"-c", "yes x | head -c 200000"};
|
|
|
|
|
output_options.captureOutput = true;
|
|
|
|
|
output_options.timeoutMs = 5000;
|
|
|
|
|
const auto output = izo::RunProcess(output_options);
|
|
|
|
|
CHECK(output);
|
|
|
|
|
CHECK(output.stdoutText.size() == 200000);
|
|
|
|
|
|
|
|
|
|
CHECK(izo::GetCurrentProcessId() > 0);
|
|
|
|
|
CHECK(izo::GetParentProcessId() > 0);
|
|
|
|
|
CHECK(izo::IsProcessRunning(izo::GetCurrentProcessId()));
|
|
|
|
|
CHECK(!izo::IsProcessRunning(0));
|
2026-06-18 19:32:27 -05:00
|
|
|
std::filesystem::remove_all(directory);
|
|
|
|
|
}
|