read and write pipes
This commit is contained in:
@@ -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());
|
||||
}
|
||||
91
tests/system/process_test.cpp
Normal file
91
tests/system/process_test.cpp
Normal 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());
|
||||
}
|
||||
Reference in New Issue
Block a user