forked from qt-creator/qt-creator
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:
@@ -30,8 +30,19 @@
|
|||||||
|
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace Utils;
|
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 {
|
class MacroMapExpander : public AbstractMacroExpander {
|
||||||
public:
|
public:
|
||||||
virtual bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander*> &seen)
|
virtual bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander*> &seen)
|
||||||
@@ -73,6 +84,8 @@ private slots:
|
|||||||
void iterations();
|
void iterations();
|
||||||
void iteratorEditsWindows();
|
void iteratorEditsWindows();
|
||||||
void iteratorEditsLinux();
|
void iteratorEditsLinux();
|
||||||
|
void exitCode_data();
|
||||||
|
void exitCode();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void iteratorEditsHelper(OsType osType);
|
void iteratorEditsHelper(OsType osType);
|
||||||
@@ -88,6 +101,9 @@ private:
|
|||||||
|
|
||||||
void tst_QtcProcess::initTestCase()
|
void tst_QtcProcess::initTestCase()
|
||||||
{
|
{
|
||||||
|
if (qEnvironmentVariableIsSet(kExitCodeSubProcessCode))
|
||||||
|
exitCodeSubProcessMain();
|
||||||
|
|
||||||
homeStr = QLatin1String("@HOME@");
|
homeStr = QLatin1String("@HOME@");
|
||||||
home = QDir::homePath();
|
home = QDir::homePath();
|
||||||
|
|
||||||
@@ -743,6 +759,50 @@ void tst_QtcProcess::iteratorEditsLinux()
|
|||||||
iteratorEditsHelper(OsTypeLinux);
|
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)
|
QTEST_MAIN(tst_QtcProcess)
|
||||||
|
|
||||||
#include "tst_qtcprocess.moc"
|
#include "tst_qtcprocess.moc"
|
||||||
|
Reference in New Issue
Block a user