Process: Add a test for reading the errorChannel

Change-Id: I3b1d35c8ef2be91ddd1591a0fa2dafedc268f916
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2023-12-06 13:10:05 +01:00
parent b6d92b3a0c
commit 1ea1ea7747
3 changed files with 33 additions and 5 deletions

View File

@@ -97,9 +97,21 @@ static void doCrash()
qFatal("The application has crashed purposefully!");
}
static int envVarIntWithDefault(const char *varName, int defaultValue)
{
bool ok = false;
const int result = qEnvironmentVariableIntValue(varName, &ok);
return ok ? result : defaultValue;
}
int ProcessTestApp::SimpleTest::main()
{
std::cout << s_simpleTestData << std::endl;
const QProcess::ProcessChannel processChannel
= QProcess::ProcessChannel(envVarIntWithDefault(envVar(), 0));
if (processChannel == QProcess::StandardOutput)
std::cout << s_outputData << std::endl;
else
std::cerr << s_errorData << std::endl;
return 0;
}

View File

@@ -81,7 +81,6 @@ private:
const Utils::Environment m_environment;
};
static const char s_simpleTestData[] = "Test process successfully executed.";
static const char s_runBlockingStdOutSubProcessMagicWord[] = "42";
// Expect ending lines detected at '|':

View File

@@ -153,6 +153,7 @@ private slots:
void mergedChannels();
void destroyBlockingProcess_data();
void destroyBlockingProcess();
void flushFinishedWhileWaitingForReadyRead_data();
void flushFinishedWhileWaitingForReadyRead();
void crash();
void crashAfterOneSecond();
@@ -1269,9 +1270,22 @@ void tst_Process::destroyBlockingProcess()
QVERIFY(!process.waitForFinished(1000));
}
void tst_Process::flushFinishedWhileWaitingForReadyRead_data()
{
QTest::addColumn<QProcess::ProcessChannel>("processChannel");
QTest::addColumn<QByteArray>("expectedData");
QTest::newRow("StandardOutput") << QProcess::StandardOutput << QByteArray(s_outputData);
QTest::newRow("StandardError") << QProcess::StandardError << QByteArray(s_errorData);
}
void tst_Process::flushFinishedWhileWaitingForReadyRead()
{
SubProcessConfig subConfig(ProcessTestApp::SimpleTest::envVar(), {});
QFETCH(QProcess::ProcessChannel, processChannel);
QFETCH(QByteArray, expectedData);
SubProcessConfig subConfig(ProcessTestApp::SimpleTest::envVar(),
QString::number(int(processChannel)));
Process process;
subConfig.setupSubProcess(&process);
@@ -1284,14 +1298,17 @@ void tst_Process::flushFinishedWhileWaitingForReadyRead()
QByteArray reply;
while (process.state() == QProcess::Running) {
process.waitForReadyRead(500);
reply += process.readAllRawStandardOutput();
if (processChannel == QProcess::StandardOutput)
reply += process.readAllRawStandardOutput();
else
reply += process.readAllRawStandardError();
if (timer.hasExpired())
break;
}
QCOMPARE(process.state(), QProcess::NotRunning);
QVERIFY(!timer.hasExpired());
QVERIFY(reply.contains(s_simpleTestData));
QVERIFY(reply.contains(expectedData));
}
void tst_Process::crash()