From 1ea1ea77477247bd0358c4e52b30c9b052f2139e Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Wed, 6 Dec 2023 13:10:05 +0100 Subject: [PATCH] Process: Add a test for reading the errorChannel Change-Id: I3b1d35c8ef2be91ddd1591a0fa2dafedc268f916 Reviewed-by: hjk Reviewed-by: Qt CI Bot --- .../process/processtestapp/processtestapp.cpp | 14 ++++++++++- .../process/processtestapp/processtestapp.h | 1 - tests/auto/utils/process/tst_process.cpp | 23 ++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/tests/auto/utils/process/processtestapp/processtestapp.cpp b/tests/auto/utils/process/processtestapp/processtestapp.cpp index d85288514ea..d9a10f31faa 100644 --- a/tests/auto/utils/process/processtestapp/processtestapp.cpp +++ b/tests/auto/utils/process/processtestapp/processtestapp.cpp @@ -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; } diff --git a/tests/auto/utils/process/processtestapp/processtestapp.h b/tests/auto/utils/process/processtestapp/processtestapp.h index adc0ebb9c88..4c7e84e3028 100644 --- a/tests/auto/utils/process/processtestapp/processtestapp.h +++ b/tests/auto/utils/process/processtestapp/processtestapp.h @@ -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 '|': diff --git a/tests/auto/utils/process/tst_process.cpp b/tests/auto/utils/process/tst_process.cpp index 0e46f7c0940..c8b1345b3a5 100644 --- a/tests/auto/utils/process/tst_process.cpp +++ b/tests/auto/utils/process/tst_process.cpp @@ -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("processChannel"); + QTest::addColumn("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()