From 642318c325c0c02485dc444d774ca9df2438f6bc Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Wed, 26 May 2021 18:45:49 +0200 Subject: [PATCH] Utils: Add an auto test for QtcProcess::exitCode() Exit code handling can break during refactorings. This addition to tst_QtcProcess tests negative, zero and positive exit codes of sub prcesses that are launched asynchronously and synchronously. Change-Id: Icbcc036d3fda7fa681958b57fd0a6f67d3a90352 Reviewed-by: hjk --- tests/auto/qtcprocess/tst_qtcprocess.cpp | 60 ++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/auto/qtcprocess/tst_qtcprocess.cpp b/tests/auto/qtcprocess/tst_qtcprocess.cpp index 6d39b71738c..ad1d95debe3 100644 --- a/tests/auto/qtcprocess/tst_qtcprocess.cpp +++ b/tests/auto/qtcprocess/tst_qtcprocess.cpp @@ -30,8 +30,19 @@ #include +#include + using namespace Utils; +const char kExitCodeSubProcessCode[] = "QTC_TST_QTCPROCESS_EXITCODE_CODE"; + +static void exitCodeSubProcessMain() +{ + const int exitCode = qEnvironmentVariableIntValue(kExitCodeSubProcessCode); + std::cout << "Exiting with code:" << exitCode << std::endl; + exit(exitCode); +} + class MacroMapExpander : public AbstractMacroExpander { public: virtual bool resolveMacro(const QString &name, QString *ret, QSet &seen) @@ -73,6 +84,8 @@ private slots: void iterations(); void iteratorEditsWindows(); void iteratorEditsLinux(); + void exitCode_data(); + void exitCode(); private: void iteratorEditsHelper(OsType osType); @@ -88,6 +101,9 @@ private: void tst_QtcProcess::initTestCase() { + if (qEnvironmentVariableIsSet(kExitCodeSubProcessCode)) + exitCodeSubProcessMain(); + homeStr = QLatin1String("@HOME@"); home = QDir::homePath(); @@ -743,6 +759,50 @@ void tst_QtcProcess::iteratorEditsLinux() iteratorEditsHelper(OsTypeLinux); } +void tst_QtcProcess::exitCode_data() +{ + QTest::addColumn("exitCode"); + + static const auto exitCodes = { +#ifdef Q_OS_WIN + "99999999", "-255", "-1", +#endif // Q_OS_WIN + "0", "1", "255" + }; + for (auto exitCode : exitCodes) + QTest::newRow(exitCode) << QString::fromLatin1(exitCode).toInt(); +} + +void tst_QtcProcess::exitCode() +{ + QFETCH(int, exitCode); + + Environment env = Environment::systemEnvironment(); + env.set(kExitCodeSubProcessCode, QString::number(exitCode)); + QStringList args = QCoreApplication::arguments(); + const QString binary = args.takeFirst(); + const CommandLine command(binary, args); + + { + QtcProcess qtcP; + qtcP.setCommand(command); + qtcP.setEnvironment(env); + qtcP.start(); + const bool finished = qtcP.waitForFinished(); + + QVERIFY(finished); + QCOMPARE(qtcP.exitCode(), exitCode); + } + { + SynchronousProcess sP; + sP.setCommand(command); + sP.setEnvironment(env); + sP.runBlocking(); + + QCOMPARE(sP.exitCode(), exitCode); + } +} + QTEST_MAIN(tst_QtcProcess) #include "tst_qtcprocess.moc"