#include "piprocess.h" #include "pitime.h" #include "gtest/gtest.h" class ProcessLauncherTest: public ::testing::Test { protected: PIProcess launcher; PIString command = #ifdef _WIN32 "cmd.exe"; #else "/bin/sh"; #endif void SetUp() override {} void TearDown() override { if (launcher.isRunning()) { launcher.waitForFinish(); } } }; TEST_F(ProcessLauncherTest, Output) { #ifdef _WIN32 PIStringList args = {"/c", "echo Hello from stdout && echo Hello from stderr 1>&2"}; #else PIStringList args = {"-c", "echo Hello from stdout; echo Hello from stderr 1>&2"}; #endif launcher.exec(command, args); ASSERT_TRUE(launcher.isRunning()); piMSleep(100); auto out = PIString::fromConsole(launcher.readOutput()); auto err = PIString::fromConsole(launcher.readError()); EXPECT_TRUE(out.contains("Hello from stdout")); EXPECT_TRUE(err.contains("Hello from stderr")); ASSERT_TRUE(launcher.waitForFinish()); int exit_code = launcher.exitCode(); EXPECT_FALSE(launcher.isRunning()); #ifdef _WIN32 EXPECT_EQ(exit_code, 0); #else EXPECT_TRUE(WIFEXITED(exit_code)); EXPECT_EQ(WEXITSTATUS(exit_code), 0); #endif } TEST_F(ProcessLauncherTest, Input) { #ifdef _WIN32 PIStringList args = {"/c", "set /p input= && echo %input%"}; #else PIStringList args = {"-c", "read input; echo $input"}; #endif launcher.exec(command, args); ASSERT_TRUE(launcher.isRunning()); PIString test_input = "Test input string\n"; EXPECT_TRUE(launcher.writeInput(test_input.toSystem())); launcher.closeInput(); piMSleep(100); auto out = PIString::fromConsole(launcher.readOutput()); EXPECT_TRUE(out.contains("Test input string")); } TEST_F(ProcessLauncherTest, DISABLED_NonexistentCommand) { PIString command = {"nonexistent_command_12345"}; launcher.exec(command); EXPECT_FALSE(launcher.isRunning()); }