fix tests

This commit is contained in:
2025-08-13 19:50:47 +03:00
parent 154cbd0160
commit 3625afa783
2 changed files with 53 additions and 38 deletions

View File

@@ -21,8 +21,8 @@
#ifndef MICRO_PIP
# include "piincludes_p.h"
# include "piprocess.h"
# include "piliterals_bytes.h"
# include "piprocess.h"
# ifndef WINDOWS
# include <csignal>
# include <sys/wait.h>
@@ -82,9 +82,9 @@ constexpr int StdFileCount = StdLast + 1;
# ifdef WINDOWS
using PipeHandleType = HANDLE;
using SizeType = DWORD;
using SizeType = DWORD;
# else
using SizeType = ssize_t;
using SizeType = ssize_t;
using PipeHandleType = int;
# endif
@@ -142,8 +142,8 @@ PRIVATE_DEFINITION_START(PIProcess)
const int pt = pipe_type;
# ifdef WINDOWS
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
satrueAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&(pipes[pt][PipeRead]), &(pipes[pt][PipeWrite]), &saAttr, 0)) return false;
return true;
@@ -172,15 +172,27 @@ PRIVATE_DEFINITION_START(PIProcess)
void closePipe(StdFile pipe_type, PipeDirection direction) {
if (grab[pipe_type]) {
closePipe(pipes[pipe_type][direction]);
}
void closePipe(PipeHandleType & hpipe) {
# ifdef WINDOWS
CloseHandle(pipes[pipe_type][direction]);
pipes[pipe_type][direction] = 0;
# else
::close(pipes[pipe_type][direction]);
pipes[pipe_type][direction] = -1;
# endif
if (hpipe] != 0) {
CloseHandle(hpipe);
hpipe = 0;
}
# else
if (hpipe != -1) {
::close(hpipe);
hpipe = -1;
}
# endif
}
void closeAllPipes() {
forEachPipe([this](PipeHandleType & hpipe) { closePipe(hpipe); });
}
@@ -190,14 +202,14 @@ PRIVATE_DEFINITION_START(PIProcess)
PIByteArray read_buffer;
read_buffer.resize(read_buffer_size);
SizeType bytes_read = 0;
size_t offset = 0;
size_t offset = 0;
while (1) {
# ifdef WINDOWS
BOOL ok = ReadFile(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset, &bytes_read, NULL);
if (!ok) bytes_read = 0;
#else
# else
bytes_read = ::read(pipes[pipe_type][PipeRead], read_buffer.data(offset), read_buffer.size() - offset);
#endif
# endif
piCout << "readed" << bytes_read;
if (bytes_read > 0) {
offset += bytes_read;
@@ -214,12 +226,13 @@ PRIVATE_DEFINITION_START(PIProcess)
bool writePipe(const PIByteArray & data) {
SizeType sz = 0;
# ifdef WINDOWS
# ifdef WINDOWS
BOOL ok = WriteFile(pipes[StdIn][PipeWrite], data.data(), data.size(), &sz, NULL);
if (!ok) sz = 0;
#else
# else
sz = ::write(pipes[StdIn][PipeWrite], data.data(), data.size());
# endif
# endif
piCout << "writePipe" << sz;
return sz == data.size_s();
}
@@ -234,7 +247,7 @@ PIProcess::PIProcess(): PIThread() {
PRIVATE->pid = 0;
PRIVATE->forEachPipe([](PipeHandleType & pipe) { pipe = -1; });
# endif
exec_start = false;
exec_start = false;
exec_finished = false;
PRIVATE->initGrab();
env = PIProcess::currentEnvironment();
@@ -243,12 +256,14 @@ PIProcess::PIProcess(): PIThread() {
PIProcess::~PIProcess() {
PIThread::stopAndWait();
PRIVATE->closeAllPipes();
}
void PIProcess::exec_() {
exec_finished = false;
exec_start = false;
exec_start = false;
PRIVATE->closeAllPipes();
startOnce();
}
@@ -321,11 +336,10 @@ void PIProcess::startProc(bool detached) {
if (PRIVATE->grab[StdErr]) fcntl(PRIVATE->pipes[StdErr][PipeRead], F_SETFL, O_NONBLOCK);
exec_start = true;
piMinSleep();
if (!detached) {
waitpid(pid_, &exit_code, 0);
exec_finished = true;
pid_ = 0;
pid_ = 0;
if (!detached) PRIVATE->pid = pid_;
}
}
@@ -387,20 +401,17 @@ void PIProcess::closeInput() {
}
void PIProcess::enableWriteStdIn(bool on)
{
void PIProcess::enableWriteStdIn(bool on) {
PRIVATE->grab[StdIn] = on;
}
void PIProcess::enableReadStdOut(bool on)
{
void PIProcess::enableReadStdOut(bool on) {
PRIVATE->grab[StdOut] = on;
}
void PIProcess::enableReadStdErr(bool on)
{
void PIProcess::enableReadStdErr(bool on) {
PRIVATE->grab[StdErr] = on;
}