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 <hjk@qt.io>
This commit is contained in:
Alessandro Portale
2021-05-26 18:45:49 +02:00
parent fcd93256c0
commit 642318c325

View File

@@ -30,8 +30,19 @@
#include <QtTest>
#include <iostream>
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<AbstractMacroExpander*> &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<int>("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"