From e094c0c0fa72d6b51e3d583374985efb614804db Mon Sep 17 00:00:00 2001 From: kh1 Date: Thu, 28 Feb 2013 11:34:07 +0100 Subject: Fix wrong check for running application. Add auto test. Reuse already existing code in package manager core. Change-Id: I4346333882e37c8edba9a2eb85a9cdd56ae5917e Reviewed-by: Niels Weber Reviewed-by: Kai Koehne Reviewed-by: Tim Jenssen --- .../fakestopprocessforupdateoperation.cpp | 80 +++++++--------------- .../installer/fakestopprocessforupdateoperation.h | 4 +- .../fakestopprocessforupdateoperation.pro | 6 ++ .../tst_fakestopprocessforupdateoperation.cpp | 78 +++++++++++++++++++++ tests/auto/installer/installer.pro | 3 +- 5 files changed, 114 insertions(+), 57 deletions(-) create mode 100644 tests/auto/installer/fakestopprocessforupdateoperation/fakestopprocessforupdateoperation.pro create mode 100644 tests/auto/installer/fakestopprocessforupdateoperation/tst_fakestopprocessforupdateoperation.cpp diff --git a/src/libs/installer/fakestopprocessforupdateoperation.cpp b/src/libs/installer/fakestopprocessforupdateoperation.cpp index 47d21c39f..ead887118 100644 --- a/src/libs/installer/fakestopprocessforupdateoperation.cpp +++ b/src/libs/installer/fakestopprocessforupdateoperation.cpp @@ -41,53 +41,10 @@ #include "fakestopprocessforupdateoperation.h" -#include -#include - -#include +#include "messageboxhandler.h" +#include "packagemanagercore.h" using namespace KDUpdater; - -/*! - Copied from QInstaller with some adjustments - Return true, if a process with \a name is running. On Windows, the comparision is case-insensitive. -*/ -static bool isProcessRunning(const QString &name, const QList &processes) -{ - for (QList::const_iterator it = processes.constBegin(); it != processes.constEnd(); ++it) { - if (it->name.isEmpty()) - continue; - -#ifndef Q_OS_WIN - if (it->name == name) - return true; - const QFileInfo fi(it->name); - if (fi.fileName() == name || fi.baseName() == name) - return true; -#else - if (it->name.toLower() == name.toLower()) - return true; - if (it->name.toLower() == QDir::toNativeSeparators(name.toLower())) - return true; - const QFileInfo fi(it->name); - if (fi.fileName().toLower() == name.toLower() || fi.baseName().toLower() == name.toLower()) - return true; -#endif - } - return false; -} - -static QStringList checkRunningProcessesFromList(const QStringList &processList) -{ - const QList allProcesses = runningProcesses(); - QStringList stillRunningProcesses; - foreach (const QString &process, processList) { - if (!process.isEmpty() && isProcessRunning(process, allProcesses)) - stillRunningProcesses.append(process); - } - return stillRunningProcesses; -} - using namespace QInstaller; FakeStopProcessForUpdateOperation::FakeStopProcessForUpdateOperation() @@ -113,18 +70,31 @@ bool FakeStopProcessForUpdateOperation::undoOperation() return false; } - QStringList processList = arguments()[0].split(QLatin1String(","), QString::SkipEmptyParts); - qSort(processList); - processList.erase(std::unique(processList.begin(), processList.end()), processList.end()); - if (!processList.isEmpty()) { - const QStringList processes = checkRunningProcessesFromList(processList); - if (!processes.isEmpty()) { - setError(KDUpdater::UpdateOperation::UserDefinedError, tr("These processes should be stopped to " - "continue:\n\n%1").arg(QDir::toNativeSeparators(processes.join(QLatin1String("\n"))))); - } + PackageManagerCore *const core = value(QLatin1String("installer")).value(); + if (!core) { + setError(KDUpdater::UpdateOperation::UserDefinedError, QObject::tr("Could not get package manager " + "core.")); return false; } - return true; + + QStringList processes = arguments().at(0).split(QLatin1Char(','), QString::SkipEmptyParts); + for (int i = processes.count() - 1; i >= 0; --i) { + if (!core->isProcessRunning(processes.at(i))) + processes.removeAt(i); + } + + if (processes.isEmpty()) + return true; + + if (processes.count() == 1) { + setError(UpdateOperation::UserDefinedError, QObject::tr("This process should be stopped before " + "continuing: %1").arg(processes.first())); + } else { + const QString sep = QString::fromWCharArray(L"\n \u2022 "); // Unicode bullet + setError(UpdateOperation::UserDefinedError, QObject::tr("These processes should be stopped before " + "continuing: %1").arg(sep + processes.join(sep))); + } + return false; } bool FakeStopProcessForUpdateOperation::testOperation() diff --git a/src/libs/installer/fakestopprocessforupdateoperation.h b/src/libs/installer/fakestopprocessforupdateoperation.h index 228d5df03..ca7ba3ccc 100644 --- a/src/libs/installer/fakestopprocessforupdateoperation.h +++ b/src/libs/installer/fakestopprocessforupdateoperation.h @@ -46,8 +46,10 @@ namespace QInstaller { -class FakeStopProcessForUpdateOperation : public Operation +class INSTALLER_EXPORT FakeStopProcessForUpdateOperation : public QObject, public Operation { + Q_OBJECT + public: FakeStopProcessForUpdateOperation(); diff --git a/tests/auto/installer/fakestopprocessforupdateoperation/fakestopprocessforupdateoperation.pro b/tests/auto/installer/fakestopprocessforupdateoperation/fakestopprocessforupdateoperation.pro new file mode 100644 index 000000000..871b668b2 --- /dev/null +++ b/tests/auto/installer/fakestopprocessforupdateoperation/fakestopprocessforupdateoperation.pro @@ -0,0 +1,6 @@ +include(../../qttest.pri) + +QT -= gui +QT += network script + +SOURCES += tst_fakestopprocessforupdateoperation.cpp diff --git a/tests/auto/installer/fakestopprocessforupdateoperation/tst_fakestopprocessforupdateoperation.cpp b/tests/auto/installer/fakestopprocessforupdateoperation/tst_fakestopprocessforupdateoperation.cpp new file mode 100644 index 000000000..1d23e1970 --- /dev/null +++ b/tests/auto/installer/fakestopprocessforupdateoperation/tst_fakestopprocessforupdateoperation.cpp @@ -0,0 +1,78 @@ +#include "fakestopprocessforupdateoperation.h" +#include "packagemanagercore.h" + +#include +#include + +using namespace KDUpdater; +using namespace QInstaller; + +class tst_FakeStopProcessForUpdateOperation : public QObject +{ + Q_OBJECT + +private slots: + void testMissingArgument() + { + FakeStopProcessForUpdateOperation op; + op.setValue(QLatin1String("installer"), QVariant::fromValue(&m_core)); + + QVERIFY(op.testOperation()); + QVERIFY(op.performOperation()); + QVERIFY(!op.undoOperation()); + + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::InvalidArguments); + QCOMPARE(op.errorString(), QString("Number of arguments does not match: one is required")); + } + + void testMissingPackageManagerCore() + { + FakeStopProcessForUpdateOperation op; + op.setArguments(QStringList() << QFileInfo(QCoreApplication::applicationFilePath()).fileName()); + + QVERIFY(op.testOperation()); + QVERIFY(op.performOperation()); + QVERIFY(!op.undoOperation()); + + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::UserDefinedError); + QCOMPARE(op.errorString(), QString("Could not get package manager core.")); + } + + void testRunningApplication() + { + const QString app = QFileInfo(QCoreApplication::applicationFilePath()).fileName(); + + FakeStopProcessForUpdateOperation op; + op.setArguments(QStringList() << app); + op.setValue(QLatin1String("installer"), QVariant::fromValue(&m_core)); + + QVERIFY(op.testOperation()); + QVERIFY(op.performOperation()); + QVERIFY(!op.undoOperation()); + + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::UserDefinedError); + QCOMPARE(op.errorString(), QString::fromLatin1("This process should be stopped before " + "continuing: %1").arg(app)); + } + + void testRunningNonApplication() + { + FakeStopProcessForUpdateOperation op; + op.setArguments(QStringList() << "dummy.exe"); + op.setValue(QLatin1String("installer"), QVariant::fromValue(&m_core)); + + QVERIFY(op.testOperation()); + QVERIFY(op.performOperation()); + QVERIFY(op.undoOperation()); + + QCOMPARE(op.errorString(), QString()); + QCOMPARE(UpdateOperation::Error(op.error()), UpdateOperation::NoError); + } + +private: + PackageManagerCore m_core; +}; + +QTEST_MAIN(tst_FakeStopProcessForUpdateOperation) + +#include "tst_fakestopprocessforupdateoperation.moc" diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index 0c1b7f975..911e1ad75 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -2,4 +2,5 @@ TEMPLATE = subdirs SUBDIRS += \ settings \ - repository + repository \ + fakestopprocessforupdateoperation -- cgit v1.2.3