fix tests
This commit is contained in:
@@ -21,8 +21,8 @@
|
|||||||
#ifndef MICRO_PIP
|
#ifndef MICRO_PIP
|
||||||
|
|
||||||
# include "piincludes_p.h"
|
# include "piincludes_p.h"
|
||||||
# include "piprocess.h"
|
|
||||||
# include "piliterals_bytes.h"
|
# include "piliterals_bytes.h"
|
||||||
|
# include "piprocess.h"
|
||||||
# ifndef WINDOWS
|
# ifndef WINDOWS
|
||||||
# include <csignal>
|
# include <csignal>
|
||||||
# include <sys/wait.h>
|
# include <sys/wait.h>
|
||||||
@@ -172,15 +172,27 @@ PRIVATE_DEFINITION_START(PIProcess)
|
|||||||
|
|
||||||
|
|
||||||
void closePipe(StdFile pipe_type, PipeDirection direction) {
|
void closePipe(StdFile pipe_type, PipeDirection direction) {
|
||||||
if (grab[pipe_type]) {
|
closePipe(pipes[pipe_type][direction]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void closePipe(PipeHandleType & hpipe) {
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
CloseHandle(pipes[pipe_type][direction]);
|
if (hpipe] != 0) {
|
||||||
pipes[pipe_type][direction] = 0;
|
CloseHandle(hpipe);
|
||||||
|
hpipe = 0;
|
||||||
|
}
|
||||||
# else
|
# else
|
||||||
::close(pipes[pipe_type][direction]);
|
if (hpipe != -1) {
|
||||||
pipes[pipe_type][direction] = -1;
|
::close(hpipe);
|
||||||
|
hpipe = -1;
|
||||||
|
}
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void closeAllPipes() {
|
||||||
|
forEachPipe([this](PipeHandleType & hpipe) { closePipe(hpipe); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -195,9 +207,9 @@ PRIVATE_DEFINITION_START(PIProcess)
|
|||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
BOOL ok = ReadFile(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset, &bytes_read, NULL);
|
BOOL ok = ReadFile(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset, &bytes_read, NULL);
|
||||||
if (!ok) bytes_read = 0;
|
if (!ok) bytes_read = 0;
|
||||||
#else
|
# else
|
||||||
bytes_read = ::read(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset);
|
bytes_read = ::read(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset);
|
||||||
#endif
|
# endif
|
||||||
piCout << "readed" << bytes_read;
|
piCout << "readed" << bytes_read;
|
||||||
if (bytes_read > 0) {
|
if (bytes_read > 0) {
|
||||||
offset += bytes_read;
|
offset += bytes_read;
|
||||||
@@ -214,12 +226,13 @@ PRIVATE_DEFINITION_START(PIProcess)
|
|||||||
|
|
||||||
bool writePipe(const PIByteArray & data) {
|
bool writePipe(const PIByteArray & data) {
|
||||||
SizeType sz = 0;
|
SizeType sz = 0;
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
BOOL ok = WriteFile(pipes[StdIn][PipeWrite], data.data(), data.size(), &sz, NULL);
|
BOOL ok = WriteFile(pipes[StdIn][PipeWrite], data.data(), data.size(), &sz, NULL);
|
||||||
if (!ok) sz = 0;
|
if (!ok) sz = 0;
|
||||||
#else
|
# else
|
||||||
sz = ::write(pipes[StdIn][PipeWrite], data.data(), data.size());
|
sz = ::write(pipes[StdIn][PipeWrite], data.data(), data.size());
|
||||||
# endif
|
# endif
|
||||||
|
piCout << "writePipe" << sz;
|
||||||
return sz == data.size_s();
|
return sz == data.size_s();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,12 +256,14 @@ PIProcess::PIProcess(): PIThread() {
|
|||||||
|
|
||||||
PIProcess::~PIProcess() {
|
PIProcess::~PIProcess() {
|
||||||
PIThread::stopAndWait();
|
PIThread::stopAndWait();
|
||||||
|
PRIVATE->closeAllPipes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::exec_() {
|
void PIProcess::exec_() {
|
||||||
exec_finished = false;
|
exec_finished = false;
|
||||||
exec_start = false;
|
exec_start = false;
|
||||||
|
PRIVATE->closeAllPipes();
|
||||||
startOnce();
|
startOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,7 +336,6 @@ void PIProcess::startProc(bool detached) {
|
|||||||
if (PRIVATE->grab[StdErr]) fcntl(PRIVATE->pipes[StdErr][PipeRead], F_SETFL, O_NONBLOCK);
|
if (PRIVATE->grab[StdErr]) fcntl(PRIVATE->pipes[StdErr][PipeRead], F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
exec_start = true;
|
exec_start = true;
|
||||||
piMinSleep();
|
|
||||||
if (!detached) {
|
if (!detached) {
|
||||||
waitpid(pid_, &exit_code, 0);
|
waitpid(pid_, &exit_code, 0);
|
||||||
exec_finished = true;
|
exec_finished = true;
|
||||||
@@ -387,20 +401,17 @@ void PIProcess::closeInput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::enableWriteStdIn(bool on)
|
void PIProcess::enableWriteStdIn(bool on) {
|
||||||
{
|
|
||||||
PRIVATE->grab[StdIn] = on;
|
PRIVATE->grab[StdIn] = on;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::enableReadStdOut(bool on)
|
void PIProcess::enableReadStdOut(bool on) {
|
||||||
{
|
|
||||||
PRIVATE->grab[StdOut] = on;
|
PRIVATE->grab[StdOut] = on;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::enableReadStdErr(bool on)
|
void PIProcess::enableReadStdErr(bool on) {
|
||||||
{
|
|
||||||
PRIVATE->grab[StdErr] = on;
|
PRIVATE->grab[StdErr] = on;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,17 +38,15 @@ TEST_F(ProcessTest, Output) {
|
|||||||
launcher.exec(command, args);
|
launcher.exec(command, args);
|
||||||
ASSERT_TRUE(launcher.isRunning());
|
ASSERT_TRUE(launcher.isRunning());
|
||||||
|
|
||||||
piMSleep(100);
|
ASSERT_TRUE(launcher.waitForFinish());
|
||||||
|
|
||||||
ASSERT_TRUE(launcher.isExecFinished());
|
ASSERT_TRUE(launcher.isExecFinished());
|
||||||
|
|
||||||
const auto out = PIString::fromConsole(launcher.readOutput());
|
const auto out = PIString::fromAscii(launcher.readOutput());
|
||||||
const auto err = PIString::fromConsole(launcher.readError());
|
const auto err = PIString::fromAscii(launcher.readError());
|
||||||
|
|
||||||
EXPECT_TRUE(out.contains("Hello from stdout"));
|
EXPECT_TRUE(out.contains("Hello from stdout"));
|
||||||
EXPECT_TRUE(err.contains("Hello from stderr"));
|
EXPECT_TRUE(err.contains("Hello from stderr"));
|
||||||
|
|
||||||
ASSERT_TRUE(launcher.waitForFinish());
|
|
||||||
const int exit_code = launcher.exitCode();
|
const int exit_code = launcher.exitCode();
|
||||||
EXPECT_FALSE(launcher.isRunning());
|
EXPECT_FALSE(launcher.isRunning());
|
||||||
|
|
||||||
@@ -71,21 +69,27 @@ TEST_F(ProcessTest, Input) {
|
|||||||
launcher.enableWriteStdIn();
|
launcher.enableWriteStdIn();
|
||||||
launcher.exec(command, args);
|
launcher.exec(command, args);
|
||||||
ASSERT_TRUE(launcher.isRunning());
|
ASSERT_TRUE(launcher.isRunning());
|
||||||
|
piMSleep(100);
|
||||||
|
EXPECT_TRUE(launcher.isExecStarted());
|
||||||
|
EXPECT_TRUE(!launcher.isExecFinished());
|
||||||
|
|
||||||
const PIString test_input = "Test input string\n";
|
const PIString test_input = "Test input string\n";
|
||||||
EXPECT_TRUE(launcher.writeInput(test_input.toSystem()));
|
EXPECT_TRUE(launcher.writeInput(test_input.toAscii()));
|
||||||
launcher.closeInput();
|
launcher.closeInput();
|
||||||
|
|
||||||
piMSleep(100);
|
ASSERT_TRUE(launcher.waitForFinish());
|
||||||
|
EXPECT_TRUE(launcher.isExecFinished());
|
||||||
|
|
||||||
const auto out = PIString::fromConsole(launcher.readOutput());
|
const auto out = PIString::fromAscii(launcher.readOutput());
|
||||||
EXPECT_TRUE(out.contains("Test input string"));
|
EXPECT_TRUE(out.contains("Test input string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_F(ProcessTest, DISABLED_NonexistentCommand) {
|
TEST_F(ProcessTest, NonexistentCommand) {
|
||||||
const PIString command = {"nonexistent_command_12345"};
|
const PIString command = {"nonexistent_command_12345"};
|
||||||
|
|
||||||
launcher.exec(command);
|
launcher.exec(command);
|
||||||
EXPECT_FALSE(launcher.isRunning());
|
ASSERT_TRUE(launcher.isRunning());
|
||||||
|
ASSERT_TRUE(launcher.waitForFinish());
|
||||||
|
EXPECT_FALSE(!launcher.isExecFinished());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user