From 07f7dd9c1434738ad59467ac8830be41db03dfa0 Mon Sep 17 00:00:00 2001
From: Rainer Keller <rainer.keller@digia.com>
Date: Tue, 4 Mar 2014 11:06:51 +0100
Subject: [PATCH] SDKtool: Check for existing objects when creating kits

Check for existing Qt versions, toolchains and devices if
those ids are given.

Change-Id: I0447d55e01cc40e43669ba7999126b0260974c0b
Reviewed-by: Rainer Keller <rainer.keller@digia.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
---
 src/tools/sdktool/adddeviceoperation.cpp    |  33 +++--
 src/tools/sdktool/adddeviceoperation.h      |   3 +
 src/tools/sdktool/addkitoperation.cpp       | 134 ++++++++++++++++----
 src/tools/sdktool/addkitoperation.h         |  10 ++
 src/tools/sdktool/addqtoperation.cpp        |  50 +++++---
 src/tools/sdktool/addqtoperation.h          |   3 +
 src/tools/sdktool/addtoolchainoperation.cpp |  32 +++--
 src/tools/sdktool/addtoolchainoperation.h   |   2 +
 src/tools/sdktool/main.cpp                  |  18 ++-
 src/tools/sdktool/operation.cpp             |   2 +-
 src/tools/sdktool/operation.h               |   2 +-
 src/tools/sdktool/rmkitoperation.cpp        |  33 ++++-
 12 files changed, 249 insertions(+), 73 deletions(-)

diff --git a/src/tools/sdktool/adddeviceoperation.cpp b/src/tools/sdktool/adddeviceoperation.cpp
index 9f745686251..75803f72b65 100644
--- a/src/tools/sdktool/adddeviceoperation.cpp
+++ b/src/tools/sdktool/adddeviceoperation.cpp
@@ -334,17 +334,13 @@ QVariantMap AddDeviceOperation::addDevice(const QVariantMap &map,
                                           const KeyValuePairList &extra)
 {
     QVariantMap result = map;
-    QVariantMap dmMap = map.value(QLatin1String(DEVICEMANAGER_ID)).toMap();
+    if (exists(map, id)) {
+        std::cerr << "Device " << qPrintable(id) << " already exists!" << std::endl;
+        return result;
+    }
 
+    QVariantMap dmMap = map.value(QLatin1String(DEVICEMANAGER_ID)).toMap();
     QVariantList devList = dmMap.value(QLatin1String(DEVICE_LIST_ID)).toList();
-    foreach (const QVariant &dev, devList) {
-        QVariantMap devData = dev.toMap();
-        QString current = devData.value(QLatin1String(DEVICE_ID_ID)).toString();
-        if (current == id) {
-            std::cerr << "Device " << qPrintable(id) << " already exists!" << std::endl;
-            return result;
-        }
-    }
 
     QVariantMap devMap
             = AddKeysOperation::addKeys(QVariantMap(),
@@ -373,6 +369,25 @@ QVariantMap AddDeviceOperation::initializeDevices()
     return data;
 }
 
+bool AddDeviceOperation::exists(const QString &id)
+{
+    QVariantMap map = load(QLatin1String("device"));
+    return exists(map, id);
+}
+
+bool AddDeviceOperation::exists(const QVariantMap &map, const QString &id)
+{
+    QVariantMap dmMap = map.value(QLatin1String(DEVICEMANAGER_ID)).toMap();
+    QVariantList devList = dmMap.value(QLatin1String(DEVICE_LIST_ID)).toList();
+    foreach (const QVariant &dev, devList) {
+        QVariantMap devData = dev.toMap();
+        QString current = devData.value(QLatin1String(DEVICE_ID_ID)).toString();
+        if (current == id)
+            return true;
+    }
+    return false;
+}
+
 Operation::KeyValuePairList AddDeviceOperation::createDevice(const QString &id, const QString &displayName,
                                                              int type, int auth, const QString &hwPlatform,
                                                              const QString &swPlatform, const QString &debugServer,
diff --git a/src/tools/sdktool/adddeviceoperation.h b/src/tools/sdktool/adddeviceoperation.h
index 02c1056a012..0a5d872ab53 100644
--- a/src/tools/sdktool/adddeviceoperation.h
+++ b/src/tools/sdktool/adddeviceoperation.h
@@ -68,6 +68,9 @@ public:
 
     static QVariantMap initializeDevices();
 
+    static bool exists(const QString &id);
+    static bool exists(const QVariantMap &map, const QString &id);
+
 private:
     static KeyValuePairList createDevice(const QString &id, const QString &displayName, int type,
                                          int auth, const QString &hwPlatform, const QString &swPlatform,
diff --git a/src/tools/sdktool/addkitoperation.cpp b/src/tools/sdktool/addkitoperation.cpp
index d0bb3e139dd..615c0d72cde 100644
--- a/src/tools/sdktool/addkitoperation.cpp
+++ b/src/tools/sdktool/addkitoperation.cpp
@@ -30,6 +30,9 @@
 #include "addkitoperation.h"
 
 #include "addkeysoperation.h"
+#include "addtoolchainoperation.h"
+#include "addqtoperation.h"
+#include "adddeviceoperation.h"
 #include "findkeyoperation.h"
 #include "findvalueoperation.h"
 #include "getoperation.h"
@@ -256,6 +259,27 @@ bool AddKitOperation::test() const
 {
     QVariantMap map = initializeKits();
 
+    QVariantMap tcMap = AddToolChainOperation::initializeToolChains();
+    tcMap = AddToolChainOperation::addToolChain(tcMap, QLatin1String("{tc-id}"), QLatin1String("TC"),
+                                                QLatin1String("/usr/bin/gcc"),
+                                                QLatin1String("x86-linux-generic-elf-32bit"),
+                                                QLatin1String("x86-linux-generic-elf-32bit"),
+                                                KeyValuePairList());
+
+    QVariantMap qtMap = AddQtOperation::initializeQtVersions();
+    qtMap = AddQtOperation::addQt(qtMap, QLatin1String("{qt-id}"), QLatin1String("Qt"),
+                                  QLatin1String("desktop-qt"), QLatin1String("/usr/bin/qmake"),
+                                  KeyValuePairList());
+
+    QVariantMap devMap = AddDeviceOperation::initializeDevices();
+    devMap = AddDeviceOperation::addDevice(devMap, QLatin1String("{dev-id}"), QLatin1String("Dev"), 0, 0,
+                                           QLatin1String("HWplatform"), QLatin1String("SWplatform"),
+                                           QLatin1String("localhost"), QLatin1String("10000-11000"),
+                                           QLatin1String("localhost"), QLatin1String(""), 42,
+                                           QLatin1String("desktop"), QLatin1String(""), 22, 10000,
+                                           QLatin1String("uname"), 1,
+                                           KeyValuePairList());
+
     if (map.count() != 3
             || !map.contains(QLatin1String(VERSION))
             || map.value(QLatin1String(VERSION)).toInt() != 1
@@ -265,10 +289,39 @@ bool AddKitOperation::test() const
             || map.value(QLatin1String(DEFAULT)).toInt() != -1)
         return false;
 
-    map = addKit(map, QLatin1String("testId"), QLatin1String("Test Kit"), QLatin1String("/tmp/icon.png"),
+    // Fail if TC is not there:
+    QVariantMap empty = addKit(map, tcMap, qtMap, devMap,
+                               QLatin1String("testId"), QLatin1String("Test Kit"), QLatin1String("/tmp/icon.png"),
+                               QString(), 1, QLatin1String("/usr/bin/gdb-test"),
+                               QByteArray("Desktop"), QLatin1String("{dev-id}"), QString(),
+                               QLatin1String("{tcXX-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
+                               KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
+    if (!empty.isEmpty())
+        return false;
+    // Fail if Qt is not there:
+    empty = addKit(map, tcMap, qtMap, devMap,
+                   QLatin1String("testId"), QLatin1String("Test Kit"), QLatin1String("/tmp/icon.png"),
+                   QString(), 1, QLatin1String("/usr/bin/gdb-test"),
+                   QByteArray("Desktop"), QLatin1String("{dev-id}"), QString(),
+                   QLatin1String("{tc-id}"), QLatin1String("{qtXX-id}"), QLatin1String("unsupported/mkspec"),
+                   KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
+    if (!empty.isEmpty())
+        return false;
+    // Fail if dev is not there:
+    empty = addKit(map, tcMap, qtMap, devMap,
+                   QLatin1String("testId"), QLatin1String("Test Kit"), QLatin1String("/tmp/icon.png"),
+                   QString(), 1, QLatin1String("/usr/bin/gdb-test"),
+                   QByteArray("Desktop"), QLatin1String("{devXX-id}"), QString(),
+                   QLatin1String("{tc-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
+                   KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
+    if (!empty.isEmpty())
+        return false;
+
+    map = addKit(map, tcMap, qtMap, devMap,
+                 QLatin1String("testId"), QLatin1String("Test Kit"), QLatin1String("/tmp/icon.png"),
                  QString(), 1, QLatin1String("/usr/bin/gdb-test"),
                  QByteArray("Desktop"), QString(), QString(),
-                 QLatin1String("{some-tc-id}"), QLatin1String("{some-qt-id}"), QLatin1String("unsupported/mkspec"),
+                 QLatin1String("{tc-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
                  KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
 
     if (map.count() != 4
@@ -304,20 +357,22 @@ bool AddKitOperation::test() const
         return false;
 
     // Ignore existing ids:
-    QVariantMap result = addKit(map, QLatin1String("testId"), QLatin1String("Test Qt Version X"), QLatin1String("/tmp/icon3.png"),
+    QVariantMap result = addKit(map, tcMap, qtMap, devMap,
+                                QLatin1String("testId"), QLatin1String("Test Qt Version X"), QLatin1String("/tmp/icon3.png"),
                                 QString(), 1, QLatin1String("/usr/bin/gdb-test3"),
                                 QByteArray("Desktop"), QString(), QString(),
-                                QLatin1String("{some-tc-id3}"), QLatin1String("{some-qt-id3}"), QLatin1String("unsupported/mkspec3"),
-                                KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue3"))));
+                                QLatin1String("{tc-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
+                                KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
     if (!result.isEmpty())
         return false;
 
     // Make sure name is unique:
-    map = addKit(map, QLatin1String("testId2"), QLatin1String("Test Kit2"), QLatin1String("/tmp/icon2.png"),
+    map = addKit(map, tcMap, qtMap, devMap,
+                 QLatin1String("testId2"), QLatin1String("Test Kit2"), QLatin1String("/tmp/icon2.png"),
                  QString(), 1, QLatin1String("/usr/bin/gdb-test2"),
-                 QByteArray("Desktop"), QLatin1String("deviceId"), QString(),
-                 QLatin1String("{some-tc-id2}"), QLatin1String("{some-qt-id2}"), QLatin1String("unsupported/mkspec2"),
-                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue2"))));
+                 QByteArray("Desktop"), QLatin1String("{dev-id}"), QString(),
+                 QLatin1String("{tc-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
+                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
     if (map.count() != 5
             || !map.contains(QLatin1String(VERSION))
             || map.value(QLatin1String(VERSION)).toInt() != 1
@@ -350,17 +405,18 @@ bool AddKitOperation::test() const
     data = profile1.value(QLatin1String(DATA)).toMap();
     if (data.count() != 8
             || !data.contains(QLatin1String(DEVICE_ID))
-            || data.value(QLatin1String(DEVICE_ID)).toString() != QLatin1String("deviceId")
+            || data.value(QLatin1String(DEVICE_ID)).toString() != QLatin1String("{dev-id}")
             || !data.contains(QLatin1String(DEBUGGER))
             || data.value(QLatin1String(DEBUGGER)).type() != QVariant::Map)
         return false;
 
     // Test debugger id:
-    map = addKit(map, QLatin1String("test with debugger Id"), QLatin1String("Test debugger Id"), QLatin1String("/tmp/icon2.png"),
+    map = addKit(map, tcMap, qtMap, devMap,
+                 QLatin1String("test with debugger Id"), QLatin1String("Test debugger Id"), QLatin1String("/tmp/icon2.png"),
                  QString::fromLatin1("debugger Id"), 0, QString(),
                  QByteArray("Desktop"), QString(), QString(),
-                 QLatin1String("{some-tc-id2}"), QLatin1String("{some-qt-id2}"), QLatin1String("unsupported/mkspec2"),
-                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue2"))));
+                 QLatin1String("{tc-id}"), QLatin1String("{qt-id}"), QLatin1String("unsupported/mkspec"),
+                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
     if (map.count() != 6
             || !map.contains(QLatin1String(VERSION))
             || map.value(QLatin1String(VERSION)).toInt() != 1
@@ -403,13 +459,30 @@ bool AddKitOperation::test() const
 }
 #endif
 
-QVariantMap AddKitOperation::addKit(const QVariantMap &map, const QString &id,
-                                    const QString &displayName, const QString &icon,
-                                    const QString &debuggerId, const quint32 &debuggerType,
-                                    const QString &debugger, const QByteArray &deviceType,
-                                    const QString &device, const QString &sysRoot,
-                                    const QString &tc, const QString &qt, const QString &mkspec,
-                                    const KeyValuePairList &extra)
+QVariantMap AddKitOperation::addKit(const QVariantMap &map,
+                                    const QString &id, const QString &displayName,
+                                    const QString &icon, const QString &debuggerId,
+                                    const quint32 &debuggerType, const QString &debugger,
+                                    const QByteArray &deviceType, const QString &device,
+                                    const QString &sysRoot, const QString &tc, const QString &qt,
+                                    const QString &mkspec, const KeyValuePairList &extra)
+{
+    QVariantMap tcMap = load(QLatin1String("toolchains"));
+    QVariantMap qtMap = load(QLatin1String("qtversions"));
+    QVariantMap devMap = load(QLatin1String("devices"));
+
+    return addKit(map, tcMap, qtMap, devMap, id, displayName, icon, debuggerId, debuggerType,
+                  debugger, deviceType, device, sysRoot, tc, qt, mkspec, extra);
+}
+
+QVariantMap AddKitOperation::addKit(const QVariantMap &map, const QVariantMap &tcMap,
+                                    const QVariantMap &qtMap, const QVariantMap &devMap,
+                                    const QString &id, const QString &displayName,
+                                    const QString &icon, const QString &debuggerId,
+                                    const quint32 &debuggerType, const QString &debugger,
+                                    const QByteArray &deviceType, const QString &device,
+                                    const QString &sysRoot, const QString &tc, const QString &qt,
+                                    const QString &mkspec, const KeyValuePairList &extra)
 {
     // Sanity check: Make sure autodetection source is not in use already:
     QStringList valueKeys = FindValueOperation::findValue(map, QVariant(id));
@@ -425,6 +498,23 @@ QVariantMap AddKitOperation::addKit(const QVariantMap &map, const QString &id,
         return QVariantMap();
     }
 
+    if (!tc.isEmpty() && !AddToolChainOperation::exists(tcMap, tc)) {
+        std::cerr << "Error: Toolchain " << qPrintable(tc) << " does not exist." << std::endl;
+        return QVariantMap();
+    }
+
+    QString qtId = qt;
+    if (!qtId.isEmpty() && !qtId.startsWith(QLatin1String("SDK.")))
+        qtId = QString::fromLatin1("SDK.") + qt;
+    if (!qtId.isEmpty() && !AddQtOperation::exists(qtMap, qtId)) {
+        std::cerr << "Error: Qt " << qPrintable(qtId) << " does not exist." << std::endl;
+        return QVariantMap();
+    }
+    if (!device.isEmpty() && !AddDeviceOperation::exists(devMap, device)) {
+        std::cerr << "Error: Device " << qPrintable(device) << " does not exist." << std::endl;
+        return QVariantMap();
+    }
+
     // Find position to insert:
     bool ok;
     int count = GetOperation::get(map, QLatin1String(COUNT)).toInt(&ok);
@@ -455,10 +545,6 @@ QVariantMap AddKitOperation::addKit(const QVariantMap &map, const QString &id,
         nameList << GetOperation::get(map, nameKey).toString();
     const QString uniqueName = makeUnique(displayName, nameList);
 
-    QString qtId = qt;
-    if (!qtId.startsWith(QLatin1String("SDK.")))
-        qtId = QString::fromLatin1("SDK.") + qt;
-
     // insert data:
     KeyValuePairList data;
     data << KeyValuePair(QStringList() << kit << QLatin1String(ID), QVariant(id));
diff --git a/src/tools/sdktool/addkitoperation.h b/src/tools/sdktool/addkitoperation.h
index 0936dd12e70..f5f6256920c 100644
--- a/src/tools/sdktool/addkitoperation.h
+++ b/src/tools/sdktool/addkitoperation.h
@@ -60,6 +60,16 @@ public:
 
     static QVariantMap initializeKits();
 
+    // internal:
+    static QVariantMap addKit(const QVariantMap &map, const QVariantMap &tcMap,
+                              const QVariantMap &qtMap, const QVariantMap &devMap,
+                              const QString &id, const QString &displayName,
+                              const QString &icon, const QString &debuggerId,
+                              const quint32 &debuggerType, const QString &debugger,
+                              const QByteArray &deviceType, const QString &device,
+                              const QString &sysRoot, const QString &tc, const QString &qt,
+                              const QString &mkspec, const KeyValuePairList &extra);
+
 private:
     QString m_id;
     QString m_displayName;
diff --git a/src/tools/sdktool/addqtoperation.cpp b/src/tools/sdktool/addqtoperation.cpp
index 444a3bbc179..316e3ec89ae 100644
--- a/src/tools/sdktool/addqtoperation.cpp
+++ b/src/tools/sdktool/addqtoperation.cpp
@@ -53,6 +53,13 @@ const char AUTODETECTION_SOURCE[] = "autodetectionSource";
 const char QMAKE[] = "QMakePath";
 const char TYPE[] = "QtVersion.Type";
 
+static QString extendId(const QString &id)
+{
+    if (!id.isEmpty() && !id.startsWith(QLatin1String("SDK.")))
+        return QString::fromLatin1("SDK.") + id;
+    return id;
+}
+
 QString AddQtOperation::name() const
 {
     return QLatin1String("addQt");
@@ -171,11 +178,11 @@ bool AddQtOperation::test() const
         return false;
 
 #if defined Q_OS_WIN
-    map = addQt(map, QLatin1String("testId"), QLatin1String("Test Qt Version"), QLatin1String("testType"),
+    map = addQt(map, QLatin1String("{some-qt-id}"), QLatin1String("Test Qt Version"), QLatin1String("testType"),
                 QLatin1String("/tmp//../tmp/test\\qmake"),
                 KeyValuePairList() << KeyValuePair(QLatin1String("extraData"), QVariant(QLatin1String("extraValue"))));
 #else
-    map = addQt(map, QLatin1String("testId"), QLatin1String("Test Qt Version"), QLatin1String("testType"),
+    map = addQt(map, QLatin1String("{some-qt-id}"), QLatin1String("Test Qt Version"), QLatin1String("testType"),
                 QLatin1String("/tmp//../tmp/test/qmake"),
                 KeyValuePairList() << KeyValuePair(QLatin1String("extraData"), QVariant(QLatin1String("extraValue"))));
 #endif
@@ -195,7 +202,7 @@ bool AddQtOperation::test() const
             || !version0.contains(QLatin1String(AUTODETECTED))
             || version0.value(QLatin1String(AUTODETECTED)).toBool() != true
             || !version0.contains(QLatin1String(AUTODETECTION_SOURCE))
-            || version0.value(QLatin1String(AUTODETECTION_SOURCE)).toString() != QLatin1String("SDK.testId")
+            || version0.value(QLatin1String(AUTODETECTION_SOURCE)).toString() != QLatin1String("SDK.{some-qt-id}")
             || !version0.contains(QLatin1String(TYPE))
             || version0.value(QLatin1String(TYPE)).toString() != QLatin1String("testType")
             || !version0.contains(QLatin1String(QMAKE))
@@ -205,7 +212,7 @@ bool AddQtOperation::test() const
         return false;
 
     // Ignore existing ids:
-    QVariantMap result = addQt(map, QLatin1String("testId"), QLatin1String("Test Qt Version2"), QLatin1String("testType2"),
+    QVariantMap result = addQt(map, QLatin1String("{some-qt-id}"), QLatin1String("Test Qt Version2"), QLatin1String("testType2"),
                                QLatin1String("/tmp/test/qmake2"),
                                KeyValuePairList() << KeyValuePair(QLatin1String("extraData"), QVariant(QLatin1String("extraValue"))));
     if (!result.isEmpty())
@@ -251,20 +258,10 @@ QVariantMap AddQtOperation::addQt(const QVariantMap &map,
                                   const QString &id, const QString &displayName, const QString &type,
                                   const QString &qmake, const KeyValuePairList &extra)
 {
-    QString sdkId = id;
-    if (!id.startsWith(QLatin1String("SDK.")))
-        sdkId = QString::fromLatin1("SDK.") + id;
+    QString sdkId = extendId(id);
 
     // Sanity check: Make sure autodetection source is not in use already:
-    QStringList valueKeys = FindValueOperation::findValue(map, sdkId);
-    bool hasId = false;
-    foreach (const QString &k, valueKeys) {
-        if (k.endsWith(QString(QLatin1Char('/')) + QLatin1String(AUTODETECTION_SOURCE))) {
-            hasId = true;
-            break;
-        }
-    }
-    if (hasId) {
+    if (exists(map, sdkId)) {
         std::cerr << "Error: Id " << qPrintable(id) << " already defined as Qt versions." << std::endl;
         return QVariantMap();
     }
@@ -315,3 +312,24 @@ QVariantMap AddQtOperation::initializeQtVersions()
     map.insert(QLatin1String(VERSION), 1);
     return map;
 }
+
+bool AddQtOperation::exists(const QString &id)
+{
+    QVariantMap map = load(QLatin1String("qtversion"));
+    return exists(map, id);
+}
+
+bool AddQtOperation::exists(const QVariantMap &map, const QString &id)
+{
+    QString sdkId = extendId(id);
+
+    // Sanity check: Make sure autodetection source is not in use already:
+    QStringList valueKeys = FindValueOperation::findValue(map, sdkId);
+    foreach (const QString &k, valueKeys) {
+        if (k.endsWith(QString(QLatin1Char('/')) + QLatin1String(AUTODETECTION_SOURCE))) {
+            return true;
+            break;
+        }
+    }
+    return false;
+}
diff --git a/src/tools/sdktool/addqtoperation.h b/src/tools/sdktool/addqtoperation.h
index 9a85e1688e9..cab59ca7055 100644
--- a/src/tools/sdktool/addqtoperation.h
+++ b/src/tools/sdktool/addqtoperation.h
@@ -55,6 +55,9 @@ public:
 
     static QVariantMap initializeQtVersions();
 
+    static bool exists(const QString &id);
+    static bool exists(const QVariantMap &map, const QString &id);
+
 private:
     QString m_id; // actually this is the autodetectionSource
     QString m_displayName;
diff --git a/src/tools/sdktool/addtoolchainoperation.cpp b/src/tools/sdktool/addtoolchainoperation.cpp
index e8767ad7224..77d9cd45cba 100644
--- a/src/tools/sdktool/addtoolchainoperation.cpp
+++ b/src/tools/sdktool/addtoolchainoperation.cpp
@@ -187,7 +187,7 @@ bool AddToolChainOperation::test() const
         return false;
 
     // Make sure name stays unique:
-    map = addToolChain(map, QLatin1String("testId2"), QLatin1String("name"), QLatin1String("/tmp/test"),
+    map = addToolChain(map, QLatin1String("{some-tc-id}"), QLatin1String("name"), QLatin1String("/tmp/test"),
                             QLatin1String("test-abi"), QLatin1String("test-abi,test-abi2"),
                             KeyValuePairList() << KeyValuePair(QLatin1String("ExtraKey"), QVariant(QLatin1String("ExtraValue"))));
     if (map.value(QLatin1String(COUNT)).toInt() != 2
@@ -206,7 +206,7 @@ bool AddToolChainOperation::test() const
         return false;
     tcData = map.value(QString::fromLatin1(PREFIX) + QLatin1Char('1')).toMap();
         if (tcData.count() != 7
-                || tcData.value(QLatin1String(ID)).toString() != QLatin1String("testId2")
+                || tcData.value(QLatin1String(ID)).toString() != QLatin1String("{some-tc-id}")
                 || tcData.value(QLatin1String(DISPLAYNAME)).toString() != QLatin1String("name2")
                 || tcData.value(QLatin1String(AUTODETECTED)).toBool() != true
                 || tcData.value(QLatin1String(PATH)).toString() != QLatin1String("/tmp/test")
@@ -225,15 +225,7 @@ QVariantMap AddToolChainOperation::addToolChain(const QVariantMap &map,
                                                 const QString &supportedAbis, const KeyValuePairList &extra)
 {
     // Sanity check: Does the Id already exist?
-    QStringList valueKeys = FindValueOperation::findValue(map, id.toUtf8());
-    bool hasId = false;
-    foreach (const QString &k, valueKeys) {
-        if (k.endsWith(QString(QLatin1Char('/')) + QLatin1String(ID))) {
-            hasId = true;
-            break;
-        }
-    }
-    if (hasId) {
+    if (exists(map, id)) {
         std::cerr << "Error: Id " << qPrintable(id) << " already defined for tool chains." << std::endl;
         return QVariantMap();
     }
@@ -284,3 +276,21 @@ QVariantMap AddToolChainOperation::initializeToolChains()
     map.insert(QLatin1String(VERSION), 1);
     return map;
 }
+
+bool AddToolChainOperation::exists(const QVariantMap &map, const QString &id)
+{
+    QStringList valueKeys = FindValueOperation::findValue(map, id.toUtf8());
+
+    foreach (const QString &k, valueKeys) {
+        if (k.endsWith(QString(QLatin1Char('/')) + QLatin1String(ID))) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool AddToolChainOperation::exists(const QString &id)
+{
+    QVariantMap map = Operation::load(QLatin1String("toolchains"));
+    return exists(map, id);
+}
diff --git a/src/tools/sdktool/addtoolchainoperation.h b/src/tools/sdktool/addtoolchainoperation.h
index 2beb83c2f63..602658fcf27 100644
--- a/src/tools/sdktool/addtoolchainoperation.h
+++ b/src/tools/sdktool/addtoolchainoperation.h
@@ -56,6 +56,8 @@ public:
                                     const KeyValuePairList &extra);
 
     static QVariantMap initializeToolChains();
+    static bool exists(const QString &id);
+    static bool exists(const QVariantMap &map, const QString &id);
 
 private:
     QString m_id;
diff --git a/src/tools/sdktool/main.cpp b/src/tools/sdktool/main.cpp
index 7ba19ba2e21..92d93268464 100644
--- a/src/tools/sdktool/main.cpp
+++ b/src/tools/sdktool/main.cpp
@@ -164,21 +164,25 @@ int main(int argc, char *argv[])
     Settings settings;
 
     QList<Operation *> operations;
-    operations << new AddDebuggerOperation
+    operations << new AddKeysOperation
+
+               << new AddDebuggerOperation
                << new AddDeviceOperation
-               << new AddKeysOperation
-               << new AddKitOperation
                << new AddQtOperation
                << new AddToolChainOperation
-               << new FindKeyOperation
-               << new FindValueOperation
+
+               << new AddKitOperation
+
                << new GetOperation
+               << new RmKitOperation
                << new RmDebuggerOperation
                << new RmDeviceOperation
                << new RmKeysOperation
-               << new RmKitOperation
                << new RmQtOperation
-               << new RmToolChainOperation;
+               << new RmToolChainOperation
+
+               << new FindKeyOperation
+               << new FindValueOperation;
 
 #ifdef WITH_TESTS
     std::cerr << std::endl << std::endl << "Starting tests..." << std::endl;
diff --git a/src/tools/sdktool/operation.cpp b/src/tools/sdktool/operation.cpp
index 5a13215d0e9..4d9c94598ae 100644
--- a/src/tools/sdktool/operation.cpp
+++ b/src/tools/sdktool/operation.cpp
@@ -95,7 +95,7 @@ Operation::KeyValuePair::KeyValuePair(const QStringList &k, const QVariant &v) :
     key(k), value(v)
 { }
 
-QVariantMap Operation::load(const QString &file) const
+QVariantMap Operation::load(const QString &file)
 {
     QVariantMap map;
 
diff --git a/src/tools/sdktool/operation.h b/src/tools/sdktool/operation.h
index bf69cc11361..7c983d2a355 100644
--- a/src/tools/sdktool/operation.h
+++ b/src/tools/sdktool/operation.h
@@ -64,7 +64,7 @@ public:
     virtual bool test() const = 0;
 #endif
 
-    QVariantMap load(const QString &file) const;
+    static QVariantMap load(const QString &file);
     bool save(const QVariantMap &map, const QString &file) const;
 
     static QVariant valueFromString(const QString &v);
diff --git a/src/tools/sdktool/rmkitoperation.cpp b/src/tools/sdktool/rmkitoperation.cpp
index 79b56fade78..72216a9669d 100644
--- a/src/tools/sdktool/rmkitoperation.cpp
+++ b/src/tools/sdktool/rmkitoperation.cpp
@@ -30,6 +30,9 @@
 #include "rmkitoperation.h"
 
 #include "addkeysoperation.h"
+#include "addtoolchainoperation.h"
+#include "adddeviceoperation.h"
+#include "addqtoperation.h"
 #include "addkitoperation.h"
 #include "findkeyoperation.h"
 #include "findvalueoperation.h"
@@ -98,21 +101,43 @@ int RmKitOperation::execute() const
 #ifdef WITH_TESTS
 bool RmKitOperation::test() const
 {
+    QVariantMap tcMap = AddToolChainOperation::initializeToolChains();
+    tcMap = AddToolChainOperation::addToolChain(tcMap, QLatin1String("{tc-id}"), QLatin1String("TC"),
+                                                QLatin1String("/usr/bin/gcc"),
+                                                QLatin1String("x86-linux-generic-elf-32bit"),
+                                                QLatin1String("x86-linux-generic-elf-32bit"),
+                                                KeyValuePairList());
+
+    QVariantMap qtMap = AddQtOperation::initializeQtVersions();
+    qtMap = AddQtOperation::addQt(qtMap, QLatin1String("{qt-id}"), QLatin1String("Qt"),
+                                  QLatin1String("desktop-qt"), QLatin1String("/usr/bin/qmake"),
+                                  KeyValuePairList());
+
+    QVariantMap devMap = AddDeviceOperation::initializeDevices();
+    devMap = AddDeviceOperation::addDevice(devMap, QLatin1String("{dev-id}"), QLatin1String("Dev"), 0, 0,
+                                           QLatin1String("HWplatform"), QLatin1String("SWplatform"),
+                                           QLatin1String("localhost"), QLatin1String("10000-11000"),
+                                           QLatin1String("localhost"), QLatin1String(""), 42,
+                                           QLatin1String("desktop"), QLatin1String(""), 22, 10000,
+                                           QLatin1String("uname"), 1,
+                                           KeyValuePairList());
+
     QVariantMap map =
-            AddKitOperation::addKit(AddKitOperation::initializeKits(),
+            AddKitOperation::addKit(AddKitOperation::initializeKits(), tcMap, qtMap, devMap,
                                     QLatin1String("testId"), QLatin1String("Test Qt Version"),
                                     QLatin1String("/tmp/icon.png"),
                                     QString(), 1, QLatin1String("/usr/bin/gdb-test"),
                                     QByteArray("Desktop"), QString(),  QString(),
-                                    QLatin1String("{some-tc-id}"), QLatin1String("{some-qt-id}"),
+                                    QLatin1String("{tc-id}"), QLatin1String("{qt-id}"),
                                     QLatin1String("unsupported/mkspec"),
                                     KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));
     map =
-            AddKitOperation::addKit(map, QLatin1String("testId2"), QLatin1String("Test Qt Version"),
+            AddKitOperation::addKit(map, tcMap, qtMap, devMap,
+                                    QLatin1String("testId2"), QLatin1String("Test Qt Version"),
                                     QLatin1String("/tmp/icon2.png"),
                                     QString(), 1, QLatin1String("/usr/bin/gdb-test2"),
                                     QByteArray("Desktop"), QString(), QString(),
-                                    QLatin1String("{some-tc-id2}"), QLatin1String("{some-qt-id2}"),
+                                    QLatin1String("{tc-id}"), QLatin1String("{qt-id}"),
                                     QLatin1String("unsupported/mkspec2"),
                                     KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue2"))));
 
-- 
GitLab