process.cpp 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**
 * @file process.cpp
 * @copyright 2013 Jolla Ltd.
 * @author Martin Kampas <martin.kampas@tieto.com>
 * @date 2013
 */

#include "process.h"

/**
 * @class Wraps QProcess for easier use within test code
 *
 * Example use:
 *
 * @code
 * Process ssu;
 * const QString output = ssu.execute("ssu", QStringList() << "mode");
 * QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
 *
 * QCOMPARE(output, "...");
 * @endcode
 */

Process::Process() : m_expectFail(false), m_timedOut(false) {}

QString Process::execute(const QString &program, const QStringList &arguments,
27 28 29 30 31 32 33 34 35
                         bool expectedResult)
{
    Q_ASSERT(m_process.state() == QProcess::NotRunning);
    m_program = program;
    m_arguments = arguments;
    m_expectFail = expectedResult == ExpectFail;
    m_process.start(program, arguments);
    m_timedOut = !m_process.waitForFinished();
    return m_process.readAllStandardOutput();
36 37
}

38 39 40 41 42 43
bool Process::hasError()
{
    return m_timedOut
           || m_process.error() != QProcess::UnknownError
           || m_process.exitStatus() != QProcess::NormalExit
           || (m_process.exitCode() != 0) != m_expectFail;
44 45
}

46 47 48 49
QString Process::fmtErrorMessage()
{
    if (!hasError()) {
        return QString();
50
    }
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    QStringList reasons;
    if (m_timedOut) {
        reasons.append("Timed out");
    } else if (m_process.exitStatus() != QProcess::NormalExit) {
        reasons.append("Process crashed");
    } else if (m_expectFail && (m_process.exitCode() == 0)) {
        reasons.append("Did not fail while it was expected to");
    } else {
        if (m_process.error() != QProcess::UnknownError) {
            reasons.append(m_process.errorString());
        }
        const QString errorOut = m_process.readAllStandardError();
        if (!errorOut.isEmpty()) {
            reasons.append(errorOut);
        }
67 68
    }

69 70 71 72
    return QString("Failed to execute `%1 %2`: %3")
           .arg(m_program)
           .arg(QStringList(m_arguments).replaceInStrings(QRegExp("^|$"), "\"").join(" "))
           .arg(reasons.join(": "));
73
}