read and write pipes

This commit is contained in:
2025-08-13 19:18:02 +03:00
parent d4254121b8
commit 154cbd0160
4 changed files with 244 additions and 145 deletions

View File

@@ -1,84 +0,0 @@
#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());
}

View File

@@ -0,0 +1,91 @@
#include "piprocess.h"
#include "pitime.h"
#include "gtest/gtest.h"
class ProcessTest: public ::testing::Test {
protected:
PIProcess launcher;
const PIString command =
#ifdef _WIN32
"cmd.exe";
#else
"/bin/sh";
#endif
void SetUp() override {
launcher.enableWriteStdIn(false);
launcher.enableReadStdOut();
launcher.enableReadStdErr();
}
void TearDown() override {
if (launcher.isRunning()) {
launcher.waitForFinish();
}
}
};
TEST_F(ProcessTest, Output) {
#ifdef _WIN32
const PIStringList args = {"/c", "echo Hello from stdout && echo Hello from stderr 1>&2"};
#else
const PIStringList args = {"-c", "echo Hello from stdout; echo Hello from stderr 1>&2"};
#endif
launcher.exec(command, args);
ASSERT_TRUE(launcher.isRunning());
piMSleep(100);
ASSERT_TRUE(launcher.isExecFinished());
const auto out = PIString::fromConsole(launcher.readOutput());
const auto err = PIString::fromConsole(launcher.readError());
EXPECT_TRUE(out.contains("Hello from stdout"));
EXPECT_TRUE(err.contains("Hello from stderr"));
ASSERT_TRUE(launcher.waitForFinish());
const 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(ProcessTest, Input) {
#ifdef _WIN32
const PIStringList args = {"/c", "set /p input= && echo %input%"};
#else
const PIStringList args = {"-c", "read input; echo $input"};
#endif
launcher.enableWriteStdIn();
launcher.exec(command, args);
ASSERT_TRUE(launcher.isRunning());
const PIString test_input = "Test input string\n";
EXPECT_TRUE(launcher.writeInput(test_input.toSystem()));
launcher.closeInput();
piMSleep(100);
const auto out = PIString::fromConsole(launcher.readOutput());
EXPECT_TRUE(out.contains("Test input string"));
}
TEST_F(ProcessTest, DISABLED_NonexistentCommand) {
const PIString command = {"nonexistent_command_12345"};
launcher.exec(command);
EXPECT_FALSE(launcher.isRunning());
}